import type { FormSchemaGetter, VbenFormSchema } from '#/adapter/form';
import type { VxeGridProps } from '#/adapter/vxe-table';
import { communityList } from '#/api/property/community';
import type {
  CommunityQuery,
  CommunityVO,
} from '#/api/property/community/model';
import { getPopupContainer } from '@vben/utils';
import { getDictOptions } from '#/utils/dict';
import { DictEnum } from '@vben/constants';

let arr: CommunityVO[] = [];
const communitySelect: VbenFormSchema = {
  component: 'ApiSelect',
  fieldName: 'communityId',
  label: '社区',
  componentProps: {
    resultField: 'list', // 根据API返回结构调整
    labelField: 'communityName',
    valueField: 'id',
    api: async () => {
      if (!arr || arr.length == 0) {
        const params: CommunityQuery = {
          pageNum: 1,
          pageSize: 500,
        };
        const res = await communityList(params);
        arr = res.rows;
      }
      return arr;
    },
  },
};

export const querySchema: FormSchemaGetter = () => [
  communitySelect,
  {
    component: 'Input',
    fieldName: 'buildingName',
    label: '建筑名称',
  },
];

// 需要使用i18n注意这里要改成getter形式 否则切换语言不会刷新
// export const columns: () => VxeGridProps['columns'] = () => [
export const columns: VxeGridProps['columns'] = [
  { type: 'checkbox', width: 60 },
  {
    title: '建筑名称',
    field: 'buildingName',
  },
  {
    title: '总层数',
    field: 'floorCount',
  },
  {
    title: '单元数',
    field: 'unitCount',
  },
  {
    title: '建筑类型',
    field: 'buildTypeName',
  },
  {
    title: '电梯数量',
    field: 'elevatorCount',
  },
  {
    title: '竣工日期',
    field: 'completionDate',
  },
  {
    title: '地址',
    field: 'addr',
  },
  {
    field: 'action',
    fixed: 'right',
    slots: { default: 'action' },
    title: '操作',
    width: 180,
  },
];

export const modalSchema: FormSchemaGetter = () => [
  {
    label: '',
    fieldName: 'id',
    component: 'Input',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    ...communitySelect,
    rules: 'required',
  },
  {
    label: '建筑名称',
    fieldName: 'buildingName',
    component: 'Input',
    rules: 'required',
  },
  {
    label: '总层数',
    fieldName: 'floorCount',
    component: 'Input',
  },
  {
    label: '单元数',
    fieldName: 'unitCount',
    component: 'Input',
  },
  {
    label: '建筑类型',
    fieldName: 'buildType',
    component: 'Select',
    componentProps: {
      getPopupContainer,
      options: getDictOptions(DictEnum.sys_build_use_type),
    },
  },
  {
    label: '电梯数量',
    fieldName: 'elevatorCount',
    component: 'Input',
  },
  {
    label: '竣工日期',
    fieldName: 'completionDate',
    component: 'DatePicker',
    componentProps: {
      showTime: true,
      format: 'YYYY-MM-DD',
      valueFormat: 'YYYY-MM-DD',
    },
  },
  {
    label: '地址',
    fieldName: 'addr',
    component: 'Input',
    rules: 'required',
  },
];
