import {
  MyBetaModalFormProps,
  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/Create';
import Update from '../modals/Update';

export default function Index(props: MyBetaModalFormProps) {
  return (
    <ProTable
      {...MyProTableProps.props}
      request={async (params, sort) =>
        MyProTableProps.request(
          { ...params, contracts_id: props?.item?.id },
          sort,
          Apis.Contract.ContractDisputeRecords.List,
        )
      }
      toolBarRender={(action) => [
        <Create
          key="Create"
          reload={action?.reload}
          item={props?.item}
          title="新增争议记录"
        />,
      ]}
      headerTitle="争议记录列表"
      search={false}
      columns={[
        MyColumns.ID({
          search: false,
        }),
        {
          title: '争议原因',
          dataIndex: 'reason',
        },
        {
          title: '处理内容',
          dataIndex: 'content',
          search: false,
        },
        {
          title: '附件',
          search: false,
          render: (_, item: any) => {
            return (
              <Space direction="vertical">
                {item?.attachments?.map((res: any, index: number) => {
                  return (
                    <a key={`item_${index}`} href={res?.url}>
                      {res?.name}
                    </a>
                  );
                })}
              </Space>
            );
          },
        },

        MyColumns.Option({
          render: (_, item: any, index, action) => (
            <Space key={`item_${index}`}>
              <Update item={item} reload={action?.reload} title="编辑" />
              <MyButtons.Delete
                onConfirm={() =>
                  Apis.Contract.ContractDisputeRecords.Delete({
                    id: item.id,
                  }).then(() => action?.reload())
                }
              />
            </Space>
          ),
        }),
      ]}
    />
  );
}
