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

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.TimeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wd.workoffice.R;
import com.wd.workoffice.app.BaseMesBean;
import com.wd.workoffice.app.WorkToolBarActivity;
import com.wd.workoffice.bean.event.AddUserDataEvent;
import com.wd.workoffice.bean.event.ModifyUserDataEvent;
import com.wd.workoffice.bean.event.RefreshMachineEvent;
import com.wd.workoffice.bean.mesBean.FactoryDetailBean;
import com.wd.workoffice.bean.mesBean.MachineDetailBean;
import com.wd.workoffice.bean.mesBean.UserDataDetailBean;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.adapter.UserDataAdd3Adapter;
import com.wd.workoffice.ui.adapter.UserDataAdd3LabelAdapter;
import com.wd.workoffice.ui.adapter.UserDataAdd3RefAdapter;
import com.wd.workoffice.ui.adapter.UserDataMachineAdapter;
import com.wd.workoffice.util.WorkUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import flexible.xd.android_base.network.rtfhttp.Transformer;
import io.reactivex.disposables.Disposable;

/**
 * 用户数据采集，
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class MesUserDataDetailActivity extends WorkToolBarActivity {
    @BindView(R.id.btn_refuse)
    Button btnRefuse;
    @BindView(R.id.btn_agree)
    Button btnReviews;
    @BindView(R.id.ll_bottom)
    LinearLayout llBottom;
    @BindView(R.id.tv_reason)
    TextView tvReason;
    @BindView(R.id.ll_reason)
    LinearLayout llReason;
    @BindView(R.id.iv_modify_client)
    ImageView ivModifyClient;
    @BindView(R.id.tv_client)
    TextView tvClient;
    @BindView(R.id.rl_client)
    RelativeLayout rlClient;
    @BindView(R.id.tv_sale)
    TextView tvSale;
    @BindView(R.id.tv_manager)
    TextView tvManager;
    @BindView(R.id.iv_modify_steel)
    ImageView ivModifySteel;
    @BindView(R.id.tv_factory)
    TextView tvFactory;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.rv_factory_data1)
    RecyclerView rvFactoryData1;
    @BindView(R.id.rv_factory_data2)
    RecyclerView rvFactoryData2;
    @BindView(R.id.rv_factory_data3)
    RecyclerView rvFactoryData3;
    @BindView(R.id.iv_modify_machine)
    ImageView ivModifyMachine;
    @BindView(R.id.tv_machine_type)
    TextView tvMachineType;
    @BindView(R.id.tv_section)
    TextView tvSection;
    @BindView(R.id.tv_steel_type)
    TextView tvSteelType;
    @BindView(R.id.tv_speed)
    TextView tvSpeed;
    @BindView(R.id.rl_middle)
    RelativeLayout rlMiddle;
    @BindView(R.id.tv_middle1)
    TextView tvMiddle1;
    @BindView(R.id.tv_middle2)
    TextView tvMiddle2;
    @BindView(R.id.tv_middle3)
    TextView tvMiddle3;
    @BindView(R.id.tv_middle4)
    TextView tvMiddle4;
    @BindView(R.id.tv_middle5)
    TextView tvMiddle5;
    @BindView(R.id.tv_middle6)
    TextView tvMiddle6;
    @BindView(R.id.tv_middle7)
    TextView tvMiddle7;
    @BindView(R.id.tv_middle8)
    TextView tvMiddle8;
    @BindView(R.id.ll_middle)
    LinearLayout llMiddle;
    @BindView(R.id.rl_cover)
    RelativeLayout rlCover;
    @BindView(R.id.tv_cover1)
    TextView tvCover1;
    @BindView(R.id.tv_cover2)
    TextView tvCover2;
    @BindView(R.id.tv_cover3)
    TextView tvCover3;
    @BindView(R.id.tv_cover4)
    TextView tvCover4;
    @BindView(R.id.ll_cover)
    LinearLayout llCover;
    @BindView(R.id.rl_protection)
    RelativeLayout rlProtection;
    @BindView(R.id.tv_protection1)
    TextView tvProtection1;
    @BindView(R.id.tv_protection2)
    TextView tvProtection2;
    @BindView(R.id.tv_protection3)
    TextView tvProtection3;
    @BindView(R.id.tv_protection4)
    TextView tvProtection4;
    @BindView(R.id.ll_protection)
    LinearLayout llProtection;
    @BindView(R.id.rv_data1)
    RecyclerView rvData1;
    @BindView(R.id.tv_add1)
    TextView tvAdd1;
    @BindView(R.id.rv_data2)
    RecyclerView rvData2;
    @BindView(R.id.rv_data3)
    RecyclerView rvData3;
    @BindView(R.id.tv_add3)
    TextView tvAdd3;
    @BindView(R.id.rv_data4)
    RecyclerView rvData4;
    @BindView(R.id.tv_add4)
    TextView tvAdd4;
    @BindView(R.id.rl_add)
    RelativeLayout rlAdd;
    @BindView(R.id.tv_factory_desc1)
    TextView tvFactoryDesc1;
    @BindView(R.id.tv_factory_desc2)
    TextView tvFactoryDesc2;
    @BindView(R.id.tv_factory_desc3)
    TextView tvFactoryDesc3;
    @BindView(R.id.rv_data5)
    RecyclerView rvData5;
    @BindView(R.id.tv_add5)
    TextView tvAdd5;
    @BindView(R.id.rv_data6)
    RecyclerView rvData6;
    @BindView(R.id.tv_add6)
    TextView tvAdd6;
    @BindView(R.id.tv_add2)
    TextView tvAdd2;
    @BindView(R.id.tv_machine_name)
    TextView tvMachineName;
    @BindView(R.id.rv_data7)
    RecyclerView rvData7;
    @BindView(R.id.tv_add7)
    TextView tvAdd7;
    @BindView(R.id.tv_check_name)
    TextView tvCheckName;
    @BindView(R.id.tv_check_time)
    TextView tvCheckTime;
    @BindView(R.id.tv_make_name)
    TextView tvMakeName;
    @BindView(R.id.tv_make_time)
    TextView tvMakeTime;
    @BindView(R.id.tv_update_name)
    TextView tvUpdateName;
    @BindView(R.id.tv_update_time)
    TextView tvUpdateTime;
    private UserDataMachineAdapter data1Adapter;
    private UserDataMachineAdapter data2Adapter;
    private UserDataMachineAdapter data3Adapter;
    private UserDataMachineAdapter data4Adapter;
    private MachineDetailBean machineDetailBean;
    private List<String> data1List;
    private List<String> data2List;
    private List<String> data3List;
    private List<String> data4List;
    private List<FactoryDetailBean.SteelFurnListBean> dataFactory1List;
    private List<FactoryDetailBean.SteelRefFurnListBean> dataFactory2List;
    private List<FactoryDetailBean.SteelLadleListBean> dataFactory3List;
    private UserDataAdd3Adapter factory1Adapter;
    private UserDataAdd3RefAdapter factory2Adapter;
    private UserDataAdd3LabelAdapter factory3Adapter;

    private UserDataMachineAdapter data5Adapter;
    private UserDataMachineAdapter data6Adapter;
    private UserDataMachineAdapter data7Adapter;
    private List<String> data5List;
    private List<String> data6List;
    private List<String> data7List;
    private UserDataDetailBean userDataDetailBean;
    private Menu userDataMenu;


    @Override
    protected void initView() {
        ButterKnife.bind(this);
        ivModifyClient.setVisibility(View.GONE);
        ivModifyMachine.setVisibility(View.GONE);
        ivModifySteel.setVisibility(View.GONE);
        tvAdd1.setVisibility(View.GONE);
        tvAdd2.setVisibility(View.GONE);
        tvAdd3.setVisibility(View.GONE);
        tvAdd4.setVisibility(View.GONE);
        tvAdd5.setVisibility(View.GONE);
        tvAdd6.setVisibility(View.GONE);
        tvAdd7.setVisibility(View.GONE);
        rvData1.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        rvData2.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        rvData3.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        rvData4.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        rvData5.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        rvData6.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        rvData7.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));

        rvFactoryData1.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        rvFactoryData2.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        rvFactoryData3.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
    }

    @Override
    protected void initData() {
        data1List = new ArrayList<>();
        data2List = new ArrayList<>();
        data3List = new ArrayList<>();
        data4List = new ArrayList<>();
        data5List = new ArrayList<>();
        data6List = new ArrayList<>();
        data7List = new ArrayList<>();
        data1Adapter = new UserDataMachineAdapter(R.layout.item_machine, data1List);
        data2Adapter = new UserDataMachineAdapter(R.layout.item_machine, data2List);
        data3Adapter = new UserDataMachineAdapter(R.layout.item_machine, data3List);
        data4Adapter = new UserDataMachineAdapter(R.layout.item_machine, data4List);
        data5Adapter = new UserDataMachineAdapter(R.layout.item_machine, data5List);
        data6Adapter = new UserDataMachineAdapter(R.layout.item_machine, data6List);
        data7Adapter = new UserDataMachineAdapter(R.layout.item_machine, data7List);
        data1Adapter.bindToRecyclerView(rvData1);
        data2Adapter.bindToRecyclerView(rvData2);
        data3Adapter.bindToRecyclerView(rvData3);
        data4Adapter.bindToRecyclerView(rvData4);
        data5Adapter.bindToRecyclerView(rvData5);
        data6Adapter.bindToRecyclerView(rvData6);
        data7Adapter.bindToRecyclerView(rvData7);
        dataFactory1List = new ArrayList<>();
        dataFactory2List = new ArrayList<>();
        dataFactory3List = new ArrayList<>();
//        factory1Adapter = new UserDataDetailFactory1Adapter(R.layout.item_user_detail_factory1, dataFactory1List);
//        factory2Adapter = new UserDataDetailFactory2Adapter(R.layout.item_user_detail_factory2, dataFactory2List);
//        factory3Adapter = new UserDataAdd3LabelAdapter(R.layout.item_add3_label, dataFactory3List);
        factory1Adapter = new UserDataAdd3Adapter(R.layout.item_user_add3, dataFactory1List);
        factory2Adapter = new UserDataAdd3RefAdapter(R.layout.item_user_add3_ref, dataFactory2List);
        factory3Adapter = new UserDataAdd3LabelAdapter(R.layout.item_add3_label, dataFactory3List);

        factory1Adapter.bindToRecyclerView(rvFactoryData1);
        factory2Adapter.bindToRecyclerView(rvFactoryData2);
        factory3Adapter.bindToRecyclerView(rvFactoryData3);
        getData();
    }

    private void isCanModify(String customerUserCode) {
        Map<String, Object> param = WorkUtils.simpleParam();
        param.put("customerUserCode", customerUserCode);
        RtfUtils.getMesRtf().canModify("1", 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()) {
                    return;
                }
                if (TextUtils.equals(data.getData().toString(), "1") || TextUtils.equals("2", getIntent().getStringExtra("status"))) {
                    userDataMenu.findItem(R.id.modify).setVisible(true);
                } else {
                    userDataMenu.findItem(R.id.modify).setVisible(false);
                }
            }
        });
    }

    private void getData() {
        Map<String, Object> param = WorkUtils.simpleParam();
        param.put("id", getIntent().getStringExtra("id"));
        RtfUtils.getMesRtf().userDataDetail("1", 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;
                }
                userDataDetailBean = JSON.parseObject(data.getData().toString(), UserDataDetailBean.class);

                UserDataDetailBean.MainInfoBean mainInfo = userDataDetailBean.getMainInfo();
                if (mainInfo != null) {
                    isCanModify(mainInfo.getCustomerUserCode());
                    tvClient.setText(mainInfo.getCustomerUserName());
                    tvManager.setText(mainInfo.getSalesManagerName());
                    tvSale.setText(mainInfo.getSalesCompanyName());

                    tvCheckName.setText(mainInfo.getApprovalUserName());
                    tvCheckTime.setText(TimeUtils.millis2String(mainInfo.getApprovalDateTime()));
                    tvUpdateName.setText(mainInfo.getUpdateUserName());
                    tvUpdateTime.setText(TimeUtils.millis2String(mainInfo.getUpdateDateTime()));
                    tvMakeName.setText(mainInfo.getCreateUserName());
                    tvMakeTime .setText(TimeUtils.millis2String(mainInfo.getCreateDateTime()));



                    if (TextUtils.equals("1", getIntent().getStringExtra("from"))) {
                        if (mainInfo.getApprovalStatus() != 0) {
                            llBottom.setVisibility(View.GONE);
                        } else {
                            if (WorkUtils.hasMesMenuPermission(JSON.parseArray(getIntent().getStringExtra("permission")), "审核")) {
                                llBottom.setVisibility(View.VISIBLE);
                            }
                        }
                    } else {
                        if (mainInfo.getApprovalStatus() == 2) {
                            llReason.setVisibility(View.VISIBLE);
                            tvReason.setText(mainInfo.getRemark());
                        } else {
                            llReason.setVisibility(View.GONE);
                        }
                    }
                }
                getMachineData(userDataDetailBean.getSteelInfo().getContinuousCasterCode());
                getFactoryData(userDataDetailBean.getSteelInfo().getSteelmakingWorksCode());
            }
        });
    }

    private void getFactoryData(String code) {
        Map<String, Object> param = WorkUtils.simpleParam();
        param.put("steelmakingWorksCode", code);
        RtfUtils.getMesRtf().factoryDetail("1", 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;
                }
                FactoryDetailBean factoryDetailBean = JSON.parseObject(data.getData().toString(), FactoryDetailBean.class);
                FactoryDetailBean.SteelWorksBean steelWorks = factoryDetailBean.getSteelWorks();
                if (steelWorks != null) {
                    tvNum.setText(steelWorks.getAnnualOutput());
                    tvFactory.setText(steelWorks.getSteelmakingWorksName());
                    tvType.setText(steelWorks.getSteelGrade());
                }
                if (factoryDetailBean.getSteelFurnList() != null) {
                    dataFactory1List.clear();
                    dataFactory1List.addAll(factoryDetailBean.getSteelFurnList());
                    factory1Adapter.notifyDataSetChanged();
                }
                if (factoryDetailBean.getSteelRefFurnList() != null) {
                    dataFactory2List.clear();
                    dataFactory2List.addAll(factoryDetailBean.getSteelRefFurnList());
                    factory2Adapter.notifyDataSetChanged();
                }
                if (factoryDetailBean.getSteelLadleList() != null) {
                    dataFactory3List.clear();
                    dataFactory3List.addAll(factoryDetailBean.getSteelLadleList());
                    factory3Adapter.notifyDataSetChanged();
                }

            }
        });
    }

    private void getMachineData(String code) {
        Map<String, Object> param = WorkUtils.simpleParam();
        param.put("continuousCasterCode", code);
        RtfUtils.getMesRtf().machineDetail("1", 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;
                }
                machineDetailBean = JSON.parseObject(data.getData().toString(), MachineDetailBean.class);
                MachineDetailBean.CastInfoBean castInfo = machineDetailBean.getCastInfo();
                if (castInfo != null) {
                    tvMachineName.setText(castInfo.getContinuousCasterName());
                    tvMachineType.setText(castInfo.getContinuousCasterType());
                    tvSection.setText(castInfo.getFractureSurface());
                    tvSteelType.setText(castInfo.getSteelGrade());
                    tvSpeed.setText(castInfo.getCastingSpeed());
                }
                MachineDetailBean.CastLadleBean castLadle = machineDetailBean.getCastLadle();
                if (castLadle != null) {
                    tvMiddle1.setText(castLadle.getCapacity());
                    tvMiddle2.setText(castLadle.getLifeSpan());
//                    tvMiddle3.setText(castLadle.getLadleCode());
                    tvMiddle4.setText(castLadle.getLiquidLevel());
                    tvMiddle5.setText(castLadle.getLiquidSteelTemperature());
                    tvMiddle6.setText(castLadle.getBakingMethod());
                    tvMiddle7.setText(castLadle.getBakingTemperature());
                    tvMiddle8.setText(String.valueOf(castLadle.getBakingDateTime()));
                }
                changeList();
            }
        });
    }

    private void changeList() {
        data1List.clear();
        data2List.clear();
        data3List.clear();
        data4List.clear();
        data5List.clear();
        data6List.clear();
        data7List.clear();
        if (machineDetailBean.getProductList() != null) {
            for (MachineDetailBean.ProductListBean productListBean : machineDetailBean.getProductList()) {
                data1List.add(productListBean.getProductsName());
            }
            data1Adapter.notifyDataSetChanged();
        }
        if (machineDetailBean.getSupplyList() != null) {
            for (MachineDetailBean.SupplyListBean productListBean : machineDetailBean.getSupplyList()) {
                data2List.add(productListBean.getProductsName());
            }
            data2Adapter.notifyDataSetChanged();
        }
        if (machineDetailBean.getOtherSupList() != null) {
            for (MachineDetailBean.OtherSupListBean productListBean : machineDetailBean.getOtherSupList()) {
                data3List.add(productListBean.getSupplierName());
            }
            data3Adapter.notifyDataSetChanged();
        }
        if (machineDetailBean.getOtherLadleList() != null) {
            for (MachineDetailBean.OtherLadleListBean productListBean : machineDetailBean.getOtherLadleList()) {
                data4List.add(productListBean.getRefractoryName());
            }
            data4Adapter.notifyDataSetChanged();
        }
        if (machineDetailBean.getStockList() != null) {
            for (MachineDetailBean.StockListBean productListBean : machineDetailBean.getStockList()) {
                data5List.add(productListBean.getProductsName());
            }
            data5Adapter.notifyDataSetChanged();
        }
        if (machineDetailBean.getSettleList() != null) {
            for (MachineDetailBean.SettleListBean productListBean : machineDetailBean.getSettleList()) {
                data6List.add(productListBean.getSettlementMethod());
            }
            data6Adapter.notifyDataSetChanged();
        }
        if (machineDetailBean.getSpeeds() != null) {
            for (MachineDetailBean.SpeedsBean productListBean : machineDetailBean.getSpeeds()) {
                data7List.add(productListBean.getSteelGrade());
            }
            data7Adapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void initEvent() {
        data1Adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(MesThreeDetailActivity.class, "id", machineDetailBean.getProductList().get(position).getId());
            }
        });
        data2Adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(MesSupplierActivity.class, "data", JSON.toJSONString(machineDetailBean.getSupplyList().get(position)));
            }
        });
        data3Adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(MesOtherSupplierDetailActivity.class, "id", machineDetailBean.getOtherSupList().get(position).getId());
            }
        });
        data4Adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(MesPackageDetailActivity.class, "id", machineDetailBean.getOtherLadleList().get(position).getId());
            }
        });
        data5Adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(MesStockDetailActivity.class, "data", JSON.toJSONString(machineDetailBean.getStockList().get(position)));
            }
        });
        data6Adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(MesMiddleDetailActivity.class, "data", JSON.toJSONString(machineDetailBean.getSettleList().get(position)));
            }
        });
        data7Adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(MesSpeedDetailActivity.class, "data", JSON.toJSONString(machineDetailBean.getSpeeds().get(position)));
            }
        });
    }

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


    @OnClick({R.id.tv_add7, R.id.tv_add5, R.id.tv_add6, R.id.btn_agree,
            R.id.btn_refuse, R.id.tv_add1, R.id.tv_add3, R.id.tv_add4, R.id.rl_add, R.id.rl_middle, R.id.rl_cover, R.id.rl_protection})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_add7:
                if (userDataDetailBean == null) {
                    toast("数据加载中，请稍后");
                    return;
                }
                startActivity(MesSpeedAddActivity.class, "continuousCasterCode", userDataDetailBean.getSteelInfo().getContinuousCasterCode()
                        , "user", userDataDetailBean.getMainInfo().getCustomerUserCode());
                break;
            case R.id.tv_add1:
                if (userDataDetailBean == null) {
                    toast("数据加载中，请稍后");
                    return;
                }
                startActivity(MesThreeAddActivity.class, "continuousCasterCode", userDataDetailBean.getSteelInfo().getContinuousCasterCode()
                        , "user", userDataDetailBean.getMainInfo().getCustomerUserCode());
                break;
            case R.id.tv_add2:
                if (userDataDetailBean == null) {
                    toast("数据加载中，请稍后");
                    return;
                }
                startActivity(MesSupplierAddActivity.class, "continuousCasterCode", userDataDetailBean.getSteelInfo().getContinuousCasterCode()
                        , "user", userDataDetailBean.getMainInfo().getCustomerUserCode());
                break;
            case R.id.tv_add3:
                if (userDataDetailBean == null) {
                    toast("数据加载中，请稍后");
                    return;
                }
                startActivity(MesOtherSupplierAddActivity.class, "continuousCasterCode", userDataDetailBean.getSteelInfo().getContinuousCasterCode()
                        , "user", userDataDetailBean.getMainInfo().getCustomerUserCode());
                break;
            case R.id.tv_add4:
                if (userDataDetailBean == null) {
                    toast("数据加载中，请稍后");
                    return;
                }
                startActivity(MesPackageAddActivity.class, "continuousCasterCode", userDataDetailBean.getSteelInfo().getContinuousCasterCode()
                        , "user", userDataDetailBean.getMainInfo().getCustomerUserCode());
                break;
            case R.id.tv_add5:
                if (userDataDetailBean == null) {
                    toast("数据加载中，请稍后");
                    return;
                }
                startActivity(MesStockAddActivity.class,
                        "continuousCasterCode", userDataDetailBean.getSteelInfo().getContinuousCasterCode()
                        , "id", userDataDetailBean.getMainInfo().getCustomerUserCode());
                break;
            case R.id.tv_add6:
                if (userDataDetailBean == null) {
                    toast("数据加载中，请稍后");
                    return;
                }
                startActivity(MesMiddleAddActivity.class, "continuousCasterCode", userDataDetailBean.getSteelInfo().getContinuousCasterCode()
                        , "user", userDataDetailBean.getMainInfo().getCustomerUserCode());
                break;
            case R.id.rl_add:
//                startActivity(MesMiddleAddActivity.class);
                break;
            case R.id.btn_agree:
                openCheck(1);
                break;
            case R.id.btn_refuse:
                openCheck(2);
                break;
            case R.id.rl_middle:
                if (llMiddle.getVisibility() == View.VISIBLE) {
                    llMiddle.setVisibility(View.GONE);
                } else {
                    llMiddle.setVisibility(View.VISIBLE);
                }
                break;
            case R.id.rl_cover:
                if (llCover.getVisibility() == View.VISIBLE) {
                    llCover.setVisibility(View.GONE);
                } else {
                    llCover.setVisibility(View.VISIBLE);
                }
                break;
            case R.id.rl_protection:
                if (llProtection.getVisibility() == View.VISIBLE) {
                    llProtection.setVisibility(View.GONE);
                } else {
                    llProtection.setVisibility(View.VISIBLE);
                }
                break;
        }
    }

    private void openCheck(int type) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View view = LayoutInflater.from(this).inflate(R.layout.view_user_check, null);
        TextView tvDesc = view.findViewById(R.id.tv_desc);
        tvDesc.setText(type == 1 ? "通过" : "驳回原因");
        TextView btnOk = view.findViewById(R.id.btn_ok);
        EditText etContent = view.findViewById(R.id.et_content);
        TextView tvClose = view.findViewById(R.id.tv_close);
        builder.setView(view);
        AlertDialog addCartDialog = builder.create();
        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submitCheck(type, etContent.getText().toString());
                addCartDialog.dismiss();
            }
        });
        tvClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addCartDialog.dismiss();
            }
        });
        addCartDialog.show();
    }

    private void submitCheck(int type, String remark) {
        Map<String, Object> param = WorkUtils.simpleParam();
        param.put("id", getIntent().getStringExtra("id"));
        param.put("approvalStatus", type == 1 ? 1 : 2);
        param.put("isCustomerArchives", type == 1);
        param.put("remark", remark);
        RtfUtils.getMesRtf().userDataCheck("1", 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;
                }
                llBottom.setVisibility(View.GONE);
                toast("提交成功");
                EventBus.getDefault().post(new AddUserDataEvent());
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        userDataMenu = menu;
        getMenuInflater().inflate(R.menu.menu_modify, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.modify:
                startActivity(MesUserDataDetailModifyActivity.class, "from", getIntent().getStringExtra("from"),
                        "id", getIntent().getStringExtra("id"), "status",
                        getIntent().getStringExtra("status"));
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    //    @Subscribe
//    public void refresh(RefreshMachineEvent event) {
//        getData();
//    }
    @Subscribe
    public void refresh(ModifyUserDataEvent event) {
        getData();
    }


    @Subscribe
    public void refresh(RefreshMachineEvent event) {
        getData();
    }

}
