package com.bjzc.zjdxj.activity;

import android.text.TextUtils;
import android.view.View;
import com.bjzc.zjdxj.R;
import com.bjzc.zjdxj.base.BaseActivity3;
import com.bjzc.zjdxj.base.BaseCallBack;
import com.bjzc.zjdxj.bean.JzBean;
import com.bjzc.zjdxj.bean.QXGDResultBean;
import com.bjzc.zjdxj.contans.Contans;
import com.bjzc.zjdxj.utils.GsonUtils;
import com.bjzc.zjdxj.utils.HttpUtils;
import com.bjzc.zjdxj.utils.SPUtils;
import com.bjzc.zjdxj.weight.DateTimePickDialog;
import com.bjzc.zjdxj.weight.InputLayout;
import com.bjzc.zjdxj.weight.ListDialog;
import com.xuexiang.xui.widget.edittext.MultiLineEditText;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import retrofit2.Call;
import retrofit2.Response;

public class QxgdActivity extends BaseActivity3 {

    InputLayout ilFxr;
    InputLayout ilFxsj;
    InputLayout ilFxrbm;
    InputLayout ilJz;
    InputLayout ilZy;
    InputLayout ilQxlx;
    InputLayout ilQxdj;
    MultiLineEditText etQxms;
    InputLayout ilFzbm;


    private List<JzBean.ResultBean> JzDatas;
    private List<String> JzDialogDatas;
    private List<JzBean.ResultBean> ZYDatas;
    private List<String> ZYDialogDatas;
    private List<JzBean.ResultBean> LXDatas;
    private List<String> LXDialogDatas;
    private List<JzBean.ResultBean> QXDJDatas;
    private List<String> QXDJDialogDatas;
    private List<JzBean.ResultBean> GDAPDatas;
    private List<String> GDAPDialogDatas;
    private String JZValue, ZYValue, LXvalue, QXDJValue, GDAPValue;



    @Override
    public int getLayoutId() {
        return R.layout.activity_qxgd;
    }

