import { rulesHelper } from '@/common';
import { Apis } from '@/gen/Apis';
import { CompanySealsTypeEnum, HouseBillsTypeEnum } 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 Selects = {
  // 楼栋下拉框
  AssetBuildings(props?: PropsType): ReturnType {
    const {
      title = '楼栋',
      key = 'asset_buildings_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.AssetBuildings.Select({
            keywords: params?.keyWords,
            asset_projects_id: params?.asset_projects_id,
            ...params,
          })
        ).data,
      ...rest,
      fieldProps: {
        showSearch: true,
        fieldNames: {
          label: 'label',
          value: 'value',
        },
        ...rest?.fieldProps,
      },
    };
  },
  // 单元下拉框
  AssetUnits(props?: PropsType): ReturnType {
    const {
      title = '单元',
      key = 'asset_buildings_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.AssetUnits.Select({
            keywords: params?.keyWords,
            asset_projects_id: params?.asset_projects_id,
            asset_buildings_id: params?.asset_buildings_id,
            ...params,
          })
        ).data,
      ...rest,
      fieldProps: {
        showSearch: true,
        fieldNames: {
          label: 'label',
          value: 'value',
        },
        ...rest?.fieldProps,
      },
    };
  },
  // 网格单元下拉框
  GridUnits(props?: PropsType): ReturnType {
    const {
      title = '单元',
      key = 'asset_buildings_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.Asset.AssetUnits.GridSelect({
          keywords: params?.keyWords,
          asset_projects_id: params?.asset_projects_id,
          ...params,
        });
        res?.data?.forEach((l: any) => {
          l.label = l.asset_building.name + l.label;
        });
        return res?.data;
      },
      ...rest,
      fieldProps: {
        showSearch: true,
        // mode: 'multiple',
        fieldNames: {
          label: 'label',
          value: 'value',
        },
        ...rest?.fieldProps,
      },
    };
  },
  //网格标识
  GetGridMark(props?: PropsType): ReturnType {
    const {
      title = '网格标识',
      key = 'grid_mark',
      required = false,
      hideInTable = true,
      ...rest
    } = props ?? {};

    return {
      title: title,
      key: key,
      valueType: 'select',
      hideInTable: hideInTable,
      formItemProps: { ...(required ? rulesHelper.number : {}) },
      request: async (params) =>
        (
          await Apis.Grid.Grids.GetGridMark({
            keywords: params?.keyWords,
            asset_projects_id: params?.asset_projects_id,
            ...params,
          })
        ).data,
      ...rest,
      fieldProps: {
        showSearch: true,
        fieldNames: {
          label: 'label',
          value: 'value',
        },
        ...rest?.fieldProps,
      },
    };
  },
  //组织
  Organizations(props?: PropsType): ReturnType {
    const {
      title = '组织',
      key = 'organizations_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.Company.Organizations.Select({
            keywords: params?.keyWords,
            companies_id: params?.companies_id,
            ...params,
          })
        ).data,
      ...rest,
      fieldProps: {
        showSearch: true,
        fieldNames: {
          label: 'label',
          value: 'value',
        },
        ...rest?.fieldProps,
      },
    };
  },
  //组织树
  OrganizationsTree(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.number : {}) },
      request: async (params) =>
        (
          await Apis.Company.Organizations.SelectTree({
            keywords: params?.keyWords,
            companies_id: params?.companies_id,
            ...params,
          })
        ).data,
      ...rest,
      fieldProps: {
        showSearch: true,
        allowClear: true,
        treeDefaultExpandAll: true,
        changeOnSelect: true, // 允许选择中间层级的组织
        fieldNames: {
          label: 'name',
          value: 'id',
          children: 'children',
        },
        ...rest?.fieldProps,
      },
    };
  },
  //员工
  Employees(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,
        };
        return (
          await Apis.Company.CompanyEmployees.Select({
            ...params,
            ...value,
          })
        ).data;
      },
      ...rest,
      fieldProps: {
        showSearch: true,
        fieldNames: {
          label: 'label',
          value: 'value',
        },
        ...rest?.fieldProps,
      },
    };
  },
  //岗位
  Positions(props?: PropsType): ReturnType {
    const {
      title = '岗位',
      key = 'positions_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,
        };
        let res = await Apis.Company.CompanyPositions.Select({
          ...params,
          ...value,
        });
        return res.data;
      },
      ...rest,
      fieldProps: {
        showSearch: true,
        fieldNames: {
          label: 'label',
          value: 'value',
        },
        ...rest?.fieldProps,
      },
    };
  },
  //项目
  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) => {
        let res = await Apis.Asset.AssetProjects.Select({
          keywords: params?.keyWords,
          ...params,
        });
        return res?.data;
      },
      ...rest,
      fieldProps: {
        showSearch: true,
        placeholder: '请选择（支持关键字搜索）',
        fieldNames: {
          label: 'label',
          value: 'value',
        },
        ...rest?.fieldProps,
      },
    };
  },
  //收款账号
  ProjectReceiptAccountsSelect(props?: PropsType): ReturnType {
    const {
      title = '收款账户',
      key = 'company_project_receipt_accounts_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.Company.CompanyProjectReceiptAccounts.ProjectReceiptAccountsSelect(
            {
              keywords: params?.keyWords,
              projects_id: params?.asset_projects_id,
              ...params,
            },
          )
        ).data,
      ...rest,
      fieldProps: {
        showSearch: true,
        fieldNames: {
          label: 'label',
          value: 'value',
        },
        ...rest?.fieldProps,
      },
    };
  },
  //机构收款账户
  CompanyAccounts(props?: PropsType): ReturnType {
    const {
      title = '收款账户',
      key = 'receipt_accounts_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.Company.CompanyReceiptAccounts.Select({
            keywords: params?.keyWords,
            companies_id: params?.companies_id,
            ...params,
          })
        ).data,
      ...rest,
      fieldProps: {
        showSearch: true,
        placeholder: '请选择（支持关键字搜索）',
        fieldNames: {
          label: 'label',
          value: 'value',
        },
        ...rest?.fieldProps,
      },
    };
  },

  //项目收款账户
  ProjectAccounts(props?: PropsType): ReturnType {
    const {
      title = '收款账户',
      key = 'company_receipt_accounts_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.Company.CompanyProjectReceiptAccounts.Select({
            keywords: params?.keyWords,
            projects_id: params?.asset_projects_id,
            ...params,
          })
        ).data,
      ...rest,
      fieldProps: {
        showSearch: true,
        fieldNames: {
          label: 'label',
          value: 'value',
        },
        ...rest?.fieldProps,
      },
    };
  },
  //房屋
  AssetHouses(props?: PropsType): ReturnType {
    const {
      title = '选择房屋',
      key = 'asset_houses_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.AssetHouses.Select({
            keywords: params?.keyWords,
            ...params,
          })
        ).data,
      ...rest,
      fieldProps: {
        showSearch: true,
        placeholder: '请选择（支持关键字搜索）',
        fieldNames: {
          label: 'label',
          value: 'value',
        },
        ...rest?.fieldProps,
      },
    };
  },
  //房屋全称
  AssetFullName(props?: PropsType): ReturnType {
    const {
      title = '选择房屋',
      key = 'asset_houses_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.AssetHouses.SelectFullName({
            keywords: params?.keyWords,
            ...params,
          })
        ).data,
      ...rest,
      fieldProps: {
        showSearch: true,
        placeholder: '请选择（支持关键字搜索）',
        fieldNames: {
          label: 'label',
          value: 'value',
        },
        ...rest?.fieldProps,
      },
    };
  },
  //获取广告位
  GetBannerSpace(props?: PropsType): ReturnType {
    const {
      title = '广告位',
      key = 'banner_spaces_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.Banner.BannerSpaces.Select({
            keywords: params?.keyWords,
            ...params,
          })
        ).data,
      ...rest,
      fieldProps: {
        showSearch: true,
        fieldNames: {
          label: 'label',
          value: 'value',
        },
        ...rest?.fieldProps,
      },
    };
  },

  //收费标准
  ChargeStandard(props?: PropsType): ReturnType {
    const {
      title = '选择收费标准',
      key = 'house_charge_standards_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.HouseCharge.HouseChargeStandards.Select({
            keywords: params?.keyWords,
            asset_projects_id: params?.asset_projects_id,
            charge_type: params?.charge_type,
            ...params,
          })
        ).data,
      ...rest,
      fieldProps: {
        showSearch: true,
        placeholder: '请选择（支持关键字搜索）',
        fieldNames: {
          label: 'label',
          value: 'value',
        },
        ...rest?.fieldProps,
      },
    };
  },

  // 物业品牌
  PropertyBrands(props?: PropsType): ReturnType {
    const {
      title = '品牌',
      key = 'company_property_brands_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.Company.CompanyPropertyBrands.Select({
            keywords: params?.keyWords,
            ...params,
          })
        ).data,
      ...rest,
      fieldProps: {
        showSearch: true,
        fieldNames: {
          label: 'label',
          value: 'value',
        },
        ...rest?.fieldProps,
      },
    };
  },

  HouseBill(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.number : {}) },
      request: async (params) => {
        let res = await Apis.Bill.HouseBills.List({
          ...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 HouseBillsTypeEnum;
            if (HouseBillsTypeEnum[typeKey]?.text) {
              typeText = HouseBillsTypeEnum[typeKey].text;
            }
          }
          l.label = `${l.year}-${String(l.month).padStart(
            2,
            '0',
          )}月[${typeText}]`;
          l.value = l.id;
        });
        return res?.data;
      },
      ...rest,
      fieldProps: {
        showSearch: true,
        fieldNames: {
          label: 'label',
          value: 'value',
        },
        ...rest?.fieldProps,
      },
    };
  },

  //组织树搜索
  OrganizationSearch(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.number : {}) },
      request: async (params) =>
        (
          await Apis.Company.Organizations.SelectTree({
            keywords: params?.keyWords,
            ...params,
          })
        ).data,
      ...rest,
      fieldProps: {
        showSearch: true,
        allowClear: true,
        changeOnSelect: true,
        treeDefaultExpandAll: true,
        fieldNames: {
          label: 'name',
          value: 'name',
          children: 'children',
        },
        ...rest?.fieldProps,
      },
    };
  },

  //获取仪表
  AssetMeters(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.number : {}) },

      request: async (params) => {
        console.log(params, '1111');
        let res = await Apis.Meter.HouseMeters.List({
          ...params,
          asset_projects_id: params?.asset_projects_id,
          charge_standards_id: params?.house_charge_standards_id,
          name: params?.keyWords || undefined,
        });
        res?.data?.map((l: any) => {
          l.label = l.id + ':' + l.name;
          l.value = l.id;
        });
        return res?.data;
      },
      ...rest,
      fieldProps: {
        showSearch: true,
        fieldNames: {
          label: 'label',
          value: 'value',
        },
        ...rest?.fieldProps,
      },
    };
  },

  //选择审批模板
  ApprovalTemplates(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.number : {}) },

      request: async (params) => {
        console.log(params, '1111');
        let res = await Apis.Approval.ApprovalTemplates.List({
          ...params,
        });
        res?.data?.map((l: any) => {
          // l.label = l.type + ':' + l.name;
          l.label = l.name;

          l.value = l.id;
        });
        return res?.data;
      },
      ...rest,
      fieldProps: {
        showSearch: true,
        fieldNames: {
          label: 'label',
          value: 'value',
        },
        ...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,
      },
    };
  },
  // 选择内部企业
  Supplier(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.number : {}) },
      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;
        });
        return res?.data;
      },
      ...rest,
      fieldProps: {
        showSearch: true,
        fieldNames: {
          label: 'label',
          value: 'value',
        },
        ...rest?.fieldProps,
      },
    };
  },
  // 选择印章
  CompanySeals(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.number : {}) },
      request: async (params) => {
        let res = await Apis.CompanySeal.CompanySeals.List({
          ...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,
      },
    };
  },
  // 合同拟制，选择印章
  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.List({
          ...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,
      },
    };
  },
};
