import { MyButtons, MyColumns, MyProTableProps } from '@/common';
import { Apis } from '@/gen/Apis';
import { ProTable } from '@ant-design/pro-components';
import { Space } from 'antd';
import Create from '../modals/InspectionRuleCreate';
import Update from '../modals/InspectionRuleUpdate';

export default function Index({ title = '巡检规则' }) {
  return (
    <ProTable
      {...MyProTableProps.props}
      request={async (params, sort) =>
        MyProTableProps.request(
          params,
          sort,
          Apis.Renovation.RenovationInspectionRules.List,
        )
      }
      toolBarRender={(action) => [
        <Create key="Create" reload={action?.reload} title={title} />,
      ]}
      columns={[
        MyColumns.ID({
          search: false,
        }),
        {
          title: '名称',
          dataIndex: 'name',
        },

        {
          title: '巡逻时段',
          search: false,
          render: (_, item: any) => {
            return (
              <Space direction="vertical" size="small">
                {item?.rule?.map((res: any, index: number) => {
                  return (
                    <div key={`item_${index}`}>
                      【{index + 1}】{res.start_time}-{res.end_time}
                    </div>
                  );
                })}
              </Space>
            );
          },
        },
        {
          title: '巡逻说明',
          dataIndex: 'description',
          search: false,
        },
        MyColumns.CreatedAt(),
        MyColumns.Option({
          render: (_, item: any, index, action) => (
            <Space key={index}>
              <Update item={item} reload={action?.reload} title={title} />
              <MyButtons.Delete
                onConfirm={() =>
                  Apis.Renovation.RenovationInspectionRules.Delete({
                    id: item.id,
                  }).then(() => action?.reload())
                }
              />
            </Space>
          ),
        }),
      ]}
    />
  );
}
