import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  MyProTableProps,
  MyTableActions,
  MyToolBarActions,
} from '@/common';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import { ConvenienceServicesTypeEnum } from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { Space } from 'antd';
import ServiceCreate from './modals/ServiceCreate';
import ServiceUpdate from './modals/ServiceUpdate';

export default function Index({ title = '便民服务' }) {
  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="convenience-services"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        request={async (params, sort) =>
          MyProTableProps.request(
            params,
            sort,
            Apis.Common.ConvenienceServices.List,
          )
        }
        toolBarRender={(action) => [
          <MyToolBarActions
            key="toolbar"
            actions={{
              select: (
                <ServiceCreate
                  key="Select"
                  reload={action?.reload}
                  title={title}
                />
              ),
            }}
          />,
        ]}
        columns={[
          MyColumns.ID({ search: false }),
          Selects?.AssetProjects({
            title: '选择项目',
            key: 'asset_projects_id',
            hidden: true,
          }),
          {
            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) {
              // 确保content始终是数组类型
              const content = Array.isArray(record?.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}>
                <MyTableActions
                  actions={{
                    update: (
                      <ServiceUpdate item={item} reload={action?.reload} />
                    ),
                    delete: (
                      <MyButtons.Delete
                        onConfirm={() =>
                          Apis.Common.ConvenienceServices.Delete({
                            id: item.id,
                          }).then(() => action?.reload())
                        }
                      />
                    ),
                  }}
                />
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
