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

import android.content.Intent;
import android.text.TextUtils;
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.bigkoo.pickerview.listener.OnTimeSelectListener;
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.AddMachineAddChildEvent;
import com.wd.workoffice.bean.event.AddMachineModifyChildEvent;
import com.wd.workoffice.bean.event.AddUserDataEvent;
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.MesClientBean;
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.UserDataAdd3AddAdapter;
import com.wd.workoffice.ui.adapter.UserDataAdd3LabelAddAdapter;
import com.wd.workoffice.ui.adapter.UserDataAdd3RefAddAdapter;
import com.wd.workoffice.ui.adapter.UserDataMachineAdapter;
import com.wd.workoffice.util.PickTimeUtils;
import com.wd.workoffice.util.WorkUtils;

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

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

import androidx.annotation.Nullable;
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 flexible.xd.android_base.utils.TimeUtils;
import io.reactivex.disposables.Disposable;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 用户数据采集，
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class MesUserDataDetailAddActivity extends WorkToolBarActivity {
    @BindView(R.id.btn_refuse)
    Button btnRefuse;
    @BindView(R.id.btn_agree)
    Button btnAgree;
    @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)
    EditText tvFactory;
    @BindView(R.id.tv_num)
    EditText tvNum;
    @BindView(R.id.tv_type)
    EditText tvType;
    @BindView(R.id.tv_factory_desc1)
    TextView tvFactoryDesc1;
    @BindView(R.id.rv_factory_data1)
    RecyclerView rvFactoryData1;
    @BindView(R.id.tv_factory_desc2)
    TextView tvFactoryDesc2;
    @BindView(R.id.rv_factory_data2)
    RecyclerView rvFactoryData2;
    @BindView(R.id.tv_factory_desc3)
    TextView tvFactoryDesc3;
    @BindView(R.id.rv_factory_data3)
    RecyclerView rvFactoryData3;
    @BindView(R.id.iv_modify_machine)
    ImageView ivModifyMachine;
    @BindView(R.id.tv_machine_type)
    EditText tvMachineType;
    @BindView(R.id.tv_section)
    EditText tvSection;
    @BindView(R.id.tv_steel_type)
    EditText tvSteelType;
    @BindView(R.id.tv_speed)
    EditText tvSpeed;
    @BindView(R.id.rl_middle)
    RelativeLayout rlMiddle;
    @BindView(R.id.tv_middle1)
    EditText tvMiddle1;
    @BindView(R.id.tv_middle2)
    EditText tvMiddle2;
    @BindView(R.id.tv_middle3)
    EditText tvMiddle3;
    @BindView(R.id.tv_middle4)
    EditText tvMiddle4;
    @BindView(R.id.tv_middle5)
    EditText tvMiddle5;
    @BindView(R.id.tv_middle6)
    EditText tvMiddle6;
    @BindView(R.id.tv_middle7)
    EditText tvMiddle7;
    @BindView(R.id.tv_middle8)
    EditText tvMiddle8;
    @BindView(R.id.ll_middle)
    LinearLayout llMiddle;
    @BindView(R.id.rl_cover)
    RelativeLayout rlCover;
    @BindView(R.id.tv_cover1)
    EditText tvCover1;
    @BindView(R.id.tv_cover2)
    EditText tvCover2;
    @BindView(R.id.tv_cover3)
    EditText tvCover3;
    @BindView(R.id.tv_cover4)
    TextView tvCover4;
    @BindView(R.id.rl_cover4)
    RelativeLayout rlCover4;
    @BindView(R.id.ll_cover)
    LinearLayout llCover;
    @BindView(R.id.rl_protection)
    RelativeLayout rlProtection;
    @BindView(R.id.tv_protection1)
    EditText tvProtection1;
    @BindView(R.id.tv_protection2)
    EditText tvProtection2;
    @BindView(R.id.tv_protection3)
    EditText tvProtection3;
    @BindView(R.id.tv_protection4)
    TextView tvProtection4;
    @BindView(R.id.rl_protection4)
    RelativeLayout rlProtection4;
    @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_data5)
    RecyclerView rvData5;
    @BindView(R.id.tv_add5)
    TextView tvAdd5;
    @BindView(R.id.rv_data4)
    RecyclerView rvData4;
    @BindView(R.id.tv_add4)
    TextView tvAdd4;
    @BindView(R.id.rv_data6)
    RecyclerView rvData6;
    @BindView(R.id.tv_add6)
    TextView tvAdd6;
    @BindView(R.id.rl_add)
    RelativeLayout rlAdd;
    @BindView(R.id.tv_machine_name)
    EditText tvMachineName;
    @BindView(R.id.tv_add2)
    TextView tvAdd2;
    @BindView(R.id.tv_factory_add1)
    TextView tvFactoryAdd1;
    @BindView(R.id.tv_factory_add2)
    TextView tvFactoryAdd2;
    @BindView(R.id.tv_factory_add3)
    TextView tvFactoryAdd3;
    private UserDataMachineAdapter data1Adapter;
    private UserDataMachineAdapter data2Adapter;
    private UserDataMachineAdapter data3Adapter;
    private UserDataMachineAdapter data4Adapter;
    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 UserDataAdd3AddAdapter factory1Adapter;
    private UserDataAdd3RefAddAdapter factory2Adapter;
    private UserDataAdd3LabelAddAdapter factory3Adapter;


    private UserDataMachineAdapter data5Adapter;
    private UserDataMachineAdapter data6Adapter;
    private List<String> data5List;
    private List<String> data6List;
    private UserDataDetailBean dataParam;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
