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

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

export const Selects = {
  Companies(props?: PropsType): ReturnType {
    const {
      title = '机构',
      key = 'parent_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.Companies.Select({
            keywords: params?.KeyWords,
            ...params,
          })
        ).data,
      ...rest,
      fieldProps: {
        showSearch: true,
        fieldNames: {
          label: 'label',
          value: 'value',
        },
        ...rest?.fieldProps,
      },
    };
  },
  // 楼栋下拉框
  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?.map((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,
        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) =>
        (
          await Apis.Company.CompanyEmployees.Select({
            keywords: params?.KeyWords,
            ...params,
          })
        ).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) =>
        (
          await Apis.Company.CompanyPositions.Select({
            keywords: params?.KeyWords,
            ...params,
          })
        ).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) =>
        (
          await Apis.Asset.AssetProjects.Select({
            keywords: params?.KeyWords,
            ...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,
        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,
        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.HouseCharage.HouseChargeStandards.Select({
            keywords: params?.KeyWords,
            asset_projects_id: params?.asset_projects_id,
            ...params,
          })
        ).data,
      ...rest,
      fieldProps: {
        showSearch: true,
        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,
      },
    };
  },
  //获取仪表
  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,
      },
    };
  },
};
