import { MyButtons, MyColumns, MyProTableProps } from '@/common';
import { Apis } from '@/gen/Apis';
import {
  HouseWorkOrdersAssignStatusEnum,
  HouseWorkOrdersLevelEnum,
  HouseWorkOrdersLocationEnum,
  HouseWorkOrdersStatusEnum,
  HouseWorkOrdersTypeEnum,
} from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { Image, Space } from 'antd';
import WorkOrderAssign from '../modals/WorkOrderAssign';
import WorkOrderCreate from '../modals/WorkOrderCreate';
import WorkOrderShow from '../modals/WorkOrderShow';
import WorkOrderUpdate from '../modals/WorkOrderUpdate';

export default function Index({ title = '客户投诉' }) {
  return (
    <ProTable
      {...MyProTableProps.props}
      headerTitle="客户投诉"
      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({
          search: false,
        }),
        MyColumns.EnumTag({
          title: '工单状态',
          dataIndex: 'status',
          valueEnum: HouseWorkOrdersStatusEnum,
        }),
        MyColumns.EnumTag({
          title: '工单类型',
          dataIndex: 'type',
          valueEnum: HouseWorkOrdersTypeEnum,
        }),
        MyColumns.EnumTag({
          title: '报修位置',
          dataIndex: 'location',
          valueEnum: HouseWorkOrdersLocationEnum,
        }),
        {
          title: '房屋信息',
          dataIndex: ['asset_house', 'full_name'],
          search: false,
          ellipsis: true,
        },
        {
          title: '工单描述',
          dataIndex: 'content',
          search: false,
          width: 120, // 关键：固定列宽（若父容器过窄，可设 minWidth: 200 优先保证列宽）
          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', gap: 6 }}>
                {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}>
              <WorkOrderShow item={item} title="详情" reload={action?.reload} />
              {item.status !== 'Completed' && (
                <WorkOrderUpdate
                  item={item}
                  reload={action?.reload}
                  title={title}
                />
              )}
              {item.assign_status === 'Unassigned' && (
                <WorkOrderAssign
                  item={item}
                  reload={action?.reload}
                  title="指派"
                />
              )}

              <MyButtons.Delete
                onConfirm={() =>
                  Apis.WorkOrder.HouseWorkOrders.Delete({
                    id: item.id,
                  }).then(() => action?.reload())
                }
              />
            </Space>
          ),
        }),
      ]}
    />
  );
}
