
import moment from 'moment';
import React, { useRef, useEffect, useState } from 'react'

import { Button, Modal, Popconfirm, message } from 'antd';
import { PlusOutlined } from '@ant-design/icons';
import { PageHeaderWrapper } from '@ant-design/pro-layout';
import { getInvoiceFillList, deleteInvoiceFill } from '@/services/invoiceFill'
import PageTable from '@/components/PageTable'
import { history } from 'umi';

const formatterTime = (val) => {
  return val ? moment(val).format('YYYY-MM-DD HH:mm') : '';
};

export default (props) => {
  const actionRef = useRef();

  const handleClick = () => {
    history.push('/invoiceFill/edit.jsx');
  }
  const handleDelete = (id) => {
    deleteInvoiceFill(id).then((res) => {
      message.success('删除成功')
      actionRef.current.reload();
    }).catch((err) => {
      console.log(err.message)
    });
  }
  const action = () => [
    <Button key="add" type="primary" icon={<PlusOutlined />} onClick={handleClick}>
      新增班级
    </Button>
  ]

  const columns = [
    {
      title: '班级名称',
      dataIndex: 'name',
      key: 'name',
      search: true,
    },
    {
      title: '创建时间',
      dataIndex: 'createDate',
      key: 'createDate',
      search: false,
      render: (t) => formatterTime(t)
    },
    {
      title: '状态',
      dataIndex: 'status',
      key: 'status',
      render: (_, record) => {
        return record.status === 0 ? '未发布' :record.status === 1 ? '进行中' : '已截止';
      },
      valueType: 'select',
      valueEnum: {
        0: { text: '未发布' },
        1: { text: '进行中' },
        2: { text: '已截止' },
      },
    },
    {
      title: '操作',
      valueType: 'option',
      key: 'option',
      ellipsis: true,
      render: (_, record) => [
        <Button type="link" key={1} onClick={() => history.push(`/invoiceFill/edit.jsx?id=${record.invoiceId}`)} style={{ padding: 0 }}>
          编辑
        </Button>,
        <Popconfirm
          key={2}
          title="您是否确认删除 ?"
          onConfirm={() => handleDelete(record.invoiceId)}
          okText="确定"
          cancelText="取消"
        >
          <Button style={{ padding: 0 }} type="link">
            删除
          </Button>
        </Popconfirm>
      ],
    },
  ]


  return (
    <PageHeaderWrapper>
      <PageTable
        columns={columns}
        request={getInvoiceFillList}
        options={false}
        actionRef={actionRef}
        toolBarRender={action}
        rowKey="invoiceId"
      />
    </PageHeaderWrapper>
  )
}
