import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  MyProTableProps,
  usePageTabs,
} from '@/common';
import { Apis } from '@/gen/Apis';
import {
  HouseWorkOrdersAssignStatusEnum,
  HouseWorkOrdersLevelEnum,
  HouseWorkOrdersStatusEnum,
  HouseWorkOrdersTypeEnum,
} from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { useNavigate } from '@umijs/max';
import { Image, Space } from 'antd';
import ComplaintAssign from './modals/ComplaintAssign';

import ComplaintShow from './modals/ComplaintShow';

export default function Index({ title = '投诉列表' }) {
  const navigate = useNavigate();

  // 注册当前页面为标签页
  usePageTabs({
    tabKey: 'complaint',
    tabLabel: title,
  });

  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="complaint"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        request={async (params, sort) =>
          MyProTableProps.request(
            {
              ...params,
              type: [HouseWorkOrdersTypeEnum.Complaint.value],
            },
            sort,
            Apis.WorkOrder.HouseWorkOrders.List,
          )
        }
        // toolBarRender={(action) => [
        //   <WorkOrderCreate
        //     key="Create"
        //     reload={action?.reload}
        //     title={title}
        //   />,
        // ]}
        columns={[
          MyColumns.ID(),
          MyColumns.EnumTag({
            title: '状态',
            dataIndex: 'status',
            valueEnum: HouseWorkOrdersStatusEnum,
          }),
          {
            title: '描述',
            dataIndex: 'content',
            width: 120, // 关键：固定列宽（若父容器过窄，可设 minWidth: 200 优先保证列宽）
            search: false,
            render: (text) => (
              <div
                style={{
                  width: '100%', // 继承列宽
                  // height: '60px', // 设置固定高度，约显示3行文本
                  overflow: 'hidden', // 超出隐藏
                  textOverflow: 'ellipsis', // 省略号
                  display: '-webkit-box',
                  WebkitBoxOrient: 'vertical',
                  WebkitLineClamp: 1, // 显示3行
                }}
              >
                {text}
              </div>
            ),
          },
          {
            title: '附件',
            dataIndex: 'attachments',
            search: false,
            render: (_, record) => {
              if (
                !Array.isArray(record.attachments) ||
                record.attachments.length === 0
              ) {
                return '无附件';
              }
              return (
                <div style={{ display: 'flex', flexWrap: 'wrap' }}>
                  {record.attachments.map((item: any, index: number) => {
                    if (item.type && item.type.includes('image')) {
                      return (
                        <Image
                          key={index}
                          width={40}
                          height={30}
                          src={item.url}
                          style={{
                            marginRight: 8,
                            marginBottom: 8,
                            objectFit: 'cover',
                          }}
                        />
                      );
                    } else if (item.type && item.type.includes('video')) {
                      return (
                        <video
                          key={index}
                          width={40}
                          height={30}
                          controls
                          style={{
                            marginRight: 8,
                            marginBottom: 8,
                            objectFit: 'cover',
                          }}
                          src={item.url}
                        />
                      );
                    }
                    return null;
                  })}
                </div>
              );
            },
          },
          MyColumns.EnumTag({
            title: '分配状态',
            dataIndex: 'assign_status',
            valueEnum: HouseWorkOrdersAssignStatusEnum,
          }),
          MyColumns.EnumTag({
            title: '优先级',
            dataIndex: 'level',
            valueEnum: HouseWorkOrdersLevelEnum,
          }),
          {
            title: '处理人',
            dataIndex: ['assign_employee', 'name'],
            search: false,
            render: (_, record) => {
              return `${record?.assign_employee?.name || ''}-${
                record?.assign_employee?.phone || ''
              }`;
            },
          },
          MyColumns.CreatedAt(),
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                <ComplaintShow
                  item={item}
                  title="详情"
                  reload={action?.reload}
                />

                {item.assign_status === 'Unassigned' && (
                  <ComplaintAssign
                    item={item}
                    reload={action?.reload}
                    title="分配"
                  />
                )}
                <MyButtons.Delete
                  onConfirm={() =>
                    Apis.WorkOrder.HouseWorkOrders.SoftDelete({
                      id: item.id,
                    }).then(() => action?.reload())
                  }
                />
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
