import {
  MyButtons,
  MyColumns,
  MyImportModal,
  MyPageContainer,
  MyProTableProps,
  MyTableActions,
  MyToolBarActions,
} from '@/common';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import {
  AssetHousesOwnershipTypeEnum,
  AssetHousesUsageEnum,
} from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { useNavigate } from '@umijs/max';
import { Tag } from 'antd';
import { useRef } from 'react';
import Delivery from './modals/Delivery';

export default function Index({ title = '房屋档案' }) {
  const navigate = useNavigate();
  const formRef = useRef<any>();

  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="archive-asset-houses"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        headerTitle="房屋档案 (按房屋查询)"
        request={async (params, sort) =>
          MyProTableProps.request(params, sort, Apis.Asset.AssetHouses.List)
        }
        formRef={formRef}
        toolBarRender={(action) => [
          <MyToolBarActions
            key="toolbar"
            actions={{
              import: (
                <MyImportModal
                  key="ImportHouse"
                  title="批量导入"
                  type="default"
                  size="middle"
                  templateApi={Apis.Archive.HouseOccupants.DownloadTemplate}
                  importApi={Apis.Archive.HouseOccupants.Import}
                  reload={action?.reload}
                />
              ),
            }}
          />,
        ]}
        columns={[
          MyColumns.ID({ search: false }),
          Selects?.AssetProjects({
            title: '选择项目',
            key: 'asset_projects_id',
            hidden: true,
            fieldProps: {
              onChange: () => {
                formRef.current?.setFieldsValue({
                  asset_buildings_id: undefined,
                  asset_units_id: undefined,
                  asset_houses_id: undefined,
                });
              },
            },
          }),
          Selects?.AssetBuildings({
            title: '选择楼栋',
            key: 'asset_buildings_id',
            hidden: true,
            dependencies: ['asset_projects_id'],
            fieldProps: {
              onChange: () => {
                formRef.current?.setFieldsValue({
                  asset_units_id: undefined,
                  asset_houses_id: undefined,
                });
              },
            },
          }),
          Selects?.AssetUnits({
            title: '选择单元',
            key: 'asset_units_id',
            hidden: true,
            dependencies: ['asset_buildings_id'],
            fieldProps: {
              onChange: () => {
                formRef.current?.setFieldsValue({
                  asset_houses_id: undefined,
                });
              },
            },
          }),
          Selects?.AssetHouses({
            title: '选择房屋',
            key: 'asset_houses_id',
            hidden: true,
            dependencies: ['asset_units_id'],
          }),
          {
            title: '项目名称',
            dataIndex: ['asset_project', 'name'],
            search: false,
            // search: {
            //   transform: (value) => {
            //     return { project_name: value };
            //   },
            // },
          },
          {
            title: '房屋名称',
            dataIndex: 'full_name',
            ellipsis: true,
          },
          // MyColumns.EnumTag({
          //   title: '房屋状态',
          //   dataIndex: 'status',
          //   valueEnum: AssetHousesStatusEnum,
          //   search: false,
          // }),

          MyColumns.EnumTag({
            title: '产权性质',
            dataIndex: 'ownership_type',
            valueEnum: AssetHousesOwnershipTypeEnum,
            search: false,
          }),
          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 || ''} m²`;
          //   },
          //   search: false,
          // },
          {
            title: '产权人',
            render(_, record) {
              const owners =
                record?.house_occupants?.filter(
                  (res: any) => res?.house_relation === 'Owner',
                ) || [];
              const residents =
                record?.house_occupants?.filter(
                  (res: any) => res?.house_relation === 'Resident',
                ) || [];
              return (
                <div>
                  {owners.length > 0 && (
                    <div>
                      {/* <Tag color="blue">产权人</Tag> */}
                      {owners.map((owner: any) => owner?.name).join('、')}
                    </div>
                  )}
                  {residents.length > 0 && (
                    <div style={{ marginTop: 4 }}>
                      <Tag color="green">住户</Tag>
                      {residents
                        .map((resident: any) => resident?.name)
                        .join('、')}
                    </div>
                  )}
                  {owners.length === 0 && residents.length === 0 && (
                    <Tag color="default">暂无</Tag>
                  )}
                </div>
              );
            },
            search: false,
          },
          MyColumns.Option({
            render: (_, item: any, __, action) => (
              <MyTableActions
                actions={{
                  view: (
                    <MyButtons.View
                      title="查看"
                      onClick={() => {
                        navigate(`/customer/archive/show/${item.id}`);
                      }}
                    />
                  ),
                  create: (
                    <Delivery
                      item={item}
                      reload={action?.reload}
                      title="添加业主"
                    />
                  ),
                }}
              />
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
