import {
  MyButtons,
  MyColumns,
  MyImportModal,
  MyPageContainer,
  MyProTableProps,
  usePageTabs,
} from '@/common';
import { Apis } from '@/gen/Apis';
import { AssetProjectsPropertyTypeEnum } from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { Space } from 'antd';
import { useNavigate } from 'react-router-dom';
import AssetCreate from './modals/AssetCreate';
import AssetInfo from './modals/AssetInfo';
import AssetUpdate from './modals/AssetUpdate';

export default function Index({ title = '项目列表' }) {
  const navigate = useNavigate();

  // 注册当前页面为标签页
  usePageTabs({
    tabKey: 'asset-projects',
    tabLabel: title,
  });
  return (
    <MyPageContainer title={title}>
      <ProTable
        {...MyProTableProps.props}
        // search={false}
        request={async (params, sort) =>
          MyProTableProps.request(params, sort, Apis.Asset.AssetProjects.List)
        }
        // headerTitle="项目列表"
        toolBarRender={(action) => [
          <MyImportModal
            key="ImportHouse"
            title="批量导入"
            type="danger"
            size="middle"
            templateApi={Apis.Asset.AssetProjects.DownloadTemplate}
            importApi={Apis.Asset.AssetProjects.Import}
            reload={action?.reload}
          />,
          <AssetCreate key="Create" reload={action?.reload} title={'项目'} />,
        ]}
        columns={[
          MyColumns.ID(),
          MyColumns.EnumTag({
            title: '类型',
            dataIndex: 'property_type',
            valueEnum: AssetProjectsPropertyTypeEnum,
            search: false,
          }),
          {
            title: '项目名称',
            dataIndex: 'name',
          },
          // {
          //   title: '项目别名',
          //   dataIndex: 'alias_name',
          // },
          {
            title: '地址',
            render: (_, i: any) => {
              return `${i?.province || ''}${i?.city || ''}${i?.district || ''}${
                i?.address || ''
              }`;
            },
            search: false,
          },
          // MyColumns.EnumTag({
          //   title: '项目状态',
          //   dataIndex: 'status',
          //   valueEnum: AssetProjectsStatusEnum,
          //   search: false,
          // }),
          // MyColumns.EnumTag({
          //   title: '委托类型',
          //   dataIndex: 'entrust_type',
          //   valueEnum: AssetProjectsEntrustTypeEnum,
          //   search: false,
          // }),
          // MyColumns.EnumTag({
          //   title: '收费方式',
          //   dataIndex: 'charge',
          //   valueEnum: AssetProjectsChargeEnum,
          //   search: false,
          // }),
          MyColumns.SoftDelete({
            title: '启/禁用',
            onRestore: Apis.Asset.AssetProjects.Restore,
            onSoftDelete: Apis.Asset.AssetProjects.SoftDelete,
            search: false,
          }),
          {
            title: '物业品牌',
            dataIndex: ['company_property_brand', 'name'],
            search: false,
          },
          {
            title: '关联机构',
            dataIndex: ['company', 'name'],
            search: {
              transform: (value) => {
                return { company_name: value };
              },
            },
          },
          // {
          //   title: '接管日期',
          //   dataIndex: 'takeover_date',
          //   search:false,
          // },
          // {
          //   title: '封园日期',
          //   dataIndex: 'closure_date',
          //   search:false,
          // },
          MyColumns.CreatedAt(),
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                <AssetInfo item={item} title="查看" reload={action?.reload} />
                <AssetUpdate item={item} title="项目" reload={action?.reload} />
                <MyButtons.View
                  title="配置"
                  onClick={() => {
                    navigate(`/asset/list/show/${item.id}`);
                  }}
                />
                <MyButtons.Delete
                  onConfirm={() =>
                    Apis.Asset.AssetProjects.Delete({ id: item.id }).then(() =>
                      action?.reload(),
                    )
                  }
                />
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
