import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  MyProTableProps,
  usePageTabs,
} from '@/common';
import { MyExport } from '@/components/MyExport';
import { Apis } from '@/gen/Apis';
import {
  AssetHousesOwnershipTypeEnum,
  AssetHousesUsageEnum,
} from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { useNavigate } from '@umijs/max';
import { Space } from 'antd';
import { useState } from 'react';
import HousesShow from '../dictionary/modals/HousesShow';
import HousesUpdate from '../dictionary/modals/HousesUpdate';

export default function Index({ title = '房屋列表' }) {
  const [getParams, setParams] = useState({});

  const navigate = useNavigate();
  // 注册当前页面为标签页
  usePageTabs({
    tabKey: 'asset-houses',
    tabLabel: title,
  });
  return (
    <MyPageContainer title={title}>
      <ProTable
        {...MyProTableProps.props}
        request={async (params, sort) => {
          setParams(params);
          return MyProTableProps.request(
            params,
            sort,
            Apis.Asset.AssetHouses.List,
          );
        }}
        toolBarRender={() => [
          <MyExport
            key="export"
            item={getParams}
            download={Apis.Asset.AssetHouses}
          />,
        ]}
        columns={[
          MyColumns.ID(),
          {
            title: '项目名称',
            dataIndex: ['asset_project', 'name'],
            search: {
              transform: (value) => {
                return { project_name: value };
              },
            },
          },
          {
            title: '房屋名称',
            dataIndex: 'full_name',
          },

          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 || 'x'}室${record?.hall || 'x'}厅${
                record?.bathroom || 'x'
              }卫${record?.kitchen || 'x'}厨${record?.balcony || '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} />
                <HousesUpdate
                  item={item}
                  reload={action?.reload}
                  title="编辑"
                />
                <MyButtons.Delete
                  onConfirm={() =>
                    Apis.Asset.AssetHouses.Delete({
                      id: item.id,
                    }).then(() => action?.reload())
                  }
                />
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
