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

export default function Index({ title = '巡检规则' }) {
  const getCurrentPermissions = useCurrentPermissions();
  let toolBarRender = (action: any) => {
    return getCurrentPermissions(
      {
        add: <Create key="Create" reload={action?.reload} title={title} />,
      },
      'MyRenovationInspectionRules',
    );
  };

  let tableRender = (item: any, action: any) => {
    return getCurrentPermissions(
      {
        update: <Update item={item} reload={action?.reload} title={title} />,
        delete: (
          <MyButtons.Delete
            onConfirm={() =>
              Apis.Renovation.RenovationInspectionRules.Delete({
                id: item.id,
              }).then(() => action?.reload())
            }
          />
        ),
      },
      'MyRenovationInspectionRules',
    );
  };
  return (
    <ProTable
      {...MyProTableProps.props}
      request={async (params, sort) =>
        MyProTableProps.request(
          params,
          sort,
          Apis.Renovation.RenovationInspectionRules.List,
        )
      }
      toolBarRender={(action) => [toolBarRender(action)]}
      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}>
              <>{tableRender(item, action)}</>
            </Space>
          ),
        }),
      ]}
    />
  );
}
