import { rulesHelper } from '@/common';
import { Apis } from '@/gen/Apis';
import { CompanySealsTypeEnum } from '@/gen/Enums';
import { ProColumns, ProFormColumnsType } from '@ant-design/pro-components';

type ReturnType = ProColumns<any, 'text'> & ProFormColumnsType<any, 'text'>;
type PropsType = { required?: boolean } & ReturnType;

export const SelectContract = {
  //选择合同的企业主体
  SupplierName(props?: PropsType): ReturnType {
    const {
      title = '选择企业主体',
      key = 'company_suppliers_id',
      required = false,
      hideInTable = true,
      ...rest
    } = props ?? {};

    return {
      title: title,
      key: key,
      valueType: 'select',
      hideInTable: hideInTable,
      formItemProps: { ...(required ? rulesHelper.text : {}) },
      request: async (params) => {
        let res = await Apis.CompanySupplier.CompanySuppliers.Select({
          ...params,
          keyWords: params?.keyWords,
        });
        res?.data?.forEach((l: any) => {
          l.label = l.name;
          // l.value = `${l.id}：${l.name}`;
          l.value = l.name;
        });
        return res?.data;
      },
      ...rest,
      fieldProps: {
        showSearch: true,
        fieldNames: {
          label: 'label',
          value: 'value',
        },
        ...rest?.fieldProps,
      },
    };
  },

  //组织树 - 确保只返回最后一个选中的值

  OrganizationsName(props?: PropsType): ReturnType {
    const {
      title = '组织',
      key = 'organizations_id',
      required = false,
      hideInTable = true,
      ...rest
    } = props ?? {};

    return {
      title: title,
      key: key,
      valueType: 'cascader',
      hideInTable: hideInTable,
      formItemProps: { ...(required ? rulesHelper.text : {}) },
      request: async (params) =>
        (
          await Apis.Company.Organizations.SelectTree({
            keywords: params?.keyWords,
            companies_id: params?.companies_id,
            ...params,
          })
        ).data,
      ...rest,
      fieldProps: {
        showSearch: true,
        changeOnSelect: true,
        fieldNames: {
          label: 'name',
          value: 'name',
          children: 'children',
        },
        ...rest?.fieldProps,
      },
    };
  },
  //获取合同类型
  ContractTypes(props?: PropsType): ReturnType {
    const {
      title = '合同类型',
      key = 'contract_types_id',
      required = false,
      hideInTable = true,
      ...rest
    } = props ?? {};

    return {
      title: title,
      key: key,
      valueType: 'select',
      hideInTable: hideInTable,
      formItemProps: { ...(required ? rulesHelper.number : {}) },
      request: async (params) => {
        let res = await Apis.Contract.ContractTypes.Select({
          ...params,
          name: params?.keyWords || undefined,
        });
        return res?.data;
      },
      ...rest,
      fieldProps: {
        showSearch: true,
        fieldNames: {
          label: 'label',
          value: 'value',
        },
        ...rest?.fieldProps,
      },
    };
  },
  //项目id
  AssetProjects(props?: PropsType): ReturnType {
    const {
      title = '选择项目',
      key = 'asset_projects_id',
      required = false,
      hideInTable = true,
      ...rest
    } = props ?? {};

    return {
      title: title,
      key: key,
      valueType: 'select',
      hideInTable: hideInTable,
      formItemProps: { ...(required ? rulesHelper.number : {}) },
      request: async (params) =>
        (
          await Apis.Asset.AssetProjects.Select({
            keywords: params?.keyWords,
            ...params,
          })
        ).data,
      ...rest,
      fieldProps: {
        showSearch: true,
        placeholder: '请选择（支持关键字搜索）',
        fieldNames: {
          label: 'label',
          value: 'value',
        },
        ...rest?.fieldProps,
      },
    };
  },
  //员工
  EmployeeName(props?: PropsType): ReturnType {
    const {
      title = '员工',
      key = 'contract_liaison',
      required = false,
      hideInTable = true,
      ...rest
    } = props ?? {};

    return {
      title: title,
      key: key,
      valueType: 'select',
      hideInTable: hideInTable,
      formItemProps: { ...(required ? rulesHelper.text : {}) },
      request: async (params) =>
        (
          await Apis.Company.CompanyEmployees.Select({
            keywords: params?.keyWords,
            ...params,
          })
        ).data,
      ...rest,
      fieldProps: {
        showSearch: true,
        fieldNames: {
          label: 'label',
          value: 'label',
        },
        ...rest?.fieldProps,
      },
    };
  },
  //审核人
  ExamineEmployees(props?: PropsType): ReturnType {
    const {
      title = '审核人',
      key = 'company_employees_id',
      required = false,
      hideInTable = true,
      ...rest
    } = props ?? {};

    return {
      title: title,
      key: key,
      valueType: 'select',
      hideInTable: hideInTable,
      formItemProps: { ...(required ? rulesHelper.number : {}) },
      request: async (params) => {
        let value = {
          keywords:
            params?.keyWords === undefined ? params?.name : params?.keyWords,
        };
        console.log(params, 'Employees');
        return (
          await Apis.Company.CompanyEmployees.Select({
            ...params,
            ...value,
          })
        ).data;
      },
      ...rest,
      fieldProps: {
        showSearch: true,
        fieldNames: {
          label: 'label',
          value: 'value',
        },
        ...rest?.fieldProps,
      },
    };
  },
  // 合同拟制，选择印章
  ContractSeals(props?: PropsType): ReturnType {
    const {
      title = '选择印章',
      key = 'id',
      required = false,
      hideInTable = true,
      ...rest
    } = props ?? {};

    return {
      title: title,
      key: key,
      valueType: 'select',
      hideInTable: hideInTable,
      formItemProps: { ...(required ? rulesHelper.array : {}) },
      request: async (params) => {
        let res = await Apis.CompanySeal.CompanySeals.Select({
          ...params,
          keyWords: params?.keyWords,
        });
        res?.data?.forEach((l: any) => {
          // 获取印章类型的友好显示名称
          let typeText = l.type;
          if (typeof l.type === 'string') {
            const typeKey = l.type as keyof typeof CompanySealsTypeEnum;
            if (CompanySealsTypeEnum[typeKey]?.text) {
              typeText = CompanySealsTypeEnum[typeKey].text;
            }
          }
          l.label = `${l.company_supplier.name} (${typeText})`;
          l.value = l.id;
        });
        return res?.data;
      },
      ...rest,
      fieldProps: {
        showSearch: true,
        fieldNames: {
          label: 'label',
          value: 'value',
        },
        ...rest?.fieldProps,
      },
    };
  },
};
