import React, { useState, useRef, useImperativeHandle, forwardRef } from 'react';
import { Form, Input, Button, Modal, message, Popconfirm, Radio } from 'antd';
import { getInvoiceDetailList, updateInvoiceDetail, deleteInvoiceDetail } from '@/services/invoiceDetail'
import { getExport } from '@/services/invoiceFill'
import PageTable from '@/components/PageTable';

const FormItem = Form.Item;
const formItemLayout = { labelCol: { span: 6 }, wrapperCol: { span: 14 } };

export default forwardRef((props, ref) => {
  const { invoiceId } = props
  const [form] = Form.useForm();
  const [editModal, setEditModal] = useState(false);
  const [modelLoading, setModelLoading] = useState(false);
  const [invoiceDetail, setInvoiceDetail] = useState()
  const actionRef = useRef();

  //弹窗保存
  const Submit = (val) => {
    updateInvoiceDetail(invoiceId, { ...invoiceDetail, ...val }).then((res) => {
      message.success('修改成功')
      setModelLoading(false)
      refrash()
      onCancel()
    }).catch((err) => {
      setModelLoading(false);
      message.error(err.message || err);
    });
  }
  // 弹窗关闭
  const onCancel = () => {
    setEditModal(false);
    setInvoiceDetail()
    form.resetFields()
  }

  const showModel = (value) => {
    form.setFieldsValue({ ...value, name: value.itemName, mergeRemark: value.mergeRemark || 0 })
    setInvoiceDetail({ ...value, name: value.itemName, mergeRemark: value.mergeRemark || 0 })
    setEditModal(true);
  }
  const refrash = () => {
    actionRef.current.reload()
  }
  useImperativeHandle(ref, () => ({
    refrash
  }))
  const handleDelete = (val) => {
    deleteInvoiceDetail(val.invoiceId,val.invoiceItemId).then(res => {
      message.success('删除成功')
      refrash()
    }).catch((err) => {
      message.error(err.message || err);
    });
  }
  const columns = [
    {
      title: '姓名',
      dataIndex: 'personName',
      key: 'personName',
    },
    {
      title: '项目',
      dataIndex: 'itemName',
      key: 'itemName',
    },
    {
      title: '开票金额',
      dataIndex: 'charge',
      key: 'charge',
      search: false
    },
    {
      title: '操作',
      valueType: 'option',
      render: (_, record) => [
        <Button
          style={{ padding: 0 }}
          type="link"
          key={1}
          onClick={() => showModel(record)}
        >
          编辑
        </Button>,
        <Popconfirm
          key={2}
          title="您是否确认删除 ?"
          onConfirm={() => handleDelete(record)}
          okText="确定"
          cancelText="取消"
        >
          <Button style={{ padding: 0 }} type="link">
            删除
          </Button>
        </Popconfirm>,
      ],
    },
  ];
  return (
    <>
      <PageTable
        request={getInvoiceDetailList}
        actionRef={actionRef}
        columns={columns}
        rowKey={(record, index) => (record, index)}
        expfunc={getExport}
        invoiceId={invoiceId}
        options={false}
        search={false}
        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="姓名">
            {invoiceDetail?.personName}
          </FormItem>
          <FormItem label="项目" name="name">
            {invoiceDetail?.name}
          </FormItem>
          <FormItem label="开票金额" name="charge" 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>
    </>
  )
})