package nckd.jimin.jyyy.hr.tsrsc.plugin.form;

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.list.IListView;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.hr.hom.common.enums.ViewTypeEnum;
import kd.hr.hom.common.util.HOMObjectUtils;
import kd.sdk.plugin.Plugin;
import nckd.jimin.jyyy.hr.tsrsc.plugin.util.MokaApiUtil;

import javax.management.Query;
import java.util.EventObject;
import java.util.Locale;

/**
 * 候选人跳转入职单
 * Tyx
 * 2025-06-13
 */
public class AppFileListEx extends AbstractListPlugin implements Plugin {

    private static String onBrdBill_entity = "hom_onbrdinfo";
    private static String appFile_entity = "tsrsc_appfile_viewmabr";
    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);

    }

    @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() == 0) {
                this.getView().showErrorNotification("请选中一行数据");
            }
            else if(selectedRows.size() > 1) {
                this.getView().showErrorNotification("请选中一行数据");
            }
            else if (selectedRows.size() == 1) {
                Object[] pks = selectedRows.getPrimaryKeyValues();
                //根据候选人PK去找入职单
                DynamicObject appFile = BusinessDataServiceHelper.loadSingle(pks[0], appFile_entity);
                String appFileNo = appFile.getString("number");

                QFilter filter = new QFilter("resumeno", "=", appFileNo);
                DynamicObject onBrdInfo = QueryServiceHelper.queryOne(onBrdBill_entity, "id,name,candidate,viewtype", new QFilter[]{filter});
                if(!ObjectUtils.isEmpty(onBrdInfo)) {

                    FormShowParameter billShowParameter = new FormShowParameter();
                    String onbrdId = onBrdInfo.getString("id");
                    String name = onBrdInfo.getString("name");
                    String viewType = onBrdInfo.getString("viewtype");
                    if (ViewTypeEnum.AGAIN.getCode().equals(viewType)) {
                        billShowParameter.setCaption(String.format(Locale.ROOT, ResManager.loadKDString("再入职办理单-%s", "OnbrdBillListPlugin_2", "hr-hom-formplugin", new Object[0]), name));
                    } else {
                        billShowParameter.setCaption(String.format(Locale.ROOT, ResManager.loadKDString("入职办理单-%s", "OnbrdBillListPlugin_0", "hr-hom-formplugin", new Object[0]), name));
                    }
                    billShowParameter.setFormId("hom_persononbrdhandlebody");
                    billShowParameter.getOpenStyle().setShowType(ShowType.MainNewTabPage);
                    billShowParameter.setStatus(OperationStatus.VIEW);
                    IFormView currView = this.getView();
                    IFormView parentView = HOMObjectUtils.getCurrAppHomeView(currView);
                    String appRootPageId = parentView == null ? currView.getFormShowParameter().getRootPageId() : parentView.getPageId();
                    String pageId = HOMObjectUtils.getUniquePageId("hom_persononbrdhandlebody", onbrdId, appRootPageId);
                    billShowParameter.setPageId(pageId);
                    billShowParameter.setCustomParam("onbrdid", onbrdId);
                    billShowParameter.setCustomParam("candidateid", onBrdInfo.getString("candidate.id"));
                    billShowParameter.getOpenStyle().setShowType(ShowType.MainNewTabPage);
                    this.getView().showForm(billShowParameter);
                }
                else {
                    this.getView().showErrorNotification("未找到对应的入职单");
                }
            }
        }
    }
}