package yem.wmc.imc.formplugin.dynamic;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.tree.TreeNode;
import kd.bos.form.CloseCallBack;
import kd.bos.form.ShowType;
import kd.bos.form.control.TreeView;
import kd.bos.form.control.events.TreeNodeClickListener;
import kd.bos.form.control.events.TreeNodeEvent;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.list.ListFilterParameter;
import kd.bos.list.ListShowParameter;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

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

/**
 * @author 赵岩峰
 * @date 2023/12/21 15:20
 * @description 发票全票池
 * @versions 1.0
 */
public class InvoiceFullTicketPool extends AbstractFormPlugin implements TreeNodeClickListener {

    @Override
    public void initialize() {
        super.initialize();
        TreeView tree = (TreeView) this.getView().getControl("yem_treeviewap");
        tree.addTreeNodeClickListener(this);
    }

    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);
        QFilter filter = new QFilter("name", QCP.equals, "全部");
        DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle("yem_imc_invoicetype", "id,number,name,yem_basedata", new QFilter[]{filter});
        DynamicObject[] yemImcInvoicetypes = BusinessDataServiceHelper.load("yem_imc_invoicetype", "id,number,name,yem_basedata", null);

        TreeView tree = (TreeView) this.getView().getControl("yem_treeviewap");
        List<TreeNode> list = new ArrayList<>();
        TreeNode treeNodeNew = new TreeNode();
//        treeNodeNew.setId("1845030858491168768");
        treeNodeNew.setId(dynamicObject.getLong("id")+"");
        treeNodeNew.setText("全部");
        list.add(treeNodeNew);
        for (int i = 0; i < yemImcInvoicetypes.length; i++) {
            TreeNode treeNode = new TreeNode();
            String name = yemImcInvoicetypes[i].getString("name");
            long aLong = yemImcInvoicetypes[i].getLong("id");
            if (aLong != dynamicObject.getLong("id")) {
                treeNode.setId(aLong + "");
                treeNode.setText(name);
                list.add(treeNode);
            }
        }
        tree.addNodes(list);
        tree.focusNode((TreeNode) list.get(0));
        tree.treeNodeClick((String) null, ((TreeNode) list.get(0)).getId());
    }

    @Override
    public void treeNodeClick(TreeNodeEvent evt) {
        Long nodeId = Long.parseLong((String) evt.getNodeId());
        ListShowParameter showParameter = new ListShowParameter();
        ListFilterParameter listFilterParameter = new ListFilterParameter();
        QFilter qFilter = new QFilter("id", QCP.equals, nodeId);
        DynamicObject invoicetype = BusinessDataServiceHelper.loadSingle("yem_imc_invoicetype", new QFilter[]{qFilter});
        DynamicObject yemBasedata = invoicetype.getDynamicObject("yem_basedata");
        if (yemBasedata != null) {
            showParameter.setBillFormId(yemBasedata.getString("number"));
            if (nodeId != 1959576494325902336L) { // 发票类型（全部）的id值
                ArrayList<QFilter> filters = new ArrayList<>();
                QFilter filter = new QFilter("yem_invoice_type", QCP.equals, nodeId);//根据发票类型过滤
                filters.add(filter);
                showParameter.setShowTitle(false);
                listFilterParameter.setQFilters(filters);
            }
            if (invoicetype != null) {
                showParameter.setCaption(invoicetype.getString("name"));
            }
            showParameter.setListFilterParameter(listFilterParameter);
            showParameter.getOpenStyle().setShowType(ShowType.InContainer);
            showParameter.getOpenStyle().setTargetKey("yem_data");
            CloseCallBack closeCallBack = new CloseCallBack(this, "bos_list");
            showParameter.setCloseCallBack(closeCallBack);
            this.getView().showForm(showParameter);
        }
    }
}





























