import { MyButtons, MyColumns, MyProTableProps } from '@/common';
import { Apis } from '@/gen/Apis';
import { ConvenienceServicesTypeEnum } from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { Space } from 'antd';
import { useEffect, useRef } from 'react';
import ServiceCreate from '../../services/modals/ServiceCreate';
import ServiceUpdate from '../../services/modals/ServiceUpdate';

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_projects_id: rest.item?.id },
            sort,
            Apis.Common.ConvenienceServices.List,
          )
        }
        search={false}
        toolBarRender={(action) => [
          <ServiceCreate
            item={rest.item}
            key="Create"
            reload={action?.reload}
          />,
        ]}
        columns={[
          MyColumns.ID({
            search: false,
          }),
          {
            title: '关联项目',
            dataIndex: ['asset_project', 'name'],
            // search: {
            //   transform: (value) => {
            //     return { project_name: value };
            //   },
            // },
            search: false,
          },
          MyColumns.EnumTag({
            title: '类型',
            dataIndex: 'type',
            valueEnum: ConvenienceServicesTypeEnum,
            search: false,
          }),
          {
            title: '服务名称',
            dataIndex: 'name',
          },

          {
            title: '联系方式',
            render(_, record) {
              const content = record?.content || [];

              // 过滤有效数据
              const validItems = content.filter(
                (item: any) => item?.name && item?.phone,
              );

              return (
                <div style={{ lineHeight: '1.6' }}>
                  {validItems.length > 0 ? (
                    validItems.map((item: any, index: number) => (
                      // 每个客户信息单独一行
                      <div key={index}>
                        {item.name}: {item.phone}
                      </div>
                    ))
                  ) : (
                    <span>暂无客户信息</span>
                  )}
                </div>
              );
            },
            search: false,
          },
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                <ServiceUpdate item={item} reload={action?.reload} />
                <MyButtons.Delete
                  onConfirm={() =>
                    Apis.Common.ConvenienceServices.Delete({
                      id: item.id,
                    }).then(() => action?.reload())
                  }
                />
              </Space>
            ),
          }),
        ]}
      />
    </>
  );
}
