package com.wd.workoffice.ui.activity.mes.ticket;

import android.content.DialogInterface;
import android.content.Intent;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.wd.workoffice.R;
import com.wd.workoffice.app.WorkToolBarActivity;
import com.wd.workoffice.ui.activity.bat.order.AddCrucibleConfirmActivity;
import com.wd.workoffice.util.DialogUtils;
import com.wd.workoffice.util.PickTimeUtils;
import com.wd.workoffice.util.WorkUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import androidx.core.widget.NestedScrollView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import flexible.xd.android_base.utils.TimeUtils;

/**
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class MesTicketConditionActivity extends WorkToolBarActivity {
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_company)
    EditText etCompany;
    @BindView(R.id.tv_status)
    TextView tvStatus;
    @BindView(R.id.rl_status)
    RelativeLayout rlStatus;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.rl_time)
    RelativeLayout rlTime;
    @BindView(R.id.srl_refresh)
    NestedScrollView srlRefresh;
    private QMUIDialog.MenuDialogBuilder statusDialog;
    private String[] statusList = new String[]{"草稿", "已提交", "执行中", "已总结", "已评价"};
    // 0草稿、1已提交、2执行中、3已总结、4已评价
    private Integer[] statusValue = new Integer[]{0, 1, 2, 3, 4};
    private Map<String, Object> param;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        param = WorkUtils.simpleParam();
        statusDialog = DialogUtils.listDialog(this);
        statusDialog.addItems(statusList, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                tvStatus.setText(statusList[which]);
                param.put("jobStatus", statusValue[which]);
                dialog.dismiss();
            }
        });
    }


    @Override
    protected void initData() {
    }

    @Override
    protected void initEvent() {
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_mes_ticket_cnd;
    }


    @OnClick({R.id.rl_status, R.id.rl_time})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_status:
                statusDialog.show();
                break;
            case R.id.rl_time:
                PickTimeUtils.showPickThree(MesTicketConditionActivity.this, new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        String time = TimeUtils.date2String(date, new SimpleDateFormat("yyyy-MM-dd"));
                        param.put("planBeginDate", time);
                        tvTime.setText(time);
                    }
                }).show();
                break;
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_finish, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.finish:
                if (!TextUtils.isEmpty(etCompany.getText().toString())){
                    param.put("customerUserName",etCompany.getText().toString());
                }
                if (!TextUtils.isEmpty(etName.getText().toString())){
                    param.put("salesCompanyName",etName.getText().toString());
                }
                Intent intent = new Intent();
                intent.putExtra("param", JSON.toJSONString(param));
                setResult(10001,intent);
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
