package com.zztrapp.zztrapp.ui.index.activity;

import android.text.InputFilter;
import android.view.Gravity;
import android.view.View;
import android.widget.AbsListView;

import com.zztrapp.zztrapp.R;
import com.zztrapp.zztrapp.base.BaseActivity;
import com.zztrapp.zztrapp.bean.ChoosePopItemBean;
import com.zztrapp.zztrapp.bean.HttpResult;
import com.zztrapp.zztrapp.bean.ZdjgdwPagelistBean;
import com.zztrapp.zztrapp.databinding.ActivityZdjgdwListEditBinding;
import com.zztrapp.zztrapp.http.HttpMethods;
import com.zztrapp.zztrapp.http.ISubscriberOnNextListener;
import com.zztrapp.zztrapp.http.ProgressSubscriber;
import com.zztrapp.zztrapp.http.RxApiManager;
import com.zztrapp.zztrapp.ui.index.callback.ChooseResultCallBack;
import com.zztrapp.zztrapp.ui.index.fragment.ChooseListFragment;
import com.zztrapp.zztrapp.utilcode.mUtil.MyUtil;
import com.zztrapp.zztrapp.utilcode.util.ToastUtils;
import com.zztrapp.zztrapp.widget.DecimalDigitsInputFilter;
import com.zztrapp.zztrapp.widget.OnSingleClickListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ZdjgdwListEditActivity extends BaseActivity {

    private ActivityZdjgdwListEditBinding binding;
    private int jumpType;//1编辑 2查看详情
    private ZdjgdwPagelistBean.RecordsBean detail;

    private List<ChoosePopItemBean> xzqhList;
    private List<ChoosePopItemBean> zzjglxList;
    private List<ChoosePopItemBean> nrmdyyList;
    private List<ChoosePopItemBean> qyztList;


    @Override
    public View getLayoutId() {
        return binding.getRoot();
    }

    @Override
    protected void initBinding() {
        binding = ActivityZdjgdwListEditBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void init() {
        binding.navView.navTitle.setText(R.string.edit);
        binding.navView.navBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        binding.jd.setFilters(new InputFilter[]{new DecimalDigitsInputFilter(3, 6)});
        binding.wd.setFilters(new InputFilter[]{new DecimalDigitsInputFilter(2, 6)});

        jumpType = getIntent().getIntExtra("jumpType", 1);
        detail = (ZdjgdwPagelistBean.RecordsBean) getIntent().getSerializableExtra("detail");

        xzqhList = new ArrayList<>();
        zzjglxList = new ArrayList<>();
        nrmdyyList = new ArrayList<>();
        qyztList = new ArrayList<>();

        //组织结构类型 --传字符串
        zzjglxList.add(new ChoosePopItemBean("0", "企业单位"));
        zzjglxList.add(new ChoosePopItemBean("1", "事业单位"));
        zzjglxList.add(new ChoosePopItemBean("2", "其他"));
        //纳入名单原因 --传字符串
        zzjglxList.add(new ChoosePopItemBean("0", "土壤污染重点监管行业"));
        zzjglxList.add(new ChoosePopItemBean("1", "年产生危险废物100吨以上"));
        zzjglxList.add(new ChoosePopItemBean("2", "持有危险废物许可证危废经莒单位"));
        zzjglxList.add(new ChoosePopItemBean("3", "运营维护生活垃圾单位"));
        zzjglxList.add(new ChoosePopItemBean("4", "三年内发生较大污染事"));
        //企业状态 --传字符串
        zzjglxList.add(new ChoosePopItemBean("0", "正常公开"));
        zzjglxList.add(new ChoosePopItemBean("1", "注销"));

        //赋值
        binding.qymc.setText(detail.getQymc());
        binding.shtyxydm.setText(detail.getShtyxydm());
        binding.xzqh.setText(detail.getCountyName());
        binding.zzjglx.setText(detail.getZzjglx());
        binding.qybm.setText(detail.getQybm());
        binding.fddbr.setText(detail.getFddbr());
        binding.hylx.setText(detail.getHylx());
        binding.hydm.setText(detail.getHydm());
        binding.lxrmc.setText(detail.getLxrmc());
        binding.lxrdh.setText(detail.getLxrdh());
        binding.dkzymj.setText(detail.getDkzymj());
        binding.jtwz.setText(detail.getJtwz());
        binding.jd.setText(detail.getJd());
        binding.wd.setText(detail.getWd());
        binding.nrmdyy.setText(detail.getNrmdyy());
        binding.pwxkzbh.setText(detail.getPwxkzbh());
        binding.qyzt.setText(detail.getQyzt());
        binding.bz.setText(detail.getBz());

        if (jumpType == 2) {
            binding.qymc.setEnabled(false);
            binding.shtyxydm.setEnabled(false);
            binding.xzqhIcon.setVisibility(View.GONE);
            binding.zzjglxIcon.setVisibility(View.GONE);
            binding.qybm.setEnabled(false);
            binding.fddbr.setEnabled(false);
            binding.hylx.setEnabled(false);
            binding.hydm.setEnabled(false);
            binding.lxrmc.setEnabled(false);
            binding.lxrdh.setEnabled(false);
            binding.dkzymj.setEnabled(false);
            binding.jtwz.setEnabled(false);
            binding.jd.setEnabled(false);
            binding.wd.setEnabled(false);
            binding.nrmdyyIcon.setVisibility(View.GONE);
            binding.pwxkzbh.setEnabled(false);
            binding.qyztIcon.setVisibility(View.GONE);
            binding.bz.setEnabled(false);
            binding.bottomView.setVisibility(View.GONE);

        } else {
            loadXzqhList();
        }
    }

    @Override
    protected void initOnClick() {
        binding.cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        binding.save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                detail.setQymc(binding.qymc.getText().toString());
                detail.setShtyxydm(binding.shtyxydm.getText().toString());
//                detail.setXzqh(binding.xzqh.getText().toString());
                detail.setZzjglx(binding.zzjglx.getText().toString());
                detail.setQybm(binding.qybm.getText().toString());
                detail.setFddbr(binding.fddbr.getText().toString());
                detail.setHylx(binding.hylx.getText().toString());
                detail.setHydm(binding.hydm.getText().toString());
                detail.setLxrmc(binding.lxrmc.getText().toString());
                detail.setLxrdh(binding.lxrdh.getText().toString());
                detail.setDkzymj(binding.dkzymj.getText().toString());
                detail.setJtwz(binding.jtwz.getText().toString());
                detail.setJd(binding.jd.getText().toString());
                detail.setWd(binding.wd.getText().toString());
                detail.setNrmdyy(binding.nrmdyy.getText().toString());
                detail.setPwxkzbh(binding.pwxkzbh.getText().toString());
                detail.setQyzt(binding.qyzt.getText().toString());
                detail.setBz(binding.bz.getText().toString());
                try {
                    HashMap<String, Object> map = MyUtil.getObjectToMap(detail);
                    editJcj(map);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        });

        //行政区划
        binding.xzqhView.setOnClickListener(new OnSingleClickListener() {
            @Override
            public void onSingleClick(View view) {
                if (jumpType == 2) {
                    return;
                }
                ChooseListFragment chooseListFragment = new ChooseListFragment(ZdjgdwListEditActivity.this, "", AbsListView.CHOICE_MODE_SINGLE, xzqhList, new ArrayList<>(), new ChooseResultCallBack() {
                    @Override
                    public void onResult(Object object) {
                        List<ChoosePopItemBean> list = (List<ChoosePopItemBean>) object;
                        if (list.size() > 0) {
                            binding.xzqh.setText(list.get(0).getName());
                            detail.setCountyCode(list.get(0).getCode());
                            detail.setCountyName(list.get(0).getName());
                        }
                    }
                });
                chooseListFragment.show(getSupportFragmentManager(), "single");
            }
        });
        //组织结构类型
        binding.zzjglxView.setOnClickListener(new OnSingleClickListener() {
            @Override
            public void onSingleClick(View view) {
                if (jumpType == 2) {
                    return;
                }
                ChooseListFragment chooseListFragment = new ChooseListFragment(ZdjgdwListEditActivity.this, "", AbsListView.CHOICE_MODE_SINGLE, zzjglxList, new ArrayList<>(), new ChooseResultCallBack() {
                    @Override
                    public void onResult(Object object) {
                        List<ChoosePopItemBean> list = (List<ChoosePopItemBean>) object;
                        if (list.size() > 0) {
                            binding.zzjglx.setText(list.get(0).getName());
                            detail.setZzjglx(list.get(0).getName());
                        }
                    }
                });
                chooseListFragment.show(getSupportFragmentManager(), "single");
            }
        });
        //纳入名单原因
        binding.nrmdyyView.setOnClickListener(new OnSingleClickListener() {
            @Override
            public void onSingleClick(View view) {
                if (jumpType == 2) {
                    return;
                }
                ChooseListFragment chooseListFragment = new ChooseListFragment(ZdjgdwListEditActivity.this, "", AbsListView.CHOICE_MODE_SINGLE, nrmdyyList, new ArrayList<>(), new ChooseResultCallBack() {
                    @Override
                    public void onResult(Object object) {
                        List<ChoosePopItemBean> list = (List<ChoosePopItemBean>) object;
                        if (list.size() > 0) {
                            binding.nrmdyy.setText(list.get(0).getName());
                            detail.setNrmdyy(list.get(0).getName());
                        }
                    }
                });
                chooseListFragment.show(getSupportFragmentManager(), "single");
            }
        });
        //企业状态
        binding.qyztView.setOnClickListener(new OnSingleClickListener() {
            @Override
            public void onSingleClick(View view) {
                if (jumpType == 2) {
                    return;
                }
                ChooseListFragment chooseListFragment = new ChooseListFragment(ZdjgdwListEditActivity.this, "", AbsListView.CHOICE_MODE_SINGLE, qyztList, new ArrayList<>(), new ChooseResultCallBack() {
                    @Override
                    public void onResult(Object object) {
                        List<ChoosePopItemBean> list = (List<ChoosePopItemBean>) object;
                        if (list.size() > 0) {
                            binding.qyzt.setText(list.get(0).getName());
                            detail.setQyzt(list.get(0).getName());
                        }
                    }
                });
                chooseListFragment.show(getSupportFragmentManager(), "single");
            }
        });
    }

    //编辑
    private void editJcj(HashMap<String, Object> map) {
        ISubscriberOnNextListener<HttpResult<String>> iSubscriberOnNextListener = new ISubscriberOnNextListener<HttpResult<String>>() {

            @Override
            public void onNext(HttpResult<String> httpResult) {
                if (httpResult.getCode() == 200) {
                    finish();
                } else {
                    ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(httpResult.getMessage());
                }
            }

            @Override
            public void onError(int code, String message) {
                ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(message);
            }
        };
        ProgressSubscriber<HttpResult<String>> httpResultProgressSubscriber = new ProgressSubscriber<>(iSubscriberOnNextListener, this, true);
        RxApiManager.get().add("editZdjgdw", httpResultProgressSubscriber);
        HttpMethods.getInstance(this).editZdjgdw(httpResultProgressSubscriber, map);
    }

    //行政区划列表
    private void loadXzqhList() {
        ISubscriberOnNextListener<HttpResult<List<ChoosePopItemBean>>> iSubscriberOnNextListener = new ISubscriberOnNextListener<HttpResult<List<ChoosePopItemBean>>>() {
            @Override
            public void onNext(HttpResult<List<ChoosePopItemBean>> httpResult) {
                if (httpResult.getCode() == 200) {
                    xzqhList = httpResult.getResult();
                } else {
                    ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(httpResult.getMessage());
                }
            }

            @Override
            public void onError(int code, String message) {
                ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(message);
            }
        };
        ProgressSubscriber<HttpResult<List<ChoosePopItemBean>>> httpResultProgressSubscriber = new ProgressSubscriber<>(iSubscriberOnNextListener, this, true);
        RxApiManager.get().add("xzbmAreaList", httpResultProgressSubscriber);
        HttpMethods.getInstance(this).xzbmAreaList(httpResultProgressSubscriber);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        binding = null;
        RxApiManager.get().cancel("editZdjgdw");
    }
}