package nckd.jimin.jyyy.hr.hspm.formplugin.web.ermanfile;

import kd.bos.bill.BillShowParameter;
import kd.bos.bill.OperationStatus;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.resource.ResManager;
import kd.bos.dataentity.utils.ObjectUtils;
import kd.bos.entity.datamodel.ListSelectedRowCollection;
import kd.bos.form.FormShowParameter;
import kd.bos.form.IFormView;
import kd.bos.form.ShowType;
import kd.bos.form.control.events.ItemClickEvent;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.list.IListView;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.print.service.BosPrintServiceHelper;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.hr.hom.business.domain.service.onbrd.IOnbrdBillDomainService;
import kd.hr.hom.common.util.HOMObjectUtils;
import kd.sdk.plugin.Plugin;

import java.util.EventObject;

/**
 * 人员档案跳转人员采集信息
 * Tyx
 * 2025-08-27
 */
public class ErFileListReFormListPluginEx extends AbstractListPlugin implements Plugin {

    private static String onBrdBill_entity = "hom_onbrdpersoninfo";
    private static String ermanfile_entity = "hspm_ermanfile";

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
//        this.addItemClickListeners("nckd_redirectonbrd");
//        this.addItemClickListeners("advcontoolbarap");
       // this.addItemClickListeners(new String[] {"billlisttop_toolbarap"});
    }

    @Override
    public void click(EventObject evt) {
        super.click(evt);
    }

    @Override
    public void itemClick(ItemClickEvent evt) {
        super.itemClick(evt);
        String itemKey = evt.getItemKey();
        if ("nckd_redirectonbrd".equals(itemKey)) {
            IListView listview = (IListView) this.getView();
            ListSelectedRowCollection selectedRows = listview.getSelectedRows();
            if(selectedRows.size() != 1) {
                this.getView().showErrorNotification("请选中一行数据");
            }
            else if (selectedRows.size() == 1) {
                Object[] pks = selectedRows.getPrimaryKeyValues();
                //获取工号
                DynamicObject ermanFile = BusinessDataServiceHelper.loadSingle(pks[0], ermanfile_entity);
                String number = ermanFile.getString("number");
                //获取入职办理单
                QFilter filter = new QFilter("employeeno", QCP.equals, number);
                DynamicObject onBrdInfo = QueryServiceHelper.queryOne(onBrdBill_entity, "id,name,candidate", new QFilter[]{filter});
                if(!ObjectUtils.isEmpty(onBrdInfo)) {
                    String onbrdId = onBrdInfo.getString("id");
                    FormShowParameter showParameter = new FormShowParameter();
                    String name = onBrdInfo.getString("name");
                    showParameter.setCaption(String.format(ResManager.loadKDString("入职人员信息-%s", "OnbrdBillPersonInfoListPlugin_0", "hr-hom-formplugin", new Object[0]), name));
                    showParameter.setFormId("hom_persononbrdinfobody");
                    showParameter.getOpenStyle().setShowType(ShowType.MainNewTabPage);
                    showParameter.setStatus(OperationStatus.VIEW);
                    showParameter.setCustomParam("onbrdid", onbrdId);
                    IFormView currView = this.getView();
                    IFormView parentView = HOMObjectUtils.getCurrAppHomeView(currView);
                    String appRootPageId = parentView == null ? currView.getFormShowParameter().getRootPageId() : parentView.getPageId();
                    String pageId = HOMObjectUtils.getUniquePageId("hom_persononbrdinfobody", onbrdId, appRootPageId);
                    showParameter.setPageId(pageId);
                    showParameter.setCustomParam("candidateid", onBrdInfo.getString("candidate"));
                    currView.showForm(showParameter);
                }
                else {
                    this.getView().showErrorNotification("未找到对应的入职单");
                }
            }
        }
        if("nckd_viewresume".equals(itemKey)) {
            IListView listview = (IListView) this.getView();
            ListSelectedRowCollection selectedRows = listview.getSelectedRows();
            if(selectedRows.size() != 1) {
                this.getView().showErrorNotification("请选中一行数据");
            }
            else if (selectedRows.size() == 1) {
                Object[] pks = selectedRows.getPrimaryKeyValues();
                String tplId = BosPrintServiceHelper.getTplIdByNum("");

            }
        }
    }
}

