import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  MyProTableProps,
  MyTableActions,
  MyToolBarActions,
} from '@/common';
import { MyExport } from '@/components/MyExport';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import {
  AssetHousesOwnershipTypeEnum,
  AssetHousesUsageEnum,
} from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { Space } from 'antd';
import { useState } from 'react';
import HousesShow from './modals/HousesShow';
import HousesUpdate from './modals/HousesUpdate';

export default function Index({ title = '房屋列表' }) {
  const [getParams, setParams] = useState({});
  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="asset-houses"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        headerTitle="房屋列表"
        tooltip="房屋列表的信息管理，请在【项目列表】的【配置】中进行操作"
        request={async (params, sort) => {
          setParams(params);
          return MyProTableProps.request(
            params,
            sort,
            Apis.Asset.AssetHouses.List,
          );
        }}
        toolBarRender={() => [
          <MyToolBarActions
            key="toolbar"
            actions={{
              export: (
                <MyExport
                  key="export"
                  item={getParams}
                  download={Apis.Asset.AssetHouses}
                />
              ),
            }}
          />,
        ]}
        columns={[
          MyColumns.ID({ search: false }),
          Selects?.AssetProjects({
            title: '选择项目',
            key: 'asset_projects_id',
            hidden: true,
          }),
          {
            title: '项目名称',
            dataIndex: ['asset_project', 'name'],
            search: false,
            // search: {
            //   transform: (value) => {
            //     return { project_name: value };
            //   },
            // },
          },
          {
            title: '房屋名称',
            dataIndex: 'full_name',
          },
          {
            title: '楼栋名称',
            dataIndex: 'building_name',
            hidden: true,
          },
          {
            title: '单元名称',
            dataIndex: 'unit_name',
            hidden: true,
          },
          MyColumns.EnumTag({
            title: '用途',
            dataIndex: 'usage',
            valueEnum: AssetHousesUsageEnum,
          }),
          {
            title: '楼层',
            dataIndex: 'floor',
            render(_, record) {
              return `${record?.floor || '-'}`;
            },
            search: false,
          },

          {
            title: '建筑面积',
            dataIndex: 'built_area',
            render(_, record) {
              return `${
                record?.built_area ? record?.built_area + ' m²' : '-'
              } `;
            },
            search: false,
          },
          {
            title: '套内面积',
            dataIndex: 'inside_area',
            render(_, record) {
              return `${
                record?.inside_area ? record?.inside_area + ' m²' : '-'
              } `;
            },
            search: false,
          },
          {
            title: '计费面积',
            dataIndex: 'chargeable_area',
            render(_, record) {
              return `${
                record?.chargeable_area ? record?.chargeable_area + ' m²' : '-'
              } `;
            },
            search: false,
          },
          {
            title: '户型',
            render(_, record) {
              return record?.room
                ? `${record?.room || 'x'}室${record?.hall || 'x'}厅${
                    record?.bathroom || 'x'
                  }卫 `
                : '-';
            },
            search: false,
          },
          // MyColumns.EnumTag({
          //   title: '朝向',
          //   dataIndex: 'orientation',
          //   valueEnum: AssetHousesOrientationEnum,
          //   search: false,
          // }),
          // MyColumns.EnumTag({
          //   title: '房屋状态',
          //   dataIndex: 'status',
          //   valueEnum: AssetHousesStatusEnum,
          //   search: false,
          // }),
          MyColumns.EnumTag({
            title: '房屋属性',
            dataIndex: 'ownership_type',
            valueEnum: AssetHousesOwnershipTypeEnum,
            search: false,
          }),

          // {
          //   title: '产权年限',
          //   dataIndex: 'ownership_term',
          //   render(_, record) {
          //     return `${record?.ownership_term || '-'} 年`;
          //   },
          //   search: false,
          // },
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                <HousesShow item={item} reload={action?.reload} />
                <MyTableActions
                  actions={{
                    update: (
                      <HousesUpdate
                        item={item}
                        reload={action?.reload}
                        title="编辑"
                      />
                    ),
                    delete: (
                      <MyButtons.Delete
                        onConfirm={() =>
                          Apis.Asset.AssetHouses.Delete({
                            id: item.id,
                          }).then(() => action?.reload())
                        }
                      />
                    ),
                  }}
                />
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
