import { ref } from 'vue';
import { QueryAvailablePersonPageParamsAll, PersonGroupItem } from '@/types/person-group/type';
import { queryAvailableUserListAll } from '@/api/system/person-group';

export const usePersonGroupFilter = () => {
  const FILTER_TYPES = [
    { type: 'realname', label: '姓名' },
    { type: 'staffNo', label: '工号' },
    { type: 'deptName', label: '部门' },
    { type: 'jobName', label: '岗位' },
  ];

  // 复用输入框类型及标签
  const personFilterType = ref({ type: 'realname', label: '姓名' });
  // 输入框绑定数据
  const personFilterValue = ref<string>();
  // 查询结果（改为直接使用数组，不再使用分页结构）
  const personFilterList = ref<PersonGroupItem[]>([]);

  // 查询事件（一次性加载全部数据）
  const getPersonFilterList = async () => {
    const res = await queryAvailableUserListAll({
      [personFilterType.value.type as keyof QueryAvailablePersonPageParamsAll]: personFilterValue.value,
    });
    // 如果有返回且搜索到则添加选中标记
    if (res && res.length > 0) {
      personFilterList.value = checkPersonList(res);
    } else {
      personFilterList.value = [];
    }
  };

  // 已选择人员
  const selectedPersonList = ref<PersonGroupItem[]>([]);
  // 查询结果添加选中标记
  const checkPersonList = (list: PersonGroupItem[]) => {
    return list.map((item) => {
      return {
        ...item,
        checked: selectedPersonList.value.some((person) => person.id === item.id),
      };
    });
  };

  // 查询结果刷新选中标记
  const refreshPersonFilterCheckedStatus = (personId: number) => {
    const person = personFilterList.value.find((item) => item.id === personId);
    if (person) {
      person.checked = selectedPersonList.value.some((person) => person.id === personId);
    }
  };

  // 添加已选择人员
  const handleAddSelectedPerson = (person: PersonGroupItem) => {
    // 检查是否已存在，避免重复添加
    if (!selectedPersonList.value.some((item) => item.id === person.id)) {
      selectedPersonList.value.push(person);
    }
    // 更新选中状态
    refreshPersonFilterCheckedStatus(person.id);
  };

  // 移除已选择人员
  const handleRemoveSelectedPerson = (personId: number) => {
    const index = selectedPersonList.value.findIndex((item) => item.id === personId);
    if (index > -1) {
      selectedPersonList.value.splice(index, 1);
      refreshPersonFilterCheckedStatus(personId);
    }
  };

  // 批量添加已选择人员
  const handleBatchAddSelectedPerson = (persons: PersonGroupItem[]) => {
    persons.forEach((person) => {
      if (!selectedPersonList.value.some((item) => item.id === person.id)) {
        selectedPersonList.value.push(person);
      }
    });
    // 批量更新选中状态
    personFilterList.value.forEach((item) => {
      item.checked = selectedPersonList.value.some((person) => person.id === item.id);
    });
  };

  // 批量移除已选择人员
  const handleBatchRemoveSelectedPerson = (personIds: number[]) => {
    personIds.forEach((personId) => {
      const index = selectedPersonList.value.findIndex((item) => item.id === personId);
      if (index > -1) {
        selectedPersonList.value.splice(index, 1);
      }
      refreshPersonFilterCheckedStatus(personId);
    });
  };

  return {
    FILTER_TYPES,
    personFilterType,
    personFilterValue,
    personFilterList,
    selectedPersonList,
    getPersonFilterList,
    handleAddSelectedPerson,
    handleRemoveSelectedPerson,
    handleBatchAddSelectedPerson,
    handleBatchRemoveSelectedPerson,
  };
};

export default usePersonGroupFilter;
