import { deleteGuaranteeTask, getGuaranteeTaskList } from '@/services/guaranteeTask';
import { queryTable } from '@/utils/request';
import { PageContainer, ProTable } from '@ant-design/pro-components';
import { Link, useNavigate } from 'react-router-dom';

import { Button, message, Popconfirm } from 'antd';
import moment from 'moment';
import { useRef, useState } from 'react';

const GuaranteeTaskList = (props) => {
  const navigate = useNavigate();
  const [isOpen, setIsOpen] = useState(false);
  const [modalData, setModalData] = useState({});
  // const [storeTypeDict, setStoreTypeDict] = useState([]);
  const actionRef = useRef();
  const formRef = useRef();

  const handleDelete = (id) => {
    if (id) {
      deleteGuaranteeTask(id).then((res) => {
        message.success('删除成功');
        actionRef.current.reload();
      });
    }
  };

  const onCancel = () => {
    setIsOpen(false);
    setModalData({});
  };

  const columns = [
    {
      title: '保障序号',
      dataIndex: 'guaranteeNo',
    },
    {
      title: '受领人',
      dataIndex: 'receiver',
    },
    {
      title: '时间区间',
      dataIndex: 'dateRange',
      valueType: 'dateRange',
      search: {
        transform: (value) => {
          console.log(value, value[0].valueOf(), 'valuevalue');

          return {
            startDate: moment(value[0]).format('yyyy-MM-DD'),
            endDate: moment(value[1]).format('yyyy-MM-DD'),
          };
        },
      },
    },
    {
      title: '保障地点',
      dataIndex: 'address',
      search: false,
    },
    {
      title: '保障人数',
      dataIndex: 'totalPersonNum',
      search: false,
    },
    {
      title: '伙食标准',
      dataIndex: 'standard',
      search: false,
    },

    {
      title: '操作',
      valueType: 'option',
      width: 200,
      render: (_, record) => [
        <Button
          key={2}
          style={{ padding: 0 }}
          type="link"
          onClick={() => {
            navigate(`/task/guaranteeTask/edit?id=${record.id}`);
          }}
        >
          修改
        </Button>,

        <Popconfirm
          key={3}
          title="您是否确认删除 ?"
          onConfirm={() => handleDelete(record.id)}
          okText="确定"
          cancelText="取消"
        >
          {/* manualPush */}
          <Button style={{ padding: 0 }} type="link">
            删除
          </Button>
        </Popconfirm>,
        <Button
          key={4}
          style={{ padding: 0 }}
          type="link"
        >
          <Link target="_blank" to={`/task/guaranteeTask/print?id=${record.id}`}>打印</Link>
        </Button>,
      ],
    },
  ];

  return (
    <PageContainer>
      <ProTable
        actionRef={actionRef}
        formRef={formRef}
        postData={(data) => {
          return data.map((x) => ({
            dateRange: [x.startDate, x.endDate],
            ...x,
          }));
        }}
        rowKey="id"
        toolBarRender={() => [
          <Button
            key="2"
            type="primary"
            onClick={() => {
              history.push('/guaranteeTask/edit');
            }}
          >
            新增
          </Button>,
        ]}
        request={queryTable(getGuaranteeTaskList)}
        columns={columns}
      />
    </PageContainer>
  );
};

export default GuaranteeTaskList;
