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

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
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.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 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 MesUserDataConditionActivity extends WorkToolBarActivity {
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_factory)
    EditText etFactory;
    @BindView(R.id.et_machine)
    EditText etMachine;
    @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;
    private QMUIDialog.MenuDialogBuilder statusDialog;
    private String[] statusList = new String[]{"已提交", "已通过", "驳回"};
    // 0草稿、1已提交、2执行中、3已总结、4已评价
    private Integer[] statusValue = new Integer[]{0, 1, 2};
    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("approvalStatus", statusValue[which]);
                dialog.dismiss();
            }
        });
    }


    @Override
    protected void initData() {
    }

    @Override
    protected void initEvent() {
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_mes_user_data_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(MesUserDataConditionActivity.this, new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        String time = TimeUtils.date2String(date, new SimpleDateFormat("yyyy-MM-dd"));
                        param.put("feedbackDateTime", 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(etFactory.getText().toString())) {
                    param.put("steelmakingWorksName", etFactory.getText().toString());
                }
                if (!TextUtils.isEmpty(etMachine.getText().toString())) {
                    param.put("continuousCasterName", etMachine.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);
    }

}
