package com.yem.wm.sys.formplugin.dynamic;

import java.util.EventObject;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import kd.bos.dataentity.serialization.SerializationUtils;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.filter.FilterCondition;
import kd.bos.form.FormShowParameter;
import kd.bos.form.IFormView;
import kd.bos.form.control.FilterGrid;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.plugin.AbstractFormPlugin;

public class VersionCheckRuleEdit extends AbstractFormPlugin {



	@Override
	public void beforeBindData(EventObject e) {
		super.beforeBindData(e);
		FormShowParameter showParameter = this.getView().getFormShowParameter();
		String entityNumber = showParameter.getCustomParam("entityNumber");
		FilterGrid filterGrid = this.getView().getControl("yem_filtergridap");
		//设置过滤控件关联单据实体
		filterGrid.setEntityNumber(entityNumber);
	}
	
	@Override
	public void afterBindData(EventObject e) {
		super.afterBindData(e);
		IFormView view = this.getView();
		FormShowParameter showParameter = view.getFormShowParameter();
		JSONObject data = showParameter.getCustomParam("data");
		if(data != null) {
			String yem_checkjson = data.getString("yem_checkjson");//序列化校验条件
			if(!yem_checkjson.isEmpty()) {
				JSONObject parseObject = JSON.parseObject(yem_checkjson);
				//加载过滤控件数据
				String yem_filtergridap = parseObject.getString("yem_filtergridap");
				if(!yem_filtergridap.isEmpty()) {
					FilterCondition filterCondition = SerializationUtils.fromJsonString(yem_filtergridap, FilterCondition.class);
					if (filterCondition != null){
				    	FilterGrid filterGrid = view.getControl("yem_filtergridap");
				    	filterGrid.SetValue(filterCondition);
				    }
				}
			}
		}
	}
	
	@Override
	public void afterCreateNewData(EventObject evt) {
		super.afterCreateNewData(evt);
		IDataModel model = this.getModel();
		IFormView view = this.getView();
		FormShowParameter showParameter = view.getFormShowParameter();
		JSONObject data = showParameter.getCustomParam("data");
		if(data != null) {
			String yem_checkjson = data.getString("yem_checkjson");//序列化校验条件
			if(!yem_checkjson.isEmpty()) {
				JSONObject parseObject = JSON.parseObject(yem_checkjson);
				//加载描述
				model.setValue("yem_desc", parseObject.getString("yem_desc"));
				//加载条件为真提示
				model.setValue("yem_truethrow", parseObject.getBoolean("yem_truethrow"));
				//加载出错提示
				model.setValue("yem_message", parseObject.getString("yem_message"));
			}
		}
		
	}
	
	@Override
	public void afterDoOperation(AfterDoOperationEventArgs e) {
		super.afterDoOperation(e);
		String operateKey = e.getOperateKey();
		switch (operateKey) {
		case "ok" :
			this.afterDoOK();
			break;
		}
	}
	
	/**
	 * 点击确定回传数据
	 * @param evt
	 * @param key
	 */
	private void afterDoOK() {
		IDataModel model = this.getModel();
		IFormView view = this.getView();
		FilterGrid filterGrid = view.getControl("yem_filtergridap");
	    FilterCondition filterCondition = filterGrid.getFilterGridState().getFilterCondition();
	    String yem_filtergridap = "";
	    if (filterCondition != null){
	    	yem_filtergridap = SerializationUtils.toJsonString(filterCondition);
	    }
	    JSONObject jsonObj = new JSONObject();
	    jsonObj.put("yem_desc", model.getValue("yem_desc"));
	    jsonObj.put("yem_truethrow", model.getValue("yem_truethrow"));
	    jsonObj.put("yem_message", model.getValue("yem_message"));
	    jsonObj.put("yem_filtergridap", yem_filtergridap);
		view.returnDataToParent(jsonObj);
        view.close();
	}
}
