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,
      },
    };
  },

  //网格标识
  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,
      },
    };
  },
  // 楼栋树形
  GetBuildingTree(props?: PropsType): ReturnType {
    const {
      title = '网格标识',
      key = 'grid_mark',
      required = false,
      hideInTable = true,
      ...rest
    } = props ?? {};

    return {
      title: title,
      key: key,
      valueType: 'treeSelect',
      hideInTable: hideInTable,
      formItemProps: { ...(required ? rulesHelper.number : {}) },
      request: async (params) =>
        (
          await Apis.Asset.AssetBuildings.GetBuildingTree({
            keywords: params?.KeyWords,
            asset_projects_id: params?.asset_projects_id,
            ...params,
          })
        ).data,
      ...rest,
      fieldProps: {
        showSearch: true,
        fieldNames: {
          label: 'name',
          value: 'id',
          children: 'children',
        },
        ...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,
            ...params,
          })
        ).data,
      ...rest,
      fieldProps: {
        showSearch: true,
        fieldNames: {
          label: 'label',
          value: 'value',
        },
        ...rest?.fieldProps,
      },
    };
  },
  //员工
  Employees(props?: PropsType): ReturnType {
    const {
      title = '员工',
      key = 'managers_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,
      },
    };
  },
  //房屋
  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,
      },
    };
  },
};
