import { MyColumns, MyProTableProps } from '@/common';
import { Apis } from '@/gen/Apis';
import {
  HouseOccupantsHouseRelationEnum,
  HouseOccupantsResidentialRelationEnum,
  HouseOccupantsStatusEnum,
} from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { Popconfirm, Space, Switch } from 'antd';
import { useEffect, useRef } from 'react';
import Delivery from '../modals/Delivery';
import AddOccupant from './modals/AddOccupant';
import ChangePhone from './modals/ChangePhone';
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) => [
          <Delivery
            key="Delivery"
            item={{ ...rest.item, size: 'middle' }}
            reload={action?.reload}
            title="添加业主"
          />,
          !rest.item?.house_occupants?.length ? (
            ''
          ) : (
            <>
              <AddOccupant
                key="AddOccupant"
                item={rest.item}
                reload={action?.reload}
                title="添加客户"
              />
              <Transfer
                key="Transfer"
                item={rest.item}
                reload={action?.reload}
                title=" 过户登记"
              />
            </>
          ),
        ]}
        search={false}
        columns={[
          {
            title: '姓名',
            dataIndex: 'name',
          },
          {
            title: '电话',
            dataIndex: 'phone',
          },

          MyColumns.EnumTag({
            title: '关联身份',
            dataIndex: 'residential_relation',
            valueEnum: HouseOccupantsResidentialRelationEnum,
            search: false,
          }),

          {
            title: '入住日期',
            dataIndex: 'is_live_in',
            render(_, record) {
              return `${record?.move_in_date || '未入住'}`;
            },
          },
          {
            title: '常用联系人',
            render: (_, item, index, action) => (
              <Popconfirm
                title={item?.is_contact ? '取消常用联系人' : '设置常用联系人'}
                description={
                  item?.is_contact
                    ? '您确认取消常用联系人吗？'
                    : '您确认设置常用联系人吗？'
                }
                onConfirm={() => {
                  Apis.Archive.HouseOccupants.ChangeIsContact({
                    id: item.id,
                    is_contact: !item.is_contact,
                  }).then(() => action?.reload());
                }}
                okText="是"
                cancelText="否"
                placement="left"
              >
                <Switch
                  checked={item?.is_contact}
                  checkedChildren="是"
                  unCheckedChildren="否"
                  style={{ cursor: 'pointer' }}
                  disabled={false} // 可根据需求设置是否禁用
                />
              </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} />
                <ChangePhone item={item} reload={action?.reload} />
                {!item?.move_in_date && (
                  <MoveIn item={item} reload={action?.reload} title="入住" />
                )}
                {item?.move_in_date && (
                  <MoveOut item={item} reload={action?.reload} title="搬离" />
                )}
                {item?.house_relation ===
                  HouseOccupantsHouseRelationEnum.NonOwner.value && (
                  <RemoveOwner item={item} reload={handleReload} title="移除" />
                )}
              </Space>
            ),
          }),
        ]}
      />
    </>
  );
}
