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

import java.util.ArrayList;
import java.util.Collections;
import java.util.EventObject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import kd.bos.bill.BillShowParameter;
import kd.bos.bill.OperationStatus;
import kd.bos.dataentity.entity.LocaleString;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.form.FormShowParameter;
import kd.bos.form.IFormView;
import kd.bos.form.ShowType;
import kd.bos.form.control.Control;
import kd.bos.form.field.ComboEdit;
import kd.bos.form.field.ComboItem;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.lang.Lang;

/**
 * 变更履历_单据详情-表单插件
 * @description:
 * @author longh
 * @date 2022年4月27日 上午10:45:51
 */
public class VersionResumeBillInfoEdit extends AbstractFormPlugin {


	public static final String HighLight = "VersionResume_HighLight";
	public static final String HighLightColor = "#e5e5e5";

	@Override
	public void afterCreateNewData(EventObject evt) {
		super.afterCreateNewData(evt);
		IFormView view = this.getView();
		FormShowParameter showParameter = view.getFormShowParameter();
		JSONObject data = showParameter.getCustomParam("data");
		if(data == null) {
			return;
		}
		String billsJson = data.getString("billsJson");
		String yem_changebill = data.getString("yem_changebill");
		Long pkid = data.getLong("pkid");
		
		List<Integer> versions = new ArrayList<>();
		JSONArray billsArray = JSONArray.parseArray(billsJson);
		for (int i = 0; i < billsArray.size(); i++) {
			JSONObject bill = billsArray.getJSONObject(i);
			if(pkid.longValue() != bill.getLong("id").longValue()) {
				versions.add(bill.getInteger("yem_version"));
			}
		}
		
		Collections.sort(versions);
		List<ComboItem> entryItems = new ArrayList<>();
		String currLCId = Lang.get().toString();
		for (int i = versions.size()-1; i >= 0; i--) {
			ComboItem comboItem = new ComboItem();
			comboItem.setValue("" + versions.get(i));
			comboItem.setCaption(new LocaleString(currLCId, "" + versions.get(i)));
			entryItems.add(comboItem);
		}
		ComboEdit entryEditor = (ComboEdit) this.getView().getControl("yem_vnum");
		entryEditor.setComboItems(entryItems);
		
		this.showBill(view, yem_changebill, pkid, new HashMap<>());
	}

	private void showBill(IFormView view, String yem_changebill, Object pkId, Map<String, Object> customParams) {
		BillShowParameter showBill = new BillShowParameter();
		showBill.setCustomParams(customParams);;
		showBill.setStatus(OperationStatus.VIEW);
		showBill.getOpenStyle().setShowType(ShowType.InContainer);
		showBill.getOpenStyle().setTargetKey("yem_billinfo");
		showBill.setFormId(yem_changebill);
		showBill.setPkId(pkId);
		view.showForm(showBill);
	}
	
	@Override
	public void registerListener(EventObject e) {
		super.registerListener(e);
		this.addClickListeners(new String[] { "yem_highlight" });
	}
	
	@Override
	public void click(EventObject evt) {
		super.click(evt);
		String key = ((Control) evt.getSource()).getKey().toLowerCase();
		switch (key) {
		case "yem_highlight":
			IFormView view = this.getView();
			IDataModel model = this.getModel();
			FormShowParameter showParameter = view.getFormShowParameter();
			String yem_vnum = (String) model.getValue("yem_vnum");
			if(yem_vnum == null || yem_vnum.isEmpty()) {
				view.showTipNotification("请选择要比较的版本。");
				return;
			}
			JSONObject data = showParameter.getCustomParam("data");
			if(data == null) {
				return;
			}
			String billsJson = data.getString("billsJson");
			String yem_changebill = data.getString("yem_changebill");
			Long pkid = data.getLong("pkid");
			
			Map<String, Object> customParams = new HashMap<>();
			JSONArray billsArray = JSONArray.parseArray(billsJson);
			for (int i = 0; i < billsArray.size(); i++) {
				JSONObject bill = billsArray.getJSONObject(i);
				String yem_version = bill.getString("yem_version");
				if(yem_version.equals(yem_vnum)) {
					customParams.put(HighLight, bill.get("id"));
				}
			}
			
			this.showBill(view, yem_changebill, pkid, customParams);
			
//			IFormView childView = this.getView().getView(childPageId);
//			Map<String, Object> param = new HashMap<>();
//	        param.put(ClientProperties.BackColor, "#FFFF77");
//	        
//			childView.updateControlMetadata("yem_version", param);
//			this.getView().sendFormAction(childView);
			break;
		default:
			break;
		}
	}
	

}
