import {
  MyColumns,
  MyPageContainer,
  MyProTableProps,
  MyTableActions,
  MyToolBarActions,
} from '@/common';
import { MyExport } from '@/components/MyExport';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import {
  AssetProjectsPropertyTypeEnum,
  AssetProjectsStatusEnum,
} from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { Space } from 'antd';
import { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import AssetInfo from './modals/AssetInfo';
import AssetUpdate from './modals/AssetUpdate';
import BuildingsManage from './modals/BuildingsManage';
import HasProjects from './modals/HasProjects';
import ParkingsManage from './modals/ParkingsManage';

export default function Index({ title = '项目信息' }) {
  const navigate = useNavigate();
  const [getParams, setParams] = useState({});

  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="asset-projects"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        // search={false}
        request={async (params, sort) => {
          setParams(params);
          return MyProTableProps.request(
            params,
            sort,
            Apis.Asset.AssetProjects.List,
          );
        }}
        headerTitle="项目列表"
        toolBarRender={(action) => [
          <MyToolBarActions
            key="toolbar"
            actions={{
              export: (
                <MyExport
                  key="export"
                  item={getParams}
                  download={Apis.Asset.AssetProjects}
                />
              ),
            }}
          />,
        ]}
        columns={[
          // MyColumns.ID(),
          // 组织树选择器
          Selects?.OrganizationSearch({
            title: '所属组织',
            key: 'organizations_id',
            colProps: { span: 24 },
            fieldProps: {
              showSearch: true,
            },
            search: {
              transform: (value) => {
                return {
                  organization_name:
                    value.length > 0 ? value[value.length - 1] : '',
                };
              },
            },
          }),
          Selects?.AssetProjects({
            title: '选择项目',
            key: 'id',
            hidden: true,
          }),
          {
            title: '项目名称',
            dataIndex: 'name',
            search: false,
            render: (_, item: any) => (
              <AssetInfo item={{ ...item, type: 'link' }} title={item?.name} />
            ),
          },
          {
            title: '所在城市',
            render: (_, i: any) => {
              return ` ${i?.city || ''} `;
            },
            search: {
              transform: (value) => {
                return { city: value };
              },
            },
          },
          MyColumns.EnumTag({
            title: '项目状态',
            dataIndex: 'status',
            valueEnum: AssetProjectsStatusEnum,
            search: false,
          }),

          MyColumns.EnumTag({
            title: '项目类型',
            dataIndex: 'property_type',
            valueEnum: AssetProjectsPropertyTypeEnum,
          }),
          {
            title: '接管面积(㎡)',
            dataIndex: 'takeover_building_area',
            render: (_, render: any) => {
              return ` ${
                render?.takeover_building_area
                  ? Math.round(render?.takeover_building_area || '')
                  : ''
              }`;
            },
            search: false,
          },
          {
            title: '接管日期',
            dataIndex: 'takeover_date',
            search: false,
          },
          {
            title: '所属组织',
            dataIndex: ['organization', 'name'],
            search: false,
          },
          MyColumns.UpdatedAt(),
          MyColumns.Option({
            render: (_, item: any, index, action) => {
              return (
                <Space key={index}>
                  <MyTableActions
                    actions={{
                      update: (
                        <AssetUpdate
                          item={item}
                          title="编辑项目"
                          key="updateInfo"
                          reload={action?.reload}
                        />
                      ),
                      buildings: (
                        <BuildingsManage
                          item={item}
                          key="BuildingsManage"
                          reload={action?.reload}
                          title="楼栋管理"
                        />
                      ),
                      parkings: (
                        <ParkingsManage
                          item={item}
                          key="ParkingsManage"
                          reload={action?.reload}
                          title="车场车位"
                        />
                      ),
                      has: (
                        <HasProjects
                          item={item}
                          key="HasProjects"
                          reload={action?.reload}
                          title="组织"
                        />
                      ),
                    }}
                    maxVisible={3}
                  />
                </Space>
              );
            },
          }),
        ]}
      />
    </MyPageContainer>
  );
}
