package yem.wm.cm.ht.form;

import java.util.EventObject;

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.LargeTextOtherEdit;
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;

/**
 * 合同条款库
 * 1、部门根据所属团队进行过滤
 * @author zhangh
 *
 */
public class ContractLibraryFormPlugin extends AbstractBillPlugIn implements BeforeF7SelectListener{

	@Override
	public void registerListener(EventObject e) {
		((BasedataEdit) this.getControl("yem_org")).addBeforeF7SelectListener(this);
		((BasedataEdit) this.getControl("yem_department")).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_languagetype":
				//设想（语言类型不能重复）
				break;
			case "yem_clausecontent"://条款内容（中文）
				String yem_clausescontent = (String) this.getModel().getValue("yem_clausecontent", rowIndex);//条款内容（中文）
				this.getModel().setValue("yem_clausecontent_tag", yem_clausescontent, rowIndex);
				setChangeValue("yem_change", yem_clausescontent, rowIndex);//原条款内容（中文
				break;

			case "yem_clausecontent_tag":
				String yem_clausecontentchina_tag = (String) this.getModel().getValue("yem_clausecontent_tag", rowIndex);
				this.getModel().setValue("yem_clausecontent", yem_clausecontentchina_tag, rowIndex);
				setChangeValue("yem_change", yem_clausecontentchina_tag, rowIndex);
				break;
//
//			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":
				this.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);
			this.getModel().setValue(key + "_tag", yem_clausescontent);
		}
	}


	@Override
	public void beforeF7Select(BeforeF7SelectEvent e) {
		String name = e.getProperty().getName();
		switch (name) {
		case "yem_org":
			this.setOrgF7Filter(e);
			break;
		case "yem_department":
		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_department = (DynamicObject) this.getModel().getValue("yem_department");
		if (yem_department != null) {
			showParameter.getListFilterParameter().setFilter(new QFilter("structure.viewparent.name", QCP.equals, yem_department.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 afterBindData(EventObject e) {
		setMustInput();//【条款名称(英文)】、【条款内容(英文)】根据【贸易类型】进行必录
	}

	/**
	 * 【条款名称(英文)】、【条款内容(英文)】根据【贸易类型】进行必录
	 */
	private void setMustInput() {
//		String yem_foreigntrade = (String) this.getModel().getValue("yem_foreigntrade");
//		TextEdit yem_clauseenglish = this.getControl("yem_clauseenglish");
//		LargeTextOtherEdit 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);
//		}
	}
}
