package yem.wm.cm.csrm.form;

import kd.bos.bill.OperationStatus;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.events.BizDataEventArgs;
import kd.bos.form.FormShowParameter;
import kd.bos.form.IFormView;
import kd.bos.form.ShowType;
import kd.bos.form.StyleCss;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.sdk.plugin.Plugin;
import yem.base.common.utils.YEM;
import yem.wm.cm.csrm.enums.PageIdEnum;
import yem.wm.cm.csrm.utils.BillCommonUtils;

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

/**
 * 客商评分动态表单插件
 *
 * @Author yunshuaiwei
 * @Date 2024/6/27 11:37
 **/
public class ScoringMerchantratPlugIn extends AbstractFormPlugin implements Plugin {

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

    @Override
    public void beforeBindData(EventObject e) {
        super.beforeBindData(e);
        FormShowParameter formShowParameter = this.getView().getFormShowParameter();
        String formId = formShowParameter.getFormId();
        if (formId.equals(PageIdEnum.SCORE_MERCHANT_LAYOUT.getPageId())) {
            //隐藏金字塔容器
            this.getView().setEnable(false, "yem_flexpanelap2");
        } else {
            pyramidPage();
        }
    }

    /**
     * 内嵌评分金字塔
     *
     * @Author yunshuaiwei
     * @Date 2024/6/27 15:01
     **/
    private void pyramidPage() {
        IFormView view = this.getView();
        Long id = (Long) view.getModel().getValue("id");
        if (YEM.isNotEmpty(id)) {
            StyleCss styleCss = new StyleCss();
            styleCss.setWidth("100%");
            styleCss.setHeight("100%");
            Map<String, Object> maps = new HashMap<>();
            maps.put("id", id);
            BillCommonUtils.openTargetBill(view, maps, PageIdEnum.SCORE_PYRAMID.getPageId(), "yem_flexpanelap4", OperationStatus.VIEW, ShowType.InContainer, styleCss);
        }
    }
}