package yem.wm.sex.fagreement;

import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.entity.plugin.annotation.PluginEvent;
import kd.bos.form.IFormView;
import kd.bos.form.field.DateTimeEdit;
import yem.base.common.utils.YEM;

import java.util.Date;

/**
 * @Description 自营出口 - 框架协议 表单主插件
 * @Author: bang
 * @CreateTime: 2025-02-18  11:31
 */
public class SexFagreementFromPlugin extends AbstractBillPlugIn {

    @Override
    @PluginEvent(enableOverride = true)
    public void propertyChanged(PropertyChangedArgs e) {
        super.propertyChanged(e);
        ChangeData[] changeSet = e.getChangeSet();
        int rowIndex = changeSet[0].getRowIndex();
        String name = e.getProperty().getName();
        switch (name){
            case "yem_startdate":// 开始日期
            case "yem_expirationdate":// 结束日期
                datePropertyChange(name);
                break;
            default:
                break;
        }
    }

    /**
     * @Description: 结束日期 不可 早于 开始日期
     * @function 功能  根据开始日期 设置结束日期最小范围
     *                 结束日期选择后判断开始日期是否有值
     * @purposes 用途 ：开始日期值改变
     *                单据新增
     */
    private void datePropertyChange(String propertyKey){
        IFormView view = this.getView();
        IDataModel model = view.getModel();
        Date yemStartdate = (Date) model.getValue("yem_startdate");
        Date yemExpirationdate = (Date) model.getValue("yem_expirationdate");
        if ("yem_startdate".equals(propertyKey)){
            if (YEM.isEmpty(yemStartdate)) return;
            ((DateTimeEdit) view.getControl("yem_expirationdate")).setMinDate(yemStartdate);
            if (!YEM.isEmpty(yemExpirationdate) && !yemExpirationdate.after(yemStartdate))
                  model.setValue("yem_expirationdate",null);
        } else {
            if (!YEM.isEmpty(yemExpirationdate) && YEM.isEmpty(yemStartdate)){
                this.getView().showErrorNotification("请先选择开始日期！");
                model.setValue("yem_expirationdate",null);
            }
        }
    }

}