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

import java.util.EventObject;

import com.alibaba.fastjson.JSONObject;

import kd.bos.bill.OperationStatus;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.serialization.SerializationUtils;
import kd.bos.entity.EntityMetadataCache;
import kd.bos.entity.MainEntityType;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.form.FormShowParameter;
import kd.bos.form.IFormView;
import kd.bos.form.IPageCache;
import kd.bos.form.ShowType;
import kd.bos.form.control.Control;
import kd.bos.form.control.EntryGrid;
import kd.bos.form.control.events.RowClickEvent;
import kd.bos.form.control.events.RowClickEventListener;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

/**
 * 变更履历-表单插件
 * yem_wm_versionresume
 * @description:
 * @author longh
 * @date 2022年1月18日 下午4:32:33
 */

public class VersionResumeEdit extends AbstractFormPlugin implements RowClickEventListener {

	private static final String KEY_INFORM = "yem_tpl_vresumebill";//【变更履历_单据详情】单据标识

	@Override
	public void afterCreateNewData(EventObject evt) {
		super.afterCreateNewData(evt);
		this.initData();
	}

	private void initData() {
		IDataModel model = this.getModel();
		IFormView view = this.getView();
		IPageCache pageCache = this.getPageCache();
		JSONObject customParams = new JSONObject();
		FormShowParameter showParameter = view.getFormShowParameter();
		JSONObject data = showParameter.getCustomParam("data");
		if(data == null) {
			return;
		}
//		Long tplPkId = data.getLong("tplPkId");
		String yem_requirebill = data.getString("yem_requirebill");
		String yem_changebill = data.getString("yem_changebill");
		Long pkId = data.getLong("pkId");
		
		DynamicObject srcBill = BusinessDataServiceHelper.loadSingle(pkId, yem_requirebill);
		model.setValue("yem_srcbillno", srcBill.getString("billno"));
		
		model.deleteEntryData("yem_entryentity");
		DynamicObject[] bills = BusinessDataServiceHelper.load(yem_changebill, "id,billno,yem_version,yem_changeman,yem_changedate,yem_effectdate", 
				new QFilter[] {new QFilter("yem_masterid", QCP.equals, pkId)}, "yem_version desc");
		for (DynamicObject bill : bills) {
			int index = model.createNewEntryRow("yem_entryentity");
			int yem_version = bill.getInt("yem_version");
			Object pkValue = bill.getPkValue();
			model.setValue("yem_version", yem_version, index);
			model.setValue("yem_billno", bill.get("billno"), index);
			model.setValue("yem_changeman", bill.get("yem_changeman"), index);
			model.setValue("yem_changedate", bill.get("yem_changedate"), index);
			model.setValue("yem_effectdate", bill.get("yem_effectdate"), index);
			model.setValue("yem_pkid", pkValue, index);
			
			if(index == 0) {
				customParams.put("pkid", pkValue);
			}
		}
		model.setValue("yem_changenumber", bills.length - 1);
		
		Object[] pkArray = new Object[bills.length];
		for (int i = 0; i < bills.length; i++) {
			pkArray[i] = bills[i].get("id");
		}
		
		MainEntityType type = EntityMetadataCache.getDataEntityType(yem_changebill);
		DynamicObject[] billDetails = BusinessDataServiceHelper.load(pkArray, type);
		String billsJson = SerializationUtils.toJsonString(billDetails);
		pageCache.put("billsJson", billsJson);
		
		customParams.put("billsJson", billsJson);
		customParams.put("yem_changebill", yem_changebill);
		
		this.showForm(view, customParams);
	}

	private void showForm(IFormView view, JSONObject customParams) {
		FormShowParameter showForm = new FormShowParameter();
		showForm.setCustomParam("data", customParams);
		showForm.setStatus(OperationStatus.EDIT);
		showForm.getOpenStyle().setShowType(ShowType.InContainer);
		showForm.getOpenStyle().setTargetKey("yem_billinfotab");
		showForm.setFormId(KEY_INFORM);
		view.showForm(showForm);
	}
	
	@Override
	public void registerListener(EventObject e) {
		super.registerListener(e);
		IFormView view = this.getView();
//		this.addClickListeners(new String[] { "yem_buttonap" });
		((EntryGrid) view.getControl("yem_entryentity")).addRowClickListener(this);
	}
	
	@Override
	public void entryRowClick(RowClickEvent evt) {
		RowClickEventListener.super.entryRowClick(evt);
		IDataModel model = this.getModel();
		IFormView view = this.getView();
		IPageCache pageCache = this.getPageCache();
		FormShowParameter showParameter = view.getFormShowParameter();
		JSONObject data = showParameter.getCustomParam("data");
		if(data == null) {
			return;
		}
		
		JSONObject customParams = new JSONObject();
		customParams.put("billsJson", pageCache.get("billsJson"));
		customParams.put("yem_changebill", data.getString("yem_changebill"));
		customParams.put("pkid", model.getValue("yem_pkid", evt.getRow()));
		
		this.showForm(view, customParams);
	}
	
	@Override
	public void afterDoOperation(AfterDoOperationEventArgs evt) {
		super.afterDoOperation(evt);
		String key = evt.getOperateKey();
		switch (key) {
		case "refresh":
			this.initData();
			break;
		default:
			break;
		}
	}
	
	@Override
	public void click(EventObject evt) {
		super.click(evt);
		String key = ((Control) evt.getSource()).getKey().toLowerCase();
		switch (key) {
//		case "yem_buttonap":
//			EntryGrid entryGrid = this.getView().getControl("yem_entryentity");
//			entryGrid.selectRows(0);
//			break;
		default:
			break;
		}
	}
}