    @Override
    public void initView() {
        initToolbar("缺陷工单提交(对接Maximo)", "提交", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(ilFxsj.getContent())) {
                    showToast("请输入时间");
                } else if (TextUtils.isEmpty(ilJz.getContent())) {
                    showToast("请选择机组");
                } else if (TextUtils.isEmpty(ilZy.getContent())) {
                    showToast("请选择专业");
                } else if (TextUtils.isEmpty(ilQxlx.getContent())) {
                    showToast("请选择缺陷类型");
                } else if (TextUtils.isEmpty(ilQxdj.getContent())) {
                    showToast("请选择缺陷等级");
                } else if (TextUtils.isEmpty(etQxms.getContentText())) {
                    showToast("请填写缺陷描述");
                } else if (TextUtils.isEmpty(ilFzbm.getContent())) {
                    showToast("请选择负责部门");
                } else {
                    UploadDatas();
                }
            }
        });

        ilFxr = findViewById(R.id.il_fxr);
        ilFxsj = findViewById(R.id.il_fxsj);
        ilFxrbm = findViewById(R.id.il_fxrbm);
        ilJz = findViewById(R.id.il_jz);
        ilZy = findViewById(R.id.il_zy);
        ilQxlx = findViewById(R.id.il_qxlx);
        ilQxdj = findViewById(R.id.il_qxdj);
        etQxms = findViewById(R.id.et_qxms);
        ilFzbm = findViewById(R.id.il_fzbm);
    }

    @Override
    public void initData() {
        JzDatas = new ArrayList<>();
        JzDialogDatas = new ArrayList<>();
        ZYDatas = new ArrayList<>();
        ZYDialogDatas = new ArrayList<>();
        LXDatas = new ArrayList<>();
        LXDialogDatas = new ArrayList<>();
        QXDJDatas = new ArrayList<>();
        QXDJDialogDatas = new ArrayList<>();
        GDAPDatas = new ArrayList<>();
        GDAPDialogDatas = new ArrayList<>();
        GETJZ();
        GETZY();
        GETLX();
        GETQXDJ();
        GETGDAP();
        ilFxr.setContent((String) SPUtils.get(context, Contans.USERNAME, ""));
        ilFxrbm.setContent((String) SPUtils.get(context, Contans.BZMC, ""));
        ilFxsj.setOnClickListener(v -> {
            ilFxsj.setContent("");
            DateTimePickDialog dialog = new DateTimePickDialog(context, (outPutDate, outPutDate1, outPutDate2, outPutDateTime3) -> ilFxsj.setContent(outPutDate));
            dialog.show();
        });
        ilJz.setOnClickListener(v -> {
            ListDialog dialog = new ListDialog(context, JzDialogDatas, (time, position) -> {
                JZValue = JzDatas.get(position).getValue();
                ilJz.setContent(JzDatas.get(position).getDescription());
            });
            dialog.show();
        });
        ilZy.setOnClickListener(v -> {
            ListDialog dialog = new ListDialog(context, ZYDialogDatas, (time, position) -> {
                ZYValue = ZYDatas.get(position).getValue();
                ilZy.setContent(ZYDatas.get(position).getDescription());
            });
            dialog.show();
        });
        ilQxlx.setOnClickListener(v -> {
            ListDialog dialog = new ListDialog(context, LXDialogDatas, (time, position) -> {
                LXvalue = LXDatas.get(position).getValue();
                ilQxlx.setContent(LXDatas.get(position).getDescription());
            });
            dialog.show();
        });
        ilQxdj.setOnClickListener(v -> {
            ListDialog dialog = new ListDialog(context, QXDJDialogDatas, (time, position) -> {
                QXDJValue = QXDJDatas.get(position).getValue();
                ilQxdj.setContent(QXDJDatas.get(position).getDescription());
            });
            dialog.show();
        });
        ilFzbm.setOnClickListener(v -> {
            ListDialog dialog = new ListDialog(context, GDAPDialogDatas, (time, position) -> {
                GDAPValue = GDAPDatas.get(position).getValue();
                ilFzbm.setContent(GDAPDatas.get(position).getDescription());
            });
            dialog.show();
        });
    }


    private void GETJZ() {
        HttpUtils.GETMAXIMOIP().GetJzList().enqueue(new BaseCallBack<JzBean>(context) {
            @Override
            public void onSuccess(Call<JzBean> call, Response<JzBean> response) {
                JzBean bean1 = response.body();
                if (bean1 != null) {
                    JzDatas.clear();
                    JzDatas.addAll(bean1.getResult());
                    initJZdatas();

                }
            }

            @Override
            public void onError(Call<JzBean> call, Throwable t) {

            }
        });
    }

    private void GETZY() {

        HttpUtils.GETMAXIMOIP().GetZyList().enqueue(new BaseCallBack<JzBean>(context) {
            @Override
            public void onSuccess(Call<JzBean> call, Response<JzBean> response) {
                JzBean bean = response.body();
                if (bean != null) {
                    ZYDatas.clear();
                    ZYDatas.addAll(bean.getResult());
                    initZYdatas();
                }
            }

            @Override
            public void onError(Call<JzBean> call, Throwable t) {

            }

        });

    }

    private void GETLX() {

        HttpUtils.GETMAXIMOIP().GetLxist().enqueue(new BaseCallBack<JzBean>(context) {
            @Override
            public void onSuccess(Call<JzBean> call, Response<JzBean> response) {
                JzBean bean = response.body();
                if (bean != null) {
                    LXDatas.clear();
                    LXDatas.addAll(bean.getResult());
                    initLXdatas();
                }
            }

            @Override
            public void onError(Call<JzBean> call, Throwable t) {

            }

        });

    }

    private void GETQXDJ() {

        HttpUtils.GETMAXIMOIP().GetQXDJist().enqueue(new BaseCallBack<JzBean>(context) {
            @Override
            public void onSuccess(Call<JzBean> call, Response<JzBean> response) {
                JzBean bean = response.body();
                if (bean != null) {
                    QXDJDatas.clear();
                    QXDJDatas.addAll(bean.getResult());
                    initQXDJdatas();
                }
            }

            @Override
            public void onError(Call<JzBean> call, Throwable t) {

            }

        });
    }

    private void GETGDAP() {

        HttpUtils.GETMAXIMOIP().GetGDAPist().enqueue(new BaseCallBack<JzBean>(context) {
            @Override
            public void onSuccess(Call<JzBean> call, Response<JzBean> response) {
                JzBean bean = response.body();
                if (bean != null) {
                    GDAPDatas.clear();
                    GDAPDatas.addAll(bean.getResult());
                    initGDAPdatas();
                }
            }

            @Override
            public void onError(Call<JzBean> call, Throwable t) {

            }

        });
    }

    private void initJZdatas() {
        for (JzBean.ResultBean bean : JzDatas) {
            JzDialogDatas.add(bean.getDescription());
        }
    }

    private void initZYdatas() {
        for (JzBean.ResultBean bean : ZYDatas) {
            ZYDialogDatas.add(bean.getDescription());
        }
    }

    private void initLXdatas() {
        for (JzBean.ResultBean bean : LXDatas) {
            LXDialogDatas.add(bean.getDescription());
        }
    }

    private void initQXDJdatas() {
        for (JzBean.ResultBean bean : QXDJDatas) {
            QXDJDialogDatas.add(bean.getDescription());
        }
    }

    private void initGDAPdatas() {
        for (JzBean.ResultBean bean : GDAPDatas) {
            GDAPDialogDatas.add(bean.getDescription());
        }
    }

    private void UploadDatas() {

        HttpUtils.GETMAXIMOIP().UploadQXgd(GetInfo()).enqueue(new BaseCallBack<QXGDResultBean>(context) {
            @Override
            public void onSuccess(Call<QXGDResultBean> call, Response<QXGDResultBean> response) {
                QXGDResultBean bean = response.body();
                if (bean != null) {
                    if (bean.getStatus().equals("1")) {
                        showToast("提交成功,请到Maximo系统查看");
                        finish();
                    }
                }
            }

            @Override
            public void onError(Call<QXGDResultBean> call, Throwable t) {

            }
        });

    }

    private String GetInfo() {
        String json = "";
        Map<String, String> map = new HashMap<String, String>();
        map.put("description", etQxms.getContentText());
        map.put("unit", JZValue);
        map.put("profession", ZYValue);
        map.put("bugtype", LXvalue);
        map.put("buglevel", QXDJValue);
        map.put("reportedby", (String) SPUtils.get(context, Contans.USERID, ""));
        map.put("bugdept", GDAPValue);
        json = GsonUtils.GsonString(map);
        return json;
    }
}
