package yem.wm.ct.formplugin.form;

import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.field.BasedataEdit;
import kd.bos.form.field.LargeTextEdit;
import kd.bos.form.field.TextEdit;
import kd.bos.form.field.events.BeforeF7SelectEvent;
import kd.bos.form.field.events.BeforeF7SelectListener;
import kd.bos.list.ListShowParameter;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.sdk.plugin.Plugin;

import java.util.EventObject;

/**
 * 合同条款库
 * @author zhangh
 *
 */
public class ContractLibraryFormPlugin extends AbstractBillPlugIn implements BeforeF7SelectListener {
    public void beforeF7Select(BeforeF7SelectEvent e) {
        String name = e.getProperty().getName();
        switch (name) {
            case "yem_org":
                this.setOrgF7Filter(e);
                break;
            case "yem_dept":
            case "yem_sharedscope":
//			this.setDeptF7Filter(e);
                break;

            default:
                break;
        }
    }

    /**
     * 部门根据所属团队进行过滤
     * @param e
     */
    public void setDeptF7Filter(BeforeF7SelectEvent e) {
        ListShowParameter showParameter = (ListShowParameter) e.getFormShowParameter();
        showParameter.getListFilterParameter().getQFilters().clear();
        DynamicObject yem_dept = (DynamicObject) this.getModel().getValue("yem_org");
        if (yem_dept != null) {
            showParameter.getListFilterParameter().setFilter(new QFilter("structure.viewparent.name", QCP.equals, yem_dept.getString("name")));
        }
    }

    /**
     * 所属团队过滤
     * @param e
     */
    private void setOrgF7Filter(BeforeF7SelectEvent e) {
        ListShowParameter showParameter = (ListShowParameter) e.getFormShowParameter();
        showParameter.getListFilterParameter().getQFilters().clear();
        showParameter.getListFilterParameter().setFilter(new QFilter("orgpattern.name", QCP.in, new String[] {"事业部","公司"}));
    }

    @Override
    public void registerListener(EventObject e) {
        ((BasedataEdit) this.getControl("yem_org")).addBeforeF7SelectListener(this);
        ((BasedataEdit) this.getControl("yem_dept")).addBeforeF7SelectListener(this);
    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        String name = e.getProperty().getName();
        ChangeData[] changeSet = e.getChangeSet();
        int rowIndex = changeSet[0].getRowIndex();
        switch (name) {
            case "yem_clausecontentchina":
                String yem_clausescontent = (String) this.getModel().getValue("yem_clausecontentchina", rowIndex);
                this.getModel().setValue("yem_clausecontentchina_tag", yem_clausescontent, rowIndex);
                setChangeValue("yem_changechina", yem_clausescontent, rowIndex);
                break;

            case "yem_clausecontentchina_tag":
                String yem_clausecontentchina_tag = (String) this.getModel().getValue("yem_clausecontentchina_tag", rowIndex);
                this.getModel().setValue("yem_clausecontentchina", yem_clausecontentchina_tag, rowIndex);
                setChangeValue("yem_changechina", yem_clausecontentchina_tag, rowIndex);
                break;

            case "yem_clausecontentenglish":
                String yem_clausecontentenglish = (String) this.getModel().getValue("yem_clausecontentenglish", rowIndex);
                this.getModel().setValue("yem_clausecontentenglish_tag", yem_clausecontentenglish, rowIndex);
                setChangeValue("yem_changeenglish", yem_clausecontentenglish, rowIndex);
                break;

            case "yem_clausecontentenglish_tag":
                String yem_clausecontentenglish_tag = (String) this.getModel().getValue("yem_clausecontentenglish_tag", rowIndex);
                this.getModel().setValue("yem_clausecontentenglish", yem_clausecontentenglish_tag, rowIndex);
                setChangeValue("yem_changeenglish", yem_clausecontentenglish_tag, rowIndex);
                break;
            case "yem_foreigntrade":
//			setMustInput();
                break;
            default:
                break;
        }
    }

    private void setChangeValue(String key, String yem_clausescontent, int rowIndex) {
        Integer yem_version = (Integer) this.getModel().getValue("yem_version");
        if (yem_version == 1) {
            this.getModel().setValue(key, yem_clausescontent, rowIndex);
            this.getModel().setValue(key + "_tag", yem_clausescontent, rowIndex);
        }
    }

    @Override
    public void afterBindData(EventObject e) {
//		setMustInput();
    }

    /**
     * 【条款名称(英文)】、【条款内容(英文)】根据【贸易类型】进行必录{作废}
     */
    public void setMustInput() {
        String yem_foreigntrade = (String) this.getModel().getValue("yem_foreigntrade");
        TextEdit yem_clauseenglish = this.getControl("yem_clauseenglish");
        LargeTextEdit yem_clausecontentenglish = this.getControl("yem_clausecontentenglish");
        if ("A".equals(yem_foreigntrade)) {
            yem_clauseenglish.setMustInput(true);
            yem_clausecontentenglish.setMustInput(true);
        }else {
            yem_clauseenglish.setMustInput(false);
            yem_clausecontentenglish.setMustInput(false);
        }
    }
}