package com.yem.em.bd.etcmanual.dynamic;

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

import com.yem.em.utils.CarShopUtils;
import kd.bos.bill.BillShowParameter;
import kd.bos.bill.OperationStatus;
import kd.bos.context.RequestContext;
import kd.bos.form.FormShowParameter;
import kd.bos.form.IFormView;
import kd.bos.form.IPageCache;
import kd.bos.form.OpenStyle;
import kd.bos.form.ShowType;
import kd.bos.form.container.Tab;
import kd.bos.form.control.Button;
import kd.bos.form.control.Control;
import kd.bos.form.control.Label;
import kd.bos.form.control.events.SearchEnterEvent;
import kd.bos.form.control.events.SearchEnterListener;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.servicehelper.user.UserServiceHelper;


/**
 * @author 夜子
 * @ClassName EtcmanualFormEdit
 * @description: EPC首页表单插件
 * @date 2023年07月25日
 * @version: 1.0
 */
public class EtcmanualFormEdit extends AbstractFormPlugin implements SearchEnterListener {

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        Button btn = this.getView().getControl("yem_buttonap");
        btn.addClickListener(this);
    }

    @Override
    public void afterBindData(EventObject e) {
        super.afterBindData(e);
        IFormView view = this.getView();
        FormShowParameter showParameter = view.getFormShowParameter();
        Map<String, Object> customParas = showParameter.getCustomParams();
        if (customParas.containsKey("etcmanualid")) {
            IPageCache cache = this.getPageCache();
            cache.put("etcmanualid", customParas.get("etcmanualid").toString());

            /**
             * 购物车标签中的数字加载
             */
            if (customParas.containsKey("orgid")) {
                int count2 = CarShopUtils.SelectCarShop(UserServiceHelper.getCurrentUserId(),
                        customParas.get("orgid").toString());
                Label lab = view.getControl("yem_labelap2");
                lab.setText(count2 + "");
                lab = view.getControl("yem_labelap2");
                view.updateView("yem_labelap2");
            }

            if (null != customParas.get("etcmanualid").toString()
                    && customParas.get("etcmanualid").toString().trim().length() > 0) {
                Tab tab = view.getControl("yem_tabap");
                tab.activeTab("yem_tabpageap");
                BillShowParameter bsp = new BillShowParameter();
                bsp.setFormId("yem_em_etcmanual");
                bsp.setPkId(Long.valueOf(customParas.get("etcmanualid").toString()));
                OpenStyle openStyle = new OpenStyle();
                openStyle.setShowType(ShowType.InContainer);
                openStyle.setTargetKey("yem_flexpanelap7");
                bsp.setHasRight(true);
                Map<String, Object> entryInfo = new HashMap<String, Object>();
                entryInfo.put("parentpageid", view.getPageId());
                entryInfo.put("orgId", customParas.get("orgId").toString());
                bsp.setCustomParams(entryInfo);
                bsp.setStatus(OperationStatus.EDIT);
                bsp.setOpenStyle(openStyle);
                view.showForm(bsp);
            }
        } else if (customParas.containsKey("purapplyid")) {
            IPageCache cache = this.getPageCache();
            cache.put("purapplyid", customParas.get("purapplyid").toString());
            if (null != customParas.get("purapplyid").toString()
                    && customParas.get("purapplyid").toString().trim().length() > 0) {
                Tab tab = view.getControl("yem_tabap");
                tab.activeTab("yem_tabpageap2");
            }
        }
    }

    @Override
    public void click(EventObject evt) {
        super.click(evt);
        Control source = (Control) evt.getSource();
        String key = source.getKey();
        if ("yem_buttonap".equals(key)) {
            String searchkey = "";
            Tab tab = this.getView().getControl("yem_tabap");
            String tabKey = tab.getCurrentTab();
                switch (tabKey) {
                case "yem_tabpageap":
                    searchkey = (String) this.getModel().getValue("yem_searchap");
                    break;
                case "yem_tabpageap1":
                    searchkey = (String) this.getModel().getValue("yem_searchap1");
                    break;
                case "yem_tabpageap2":
                    searchkey = (String) this.getModel().getValue("yem_searchap2");
                    break;
            }
            initData(searchkey, tabKey);
        }
    }

    @Override
    public void search(SearchEnterEvent serachEvt) {
        String text = serachEvt.getText();
        Tab tab = this.getView().getControl("yem_tabap");
        String tabKey = tab.getCurrentTab();
        initData(text, tabKey);
        serachEvt.setText(text);
    }

    private void initData(String text, String tabKey) {
        IPageCache cache = this.getPageCache();
        switch (tabKey) {
            case "yem_tabpageap":
                cache.put("searchText_1", text);
                if (null != text && text.trim().length() > 0) {
                    FormShowParameter bsp = new FormShowParameter();
                    bsp.setFormId("yem_em_etcselectlist");
                    OpenStyle openStyle = new OpenStyle();
                    openStyle.setShowType(ShowType.InContainer);
                    openStyle.setTargetKey("yem_flexpanelap7");
                    Map<String, Object> entryInfo = new HashMap<String, Object>();
                    entryInfo.put("parentpageid", this.getView().getPageId());
                    entryInfo.put("serachtext", text);
                    entryInfo.put("orgId", RequestContext.get().getOrgId() + "");
                    bsp.setCustomParams(entryInfo);
                    bsp.setStatus(OperationStatus.VIEW);
                    bsp.setOpenStyle(openStyle);
                    this.getView().showForm(bsp);
                } else {
                    FormShowParameter showParameter = this.getView().getFormShowParameter();
                    Map<String, Object> customParas = showParameter.getCustomParams();
                    if (customParas.containsKey("etcmanualid")) {
                        if (null != customParas.get("etcmanualid").toString()
                                && customParas.get("etcmanualid").toString().trim().length() > 0) {
                            Tab tab2 = this.getView().getControl("yem_tabap");
                            tab2.activeTab("yem_tabpageap");
                            BillShowParameter bsp = new BillShowParameter();
                            bsp.setFormId("yem_em_etcmanual");
                            bsp.setPkId(Long.valueOf(customParas.get("etcmanualid").toString()));
                            OpenStyle openStyle = new OpenStyle();
                            openStyle.setShowType(ShowType.InContainer);
                            openStyle.setTargetKey("yem_flexpanelap7");
                            bsp.setHasRight(true);
                            Map<String, Object> entryInfo = new HashMap<String, Object>();
                            entryInfo.put("parentpageid", this.getView().getPageId());
                            entryInfo.put("orgId", RequestContext.get().getOrgId() + "");
                            bsp.setCustomParams(entryInfo);
                            bsp.setStatus(OperationStatus.EDIT);
                            bsp.setOpenStyle(openStyle);
                            this.getView().showForm(bsp);
                        }
                    } else {
                        FormShowParameter bsp = new FormShowParameter();
                        bsp.setFormId("yem_em_emtryform");
                        OpenStyle openStyle = new OpenStyle();
                        openStyle.setShowType(ShowType.InContainer);
                        openStyle.setTargetKey("yem_flexpanelap7");
                        bsp.setHasRight(true);
                        bsp.setStatus(OperationStatus.VIEW);
                        bsp.setOpenStyle(openStyle);
                        this.getView().showForm(bsp);
                    }
                }
                break;
            case "yem_tabpageap1":
                cache.put("searchText_2", text);
                if (null != text && text.trim().length() > 0) {
                    FormShowParameter bsp = new FormShowParameter();
                    bsp.setFormId("yem_em_epcselectlist");
                    OpenStyle openStyle = new OpenStyle();
                    openStyle.setShowType(ShowType.InContainer);
                    openStyle.setTargetKey("yem_flexpanelap7");
                    Map<String, Object> entryInfo = new HashMap<String, Object>();
                    entryInfo.put("parentpageid", this.getView().getPageId());
                    entryInfo.put("serachtext", text);
                    entryInfo.put("orgId", RequestContext.get().getOrgId() + "");
                    bsp.setCustomParams(entryInfo);
                    bsp.setStatus(OperationStatus.VIEW);
                    bsp.setOpenStyle(openStyle);
                    this.getView().showForm(bsp);
                } else {
                    FormShowParameter bsp = new FormShowParameter();
                    bsp.setFormId("yem_em_emtryform");
                    OpenStyle openStyle = new OpenStyle();
                    openStyle.setShowType(ShowType.InContainer);
                    openStyle.setTargetKey("yem_flexpanelap7");
                    bsp.setHasRight(true);
                    bsp.setStatus(OperationStatus.VIEW);
                    bsp.setOpenStyle(openStyle);
                    this.getView().showForm(bsp);
                }
                break;
            case "yem_tabpageap2":
                cache.put("searchText_3", text);
                if (null != text && text.trim().length() > 0) {
                    FormShowParameter bsp = new FormShowParameter();
                    bsp.setFormId("yem_em_purselectlist");
                    OpenStyle openStyle = new OpenStyle();
                    openStyle.setShowType(ShowType.InContainer);
                    openStyle.setTargetKey("yem_flexpanelap7");
                    Map<String, Object> entryInfo = new HashMap<String, Object>();
                    entryInfo.put("serachtext", text);
                    bsp.setCustomParams(entryInfo);
                    bsp.setStatus(OperationStatus.VIEW);
                    bsp.setOpenStyle(openStyle);
                    this.getView().showForm(bsp);
                } else {
                    FormShowParameter bsp = new FormShowParameter();
                    bsp.setFormId("yem_em_emtryform");
                    OpenStyle openStyle = new OpenStyle();
                    openStyle.setShowType(ShowType.InContainer);
                    openStyle.setTargetKey("yem_flexpanelap7");
                    bsp.setHasRight(true);
                    bsp.setStatus(OperationStatus.VIEW);
                    bsp.setOpenStyle(openStyle);
                    this.getView().showForm(bsp);
                }
                break;
            default:
                FormShowParameter bsp = new FormShowParameter();
                bsp.setFormId("yem_em_emtryform");
                OpenStyle openStyle = new OpenStyle();
                openStyle.setShowType(ShowType.InContainer);
                openStyle.setTargetKey("yem_flexpanelap7");
                bsp.setHasRight(true);
                bsp.setStatus(OperationStatus.VIEW);
                bsp.setOpenStyle(openStyle);
                this.getView().showForm(bsp);
                break;
        }
    }
}
