import { MyColumns, MyProTableProps } from '@/common';
import { Apis } from '@/gen/Apis';
import {
  HouseOccupantsHouseRelationEnum,
  HouseOccupantsRelationWithOwnerEnum,
  HouseOccupantsStatusEnum,
} from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { Space } from 'antd';
import { useEffect, useRef } from 'react';
import AddOccupant from '../modals/AddOccupant';
import AddOwner from '../modals/AddOwner';
import MoveIn from '../modals/MoveIn00';
import MoveOut from '../modals/MoveOut';
import OccupantShow from '../modals/OccupantShow';
import OccupantsUpdate from '../modals/OccupantsUpdate';
import Transfer from '../modals/Transfer';

export default function Index({ ...rest }) {
  const actionLooks = useRef<any>();
  useEffect(() => {
    actionLooks?.current.reloadAndRest();
  }, [rest.loadmore]);

  return (
    <>
      <ProTable<Record<any, any>>
        {...MyProTableProps.props}
        actionRef={actionLooks}
        request={async (params, sort) =>
          MyProTableProps.request(
            {
              ...params,
              asset_houses_id: rest.item?.asset_houses_id,
              status: 'Normal',
            },
            sort,
            Apis.Archive.HouseOccupants.List,
          )
        }
        toolBarRender={(action) => [
          <AddOwner
            key="add"
            item={rest.item}
            reload={action?.reload}
            title="添加产权人"
          />,
          <Transfer
            key="Transfer"
            item={rest.item}
            reload={action?.reload}
            title=" 过户登记"
          />,
          <AddOccupant
            key="AddOccupant"
            item={rest.item}
            reload={action?.reload}
            title="添加住户"
          />,
        ]}
        search={false}
        columns={[
          MyColumns.EnumTag({
            title: '房客关系',
            dataIndex: 'house_relation',
            valueEnum: HouseOccupantsHouseRelationEnum,
            search: false,
          }),
          MyColumns.EnumTag({
            title:
              rest?.item?.status === 'Rented' ? '与租客关系' : '与业主关系',
            dataIndex: 'relation_with_owner',
            valueEnum: HouseOccupantsRelationWithOwnerEnum,
            search: false,
          }),
          {
            title: '姓名',
            dataIndex: 'name',
          },
          {
            title: '电话',
            dataIndex: 'phone',
          },

          {
            title: '是否入住',
            dataIndex: 'is_live_in',
            render(_, record) {
              return `${record?.is_live_in ? '是' : '-'} `;
            },
          },
          {
            title: '入住日期',
            dataIndex: 'is_live_in',
            render(_, record) {
              return `${record?.move_in_date || '-'}`;
            },
          },
          MyColumns.EnumTag({
            title: '状态',
            dataIndex: 'status',
            valueEnum: HouseOccupantsStatusEnum,
            search: false,
          }),
          // MyColumns.UpdatedAt(),
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                <OccupantShow item={item} reload={action?.reload} />
                {item?.house_relation !== 'Owner' && (
                  <OccupantsUpdate item={item} reload={action?.reload} />
                )}
                {!item?.is_live_in && (
                  <MoveIn item={item} reload={action?.reload} title="入住" />
                )}
                {item?.move_in_date && (
                  <MoveOut item={item} reload={action?.reload} title="搬离" />
                )}
                {/* <MyButtons.Delete
                  onConfirm={() =>
                    Apis.Archive.HouseOccupants.Delete({ id: item.id }).then(
                      () => action?.reload(),
                    )
                  }
                /> */}
              </Space>
            ),
          }),
        ]}
      />
    </>
  );
}
