package com.yem.wm.bc.bill.from;

import com.yem.wm.bc.change.business.ChangeModelHelper;
import com.yem.wm.utils.StringUtils;
import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.form.FormShowParameter;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.events.PreOpenFormEventArgs;
import kd.bos.form.operate.FormOperate;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.bos.servicehelper.operation.OperationServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.util.EventObject;

/**
 * @author 夜子
 * @ClassName YemBillPlugInBaseFrom
 * @description: TODO
 * @date 2023年08月11日
 * @version: 1.0
 */
public class YemBillPlugInBaseFrom extends AbstractBillPlugIn {

    @Override
    public void preOpenForm(PreOpenFormEventArgs e) {
        super.preOpenForm(e);
//        FormShowParameter fsp=  e.getFormShowParameter();
//        String billtype  = fsp.getFormConfig().getEntityTypeId();
//        if(ChangeModelHelper.getChangeModel5SrcBill(billtype)!=null){
//            fsp.addCustPlugin("com.yem.wm.bc.change.from.SrcBillChangePlugin");
//        }
//        if(ChangeModelHelper.getChangeModel5XBill(billtype)!=null){
//            fsp.addCustPlugin("com.yem.wm.bc.change.from.XBillChangePlugin");
//        }

    }

    @Override
    public void afterBindData(EventObject e) {
        super.afterBindData(e);
//        DynamicObject yemBilltype = (DynamicObject) this.getModel().getValue("yem_billtype");
//        if(yemBilltype==null){
//            this.getView().showErrorNotification("请添加/选择单据类型！");
//            return;
//        }
//        Object billno = this.getModel().getValue("billno");
//        this.getModel().setValue("yem_title",yemBilltype.getString("name")+"-"+billno);
//        String billtype  =  this.getModel().getDataEntityType().getName();
//        this.getView().setVisible(ChangeModelHelper.getChangeModel5SrcBill(billtype) != null,"bar_change");
//        this.getView().setVisible(ChangeModelHelper.getChangeModel5XBill(billtype) != null,"bar_bizvalid");
//        this.getView().setVisible(ChangeModelHelper.getChangeModel5SrcBill(billtype) != null || ChangeModelHelper.getChangeModel5XBill(billtype) != null,"bar_bizchangeresume");

    }

    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs e) {
        super.beforeDoOperation(e);
        FormOperate formOperate = (FormOperate) e.getSource();
        String operateKey = formOperate.getOperateKey();
        IDataModel model = getModel();
        //撤销前先判断有没有智能审批
        if("unsubmit".equals(operateKey)||"audit".equals(operateKey)){
            DynamicObject obj = this.getModel().getDataEntity(true);
//            //根据单据标识获取单据注册信息
//            QFilter qFilter = new QFilter("yem_isdisable", QCP.equals, false);
//            qFilter.and(new QFilter("yem_formid.number", QCP.equals, obj.getDynamicObjectType().getName()));
//            if(obj.containsProperty("yem_billtype")){
//                qFilter.and(new QFilter("yem_billtype.fbasedataid.number", QCP.in, obj.getString("yem_billtype.number")));
//            }
//            if("yem_orderproreqbill".equals(obj.getDynamicObjectType().getName())){
//                qFilter.and(new QFilter("yem_isvmi", QCP.equals, obj.get("yem_isvmi")));
//            }
//            if("yem_placeexamine".equals(obj.getDynamicObjectType().getName())){
//                qFilter.and(new QFilter("yem_salesordertype",QCP.equals,obj.getString("yem_salesordertype")));
//            }
//            boolean b = QueryServiceHelper.exists("yem_ia_intebilllogon", qFilter.toArray());
            String yem_forminstid = obj.getString("yem_forminstid");
            String yem_flowinstid = obj.getString("yem_flowinstid");
            if(!StringUtils.isBlank(yem_forminstid)||!StringUtils.isBlank(yem_flowinstid)){
                e.setCancel(true);
                if("unsubmit".equals(operateKey)){
                    getView().showErrorNotification("本单存在智能审批无法进行手工撤销，请在云之家进行驳回处理！");
                }else{
                    getView().showErrorNotification("本单存在智能审批无法进行手工审核，请在云之家进行审批处理！");
                }
            }
        }
        if("save".equals(operateKey)||"submit".equals(operateKey)){
            DynamicObject dynamicObject = this.getModel().getDataEntity(true);
            if (dynamicObject.containsProperty("yem_billtype")) {
                String typename = dynamicObject.getString("yem_billtype.name");
                Object billno = dynamicObject.get("billno");
                dynamicObject.set("yem_title", typename + "-" + billno);
                model.setValue("yem_title",typename+"-"+billno);
            }
        }

    }

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs e) {
        super.afterDoOperation(e);
        String operateKey = e.getOperateKey();
        if("submit".equals(operateKey)){
            DynamicObject dataEntity = this.getModel().getDataEntity(true);
            this.getView().updateView("yem_forminstid");
            this.getView().updateView("yem_flowinstid");
            this.getView().updateView("yem_isyzjreject");
            this.getView().invokeOperation("refresh");
        }
    }
}
