package nckd.jxccl.hrmp.hbpm.plugin.form.hr;

import kd.bos.consts.OrgViewTypeConst;
import kd.bos.context.RequestContext;
import kd.bos.form.events.FilterContainerSearchClickArgs;
import kd.bos.form.events.SetFilterEvent;
import kd.bos.list.events.BeforeShowBillFormEvent;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.permission.api.HasPermOrgResult;
import kd.bos.servicehelper.model.PermissionStatus;
import kd.bos.servicehelper.permission.PermissionServiceHelper;
import nckd.jxccl.hrmp.hbpm.common.hr.PositionBillConstant;

import java.util.List;
import java.util.Objects;

/**
 *  * 岗位申请单列表插件
 * @entity: nckd_positionbill
 * @from: kd.hr.homs.formplugin.web.orgbatch.AdminOrgBatchBillListPlugin
 * @author: jtd
 * @date: 2025/12/31 20:48
 */
public class PositionBillListPlugin extends AbstractListPlugin {

    @Override
    public void setFilter(SetFilterEvent setFilterEvent) {
        super.setFilter(setFilterEvent);
        setFilterEvent.setOrderBy(String.format("%s desc, %s desc", PositionBillConstant.CREATE_TIME_KEY, PositionBillConstant.BILL_NO_KEY));
    }

    @Override
    public void beforeShowBill(BeforeShowBillFormEvent e) {
        super.beforeShowBill(e);
        String orgId = getView().getPageCache().get(PositionBillConstant.PC_BUSINESSUNIT);
        if (Objects.isNull(orgId)) {
            if (getView().getPageCache().get(PositionBillConstant.PC_ISFROMBILLCLICK) != null) {
                getView().getPageCache().remove(PositionBillConstant.PC_ISFROMBILLCLICK);
                e.getParameter().setCustomParam(PositionBillConstant.CP_ISFROMBILLCLICK, Boolean.TRUE.toString());
            } else {
                HasPermOrgResult hrPermOrg = PermissionServiceHelper.getAllPermOrgs(RequestContext.get().getCurrUserId(), OrgViewTypeConst.HR_OD, PositionBillConstant.HOMS_APP, PositionBillConstant.HAOS_ADMINORGDETAIL_ENTITY, PermissionStatus.View, false);
                List<Long> hasPermOrgs = hrPermOrg.getHasPermOrgs();
                if (!Objects.isNull(hasPermOrgs) && hasPermOrgs.size() != 0) {
                    if (hasPermOrgs.contains(RequestContext.get().getOrgId())) {
                        e.getParameter().setCustomParam(PositionBillConstant.CP_BUSINESSUNIT, String.valueOf(RequestContext.get().getOrgId()));
                    } else {
                        e.getParameter().setCustomParam(PositionBillConstant.CP_BUSINESSUNIT, String.valueOf(hasPermOrgs.get(0)));
                    }
                }
            }
        } else {
            e.getParameter().setCustomParam(PositionBillConstant.CP_BUSINESSUNIT, orgId);
        }

        e.getParameter().setCustomParam(PositionBillConstant.CP_FROMLIST, Boolean.TRUE.toString());
    }

    @Override
    public void filterContainerAfterSearchClick(FilterContainerSearchClickArgs args) {
        Object filterValue = args.getFilterValue(String.join(".", PositionBillConstant.ORG_KEY, PositionBillConstant.ID_KEY));
        if (!Objects.isNull(filterValue) && filterValue instanceof Long) {
            getView().getPageCache().put(PositionBillConstant.PC_BUSINESSUNIT, filterValue.toString());
        } else {
            getView().getPageCache().remove(PositionBillConstant.PC_BUSINESSUNIT);
        }

        super.filterContainerAfterSearchClick(args);
    }

}
