package xn.hxp.ui.plan.already;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.viewbinding.ViewBinding;

import com.blankj.utilcode.util.ClickUtils;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ThreadUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.kongzue.dialogx.dialogs.PopTip;
import com.rc.core.ui.activity.BaseActivity;
import com.rc.httpcore.HttpConfig;
import com.rc.httpcore.bean.HxpChemicalVo;
import com.rc.httpcore.client.HttpTool;
import com.rc.httpcore.exception.NetException;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Set;

import okhttp3.Response;
import xn.hxp.app.ChemicalApp;
import xn.hxp.databinding.ActivityAlreadyBinding;
import xn.hxp.ui.plan.PlanAddData;
import xn.hxp.ui.plan.room.RoomTool;
import xn.hxp.ui.plan.room.bean.input_add.InventoryItemBean;
import xn.hxp.ui.plan.room.bean.locker.CabinetLayerSelect;
import xn.hxp.ui.plan.room.bean.locker.HxpCabinetDoorVo;
import xn.hxp.ui.plan.room.bean.locker.HxpCabinetLockVo;
import xn.hxp.ui.plan.room.bean.locker.HxpCabinetVo;
import xn.hxp.ui.plan.room.bean.locker.HxpLabCabinetListVo;
import xn.hxp.ui.plan.room.bean.locker.HxpLabCabinetVo;
import xn.hxp.ui.plan.room.dao.HxpCabinetDAO;
import xn.hxp.ui.plan.room.dao.HxpDoorDAO;
import xn.hxp.ui.plan.room.dao.HxpInventoryDAO;
import xn.hxp.ui.plan.save_list.SaveListActivity;
import xn.hxp.ui.plan.unlock.UnlockActivity;
import xn.hxp.weidith.SelectAlreadyDialog;

public class AlreadyActivity extends BaseActivity {
    private ActivityAlreadyBinding binding;
    private List<HxpChemicalVo> hxpChemicalVoList = new ArrayList<>();
    private Set<String> checkWaitIdSet = new HashSet<>();
    private AlreadyAdapter alreadyAdapter;

    private int page = 1; // 页数
    private int pages = Integer.MAX_VALUE;
    private int type = 0; // 0:与自己相关 1:其它
    private String applyUserName;// 申领人
    private String applyOrder; // 申领单号
    private String topicUserName;// 归属人

    private ApplyOrderBean applyOrderBean;
    private ActivityResultLauncher<Intent> intentActivityResultLauncher;

