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

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.wd.workoffice.R;
import com.wd.workoffice.app.BaseMesBean;
import com.wd.workoffice.app.WorkToolBarActivity;
import com.wd.workoffice.bean.event.CheckFeedbackEvent;
import com.wd.workoffice.bean.mesBean.FeedbackDetailBean;
import com.wd.workoffice.bean.mesBean.ManagerUserBean;
import com.wd.workoffice.bean.mesBean.MesProcessBean;
import com.wd.workoffice.bean.mesBean.QuestionBean;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.util.DataKeeper;
import com.wd.workoffice.util.PickTimeUtils;
import com.wd.workoffice.util.WorkUtils;

import org.greenrobot.eventbus.EventBus;

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

import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import flexible.xd.android_base.network.rtfhttp.Transformer;
import flexible.xd.android_base.utils.TimeUtils;
import io.reactivex.disposables.Disposable;

/**
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class MesQualityAboutDeptActivity extends WorkToolBarActivity {
    @BindView(R.id.tv_factory_reason)
    EditText tvFactoryReason;
    @BindView(R.id.tv_factory_suggest)
    EditText tvFactorySuggest;
    @BindView(R.id.tv_factory_status)
    EditText tvFactoryStatus;
    @BindView(R.id.tv_process)
    TextView tvProcess;
    @BindView(R.id.rl_process)
    RelativeLayout rlProcess;
    @BindView(R.id.tv_duty_person)
    TextView tvDutyPerson;
    @BindView(R.id.rl_duty_person)
    RelativeLayout rlDutyPerson;
    @BindView(R.id.tv_verify_person)
    TextView tvVerifyPerson;
    @BindView(R.id.rl_verify_person)
    RelativeLayout rlVerifyPerson;
    @BindView(R.id.tv_question)
    TextView tvQuestion;
    @BindView(R.id.rl_question)
    RelativeLayout rlQuestion;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.rl_time)
    RelativeLayout rlTime;
    private Map<String, Object> param;
    private FeedbackDetailBean data;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
    }


    @Override
    protected void initData() {
        param = WorkUtils.simpleParam();
        data = JSON.parseObject(getIntent().getStringExtra("data"), FeedbackDetailBean.class);
        param.put("id", data.getId());
        param.put("procId", data.getProcId());
    }

    private void submit() {
//        tvProcess.setText(detailBean.getTreatRectPlan());
//        tvDutyPerson.setText(detailBean.getResponsibleDealName());
//        tvVerifyPerson.setText(detailBean.getApprovalUserName());
//        tvQuestion.setText(detailBean.getDefectMudName());
//        tvTime.setText(detailBean.getFactoryTreatmentDateTime());
        param.put("analyCause",tvFactoryReason.getText().toString());
        param.put("factoryImplement",tvFactoryStatus.getText().toString());
        param.put("treatRectPlan",tvFactorySuggest.getText().toString());
        param.put("verifyUserDate",TimeUtils.getNowString());
        String mesMenuId = WorkUtils.getMesMenuId("市场服务", "质量反馈");
        if (TextUtils.isEmpty(mesMenuId)){
            toast("您没有权限哦");
            return;
        }
        RtfUtils.getMesRtf().startFeedback(WorkUtils.getMesMenuId("市场服务","质量反馈"), WorkUtils.convertMapToBody(param)).compose(Transformer.schedule()).subscribe(new WorkObserver<BaseMesBean>() {
            @Override
            public void doOnSubscribe(Disposable d) {
            }

            @Override
            public void onFail(String errorMsg) {
                hideLoading();
                toast(errorMsg);
            }

            @Override
            public void onSuccess(BaseMesBean data) {
                hideLoading();
                if (!data.isSuccess()) {
                    toast(data.getMessage());
                    return;
                }
                EventBus.getDefault().post(new CheckFeedbackEvent());
                toast("提交成功");
                finish();
            }
        });
    }

    @Override
    protected void initEvent() {
    }

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

    @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:
                submit();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @OnClick({R.id.rl_process, R.id.rl_duty_person, R.id.rl_verify_person, R.id.rl_question, R.id.rl_time})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_process:
                startActivityForResult(MesChooseProcessActivity.class,10003,"id", data.getProductCode());
                break;
            case R.id.rl_duty_person:
                startActivityForResult(MesChooseMangerActivity.class,10001);
                break;
            case R.id.rl_verify_person:
                startActivityForResult(MesChooseMangerActivity.class,10002);
                break;
            case R.id.rl_question:
                startActivityForResult(MesChooseQuestionActivity.class,10004,"id", data.getProductCode());
                break;
            case R.id.rl_time:
                PickTimeUtils.showPickThree(this, new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        tvTime.setText(TimeUtils.date2String(date,new SimpleDateFormat("yyyy-MM-dd")));
                        param.put("factoryTreatmentDateTime",TimeUtils.date2String(date,new SimpleDateFormat("yyyy-MM-dd")));
                    }
                }).show();
                break;
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 10001:
                if (resultCode == 10001) {
                    ManagerUserBean.ListBean manager = JSON.parseObject(data.getStringExtra("data"), ManagerUserBean.ListBean.class);
                    param.put("responsibleUserId",manager.getId());
                    param.put("responsibleUserName",manager.getName());
                    tvDutyPerson.setText(manager.getName());
                }
                break;
            case 10002:
                if (resultCode == 10001) {
                    ManagerUserBean.ListBean manager = JSON.parseObject(data.getStringExtra("data"), ManagerUserBean.ListBean.class);
                    param.put("verifyUserId",manager.getId());
                    param.put("verifyUserName",manager.getName());
                    tvVerifyPerson.setText(manager.getName());
                }
                break;
            case 10003:
                if (resultCode == 10001) {
                    MesProcessBean process = JSON.parseObject(data.getStringExtra("data"), MesProcessBean.class);
                    param.put("procedureId",process.getId());
                    param.put("procedureCode",process.getProcedureCode());
                    param.put("procedureName",process.getProcedureName());
                    tvProcess.setText(process.getProcedureName());
                }
                break;
            case 10004:
                if (resultCode == 10001) {
                    QuestionBean.ListBean manager = JSON.parseObject(data.getStringExtra("data"), QuestionBean.ListBean.class);
                    param.put("defectMudId",manager.getId());
                    param.put("defectMudCode",manager.getMaterialCode());
                    param.put("defectMudName",manager.getMaterialName());
                    tvQuestion.setText(manager.getMaterialName());
                }
                break;
        }
    }
}
