package com.yem.wm.bc.change.list;

import com.yem.wm.bc.common.CommonUtils;
import kd.bos.bill.BillOperationStatus;
import kd.bos.bill.BillShowParameter;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.entity.LocaleString;
import kd.bos.dataentity.resource.ResManager;
import kd.bos.entity.datamodel.ListSelectedRow;
import kd.bos.entity.datamodel.events.BeforePackageDataEvent;
import kd.bos.form.FormShowParameter;
import kd.bos.form.IFormView;
import kd.bos.form.ShowType;
import kd.bos.form.container.Container;
import kd.bos.form.container.Tab;
import kd.bos.form.control.Button;
import kd.bos.form.control.Control;
import kd.bos.form.events.SetFilterEvent;
import kd.bos.form.field.ComboEdit;
import kd.bos.form.field.ComboItem;
import kd.bos.lang.Lang;
import kd.bos.list.BillList;
import kd.bos.list.IListView;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;

import java.util.*;

/**
 * @author 夜子
 * @ClassName ChangeResumeListPlugin
 * @description: 变更日志卡片列表插件
 * @date 2023年08月10日
 * @version: 1.0
 */
public class ChangeResumeListPlugin extends AbstractListPlugin {

    public void setFilter(SetFilterEvent e) {
        super.setFilter(e);
        FormShowParameter showParameter = this.getView().getFormShowParameter();
        Object srcBillId = showParameter.getCustomParam("srcbillid");
        if (!CommonUtils.isNull(srcBillId)) {
            QFilter qFilter1 = new QFilter("srcbillid", "=", srcBillId);
            QFilter qFilter2 = QFilter.isNotNull("xbillentity");
            QFilter qFilter3 = new QFilter("xbillentity", "!=", "");
            List<QFilter> listFilter = e.getQFilters();
            listFilter.add(qFilter1);
            listFilter.add(qFilter2);
            listFilter.add(qFilter3);
        }

    }

    public void beforePackageData(BeforePackageDataEvent e) {
        super.beforePackageData(e);
        DynamicObjectCollection dynamicObjects = e.getPageData();
        IListView listview = (IListView)this.getView();

        List<String> versions = new ArrayList<>();
        Map<String, Object> resultMap = new HashMap<>();
        for (DynamicObject dynamicObject : dynamicObjects) {
            int changenumber = (Integer) dynamicObject.get("currentchangenumber");
            String billInfo = dynamicObject.get("xbillid") + "," + dynamicObject.get("xbillentity");
            if (0 != changenumber) {
                dynamicObject.set("srcbillversion", dynamicObject.get("srcbillversion") + "." + dynamicObject.get("srcbillsubversion"));
            }

            if (!CommonUtils.isNull(billInfo)) {
                listview.getPageCache().put(String.valueOf(dynamicObject.getPkValue()), billInfo);
            }

            versions.add(dynamicObject.getString("srcbillversion"));
        }
        Collections.sort(versions);
        List<ComboItem> entryItems = new ArrayList<>();
        String currLCId = Lang.get().toString();
        for (int i = versions.size()-1; i >= 0; i--) {
            ComboItem comboItem = new ComboItem();
            comboItem.setValue(versions.get(i));
            comboItem.setCaption(new LocaleString(currLCId, "" + versions.get(i)));
            entryItems.add(comboItem);
        }
        IFormView formView = this.getView().getParentView();
        ComboEdit entryEditor = (ComboEdit) formView.getControl("vnum");
        entryEditor.setComboItems(entryItems);

        FormShowParameter showParameter = this.getView().getFormShowParameter();
        Object xbillIdObj = showParameter.getCustomParam("xbillid");
        if (dynamicObjects.size() > 0) {
            int row = 0;
            DynamicObject dynamicObject = (DynamicObject)dynamicObjects.get(0);
            if (!CommonUtils.isNull(xbillIdObj)) {
                for(Iterator<DynamicObject> var8 = dynamicObjects.iterator(); var8.hasNext(); ++row) {
                    DynamicObject dynamicObj = (DynamicObject)var8.next();
                    if (xbillIdObj.equals(dynamicObj.get("xbillid"))) {
                        dynamicObject = dynamicObj;
                        break;
                    }
                }
            }

            Object pkValue = dynamicObject.getPkValue();
            if (!CommonUtils.isNull(pkValue)) {
                this.getView().getPageCache().put("cardflexpanelap", String.valueOf(pkValue));
            }

            long xbillId = (Long)dynamicObject.get("xbillid");
            this.showChangeDetails(xbillId);
            this.showBillinfo(xbillId, (String)dynamicObject.get("xbillentity"));
            BillList billList = this.getControl("billlistap");
            billList.selectRows(row);
        } else {
            formView.setVisible(false, "change_tabap");
            formView.setVisible(false, "billinfo");
        }
        this.getView().sendFormAction(formView);

    }

