package yem.wm.bd.companymaninfor.dynamic;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.form.IFormView;
import kd.bos.form.control.Control;
import kd.bos.form.control.EntryGrid;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import yem.wm.bd.util.BothCardHelper;

import java.util.ArrayList;
import java.util.EventObject;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description 公司人员信息--表单插件
 *            1、加载界面时，默认获取《公司抬头》中的{联系人信息}自动给单据体赋值
 *            2、选中行数据之后，点击确定给父页面返回数据
 * @Author: zhangh
 * @CreateTime: 2025-02-08  14:39
 */
public class CompanyManInforDynamicPlugin extends AbstractFormPlugin {
    @Override
    public void afterCreateNewData(EventObject e) {
        IFormView view = this.getView();//动态表单的view视图
        IDataModel model = view.getModel();//动态表单的model模型

        IFormView parentView = view.getParentView();//获取父页面的view视图
        IDataModel parentModel = parentView.getModel();//获取父页面的model模型

        DynamicObject company = (DynamicObject) parentModel.getValue("yem_org");//获取父页面的【公司字段】
        if (company != null) {
            String orgtype = (String) parentModel.getValue("yem_orgtype");//获取父页面的【组织类型】
            DynamicObject org = null;
            if ("bos_org".equals(orgtype)) {
                org = BothCardHelper.queryCompany(company);//系统参数是《业务单元》，获取《公司抬头》
            } else {
                org = BusinessDataServiceHelper.loadSingleFromCache(company.getLong("id"), company.getDataEntityType().getName());
            }

            if (org != null) {
                DynamicObjectCollection linkmanentry = org.getDynamicObjectCollection("yem_linkmanentry");//《公司抬头》信息中{联系人信息}
                for (DynamicObject linkman : linkmanentry) {
                    int row = model.createNewEntryRow("yem_entryentity");//动态表单单据体添加行
                    model.setValue("yem_companylinkman", linkman.get("yem_contactperson"), row);//人员
                    model.setValue("yem_companytel", linkman.get("yem_phonenum"), row);//电话
                    model.setValue("yem_companyemail", linkman.get("yem_addemail"), row);//E-Mail
                    model.setValue("yem_companyvx", linkman.get("yem_suppliervx"), row);//微信
                    model.setValue("yem_companyqq", linkman.get("yem_supplierqq"), row);//QQ
                    model.setValue("yem_companyfax", org.get("yem_fax"), row);//传真
                }
            }
        }
    }

    @Override
    public void click(EventObject evt) {
        super.click(evt);
        String key = ((Control) evt.getSource()).getKey();
        switch (key) {
            case "btnok"://确定按钮
                this.btnOk();//点击确定按钮返回数据
                break;
        }
    }

    /**
     * @Description: 点击确定给父页面返回信息
     * @function 功能
     * @purposes 用途
     * @return void
     */
    public void btnOk() {
        EntryGrid entryGrid = this.getView().getControl("yem_entryentity");//获取单据体控件
        int[] selectRows = entryGrid.getSelectRows();//获取选中的行数
        List<DynamicObject> list = new ArrayList<>();
        for (int rowIndex : selectRows) {
            DynamicObject entryRowEntity = this.getModel().getEntryRowEntity("yem_entryentity", rowIndex);
            list.add(entryRowEntity);
        }

        List<DynamicObject> defaultInfor = list.stream().filter(x -> x.getBoolean("yem_default") == true).collect(Collectors.toList());//判断选中的行数中是否勾选了【默认】
        if (defaultInfor.size() == 0 || defaultInfor.size() > 1){
            this.getView().showErrorNotification("公司联系人信息中需要有勾选一个默认的联系人。");
            return;
        }
        this.getView().returnDataToParent(list);
        this.getView().close();
    }

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        this.addClickListeners("btnok");
    }
}
