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 {
  AssetCarPortsPropertyTypeEnum,
  AssetCarPortsStatusEnum,
  AssetCarPortsTypeEnum,
} from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { Space } from 'antd';
import { useState } from 'react';

import HousesShow from '../houses/modals/HousesShow';
import AreaUpdate from './modals/AreaUpdate';
import SpaceShow from './modals/SpaceShow';
import SpaceUpdate from './modals/SpaceUpdate';

export default function Index({ title = '车位列表' }) {
  const [getParams, setParams] = useState({ page: 1 });
  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="asset_parking_places"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        headerTitle="车位列表"
        tooltip="车位列表的信息管理，请在【项目列表】中进行操作"
        request={async (params, sort) =>
          MyProTableProps.request(
            params,
            sort,
            Apis.Asset.AssetCarPorts.List,
            setParams,
          )
        }
        toolBarRender={(action) => [
          <MyToolBarActions
            key="toolbar"
            actions={{
              export: (
                <MyExport
                  key="export"
                  item={{ current: getParams?.page || 1, ...getParams }}
                  reload={action?.reload}
                  download={Apis.Asset.AssetCarPorts}
                />
              ),
            }}
          />,
        ]}
        columns={[
          MyColumns.ID({ search: false }),
          Selects?.AssetProjects({
            title: '选择项目',
            key: 'asset_projects_id',
            hidden: true,
          }),
          {
            title: '项目名称',
            dataIndex: ['asset_project', 'name'],
            search: false,
          },
          {
            title: '车位全称',
            dataIndex: 'full_name',
          },
          {
            title: '车场名称',
            dataIndex: ['asset_parking_place', 'name'],
            search: {
              transform: (value) => {
                return { parking_place_name: value };
              },
            },
            render: (_, item: any) => (
              <AreaUpdate
                item={{
                  ...item,
                  type: 'link',
                  titl: `${item?.asset_parking_place?.name}`,
                }}
                title={item?.asset_parking_place?.name}
              />
            ),
          },
          {
            title: '车位号',
            dataIndex: 'name',
            search: false,
          },

          MyColumns.EnumTag({
            title: '类型',
            dataIndex: 'type',
            valueEnum: AssetCarPortsTypeEnum,
          }),
          MyColumns.EnumTag({
            title: '产权类型',
            dataIndex: 'property_type',
            valueEnum: AssetCarPortsPropertyTypeEnum,
          }),
          MyColumns.EnumTag({
            title: '状态',
            dataIndex: 'status',
            valueEnum: AssetCarPortsStatusEnum,
          }),
          {
            title: '建筑面积',
            dataIndex: 'built_area',
            search: false,
          },
          {
            title: '关联房屋',
            dataIndex: 'asset_houses_id',
            search: false,
            render: (_, item: any) =>
              item?.asset_houses_id ? (
                <HousesShow
                  item={{ id: item?.asset_houses_id, type: 'link' }}
                  title="查看"
                />
              ) : (
                '-'
              ),
          },
          // MyColumns.CreatedAt(),
          MyColumns.UpdatedAt(),
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                <SpaceShow item={item} reload={action?.reload} title={title} />
                <MyTableActions
                  actions={{
                    update: (
                      <SpaceUpdate
                        item={item}
                        reload={action?.reload}
                        title={title}
                      />
                    ),
                    delete: (
                      <MyButtons.Delete
                        onConfirm={() =>
                          Apis.Asset.AssetCarPorts.Delete({
                            id: item.id,
                          }).then(() => action?.reload())
                        }
                      />
                    ),
                  }}
                />
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