    public void registerListener(EventObject e) {
        super.registerListener(e);
        Container billList2 = (Container)this.getView().getControl("cardflexpanelap");
        billList2.addClickListener(this);
    }

    public void click(EventObject e) {
        Control control = (Control)e.getSource();
        String key = control.getKey();
        if ("cardflexpanelap".equals(key)) {
            BillList billList = (BillList)this.getView().getControl("billlistap");
            ListSelectedRow row = billList.getCurrentSelectedRowInfo();
            Long selectId = (Long)row.getPrimaryKeyValue();
            String rowID = this.getView().getPageCache().get("cardflexpanelap");
            if (!CommonUtils.isNull(rowID) && rowID.equals(String.valueOf(selectId))) {
                return;
            }

            this.getView().getPageCache().put("cardflexpanelap", String.valueOf(selectId));
            IListView listview = (IListView)this.getView();
            long billID = 0L;
            String billentity = null;
            String xbill = listview.getPageCache().get(String.valueOf(selectId));
            if (!CommonUtils.isNull(xbill)) {
                String[] xbillinfo = xbill.split(",");
                if (xbillinfo.length == 2) {
                    if (!CommonUtils.isNull(xbillinfo[0])) {
                        billID = Long.parseLong(xbillinfo[0]);
                    }

                    billentity = xbillinfo[1];
                }
            }

            if (CommonUtils.isNull(billentity) || billID == 0L) {
                DynamicObject data = BusinessDataServiceHelper.loadSingle(selectId, "yem_changeresume");
                if (data != null) {
                    billentity = (String)data.get("xbillentity");
                    billID = (Long)data.get("xbillid");
                }
            }

            this.showBillinfo(billID, billentity);
            this.showChangeDetails(billID);
        }
    }

    public void showChangeDetails(long billID) {
        QFilter qFilter = new QFilter("xbillid", "=", billID);
        DynamicObject dys = BusinessDataServiceHelper.loadSingleFromCache("yem_changeresumedetail", "id", new QFilter[]{qFilter});
        IFormView formView = this.getView().getParentView();
        if (dys != null) {
            Object canResumeCardID = dys.getPkValue();
            if (!CommonUtils.isNull(canResumeCardID)) {
                long xbilllogtplID = (Long)canResumeCardID;
                Tab tab = (Tab)formView.getControl("tabap");
                tab.activeTab("change_tabap");
                BillShowParameter showParameter = new BillShowParameter();
                showParameter.getOpenStyle().setShowType(ShowType.InContainer);
                showParameter.getOpenStyle().setTargetKey("xbilllogtpl");
                showParameter.setFormId("yem_changeresumedetail");
                showParameter.setPkId(xbilllogtplID);
                showParameter.setBillStatus(BillOperationStatus.EDIT);
                formView.showForm(showParameter);
                formView.setVisible(true, "change_tabap");
                formView.updateView("change_tabap");
            } else {
                formView.setVisible(false, "change_tabap");
            }
        } else {
            formView.setVisible(false, "change_tabap");
            Tab tab = (Tab)formView.getControl("tabap");
            tab.activeTab("billinfo_tabap");
        }

        this.getView().sendFormAction(formView);
    }

    public void showBillinfo(long billID, String billentity) {
        Map<String, Object> customParams = new HashMap<>();
        customParams.put("InContainer","InContainer");
        IFormView formView = this.getView().getParentView();
        if (!CommonUtils.isNull(billentity) && billID != 0L) {
            QFilter qf1 = new QFilter("xbillid", "=", billID);
            boolean changeReCord = QueryServiceHelper.exists("yem_changeresume", new QFilter[]{qf1});
            if (changeReCord) {
                formView.setVisible(true, "billinfo");
                BillShowParameter showParameter = new BillShowParameter();
                showParameter.setCustomParams(customParams);
                showParameter.getOpenStyle().setShowType(ShowType.InContainer);
                showParameter.getOpenStyle().setTargetKey("billinfo"); //设定表单标识为billinfo
                showParameter.setFormId(billentity);
                showParameter.setPkId(billID);
                showParameter.setBillStatus(BillOperationStatus.VIEW);
                formView.showForm(showParameter);
            } else {
                this.getView().showTipNotification(ResManager.loadKDString("数据已不存在，请刷新页面。", "ChangeResumeListPlugin_0", "yem-yc-changemodel", new Object[0]));
                formView.setVisible(false, "billinfo");
            }
        } else {
            formView.setVisible(false, "billinfo");
        }
        this.getView().sendFormAction(formView);
    }
}
