package yem.wm.cm.csrm.form;

import cn.hutool.core.util.CreditCodeUtil;
import kd.bos.bill.OperationStatus;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.events.BizDataEventArgs;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.form.FormShowParameter;
import kd.bos.form.ShowType;
import kd.bos.form.StyleCss;
import kd.bos.form.container.Tab;
import kd.bos.form.control.events.ItemClickEvent;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.mvc.form.FormView;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.sdk.plugin.Plugin;
import yem.wm.cm.csrm.enums.PageIdEnum;

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

/**
 * 客商准入表单插件
 *
 * @Author yunshuaiwei
 * @Date 2024/5/29 15:59
 **/
public class MerchantApplyFormPlugIn extends AbstractFormPlugin implements Plugin {

    Log log = LogFactory.getLog(MerchantApplyFormPlugIn.class);

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        // 侦听主菜单按钮点击事件
        this.addItemClickListeners("tbmain");
    }

    @Override
    public void itemClick(ItemClickEvent evt) {
        super.itemClick(evt);
    }

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs e) {
        super.afterDoOperation(e);
        String key = e.getOperateKey();
        OperationResult operationResult = e.getOperationResult();
        super.afterDoOperation(e);
        switch (key) {
            //重载企查查
            case "dockingqcc":
            case "submitdonothing":
                if (operationResult.isSuccess()) {
                    String taskLogId = operationResult.getMessage();
                    String societyCreditCode = (String) this.getView().getModel().getValue("yem_societycreditcode");
                    boolean creditCode = CreditCodeUtil.isCreditCode(societyCreditCode);
                    if (!creditCode) {
                        this.getView().showErrorNotification("统一社会信用代码格式错误");
                    }else {
                        log.debug("多任务处理进度ID={}", taskLogId);
                        this.getView().setEnable(false, "yem_societycreditcode", "yem_bar_dockingqcc", "yem_submitdothing");
                        openTaskScheduleBill(taskLogId,societyCreditCode);
                    }
                }
                break;
            default:
                break;
        }
    }

    /**
     * 打开任务进度内嵌表
     *
     * @Author yunshuaiwei
     * @Date 2024/5/29 15:57
     **/
    private void openTaskScheduleBill(String taskLogId,String searchKey) {
        FormShowParameter showParameter = new FormShowParameter();
        showParameter.setStatus(OperationStatus.ADDNEW);
        showParameter.getOpenStyle().setShowType(ShowType.InContainer);
        Map<String, Object> map = null;
        //子页面编码
        showParameter.setFormId(PageIdEnum.MULTI_TASK.getPageId());
        showParameter.getOpenStyle().setTargetKey("yem_flexpanelap5");
        showParameter.setCustomParams(map);
        StyleCss styleCss = new StyleCss();
        styleCss.setWidth("100%");
        styleCss.setHeight("100%");
        showParameter.getOpenStyle().setInlineStyleCss(styleCss);
        showParameter.setCustomParam("taskLogId", taskLogId);
        showParameter.setCustomParam("searchKey", searchKey);
        showParameter.setCustomParam("billNo", PageIdEnum.MERCHANT_APPLY.getPageId());
        this.getView().setVisible(true, "yem_flexpanelap3");
        //激活任务进度页签
        Tab tab = this.getView().getControl("yem_tabap2");
        tab.activeTab("yem_tabpageap11");
        //隐藏金字塔页签
        this.getView().setVisible(false, "yem_tabpageap12");
        this.getView().showForm(showParameter);
    }

    @Override
    public void beforeBindData(EventObject e) {
        super.beforeBindData(e);
        // TODO 在此添加业务逻辑
        FormView source = (FormView) e.getSource();
        String formId = source.getFormId();
        //隐藏任务进度和金字塔容器
        if (PageIdEnum.MERCHANT_APPLY.getPageId().equals(formId)) {
            this.getView().setVisible(false, "yem_flexpanelap3");
        }
    }

    @Override
    public void createNewData(BizDataEventArgs e) {
        // TODO 在此添加业务逻辑
        FormShowParameter formShowParameter = this.getView().getFormShowParameter();
        String formId = formShowParameter.getFormId();
        if (PageIdEnum.MERCHANT_APP_LAYOUT.getPageId().equals(formId)) {
            Long merchantId = formShowParameter.getCustomParam("merchantId");
            DynamicObject object = BusinessDataServiceHelper.loadSingle(PageIdEnum.MERCHANT_APPLY.getPageId(), new QFilter[]{new QFilter("id", QCP.equals, merchantId)});
            e.setDataEntity(object);
        }
    }
}