    @Override
    protected ViewBinding setViewBinding() {
        return binding = ActivityAlreadyBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void onInit() {
        RoomTool.getInstance().hxpCabinetDAO().deleteAll();
        RoomTool.getInstance().hxpDoorDAO().deleteAll();
        RoomTool.getInstance().hxpLayerDAO().deleteAll();
        RoomTool.getInstance().hxpInventoryDAO().deleteAll();
        RoomTool.getInstance().hxpLabDirectorDAO().deleteAll();
        RoomTool.getInstance().hxpLabSafeDAO().deleteAll();
        // 位置选择
        intentActivityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                if (result.getResultCode() == RESULT_OK) {
                    showLoading("正在修改存储位置...");
                    Intent resultData = result.getData();
                    if (null != resultData && resultData.hasExtra("data")) {
                        Bundle bundle = resultData.getBundleExtra("data");
                        if (null != bundle) {
                            CabinetLayerSelect cabinetLayerSelect = (CabinetLayerSelect) bundle.getSerializable("Change");
                            LogUtils.json("选择的柜层", cabinetLayerSelect);
                            if (null != cabinetLayerSelect) {

                                List<HxpChemicalVo> beanArrayList = new ArrayList<>();
                                for (int i = 0; i < hxpChemicalVoList.size(); i++) {
                                    HxpChemicalVo recordsBean = hxpChemicalVoList.get(i);
                                    if (recordsBean.isSelect()) {
                                        beanArrayList.add(recordsBean);
                                    }
                                }

                                for (int i = 0; i < beanArrayList.size(); i++) {
                                    HxpChemicalVo recordsBean = beanArrayList.get(i);
                                    recordsBean.setCabinetId(cabinetLayerSelect.getCabinetId());
                                    recordsBean.setDoorId(cabinetLayerSelect.getDoorId());
                                    recordsBean.setDoorLayers(cabinetLayerSelect.getLayer());
                                }
                                ThreadUtils.executeByCached(new ThreadUtils.SimpleTask<Object>() {
                                    @Override
                                    public Object doInBackground() throws Throwable {
                                        Response response = HttpTool.changeDoor(beanArrayList);
                                        String json = response.body().string();
                                        LogUtils.d("柜层", json);
                                        return null;
                                    }

                                    @Override
                                    public void onSuccess(Object result) {
                                        dismissLoading();
                                        getHxpStockWait(false);
                                    }
                                });
                            }
                        }
                    }
                }
            }
        });

        // 全选/全返选
        binding.selectAllCB.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
            @Override
            public void onDebouncingClick(View v) {
                if (null != hxpChemicalVoList && !hxpChemicalVoList.isEmpty()) {
                    List<String> waitArray = new ArrayList<>();
                    for (int i = 0; i < hxpChemicalVoList.size(); i++) {
                        HxpChemicalVo recordsBean = hxpChemicalVoList.get(i);
                        recordsBean.setSelect(binding.selectAllCB.isChecked());
                        if (binding.selectAllCB.isChecked()) {
                            waitArray.add(recordsBean.getWaitId());
                        }
                    }
                    alreadyAdapter.update(type);
                    LogUtils.json(waitArray);
                } else {
                    showToast("未有可操作的数据!");
                    binding.selectAllCB.setChecked(false);
                }
            }
        });
        // 已申领类型选择
        binding.typeRG.setOnCheckedChangeListener((group, checkedId) -> {
            // 与我相关
            if (checkedId == binding.relatedRB.getId()) {
                binding.otherRB.setText("其它");
                type = 0;
            }
            // 其它
            else if (checkedId == binding.otherRB.getId()) {
                binding.relatedRB.setText("与我相关");
                type = 1;
            }
            applyOrder = "";
            applyUserName = "";
            topicUserName = "";
            binding.applicantCodeSearchTV.setText("申领单号");
            binding.applicantSearchTV.setText("申领人");
            binding.belongSearchTV.setText("归属人");
            getHxpStockWait(true);
        });
        // 根据申领单号查询
        binding.applicantCodeSearchRL.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
            @Override
            public void onDebouncingClick(View v) {

                List<String> applyOrderNumberList;
                if (type == 0) {
                    applyOrderNumberList = applyOrderBean.getTypeOwner().getApplyOrderNumbers();
                } else {
                    applyOrderNumberList = applyOrderBean.getTypeOther().getApplyOrderNumbers();
                }
                if (null != applyOrderNumberList && !applyOrderNumberList.isEmpty()) {
                    new SelectAlreadyDialog(AlreadyActivity.this, "选择申领单号", applyOrderNumberList, new SelectAlreadyDialog.SelectedListener() {
                        @Override
                        public void selected(String string) {
                            applyOrder = string;
                            binding.applicantCodeSearchTV.setText(applyOrder);
                            binding.applicantCodeSearchTV.setSelected(true);
                            getHxpStockWait(true);
                        }

                        @Override
                        public void clear() {
                            applyOrder = "";
                            binding.applicantCodeSearchTV.setText("申领单号");
                            getHxpStockWait(true);
                        }
                    }).show();
                } else {
                    showToast("暂无数据");
                }
            }
        });
        // 根据申领人查询
        binding.applicantSearchRL.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
            @Override
            public void onDebouncingClick(View v) {

                List<String> applicantList;
                if (type == 0) {
                    applicantList = applyOrderBean.getTypeOwner().getApplicants();
                } else {
                    applicantList = applyOrderBean.getTypeOther().getApplicants();
                }
                if (null != applicantList && !applicantList.isEmpty()) {
                    new SelectAlreadyDialog(AlreadyActivity.this, "选择申领人", applicantList, new SelectAlreadyDialog.SelectedListener() {
                        @Override
                        public void selected(String string) {
                            applyUserName = string;
                            binding.applicantSearchTV.setText(applyUserName);
                            binding.applicantSearchTV.setSelected(true);
                            getHxpStockWait(true);
                        }

                        @Override
                        public void clear() {
                            applyUserName = "";
                            binding.applicantSearchTV.setText("申领人");
                            getHxpStockWait(true);
                        }
                    }).show();
                } else {
                    showToast("暂无数据");
                }
            }
        });
        // 根据归属人查询
        binding.belongSearchRL.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
            @Override
            public void onDebouncingClick(View v) {

                List<String> ownerList;
                if (type == 0) {
                    ownerList = applyOrderBean.getTypeOwner().getOwners();
                } else {
                    ownerList = applyOrderBean.getTypeOther().getOwners();
                }
                if (null != ownerList && !ownerList.isEmpty()) {
                    new SelectAlreadyDialog(AlreadyActivity.this, "选择归属人", ownerList, new SelectAlreadyDialog.SelectedListener() {
                        @Override
                        public void selected(String string) {
                            topicUserName = string;
                            binding.belongSearchTV.setText(topicUserName);
                            binding.belongSearchTV.setSelected(true);
                            getHxpStockWait(true);
                        }

                        @Override
                        public void clear() {
                            topicUserName = "";
                            binding.belongSearchTV.setText("归属人");
                            getHxpStockWait(true);
                        }
                    }).show();
                } else {
                    showToast("暂无数据");
                }
            }
        });
        // 一键入库
        ClickUtils.applySingleDebouncing(binding.subAdd, v -> {

            showLoading("处理中...");
            ThreadUtils.executeByCached(new ThreadUtils.SimpleTask<PlanAddData>() {
                @Override
                public PlanAddData doInBackground() throws Throwable {
                    List<HxpChemicalVo> autoChemicalList = new ArrayList<>();
                    for (int i = 0; i < hxpChemicalVoList.size(); i++) {
                        HxpChemicalVo recordsBean = hxpChemicalVoList.get(i);
                        if (recordsBean.isSelect()) {
                            autoChemicalList.add(recordsBean);
                        }
                    }
                    // 为需要自动入库的化学品创建清单
                    AlreadyTool.INSTANCE.updateHxpInventoryList(autoChemicalList);
                    HxpDoorDAO hxpDoorDAO = RoomTool.getInstance().hxpDoorDAO();
                    HxpInventoryDAO hxpInventoryDAO = RoomTool.getInstance().hxpInventoryDAO();
                    HxpCabinetDAO hxpCabinetDAO = RoomTool.getInstance().hxpCabinetDAO();
                    List<InventoryItemBean> inventoryItemBeanList = hxpInventoryDAO.getAll();
                    List<HxpCabinetVo> cabinetList = hxpCabinetDAO.getAll();
                    List<HxpCabinetDoorVo> doorList = hxpDoorDAO.getAll();
                    // 删除清单外的柜门
                    for (int i = 0; i < doorList.size(); i++) {
                        HxpCabinetDoorVo doorVo = doorList.get(i);
                        boolean hasDoorId = false;
                        for (int j = 0; j < inventoryItemBeanList.size(); j++) {
                            InventoryItemBean inventoryItemBean = inventoryItemBeanList.get(j);
                            if (inventoryItemBean.getDoorId() == doorVo.getDoorId()) {
                                hasDoorId = true;
                                break;
                            }
                        }
                        if (!hasDoorId) {
                            doorList.remove(i);
                            i--;
                        }

                    }
                    // 删除没有智能锁的柜门
                    Iterator<HxpCabinetDoorVo> doorVoIterator = doorList.iterator();
                    while (doorVoIterator.hasNext()) {
                        HxpCabinetDoorVo doorVo = doorVoIterator.next();
                        List<HxpCabinetLockVo> hxpCabinetLockVoList = doorVo.getCabinetLockVoList();
                        if (null == hxpCabinetLockVoList || hxpCabinetLockVoList.isEmpty()) {
                            doorVoIterator.remove();
                        } else {
                            HxpCabinetLockVo cabinetLockVo = hxpCabinetLockVoList.get(0);
                            if (2 != cabinetLockVo.getUnlockingMethod()) {
                                doorVoIterator.remove();
                            }
                        }
                    }
                    // 删除不需要开门的柜子
                    Iterator<HxpCabinetVo> cabinetVoIterator = cabinetList.iterator();
                    while (cabinetVoIterator.hasNext()) {
                        HxpCabinetVo cabinetVo = cabinetVoIterator.next();
                        boolean hasDoor = false;
                        for (int i = 0; i < doorList.size(); i++) {
                            HxpCabinetDoorVo doorVo = doorList.get(i);
                            if (cabinetVo.getCabinetId() == doorVo.getCabinetId()) {
                                hasDoor = true;
                                break;
                            }
                        }
                        if (!hasDoor) {
                            cabinetVoIterator.remove();
                        }
                    }
                    PlanAddData planAddData = new PlanAddData();
                    planAddData.setCabinetList(cabinetList);
                    planAddData.setDoorList(doorList);
                    planAddData.setAutoChemicalList(autoChemicalList);
                    return planAddData;
                }

                @Override
                public void onSuccess(PlanAddData planAddData) {
                    dismissLoading();
                    LogUtils.json(planAddData);
                    List<HxpCabinetVo> cabinetList = planAddData.getCabinetList();
                    List<HxpCabinetDoorVo> doorList = planAddData.getDoorList();
                    LogUtils.d("需要开锁", cabinetList.size(), doorList.size());
                    // 不需要开门
                    if (cabinetList.isEmpty() || doorList.isEmpty()) {
                        autoSave(planAddData);
                        return;
                    }
                    String json = GsonUtils.toJson(planAddData);
                    Intent intent = new Intent(AlreadyActivity.this, UnlockActivity.class);
                    intent.putExtra("data", json);
                    intent.putExtra("isAuto", true);
                    startActivity(intent);
                    finish();
                }
            });
        });

        // 上一页
        binding.lastPage.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
            @Override
            public void onDebouncingClick(View v) {
                int last = page - 1;
                if (last < 1) {
                    showToast("没有更多数据!");
                    return;
                }
                page = last;
                getHxpStockWait(false);
            }
        });
        // 下一页
        binding.nextPage.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
            @Override
            public void onDebouncingClick(View v) {
                int next = page + 1;
                if (next > pages) {
                    showToast("没有更多数据!");
                    return;
                }
                page = next;
                getHxpStockWait(false);
            }
        });

        try {
            // 日期
            binding.nowTime.setText(TimeUtils.getNowString(new SimpleDateFormat("MM月dd日 EEEE ", Locale.getDefault())));
            // 用户
            Glide.with(this).load(HttpConfig.Companion.getAPI_BASE_IMG_URL() + ChemicalApp.userData.getAvatar()).into(binding.imageName);
            String userName = ChemicalApp.userData.getUserName();
            binding.tvName.setText(TextUtils.isEmpty(userName) ? "" : userName);
            // 实验室logo
            Glide.with(this).load(HttpConfig.Companion.getAPI_BASE_IMG_URL() + ChemicalApp.confs.getCircularLogo()).into(binding.image);
            // 实验室名称
            String subName = ChemicalApp.subjectName;
            subName = TextUtils.isEmpty(subName) ? "" : subName;
            // 房间号
            String roomNum = ChemicalApp.subRoom;
            roomNum = TextUtils.isEmpty(roomNum) ? "" : roomNum;
            binding.deptName.setText(subName + "-" + roomNum);
            getHxpStockWait(true);
            getHxpCabinetList(ChemicalApp.subjectId);
            getApplyList();
        } catch (Exception e) {
            LogUtils.e(Log.getStackTraceString(e));
        }
        // 退出
        binding.tvOutLogin.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
            @Override
            public void onDebouncingClick(View v) {
                try {
                    HttpTool.logout();
                } catch (IOException e) {
                    LogUtils.e(Log.getStackTraceString(e));
                }
            }
        });
        binding.tvReturn.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
            @Override
            public void onDebouncingClick(View v) {
                finish();
            }
        });
    }

    private void autoSave(PlanAddData planAddData) {
        ThreadUtils.executeByCached(new ThreadUtils.SimpleTask<String>() {
            @Override
            public String doInBackground() throws Throwable {

                List<HxpChemicalVo> hxpChemicalVos = planAddData.getAutoChemicalList();
                JSONArray jsonArray = new JSONArray();
                for (int i = 0; i < hxpChemicalVos.size(); i++) {
                    String waitId = hxpChemicalVos.get(i).getWaitId();
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("waitId", waitId);
                    jsonArray.put(jsonObject);
                }
                LogUtils.d(jsonArray);
                Response response = HttpTool.autoChemicals(jsonArray.toString());
                if (response.isSuccessful()) {
                    String json = response.body().string();
                    JSONObject jsonObject = new JSONObject(json);
                    int code = jsonObject.getInt("code");
                    if (code == 200) {
                        return "";
                    } else {
                        return jsonObject.getString("message");
                    }
                } else {
                    return response.message();
                }
            }

            @Override
            public void onSuccess(String result) {
                dismissLoading();
                if (!TextUtils.isEmpty(result)) {
                    binding.subAdd.setEnabled(true);
                    PopTip.show(result);
                } else {
                    finish();
                    startActivity(new Intent(AlreadyActivity.this, SaveListActivity.class));
                }
            }
        });
    }


    /**
     * 获取单号检索列表
     */
    private void getApplyList() {
        ThreadUtils.executeByCached(new ThreadUtils.SimpleTask<List<String>>() {
            @Override
            public List<String> doInBackground() throws Throwable {
                Response response = HttpTool.getApplyOrder(ChemicalApp.subjectId);
                String json = response.body().string();
                LogUtils.d(json);
                JSONObject jsonObject = new JSONObject(json);
                if (jsonObject.getInt("code") == 200) {
                    JSONObject data = jsonObject.getJSONObject("data");
                    applyOrderBean = GsonUtils.fromJson(data.toString(), ApplyOrderBean.class);
                } else {
                    String msg = jsonObject.getString("message");
                    ToastUtils.showLong(msg);
                }
                return null;
            }

            @Override
            public void onSuccess(List<String> applyList) {
                dismissLoading();
            }
        });
    }

    /**
     * 获取指定实验室的柜子信息
     */
    private void getHxpCabinetList(String subjectId) {
        ThreadUtils.executeByCached(new ThreadUtils.SimpleTask<List<HxpCabinetVo>>() {
            @Override
            public List<HxpCabinetVo> doInBackground() throws Throwable {
                Response response = HttpTool.getCabinetList(subjectId);
                if (response.isSuccessful()) {
                    String json = response.body().string();
                    HxpLabCabinetListVo hxpCabinetListVo = GsonUtils.fromJson(json, HxpLabCabinetListVo.class);
                    if (hxpCabinetListVo.getCode() == 200) {
                        HxpLabCabinetVo hxpLabCabinetVo = hxpCabinetListVo.getData();
                        try {
                            // 实验室负责人
                            RoomTool.getInstance().hxpLabDirectorDAO().insertAll(hxpLabCabinetVo.getAdminList());
                            // 实验室安全员
                            RoomTool.getInstance().hxpLabSafeDAO().insertAll(hxpLabCabinetVo.getSafeList());
                        } catch (Exception e) {
                            LogUtils.e(Log.getStackTraceString(e));
                        }

                        List<HxpCabinetVo> hxpCabinetVoList = hxpLabCabinetVo.getCabinetList();
                        if (null != hxpCabinetVoList && !hxpCabinetVoList.isEmpty()) {
                            RoomTool.getInstance().hxpCabinetDAO().insertAll(hxpCabinetVoList);
                            // 录入柜子
                            for (int i = 0; i < hxpCabinetVoList.size(); i++) {
                                HxpCabinetVo hxpCabinetVo = hxpCabinetVoList.get(i);
                                List<HxpCabinetDoorVo> hxpCabinetDoorVoList = hxpCabinetVo.getCabinetDoorVoList();
                                if (null != hxpCabinetDoorVoList && !hxpCabinetDoorVoList.isEmpty()) {
                                    // 录入柜门
                                    RoomTool.getInstance().hxpDoorDAO().insertAll(hxpCabinetDoorVoList);
                                }
                            }
                        }
                    } else {
                        throw new NetException("" + hxpCabinetListVo.getCode(), hxpCabinetListVo.getMessage());
                    }
                } else {
                    throw new NetException("" + response.code(), response.message());
                }
                return null;
            }

            @Override
            public void onSuccess(List<HxpCabinetVo> hxpCabinetVoList) {

            }
        });
    }

    /**
     * 获取已申领未入库列表
     */
    private void getHxpStockWait(boolean reSet) {
        if (reSet) {
            page = 1;
        }
        showLoading("加载中...");
        for (int i = 0; i < hxpChemicalVoList.size(); i++) {
            HxpChemicalVo hxpChemicalVo = hxpChemicalVoList.get(i);
            if (hxpChemicalVo.isSelect()) {
                checkWaitIdSet.add(hxpChemicalVo.getWaitId());
            } else {
                checkWaitIdSet.remove(hxpChemicalVo.getWaitId());
            }
        }

        ThreadUtils.executeByCached(new ThreadUtils.SimpleTask<AlreadyBean>() {
            @Override
            public AlreadyBean doInBackground() throws Throwable {
                Response response = HttpTool.getHxpStockWait(type, ChemicalApp.subjectId, page, 10, applyOrder, applyUserName, topicUserName);
                String json = response.body().string();
                JSONObject jsonObject = new JSONObject(json);
                if (jsonObject.getInt("code") == 200) {
                    return GsonUtils.fromJson(jsonObject.getJSONObject("data").toString(), AlreadyBean.class);
                } else {
                    String msg = jsonObject.getString("message");
                    ToastUtils.showLong(msg);
                    return null;
                }
            }

            @Override
            public void onSuccess(AlreadyBean result) {
                if (null != result) {
                    hxpChemicalVoList.clear();
                    List<HxpChemicalVo> resultRecords = result.getPageData().getRecords();
                    if (null != resultRecords && !resultRecords.isEmpty()) {
                        hxpChemicalVoList.addAll(resultRecords);
                    } else {
                        showToast("暂无数据!");
                    }

                    for (String checkWaitId : checkWaitIdSet) {
                        for (int i = 0; i < hxpChemicalVoList.size(); i++) {
                            HxpChemicalVo hxpChemicalVo = hxpChemicalVoList.get(i);
                            if (hxpChemicalVo.getWaitId().equals(checkWaitId)) {
                                hxpChemicalVo.setSelect(true);
                                break;
                            }
                        }
                    }
                    if (null == alreadyAdapter) {
                        alreadyAdapter = new AlreadyAdapter(AlreadyActivity.this, hxpChemicalVoList, intentActivityResultLauncher);
                        binding.alreadyLV.setAdapter(alreadyAdapter);
                    } else {
                        alreadyAdapter.update(type);
                    }
                    if (result.isAdmin()) {
                        binding.typeRG.setVisibility(View.VISIBLE);
                        binding.relatedRB.setText("与我相关(" + result.getPersonal() + ")");
                        binding.otherRB.setText("其它(" + result.getOther() + ")");
                    } else {
                        int total = result.getPageData().getTotal();
                        binding.alreadyName.setText("已申领危化品存储(" + total + ")");
                        if (total == 0) {
                            showToast("暂无您可处理的危化品!");
                        }
                    }
                    // 最大页数
                    pages = result.getPageData().getPages();

                    binding.pageItem.setText(page + "/" + pages);

                    // 是否全选
                    boolean isAllCheck = true;
                    for (int i = 0; i < hxpChemicalVoList.size(); i++) {
                        HxpChemicalVo hxpChemicalVo = hxpChemicalVoList.get(i);
                        if (!hxpChemicalVo.isSelect()) {
                            isAllCheck = false;
                            break;
                        }
                    }
                    if (hxpChemicalVoList.isEmpty()) {
                        binding.pageControl.setVisibility(View.INVISIBLE);
                        binding.selectAllCB.setChecked(false);
                    } else {
                        binding.pageControl.setVisibility(View.VISIBLE);
                        binding.selectAllCB.setChecked(isAllCheck);
                    }

                    binding.selectAllCB.setEnabled(type == 0);

                }
                dismissLoading();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        intentActivityResultLauncher.unregister();
    }

    @Override
    protected void cdFinish() {
        finish();
    }

    @Override
    protected void cdTime(int cd) {
        binding.tvReturn.setText("返回" + cd + "s");
    }
}