//        changeViewModify(1);
//        changeViewModify(2);
        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));

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

        llReason.setVisibility(View.GONE);
        llBottom.setVisibility(View.GONE);
    }

    @Override
    protected void initData() {
        dataParam = JSON.parseObject(getIntent().getStringExtra("data"), UserDataDetailBean.class);
        data1List = new ArrayList<>();
        data2List = new ArrayList<>();
        data3List = new ArrayList<>();
        data4List = new ArrayList<>();
        data5List = new ArrayList<>();
        data6List = 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);
        data1Adapter.bindToRecyclerView(rvData1);
        data2Adapter.bindToRecyclerView(rvData2);
        data3Adapter.bindToRecyclerView(rvData3);
        data4Adapter.bindToRecyclerView(rvData4);
        data5Adapter.bindToRecyclerView(rvData5);
        data6Adapter.bindToRecyclerView(rvData6);
        dataFactory1List = new ArrayList<>();
        dataFactory2List = new ArrayList<>();
        dataFactory3List = new ArrayList<>();
        factory1Adapter = new UserDataAdd3AddAdapter(R.layout.item_user_add3_add, dataFactory1List);
        factory2Adapter = new UserDataAdd3RefAddAdapter(R.layout.item_user_add3_add, dataFactory2List);
        factory3Adapter = new UserDataAdd3LabelAddAdapter(R.layout.item_user_add_factory_label, dataFactory3List);
        factory1Adapter.bindToRecyclerView(rvFactoryData1);
        factory2Adapter.bindToRecyclerView(rvFactoryData2);
        factory3Adapter.bindToRecyclerView(rvFactoryData3);
        changeInfo();
    }



    private void changeInfo() {
        UserDataDetailBean.MainInfoBean mainInfo = dataParam.getMainInfo();
        if (mainInfo != null) {
            tvClient.setText(mainInfo.getCustomerUserName());
            tvManager.setText(mainInfo.getSalesManagerName());
            tvSale.setText(mainInfo.getSalesCompanyName());
            tvReason.setText(mainInfo.getRemark());
        }
        if (!TextUtils.equals("1", getIntent().getStringExtra("from"))) {//不是多步新增，需获取数据
            getMachineData(dataParam.getSteelInfo().getContinuousCasterCode());
            getFactoryData(dataParam.getSteelInfo().getSteelmakingWorksCode());
        }else {
            changeFactoryInfo();
            changeList();
        }
    }

    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);
                dataParam.setSteelWorks(factoryDetailBean.getSteelWorks());
                dataParam.setSteelFurnList(factoryDetailBean.getSteelFurnList());
                dataParam.setSteelRefFurnList(factoryDetailBean.getSteelRefFurnList());
                dataParam.setSteelLadleList(factoryDetailBean.getSteelLadleList());
                changeFactoryInfo();
            }
        });
    }

    private void changeFactoryInfo() {
        FactoryDetailBean.SteelWorksBean steelWorks = dataParam.getSteelWorks();
        if (steelWorks != null) {
            tvNum.setText(steelWorks.getAnnualOutput());
            tvFactory.setText(steelWorks.getSteelmakingWorksName());
            tvType.setText(steelWorks.getSteelGrade());
        }
        if (dataParam.getSteelFurnList() != null) {
            dataFactory1List.clear();
            dataFactory1List.addAll(dataParam.getSteelFurnList());
            factory1Adapter.notifyDataSetChanged();
        }
        if (dataParam.getSteelRefFurnList() != null) {
            dataFactory2List.clear();
            dataFactory2List.addAll(dataParam.getSteelRefFurnList());
            factory2Adapter.notifyDataSetChanged();
        }
        if (dataParam.getSteelLadleList() != null) {
            dataFactory3List.clear();
            dataFactory3List.addAll(dataParam.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 machineDetailBean = JSON.parseObject(data.getData().toString(), MachineDetailBean.class);
                dataParam.setCastInfo(machineDetailBean.getCastInfo());
                dataParam.setCastLadle(machineDetailBean.getCastLadle());
                dataParam.setCovAgent(machineDetailBean.getCovAgent());
                dataParam.setMouldFlu(machineDetailBean.getMouldFlu());
                changeList();
            }
        });
    }

    private void changeList() {
        MachineDetailBean.CastInfoBean castInfo = dataParam.getCastInfo();
        if (castInfo != null) {
            tvMachineType.setText(castInfo.getContinuousCasterType());
            tvMachineName.setText(castInfo.getContinuousCasterName());
            tvSection.setText(castInfo.getFractureSurface());
            tvSteelType.setText(castInfo.getSteelGrade());
            tvSpeed.setText(castInfo.getCastingSpeed());
        }
        MachineDetailBean.CastLadleBean castLadle = dataParam.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()));
        }
        MachineDetailBean.CovAgentBean covAgent = dataParam.getCovAgent();
        if (covAgent != null) {
            tvCover1.setText(covAgent.getLadleCoveringAgentName());
            tvCover2.setText(covAgent.getAcidAlkaliDegree());
            tvCover3.setText(covAgent.getChemicalComposition());
            tvCover4.setText(covAgent.getSupplierName());
        }
        MachineDetailBean.MouldFluBean mouldFlu = dataParam.getMouldFlu();
        if (mouldFlu != null) {
            tvProtection1.setText(mouldFlu.getMouldFluxesName());
            tvProtection2.setText(mouldFlu.getAcidAlkaliDegree());
            tvProtection3.setText(mouldFlu.getChemicalComposition());
            tvProtection4.setText(mouldFlu.getSupplierName());
        }
        data1List.clear();
        data2List.clear();
        data3List.clear();
        data4List.clear();
        data5List.clear();
        data6List.clear();
        if (dataParam.getProductList() != null) {
            for (MachineDetailBean.ProductListBean productListBean : dataParam.getProductList()) {
                data1List.add(productListBean.getProductsName());
            }
            data1Adapter.notifyDataSetChanged();
        }
        if (dataParam.getSupplyList() != null) {
            for (MachineDetailBean.SupplyListBean productListBean : dataParam.getSupplyList()) {
                data2List.add(productListBean.getProductsName());
            }
            data2Adapter.notifyDataSetChanged();
        }
        if (dataParam.getOtherSupList() != null) {
            for (MachineDetailBean.OtherSupListBean productListBean : dataParam.getOtherSupList()) {
                data3List.add(productListBean.getSupplierName());
            }
            data3Adapter.notifyDataSetChanged();
        }
        if (dataParam.getOtherLadleList() != null) {
            for (MachineDetailBean.OtherLadleListBean productListBean : dataParam.getOtherLadleList()) {
                data4List.add(productListBean.getRefractoryName());
            }
            data4Adapter.notifyDataSetChanged();
        }
        if (dataParam.getStockList() != null) {
            for (MachineDetailBean.StockListBean productListBean : dataParam.getStockList()) {
                data5List.add(productListBean.getProductsName());
            }
            data5Adapter.notifyDataSetChanged();
        }
        if (dataParam.getSettleList() != null) {
            for (MachineDetailBean.SettleListBean productListBean : dataParam.getSettleList()) {
                data6List.add(productListBean.getSettlementMethod());
            }
            data6Adapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void initEvent() {
        factory1Adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                dataFactory1List.remove(position);
                factory1Adapter.notifyDataSetChanged();
            }
        });
        factory2Adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                dataFactory2List.remove(position);
                data2Adapter.notifyDataSetChanged();
            }
        });
        factory3Adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.tv_delete:
                        dataFactory3List.remove(position);
                        factory3Adapter.notifyDataSetChanged();
                        break;
                    case R.id.rl_time:
                        PickTimeUtils.showPickThree(MesUserDataDetailAddActivity.this, new OnTimeSelectListener() {
                            @Override
                            public void onTimeSelect(Date date, View v) {
                                dataFactory3List.get(position).setCastingDateTime(TimeUtils.date2Millis(date) / 1000);
                                factory3Adapter.notifyDataSetChanged();
                            }
                        }).show();
                        break;
                }

            }
        });
        String from = getIntent().getStringExtra("from");//1 .多步骤新增（同时新增）
        data1Adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (TextUtils.equals(from, "1")) {
                    startActivity(MesThreeModifyActivity.class,
                            "position", position + "", "from", "1", "data", JSON.toJSONString(dataParam.getProductList().get(position)));
                } else {
                    startActivity(MesThreeModifyActivity.class, "data", JSON.toJSONString(dataParam.getProductList().get(position)));
                }
            }
        });
        data2Adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (TextUtils.equals(from, "1")) {
                    startActivity(MesSupplierModifyActivity.class,
                            "position", position + "", "from", "1", "data", JSON.toJSONString(dataParam.getSupplyList().get(position)));
                } else {
                    startActivity(MesSupplierModifyActivity.class, "data", JSON.toJSONString(dataParam.getSupplyList().get(position)));
                }
            }
        });
        data3Adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (TextUtils.equals(from, "1")) {
                    startActivity(MesOtherSupplierModifyActivity.class,
                            "position", position + "", "from", "1", "data", JSON.toJSONString(dataParam.getOtherSupList().get(position)));
                } else {
                    startActivity(MesOtherSupplierModifyActivity.class, "data", JSON.toJSONString(dataParam.getOtherSupList().get(position)));
                }
            }
        });
        data4Adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (TextUtils.equals(from, "1")) {
                    startActivity(MesPackageModifyActivity.class,
                            "position", position + "", "from", "1", "data", JSON.toJSONString(dataParam.getOtherLadleList().get(position)));
                } else {
                    startActivity(MesPackageModifyActivity.class, "data", JSON.toJSONString(dataParam.getOtherLadleList().get(position)));
                }
            }
        });
        data5Adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (TextUtils.equals(from, "1")) {
                    startActivity(MesStockModifyActivity.class,
                            "position", position + "", "from", "1", "data", JSON.toJSONString(dataParam.getStockList().get(position)));
                } else {
                    startActivity(MesStockModifyActivity.class, "data", JSON.toJSONString(dataParam.getStockList().get(position)));
                }
            }
        });
        data6Adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (TextUtils.equals(from, "1")) {
                    startActivity(MesMiddleModifyActivity.class,
                            "position", position + "", "from", "1", "data", JSON.toJSONString(dataParam.getSettleList().get(position)));
                } else {
                    startActivity(MesMiddleModifyActivity.class, "data", JSON.toJSONString(dataParam.getSettleList().get(position)));
                }
            }
        });
    }

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

    @OnClick({R.id.tv_factory_add1, R.id.tv_factory_add2, R.id.tv_factory_add3,
            R.id.rl_cover4, R.id.rl_protection4, R.id.iv_modify_client,
            R.id.iv_modify_machine, R.id.iv_modify_steel,
            R.id.tv_add5, R.id.tv_add6, R.id.tv_add1, R.id.tv_add3,
            R.id.tv_add4,R.id.rl_middle,R.id.rl_cover,R.id.rl_protection})
    public void onViewClicked(View view) {
        String from = getIntent().getStringExtra("from");//1 .多步骤新增（同时新增）
        switch (view.getId()) {
            case R.id.tv_factory_add1:
                FactoryDetailBean.SteelFurnListBean data1 = new FactoryDetailBean.SteelFurnListBean();
                dataFactory1List.add(data1);
                factory1Adapter.notifyDataSetChanged();
                break;
            case R.id.tv_factory_add2:
                FactoryDetailBean.SteelRefFurnListBean data2 = new FactoryDetailBean.SteelRefFurnListBean();
                dataFactory2List.add(data2);
                factory2Adapter.notifyDataSetChanged();
                break;
            case R.id.tv_factory_add3:
                FactoryDetailBean.SteelLadleListBean data3 = new FactoryDetailBean.SteelLadleListBean();
                dataFactory3List.add(data3);
                factory3Adapter.notifyDataSetChanged();
                break;
            case R.id.rl_cover4:
                startActivityForResult(MesChooseClientActivity.class,
                        10001, "from", "1");
                break;
            case R.id.rl_protection4:
                startActivityForResult(MesChooseClientActivity.class,
                        10002, "from", "1");
                break;
            case R.id.iv_modify_client:
                break;
            case R.id.iv_modify_machine:
                if (TextUtils.equals("1", from)) {
                    toast("保存成功");
                } else {
                    submitMachine();
                }
                break;
            case R.id.iv_modify_steel:
                if (TextUtils.equals("1", from)) {
                    toast("保存成功");
                } else {
                    submitFactory();
                }
                break;
            case R.id.tv_add1:
                if (TextUtils.equals("1", from)) {
                    startActivity(MesThreeAddActivity.class,
                            "from", "1", "continuousCasterCode", dataParam.getSteelInfo().getContinuousCasterCode());
                } else {
                    startActivity(MesThreeAddActivity.class, "continuousCasterCode", dataParam.getSteelInfo().getContinuousCasterCode());
                }
                break;
            case R.id.tv_add2:
                if (TextUtils.equals("1", from)) {
                    startActivity(MesSupplierAddActivity.class,
                            "from", "1", "continuousCasterCode", dataParam.getSteelInfo().getContinuousCasterCode());
                } else {
                    startActivity(MesSupplierAddActivity.class, "continuousCasterCode", dataParam.getSteelInfo().getContinuousCasterCode());
                }
//                startActivity(MesSupplierAddActivity.class, "continuousCasterCode", userDataDetailBean.getSteelInfo().getContinuousCasterCode());
                break;
            case R.id.tv_add3:
                if (TextUtils.equals("1", from)) {
                    startActivity(MesOtherSupplierAddActivity.class,
                            "from", "1", "continuousCasterCode", dataParam.getSteelInfo().getContinuousCasterCode());
                } else {
                    startActivity(MesOtherSupplierAddActivity.class, "continuousCasterCode", dataParam.getSteelInfo().getContinuousCasterCode());
                }
//                startActivity(MesOtherSupplierAddActivity.class, "continuousCasterCode", userDataDetailBean.getSteelInfo().getContinuousCasterCode());
                break;
            case R.id.tv_add4:
                if (TextUtils.equals("1", from)) {
                    startActivity(MesPackageAddActivity.class,
                            "from", "1", "continuousCasterCode", dataParam.getSteelInfo().getContinuousCasterCode());
                } else {
                    startActivity(MesPackageAddActivity.class, "continuousCasterCode", dataParam.getSteelInfo().getContinuousCasterCode());
                }
//                startActivity(MesPackageAddActivity.class, "continuousCasterCode", userDataDetailBean.getSteelInfo().getContinuousCasterCode());
                break;
            case R.id.tv_add5:
                if (TextUtils.equals("1", from)) {
                    startActivity(MesStockAddActivity.class,
                            "from", "1", "continuousCasterCode", dataParam.getSteelInfo().getContinuousCasterCode()
                            , "id", dataParam.getMainInfo().getCustomerUserCode());
                } else {
                    startActivity(MesStockAddActivity.class,
                            "continuousCasterCode", dataParam.getSteelInfo().getContinuousCasterCode()
                            , "id", dataParam.getMainInfo().getCustomerUserCode());
                }
//                startActivity(MesStockAddActivity.class,
//                        "continuousCasterCode", userDataDetailBean.getSteelInfo().getContinuousCasterCode()
//                        , "id", userDataDetailBean.getMainInfo().getCustomerUserCode());
                break;
            case R.id.tv_add6:
                if (TextUtils.equals("1", from)) {
                    startActivity(MesMiddleAddActivity.class,
                            "from", "1", "continuousCasterCode", dataParam.getSteelInfo().getContinuousCasterCode());
                } else {
                    startActivity(MesMiddleAddActivity.class, "continuousCasterCode", dataParam.getSteelInfo().getContinuousCasterCode());
                }
//                startActivity(MesMiddleAddActivity.class, "continuousCasterCode", userDataDetailBean.getSteelInfo().getContinuousCasterCode());
                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 changeViewModify(int type) {
        switch (type) {
            case 1:
                changeViewClick(tvNum, !tvNum.isClickable());
                changeViewClick(tvFactory, !tvFactory.isClickable());
                changeViewClick(tvType, !tvType.isClickable());
                break;
            case 2:
                changeViewClick(tvMachineType, !tvMachineType.isClickable());
                changeViewClick(tvSection, !tvSection.isClickable());
                changeViewClick(tvSteelType, !tvSteelType.isClickable());
                changeViewClick(tvSpeed, !tvSpeed.isClickable());

                changeViewClick(tvMiddle1, !tvMiddle1.isClickable());
                changeViewClick(tvMiddle2, !tvMiddle2.isClickable());
                changeViewClick(tvMiddle3, !tvMiddle3.isClickable());
                changeViewClick(tvMiddle4, !tvMiddle4.isClickable());
                changeViewClick(tvMiddle5, !tvMiddle5.isClickable());
                changeViewClick(tvMiddle6, !tvMiddle6.isClickable());
                changeViewClick(tvMiddle7, !tvMiddle7.isClickable());
                changeViewClick(tvMiddle8, !tvMiddle8.isClickable());

                changeViewClick(tvCover1, !tvCover1.isClickable());
                changeViewClick(tvCover2, !tvCover2.isClickable());
                changeViewClick(tvCover3, !tvCover3.isClickable());
//                changeViewClick(tvCover4, !tvCover4.isClickable());
                rlCover4.setClickable(!rlCover4.isClickable());
                changeViewClick(tvProtection1, !tvProtection1.isClickable());
                changeViewClick(tvProtection2, !tvProtection2.isClickable());
                changeViewClick(tvProtection3, !tvProtection3.isClickable());
//                changeViewClick(tvProtection4, !tvProtection4.isClickable());
                tvProtection4.setClickable(!tvProtection4.isClickable());
                break;
        }
    }

    private void changeViewClick(EditText view, boolean canClick) {
        view.setClickable(canClick);
        view.setFocusable(canClick);
        view.setFocusableInTouchMode(canClick);
    }


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


    private void submitAll() {
//        Map<String, Object> param = WorkUtils.simpleParam();
//        param.put("mainInfo", data);
//        JSONObject steelInfo = new JSONObject();
//        steelInfo.put("steelmakingWorksCode", data.getSteelmakingWorksCode());
////        steelInfo.put("steelmakingFurnaceCode", data.getSteelmakingWorksCode());
////        steelInfo.put("refiningFurnaceCode", data.getSteelmakingWorksCode());
////        steelInfo.put("ladleCode", data.getSteelmakingWorksCode());
//        steelInfo.put("continuousCasterCode", data.getContinuousCasterCode());
//        param.put("steelInfo", steelInfo);
//        Map<String, Object> factoryMap = JSON.parseObject(getIntent().getStringExtra("factory"), Map.class);
//        Map<String, Object> machineMap = JSON.parseObject(JSON.toJSONString(machineDetailBean), Map.class);
//        for (Map.Entry<String, Object> factoryEntry : factoryMap.entrySet()) {
//            param.put(factoryEntry.getKey(), factoryEntry.getValue());
//        }
//        for (Map.Entry<String, Object> machineEntry : machineMap.entrySet()) {
//            param.put(machineEntry.getKey(), machineEntry.getValue());
//        }
        changeFactoryParam();
        changeMachineParam();
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), JSON.toJSONString(dataParam));
        showLoading();
        RtfUtils.getMesRtf().userDataAdd("1", "1", requestBody).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;
                }
                toast("添加成功");
                EventBus.getDefault().post(new AddUserDataEvent());
                finish();
            }
        });
    }

    private void changeMachineParam() {
        MachineDetailBean.CastInfoBean castInfo = dataParam.getCastInfo();
        if (castInfo == null) {
            castInfo = new MachineDetailBean.CastInfoBean();
            dataParam.setCastInfo(castInfo);
        }
        castInfo.setContinuousCasterName(tvMachineName.getText().toString());
        castInfo.setContinuousCasterType(tvMachineType.getText().toString());
        castInfo.setFractureSurface(tvSection.getText().toString());
        castInfo.setSteelGrade(tvSteelType.getText().toString());
        castInfo.setCastingSpeed(tvSpeed.getText().toString());
        castInfo.setSteelmakingWorksCode(dataParam.getMainInfo().getSteelmakingWorksCode());
        castInfo.setSteelmakingWorksName(dataParam.getMainInfo().getSteelmakingWorksName());

        MachineDetailBean.CastLadleBean castLadle = dataParam.getCastLadle();
        if (castLadle == null) {
            castLadle = new MachineDetailBean.CastLadleBean();
            dataParam.setCastLadle(castLadle);
        }
        castLadle.setCapacity(tvMiddle1.getText().toString());
        castLadle.setLifeSpan(tvMiddle2.getText().toString());
//        castLadle.setLadleCode(tvMiddle3.getText().toString());
        castLadle.setLiquidLevel(tvMiddle4.getText().toString());
        castLadle.setLiquidSteelTemperature(tvMiddle5.getText().toString());
        castLadle.setBakingMethod(tvMiddle6.getText().toString());
        castLadle.setBakingTemperature(tvMiddle7.getText().toString());
        String time = tvMiddle8.getText().toString();
        if (!TextUtils.isEmpty(time)) {
            castLadle.setBakingDateTime(Long.valueOf(tvMiddle8.getText().toString()));
        }

        MachineDetailBean.CovAgentBean covAgent = dataParam.getCovAgent();
        if (covAgent == null) {
            covAgent = new MachineDetailBean.CovAgentBean();
            dataParam.setCovAgent(covAgent);
        }
        covAgent.setLadleCoveringAgentName(tvCover1.getText().toString());
        covAgent.setAcidAlkaliDegree(tvCover2.getText().toString());
        covAgent.setChemicalComposition(tvCover3.getText().toString());
        covAgent.setSupplierName(tvCover4.getText().toString());


        MachineDetailBean.MouldFluBean mouldFlu = dataParam.getMouldFlu();
        if (mouldFlu == null) {
            mouldFlu = new MachineDetailBean.MouldFluBean();
            dataParam.setMouldFlu(mouldFlu);
        }
        mouldFlu.setMouldFluxesName(tvProtection1.getText().toString());
        mouldFlu.setAcidAlkaliDegree(tvProtection2.getText().toString());
        mouldFlu.setChemicalComposition(tvProtection3.getText().toString());
        mouldFlu.setSupplierName(tvProtection4.getText().toString());
    }

    private void changeFactoryParam() {
        FactoryDetailBean.SteelWorksBean steelWorks = dataParam.getSteelWorks();
        if (steelWorks != null) {
            steelWorks.setAnnualOutput(tvNum.getText().toString());
            steelWorks.setSteelmakingWorksName(tvFactory.getText().toString());
            steelWorks.setSteelGrade(tvType.getText().toString());
        }
        if (dataParam.getSteelFurnList() != null) {
            dataParam.getSteelFurnList().clear();
            dataParam.getSteelFurnList().addAll(dataFactory1List);
        }
        if (dataParam.getSteelRefFurnList() != null) {
            dataParam.getSteelRefFurnList().clear();
            dataParam.getSteelRefFurnList().addAll(dataFactory2List);
        }
        if (dataParam.getSteelLadleList() != null) {
            dataParam.getSteelLadleList().clear();
            dataParam.getSteelLadleList().addAll(dataFactory3List);
        }
    }

    private void submitMachine() {
        showLoading();
        changeMachineParam();
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), JSON.toJSONString(dataParam));
        RtfUtils.getMesRtf().machineAdd("1", "2", requestBody).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;
                }
                toast("编辑成功");
            }
        });
    }

    private void submitFactory() {
        if (!checkParam()) {
            return;
        }
        changeFactoryParam();
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), JSON.toJSONString(dataParam));
        showLoading();
        RtfUtils.getMesRtf().factoryAdd("1", "2", requestBody).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;
                }
                toast("编辑成功");
            }
        });
    }

    private boolean checkParam() {
        boolean dataHasNull = false;
        for (FactoryDetailBean.SteelFurnListBean steelFurnListBean : dataFactory1List) {
            if (TextUtils.isEmpty(steelFurnListBean.getCapacity())
                    || TextUtils.isEmpty(steelFurnListBean.getType())) {
                dataHasNull = true;
            }
        }
        if (dataHasNull) {
            toast("请填写炼钢厂数据");
            return false;
        }
        for (FactoryDetailBean.SteelRefFurnListBean steelRefFurnListBean : dataFactory2List) {
            if (TextUtils.isEmpty(steelRefFurnListBean.getModelNumber())
                    || TextUtils.isEmpty(steelRefFurnListBean.getCalciumTreatmentSituation())) {
                dataHasNull = true;
            }
        }
        if (dataHasNull) {
            toast("请填写精炼厂数据");
            return false;
        }
        for (FactoryDetailBean.SteelLadleListBean steelLadleListBean : dataFactory3List) {
            if (TextUtils.isEmpty(steelLadleListBean.getCapacity()) || steelLadleListBean.getCastingDateTime() == null
                    || TextUtils.isEmpty(steelLadleListBean.getTemperatureRange())) {
                dataHasNull = true;
            }
        }
        if (dataHasNull) {
            toast("请填写钢包数据");
            return false;
        }
        return true;
    }

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

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

    @Subscribe
    public void refresh(AddMachineAddChildEvent event) {
        switch (event.getType()) {
            case 1:
                List<MachineDetailBean.ProductListBean> productList = dataParam.getProductList();
                if (productList == null) {
                    dataParam.setProductList(new ArrayList<>());
                }
                dataParam.getProductList().add(JSON.parseObject(event.getData(), MachineDetailBean.ProductListBean.class));
                break;
            case 2:
                List<MachineDetailBean.SupplyListBean> supplierList = dataParam.getSupplyList();
                if (supplierList == null) {
                    dataParam.setSupplyList(new ArrayList<>());
                }
                dataParam.getSupplyList().add(JSON.parseObject(event.getData(), MachineDetailBean.SupplyListBean.class));

                break;
            case 3:
                List<MachineDetailBean.OtherSupListBean> supplyList = dataParam.getOtherSupList();
                if (supplyList == null) {
                    dataParam.setOtherSupList(new ArrayList<>());
                }
                dataParam.getOtherSupList().add(JSON.parseObject(event.getData(), MachineDetailBean.OtherSupListBean.class));
                break;
            case 4:
                List<MachineDetailBean.OtherLadleListBean> otherLadleList = dataParam.getOtherLadleList();
                if (otherLadleList == null) {
                    dataParam.setOtherLadleList(new ArrayList<>());
                }
                dataParam.getOtherLadleList().add(JSON.parseObject(event.getData(), MachineDetailBean.OtherLadleListBean.class));
                break;
            case 5:
                List<MachineDetailBean.StockListBean> stockList = dataParam.getStockList();
                if (stockList == null) {
                    dataParam.setStockList(new ArrayList<>());
                }
                dataParam.getStockList().add(JSON.parseObject(event.getData(), MachineDetailBean.StockListBean.class));
                break;
            case 6:
                List<MachineDetailBean.SettleListBean> settleList = dataParam.getSettleList();
                if (settleList == null) {
                    dataParam.setSettleList(new ArrayList<>());
                }
                dataParam.getSettleList().add(JSON.parseObject(event.getData(), MachineDetailBean.SettleListBean.class));
                break;
        }
        changeList();
    }

    @Subscribe
    public void modify(AddMachineModifyChildEvent event) {
        switch (event.getType()) {
            case 1:
                List<MachineDetailBean.ProductListBean> productList = dataParam.getProductList();
                if (productList == null) {
                    dataParam.setProductList(new ArrayList<>());
                }
                productList.remove(event.getPosition());
                productList.add(event.getPosition(), JSON.parseObject(event.getData(), MachineDetailBean.ProductListBean.class));
                break;
            case 2:
                List<MachineDetailBean.SupplyListBean> supplierList = dataParam.getSupplyList();
                if (supplierList == null) {
                    dataParam.setSupplyList(new ArrayList<>());
                }
                supplierList.remove(event.getPosition());
                supplierList.add(event.getPosition(), JSON.parseObject(event.getData(), MachineDetailBean.SupplyListBean.class));
                break;
            case 3:
                List<MachineDetailBean.OtherSupListBean> supplyList = dataParam.getOtherSupList();
                if (supplyList == null) {
                    dataParam.setOtherSupList(new ArrayList<>());
                }
                supplyList.remove(event.getPosition());
                supplyList.add(event.getPosition(), JSON.parseObject(event.getData(), MachineDetailBean.OtherSupListBean.class));
                break;
            case 4:
                List<MachineDetailBean.OtherLadleListBean> otherLadleList = dataParam.getOtherLadleList();
                if (otherLadleList == null) {
                    dataParam.setOtherLadleList(new ArrayList<>());
                }
                otherLadleList.remove(event.getPosition());
                otherLadleList.add(event.getPosition(), JSON.parseObject(event.getData(), MachineDetailBean.OtherLadleListBean.class));
                break;
            case 5:
                List<MachineDetailBean.StockListBean> stockList = dataParam.getStockList();
                if (stockList == null) {
                    dataParam.setStockList(new ArrayList<>());
                }
                stockList.remove(event.getPosition());
                stockList.add(event.getPosition(), JSON.parseObject(event.getData(), MachineDetailBean.StockListBean.class));
                break;
            case 6:
                List<MachineDetailBean.SettleListBean> settleList = dataParam.getSettleList();
                if (settleList == null) {
                    dataParam.setSettleList(new ArrayList<>());
                }
                settleList.remove(event.getPosition());
                settleList.add(event.getPosition(), JSON.parseObject(event.getData(), MachineDetailBean.SettleListBean.class));
                break;
        }
        changeList();
    }

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

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 10001:
                if (resultCode == 10001) {
                    MesClientBean.ListBean modifyClient = JSON.parseObject(data.getStringExtra("data"), MesClientBean.ListBean.class);
                    MachineDetailBean.CovAgentBean covAgent = dataParam.getCovAgent();
                    if (covAgent == null) {
                        covAgent = new MachineDetailBean.CovAgentBean();
                        dataParam.setCovAgent(covAgent);
                    }
                    tvCover4.setText(modifyClient.getCustomerName());
                    covAgent.setSupplierCode(modifyClient.getCustomerCode());
                    covAgent.setSupplierName(modifyClient.getCustomerName());
                }
                break;
            case 10002:
                if (resultCode == 10001) {
                    MesClientBean.ListBean modifyClient = JSON.parseObject(data.getStringExtra("data"), MesClientBean.ListBean.class);
                    MachineDetailBean.MouldFluBean mouldFlu = dataParam.getMouldFlu();
                    if (mouldFlu == null) {
                        mouldFlu = new MachineDetailBean.MouldFluBean();
                        dataParam.setMouldFlu(mouldFlu);
                    }
                    tvProtection4.setText(modifyClient.getCustomerName());
                    mouldFlu.setSupplierCode(modifyClient.getCustomerCode());
                    mouldFlu.setSupplierCode(modifyClient.getCustomerName());
                }
                break;
        }
    }

}
