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

import java.util.EventObject;

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

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.metadata.IDataEntityProperty;
import kd.bos.dataentity.metadata.dynamicobject.DynamicObjectType;
import kd.bos.entity.EntityMetadataCache;
import kd.bos.entity.MainEntityType;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.property.DecimalProp;
import kd.bos.entity.property.EntryProp;
import kd.bos.form.FormShowParameter;
import kd.bos.form.IFormView;
import kd.bos.form.control.Control;
import kd.bos.form.plugin.AbstractFormPlugin;

/**
 * 校验字段-表单插件
 * @description:回填变更方案删行校验字段
 * @author longh
 * @date 2021年11月25日 上午11:18:01
 */

public class VersionCheckColsEdit extends AbstractFormPlugin {


	@Override
	public void afterCreateNewData(EventObject evt) {
		super.afterCreateNewData(evt);
		IDataModel model = this.getModel();
		IFormView view = this.getView();
		FormShowParameter showParameter = view.getFormShowParameter();
		String entityNumber = showParameter.getCustomParam("entityNumber");
		JSONObject data = showParameter.getCustomParam("data");
		
		model.deleteEntryData("yem_entryentity");
		if(entityNumber != null && data != null) {
			String yem_checkdelentity = data.getString("yem_checkdelentity");//校验实体
			String yem_checkcolsjson = data.getString("yem_checkcolsjson");//序列化校验字段
			JSONArray cols = new JSONArray();
			if(!yem_checkcolsjson.isEmpty()) {
				cols = JSON.parseArray(yem_checkcolsjson);
			}
			
			MainEntityType mainType = EntityMetadataCache.getDataEntityType(entityNumber);
			IDataEntityProperty iDataEntityProperty = mainType.getProperties().get(yem_checkdelentity);
			DynamicObjectType subType = ((EntryProp) iDataEntityProperty).getDynamicCollectionItemPropertyType();
			
			for (IDataEntityProperty property : subType.getProperties()) {
				String name = property.getName();
				if(name.isEmpty() || name.equals("seq")) {
					continue;
				}
				if(property instanceof DecimalProp) {
					String displayName = property.getDisplayName() == null ? "" : property.getDisplayName().getLocaleValue();
					int index = model.createNewEntryRow("yem_entryentity");
					model.setValue("yem_col", displayName, index);
					model.setValue("yem_colno", name, index);
					if(cols.contains(name)) {
						model.setValue("yem_checkbox", true, index);
					}
				}
			}
		}
	}
	
	@Override
	public void registerListener(EventObject e) {
		super.registerListener(e);
		this.addClickListeners(new String[] { "btnok" });
	}
	
	@Override
	public void click(EventObject evt) {
		super.click(evt);
		String key = ((Control) evt.getSource()).getKey().toLowerCase();
		switch (key) {
		case "btnok":
			this.clickBtnOK();
			break;
		default:
			break;
		}
	}
	
	/**
	 * 点击确定回传数据
	 * @param evt
	 * @param key
	 */
	private void clickBtnOK() {
		IDataModel model = this.getModel();
		IFormView view = this.getView();
		JSONArray cols = new JSONArray();
		JSONArray colnos = new JSONArray();
		
		DynamicObjectCollection entry = model.getEntryEntity("yem_entryentity");
		for (DynamicObject row : entry) {
			if(row.getBoolean("yem_checkbox")) {
				cols.add(row.getString("yem_col"));
				colnos.add(row.getString("yem_colno"));
			}
		}
		
	    JSONObject jsonObj = new JSONObject();
	    jsonObj.put("yem_col", cols);
	    jsonObj.put("yem_colno", colnos);
		view.returnDataToParent(jsonObj);
        view.close();
	}
}
