import React, { useState, useRef } from 'react';
import { Form, Input, Popconfirm, Button, Modal, message } from 'antd';
import { PlusOutlined, DownloadOutlined } from '@ant-design/icons';
import { getInvoicePersonList, saveInvoicePerson, deleteInvoicePerson } from '@/services/invoicePerson'
import PageTable from '@/components/PageTable';
import { UploadExcel } from '@/components/Upload';

const FormItem = Form.Item;
const formItemLayout = { labelCol: { span: 6 }, wrapperCol: { span: 14 } };
const modelUrl='https://h5.njyunzhi.com/assets/%E6%8A%A5%E9%94%80%E4%BA%BA%E5%91%98%E6%A8%A1%E6%9D%BF.xlsx'

export default (props) => {
  const { invoiceId } = props
  const [form] = Form.useForm();
  const [editModal, setEditModal] = useState(false);
  const [modelLoading, setModelLoading] = useState(false);
  const actionRef = useRef();
  //弹窗保存
  const Submit = (val) => {
    setModelLoading(true);
    saveInvoicePerson(val, invoiceId).then(res => {
      message.success('添加成功')
      setModelLoading(false)
      refrash()
      onCancel();
    }).catch((err) => {
      setModelLoading(false);
      message.error(err.message || err);
    });
  }
  // 弹窗关闭
  const onCancel = () => {
    setEditModal(false);
    form.resetFields();
  }

  const showModel = () => {
    setEditModal(true);
  }
  const refrash=()=>{
    console.log(666);
    actionRef.current.reload()
  }
  //删除人员
  const handleDelete = (id) => {
    deleteInvoicePerson(id).then(res => {
      message.success('删除成功')
      refrash()
    }).catch((err) => {
      message.error(err.message || err);
    });
  }
  const downloadExcel=()=>{
    const link=document.createElement('a')
    link.href=modelUrl
    link.click()
  }
  const actions = () => [
    <Button key="dowload" icon={<DownloadOutlined />} onClick={downloadExcel} >
      下载模板
    </Button>,
    <UploadExcel key='upload' id={invoiceId} refrash={refrash}/>,
    <Button key="add" icon={<PlusOutlined />} onClick={showModel}>
      新增报销人员
    </Button>,
  ];
  const columns = [
    {
      title: '所属公司',
      dataIndex: 'orgName',
      key: 'orgName',
    },
    {
      title: '报销人',
      dataIndex: 'name',
      key: 'name',
    },
    {
      title: '操作',
      valueType: 'option',
      render: (_, record) => [
        <Popconfirm
          key={2}
          title="您是否确认删除 ?"
          onConfirm={() => handleDelete(record.invoicePersonId)}
          okText="确定"
          cancelText="取消"
        >
          <Button style={{ padding: 0 }} type="link">
            删除
          </Button>
        </Popconfirm>,
      ],
    },
  ];
  return (
    <>
      <PageTable
        request={getInvoicePersonList}
        toolBarRender={actions}
        actionRef={actionRef}
        columns={columns}
        rowKey="invoicePersonId"
        options={false}
        search={false}
        pagination={{pageSize:500}}
        params={{ invoiceId }}
      />
      <Modal
        forceRender
        title='报销人员新增'
        visible={editModal}
        onCancel={onCancel}
        keyboard={false}
        maskClosable={false}
        destroyOnClose={true}
        footer={null}
      >
        <Form {...formItemLayout} onFinish={Submit} form={form}>
          <FormItem label="所属公司" name="orgName" rules={[{ required: true, message: '请输入' }]}>
            <Input placeholder="请输入" />
          </FormItem>
          <FormItem label="报销人" name="name" rules={[{ required: true, message: '请输入' }]}>
            <Input placeholder="请输入" />
          </FormItem>
          <FormItem label=" " colon={false}>
            <Button type="default" onClick={onCancel}>
              取消
            </Button>
            <Button
              type="primary"
              loading={modelLoading}
              htmlType="Submit"
              style={{ marginLeft: '4em' }}
            >
              确认
            </Button>
          </FormItem>
        </Form>
      </Modal>
    </>
  )
}