package yem.wm.cm.ht.form;

import java.util.EventObject;

import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.utils.StringUtils;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.control.Control;
import kd.bos.form.events.ClosedCallBackEvent;
import kd.bos.form.field.BasedataEdit;
import kd.bos.lang.Lang;
import yem.base.common.module.meta.propselect.helper.MetaPropertyHelper;
import yem.base.common.utils.YEM;

/**
 * 字段对照
 * 1、选择字段标识带出字段名称和显示格式
 * @author zhangh
 *
 */
public class ClauseContrastFormPlugin extends AbstractBillPlugIn{

	@Override
	public void registerListener(EventObject e) {
		this.addClickListeners(new String[]{"yem_fieldkey"});
	}
	
	@Override
	public void click(EventObject evt) {
		String key = ((Control) evt.getSource()).getKey();
		switch (key) {
		case "yem_fieldkey"://字段标识
			this.clickBillCol(evt, key, "yem_billkey", "number");//需求单据
			break;
		}
	}

	/**
	 * 打开动态表单，读入本单字段，弹出字段选择器
	 */
	private void clickBillCol(EventObject evt, String key, String baseCol, String childCol) {
		DynamicObject requireBill = MetaPropertyHelper.getAndCheckF7Value(this.getView(), baseCol);//获取并检查F7字段值，用于获取依赖的F7字段并检查是否存在及录入
		if(requireBill != null) {
			//获取需求单据的单据标识
			String formKey = requireBill.getString(childCol);
			//打开动态表单，读入本单字段，弹出字段选择器
			MetaPropertyHelper.showPropertySelect(this, key, formKey, false);
		}
	}
	
	@Override
	public void closedCallBack(ClosedCallBackEvent e) {
		String key = e.getActionId();
		switch (key) {
		case "yem_fieldkey":
			this.callBackEntryData(e);//将数据返回条款对照
			break;
		default:
			break;
		}
	}

	/**
	 * 将数据返回条款对照
	 * @param e
	 */
	private void callBackEntryData(ClosedCallBackEvent e) {
		DynamicObjectCollection data = (DynamicObjectCollection) e.getReturnData();
		if (YEM.isNotEmpty(data)) {
			for (int i = 0; i < data.size(); ++i) {
				DynamicObject info = data.get(i);
				String fullnumber = info.getString("yem_number");
				String fullname = info.getString("yem_name");
				String classSimpleName = info.getString("yem_datatype");
				this.getModel().setValue("yem_fieldkey", fullnumber);//字段标识
				this.getModel().setValue("name", fullname);//名称
				this.getModel().setValue("yem_format", classSimpleName);//显示格式
				this.getModel().setValue("yem_field", info.getString("yem_fullnumber"));//字段全称
			}
		}
	}
	
	@Override
	public void afterBindData(EventObject e) {
		this.mustInputFieldType();
	}
	
	private void mustInputFieldType() {
		String yem_fieldkey = (String) this.getModel().getValue("yem_fieldkey");
		BasedataEdit yem_fieldtype = this.getControl("yem_fieldtype");//字段类型
		if (StringUtils.isNotEmpty(yem_fieldkey)) {
			yem_fieldtype.setMustInput(false);//设置是否必录
		}else {
			yem_fieldtype.setMustInput(true);//设置是否必录
		}
	}

	@Override
	public void propertyChanged(PropertyChangedArgs e) {
		Lang lang = YEM.requestContext().getLang();
		String name = e.getProperty().getName();
		switch (name) {
		case "yem_fieldkey":
			this.mustInputFieldType();
			break;

		case "yem_billkey":
			this.getModel().setValue("yem_fieldkey", "");
			this.getModel().setValue("yem_format", "");
			this.getModel().setValue("yem_field", "");
		default:
			break;
		}
	}
}
