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 { Popconfirm, Space, Tag } from 'antd';
import { useEffect, useRef } from 'react';
import AddOccupant from './modals/AddOccupant';
import AddRent from './modals/AddRent';
import MoveIn from './modals/MoveIn';
import MoveOut from './modals/MoveOut';
import OccupantShow from './modals/OccupantShow';
import OccupantsUpdate from './modals/OccupantsUpdate';
import RemoveOwner from './modals/RemoveOwner';
import Transfer from './modals/Transfer';

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

  const handleReload = () => {
    actionLooks?.current.reload();
    rest.reload?.(); // 调用父组件的reload函数更新Pending计数
  };

  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) => [
          <AddOccupant
            key="AddOccupant"
            item={rest.item}
            reload={action?.reload}
            title="添加住户"
          />,
          <AddRent
            key="AddOccupant"
            item={rest.item}
            reload={action?.reload}
            title="添加租客"
          />,
          <Transfer
            key="Transfer"
            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' ? '与租客关系' : '与业主关系',
            title: '关系说明',
            dataIndex: 'relation_with_owner',
            valueEnum: HouseOccupantsRelationWithOwnerEnum,
            search: false,
          }),
          {
            title: '姓名',
            dataIndex: 'name',
          },
          {
            title: '电话',
            dataIndex: 'phone',
          },
          {
            title: '入住日期',
            dataIndex: 'is_live_in',
            render(_, record) {
              return `${record?.move_in_date || '未入住'}`;
            },
          },
          {
            title: '常用联系人',
            render: (_, item, index, action) =>
              item?.is_contact ? (
                <Popconfirm
                  title="设置常用联系人"
                  description="您确认设置常用联系人吗？"
                  onConfirm={() => {
                    Apis.Archive.HouseOccupants.ChangeIsContact({
                      id: item.id,
                      is_contact: false,
                    }).then(() => action?.reload());
                  }}
                  okText="是"
                  cancelText="否"
                >
                  <Tag color="green" style={{ cursor: 'pointer' }}>
                    是
                  </Tag>
                </Popconfirm>
              ) : (
                <Popconfirm
                  title="取消"
                  description="您确认取消常用联系人吗？"
                  onConfirm={() => {
                    Apis.Archive.HouseOccupants.ChangeIsContact({
                      id: item.id,
                      is_contact: true,
                    }).then(() => action?.reload());
                  }}
                  okText="是"
                  cancelText="否"
                >
                  <Tag color="gray" style={{ cursor: 'pointer' }}>
                    否
                  </Tag>
                </Popconfirm>
              ),
            search: false,
          },
          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} />
                <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="搬离" />
                )}
                <RemoveOwner item={item} reload={handleReload} title="移除" />
              </Space>
            ),
          }),
        ]}
      />
    </>
  );
}
