package com.yem.em.bd.project.list;

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


import com.yem.em.utils.BaseDataFilterUtils;
import com.yem.em.utils.EmptyUtils;
import kd.bos.bill.BillShowParameter;
import kd.bos.bill.OperationStatus;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.resource.ResManager;
import kd.bos.entity.datamodel.ListSelectedRow;
import kd.bos.entity.datamodel.ListSelectedRowCollection;
import kd.bos.entity.tree.TreeNode;
import kd.bos.filter.FilterContainer;
import kd.bos.form.FormShowParameter;
import kd.bos.form.ShowType;
import kd.bos.form.control.TreeView;
import kd.bos.form.control.events.ItemClickEvent;
import kd.bos.form.control.events.RefreshNodeEvent;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.list.BillList;
import kd.bos.list.ITreeListView;
import kd.bos.list.ListFilterParameter;
import kd.bos.list.ListShowParameter;
import kd.bos.list.plugin.AbstractTreeListPlugin;
import kd.bos.mvc.list.ListView;
import kd.bos.mvc.list.TreeListModel;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

/**
 * @author 夜子
 * @ClassName ProjectList
 * @description: 公司项目列表
 * @date 2023年07月12日
 * @version: 1.0
 */
public class ProjectList extends AbstractTreeListPlugin {

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        FilterContainer filterContainer = this.getControl("filtercontainerap");
        filterContainer.addAfterSearchClickListener((searchClickEvent) -> this.refreshTreeRootNode());
    }

    private void refreshTreeRootNode() {
        String rootId = this.treeListView.getTreeModel().getRoot().getId();
        this.treeListView.getTreeModel().setCurrentNodeId(rootId);
        TreeView treeview = this.getTreeListView().getTreeView();
        treeview.focusNode(this.treeListView.getTreeModel().getRoot());

        this.treeListView.refresh();
        String orgId = this.getPageCache().get("createOrg");
        if (!orgId.equals("100000")) {
            this.getView().setVisible(Boolean.FALSE,
                    "tblnew", "tblsubmit", "tbldel", "tblaudit", "tbldisable", "tp_baritemap",
                    "baritemap", "tblassign", "bar_manage", "tp_baritemap1");
            this.getView().setVisible(Boolean.FALSE,
                    "btnnew", "btnedit", "btndel", "searchap");
        } else {
            this.getView().setVisible(Boolean.TRUE, "tblnew", "tblsubmit", "tbldel", "tblaudit",
                    "tbldisable", "tp_baritemap", "baritemap", "tblassign", "bar_manage", "tp_baritemap1");
            this.getView().setVisible(Boolean.TRUE,
                    "btnnew", "btnedit", "btndel", "searchap");
        }
    }

    @Override
    public void refreshNode(RefreshNodeEvent e) {
        super.refreshNode(e);
        if (this.getControlFilters() != null) {
            String orgId = this.getPageCache().get("createOrg");
            if (!orgId.equals("100000")) {
                ListView listView = (ListView) this.getView();
                ITreeListView treeListView = listView.getTreeListView();
                TreeListModel treeListModel = (TreeListModel) treeListView.getTreeModel();
                QFilter baseDataFilter = BaseDataFilterUtils.getBaseDataFilter("yem_em_projectgroup",
                        Long.parseLong(orgId));
                treeListModel.getTreeFilter().add(baseDataFilter);
            }
        }
    }

    @Override
    public void afterBindData(EventObject e) {
        super.afterBindData(e);
        String orgId = this.getPageCache().get("createOrg");
        if (!orgId.equals("100000")) {
            this.getView().setVisible(Boolean.FALSE,
                    "tblnew", "tblsubmit", "tbldel", "tblaudit", "tbldisable", "tp_baritemap",
                    "baritemap", "tblassign", "bar_manage", "tp_baritemap1");
        } else {
            this.getView().setVisible(Boolean.TRUE, "tblnew", "tblsubmit", "tbldel", "tblaudit",
                    "tbldisable", "tp_baritemap", "baritemap", "tblassign", "bar_manage", "tp_baritemap1");
        }
    }

    @Override
    public void initTreeToolbar(EventObject e) {
        super.initTreeToolbar(e);
        String orgId = this.getPageCache().get("createOrg");
        if (!orgId.equals("100000")) {
            this.getView().setVisible(Boolean.FALSE,
                    "btnnew", "btnedit", "btndel", "searchap");
        } else {
            this.getView().setVisible(Boolean.FALSE,
                    "btnnew", "btnedit", "btndel", "searchap");
        }
    }

    @Override
    public void initializeTree(EventObject arg0) {
        TreeNode root = this.getTreeModel().createRootNode();
        String orgId = this.getPageCache().get("createOrg");
        QFilter baseDataFilter = BaseDataFilterUtils.getBaseDataFilter("yem_em_projectgroup", Long.parseLong(orgId));
        this.getTreeModel().getTreeFilter().add(baseDataFilter);
        this.getTreeModel().setRoot(root);
        this.getTreeModel().setCurrentNodeId(root.getId());
    }

    @Override
    public void itemClick(ItemClickEvent evt) {
        super.itemClick(evt);
        if (evt.getItemKey().equals("yem_baritemap")) {
            BillList billList = (BillList) this.getView().getControl("billlistap");
            ListSelectedRowCollection selectedRows = billList.getSelectedRows();
            if (selectedRows.size() == 1) {
                ListSelectedRow lsr = selectedRows.get(0);
                if (lsr != null && lsr.getPrimaryKeyValue() != null) {
                    Long fid = (Long) lsr.getPrimaryKeyValue();
                    QFilter qFilter1 = new QFilter("id", "=", fid);
                    DynamicObject project = BusinessDataServiceHelper.loadSingle("yem_em_project",
                            "id,number,yem_materiel", new QFilter[] { qFilter1 });
                    if (project != null) {
                        DynamicObject materiel = project.getDynamicObject("yem_materiel");
                        if (materiel != null) {
                            QFilter qFilter2 = new QFilter("yem_etcnumber", "=", materiel.getPkValue());
                            DynamicObject etcmanual = BusinessDataServiceHelper.loadSingle("yem_em_etcmanual",
                                    "id,number,yem_etcnumber", new QFilter[] { qFilter2 });
                            if (etcmanual != null) {
                                BillShowParameter bsp = new BillShowParameter();
                                bsp.setStatus(OperationStatus.VIEW);
                                bsp.getOpenStyle().setShowType(ShowType.MainNewTabPage);
                                bsp.setFormId("yem_em_etcmanual");
                                bsp.setPkId(Long.valueOf(etcmanual.getPkValue().toString()));
                                this.getView().showForm(bsp);
                            }
                        }
                    }
                }
            } else if (selectedRows.size() == 0) {
                this.getView()
                        .showTipNotification(ResManager.loadKDString("请先选择数据行！", "ProjectList_0", "bfcl_equipment"));
            } else if (selectedRows.size() > 1) {
                this.getView()
                        .showTipNotification(ResManager.loadKDString("只能选择一条数据！", "ProjectList_1", "bfcl_equipment"));
            }
        }
    }

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs e) {
        super.afterDoOperation(e);
        List<QFilter> qFiltersList = new ArrayList<>();
        String itemKey = e.getOperateKey();
        BillList billList = this.getView().getControl("billlistap");
        ListSelectedRowCollection selectedRows = billList.getSelectedRows();
        if (EmptyUtils.isNotEmpty(itemKey)) {
            if ("equipercordsdo".equals(itemKey)) {// 设备台账
                if (e.getOperationResult().getSuccessPkIds().size() > 0
                        && e.getOperationResult().getSuccessPkIds().size() == 1) {
                    Long fid = (Long) e.getOperationResult().getSuccessPkIds().get(0);
                    QFilter qFilter1 = new QFilter("id", "=", fid);
                    DynamicObject project = BusinessDataServiceHelper.loadSingle("yem_em_project", "id,number",
                            new QFilter[] { qFilter1 });
                    if (project != null) {
                        qFiltersList.add(new QFilter("yem_company", QCP.equals, project.getPkValue()));
                    }
                    ListShowParameter listpara = new ListShowParameter();
                    listpara.setBillFormId("yem_em_devicefiles");
                    listpara.setParentPageId(this.getView().getPageId());
                    listpara.setMultiSelect(false);
                    listpara.setStatus(OperationStatus.VIEW);
                    listpara.getOpenStyle().setShowType(ShowType.MainNewTabPage);
                    listpara.getOpenStyle().setCacheId(listpara.getPageId());

                    ListFilterParameter listFilterParameter = new ListFilterParameter();
                    if (qFiltersList.size() > 0) {
                        listFilterParameter.setQFilters(qFiltersList);
                    }
                    listpara.setListFilterParameter(listFilterParameter);
                    this.getView().showForm(listpara);
                    selectedRows.clear();
                    this.getView().invokeOperation("refresh");
                }
            }
            if ("etcmanual".equals(itemKey)) {// 设备电子目录
                if (e.getOperationResult().getSuccessPkIds().size() > 0
                        && e.getOperationResult().getSuccessPkIds().size() == 1) {
                    Long fid = (Long) e.getOperationResult().getSuccessPkIds().get(0);
                    QFilter qFilter1 = new QFilter("id", "=", fid);
                    DynamicObject project = BusinessDataServiceHelper.loadSingle("yem_em_project",
                            "id,number,yem_model", new QFilter[] { qFilter1 });
                    if (project != null) {
                        DynamicObject etcmanual = project.getDynamicObject("yem_model");
                        if (etcmanual != null) {
                            String pkId = etcmanual.getPkValue().toString();
                            FormShowParameter bsp = new FormShowParameter();
                            bsp.setStatus(OperationStatus.EDIT);
                            bsp.getOpenStyle().setShowType(ShowType.MainNewTabPage);
                            bsp.setParentPageId(this.getView().getPageId());
                            Map<String, Object> entryInfo = new HashMap<>();
                            entryInfo.put("etcmanualid", pkId);
                            String orgId = this.getPageCache().get("createOrg");
                            entryInfo.put("orgId", orgId);
                            bsp.setCustomParams(entryInfo);
                            bsp.setFormId("yem_em_epchome");
                            this.getView().showForm(bsp);
                        } else {
                            this.getView().showErrorNotification(
                                    ResManager.loadKDString("暂无设备电子目录信息！", "ProjectList_2", "bfcl_equipment"));
                        }
                    }
                }
            }
        }
    }

}
