package nckd.jxccl.hr.psms.plugin.form.other;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import kd.bos.consts.PermItemConst;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.entity.LocaleString;
import kd.bos.entity.datamodel.ListSelectedRow;
import kd.bos.entity.datamodel.ListSelectedRowCollection;
import kd.bos.entity.datamodel.events.PackageDataEvent;
import kd.bos.form.CloseCallBack;
import kd.bos.form.FormShowParameter;
import kd.bos.form.IPageCache;
import kd.bos.form.ListVisible;
import kd.bos.form.ShowType;
import kd.bos.form.control.grid.DataGrid;
import kd.bos.form.control.grid.events.BeforeCreateDataGridColumnsEvent;
import kd.bos.form.control.grid.events.BeforeCreateDataGridColumnsListener;
import kd.bos.form.control.grid.events.DataGridBindDataEvent;
import kd.bos.form.control.grid.events.DataGridBindDataListener;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.events.BeforeCreateListColumnsArgs;
import kd.bos.form.events.BeforeCreateListDataProviderArgs;
import kd.bos.form.events.ClosedCallBackEvent;
import kd.bos.list.IListColumn;
import kd.bos.list.ListColumn;
import kd.bos.list.ListShowParameter;
import kd.bos.list.events.ListRowClickEvent;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.mvc.list.ListDataProvider;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.hr.hbp.common.model.AuthorizedOrgResultWithSub;
import kd.hr.hbp.common.model.OrgSubInfo;
import kd.sdk.hr.hbp.business.helper.permission.HRPermissionServiceHelper;
import kd.sdk.plugin.Plugin;
import nckd.jxccl.base.common.constant.FormConstant;
import nckd.jxccl.base.common.utils.QueryFieldBuilder;
import nckd.jxccl.hr.psms.common.PositionStructureConstant;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.EventObject;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
* 关键行为评价表
* 实体标识：nckd_keybehaveval
* @author W.Y.C
* @date 2025/12/25 18:59
* @version 1.0
*/
public class KeyBeHavEvalListPlugin extends AbstractListPlugin implements Plugin {

    @Override
    public void listRowClick(ListRowClickEvent evt) {
        // 获取当前点击的行
        ListSelectedRow currentRow = evt.getCurrentListSelectedRow();
        // 获取所有选中的行（在已勾选多行的情况下）
        ListSelectedRowCollection selectedRows = evt.getListSelectedRowCollection();
        // 后续处理...
    }

    @Override
    public void afterCreateNewData(EventObject e) {
        ListShowParameter listShowParameter = (ListShowParameter)this.getView().getFormShowParameter();
        listShowParameter.setMultiSelect(false);
    }

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs e) {
        String operateKey = e.getOperateKey();
        if(e.getOperationResult() != null && e.getOperationResult().isSuccess()){
            if("newappointment".equalsIgnoreCase(operateKey)) {
                ListSelectedRowCollection selectedRows = this.getSelectedRows();
                FormShowParameter showParameter = new FormShowParameter();
                showParameter.setFormId("nckd_newseniorappoint");
                showParameter.getOpenStyle().setShowType(ShowType.Modal);
                showParameter.setCaption("新建聘任");
                showParameter.setCustomParam("selectRow", selectedRows.get(0).getPrimaryKeyValue());
                showParameter.setCloseCallBack(new CloseCallBack(this, "nckd_newseniorappoint"));
                this.getView().showForm(showParameter);
            }
            this.getView().invokeOperation(FormConstant.REFRESH_OP);
        }
    }

    @Override
    public void closedCallBack(ClosedCallBackEvent closedCallBackEvent) {
        String actionId = closedCallBackEvent.getActionId();
        if("nckd_newseniorappoint".equalsIgnoreCase(actionId)){
            Object returnData = closedCallBackEvent.getReturnData();
            if(returnData != null) {
                this.getView().showSuccessNotification("聘任成功！");
            }
        }
    }


    private static List<Long> extractOrgIds(List<OrgSubInfo> orgSubInfos) {
        return orgSubInfos.stream()
                .map(OrgSubInfo::getOrgId)
                .collect(Collectors.toList());
    }

}