import React, { useEffect } from 'react';
import {
  getFlowNodeExtendPage,
  operateFlowNodeExtendPage,
} from '@/services/platform/flow';
import { Form, Modal, Input, notification } from 'antd';

const ProcessConfig = props => {
  const {
    onSubumit,
    handleCancel,
    visible,
    msg,
    flowNodeId,
    modalType,
    title,
  } = props;
  const [form] = Form.useForm();
  useEffect(() => {
    form.resetFields();
    if (visible) {
      if (modalType === 'edit') {
        getFormData();
      } else {
        form.setFieldsValue({ FlowName: title });
      }
    }
  }, [visible]);
  // 获取表单回显
  const getFormData = () => {
    getFlowNodeExtendPage({ flowNodeExtendPageId: msg.ID }).then(res => {
      if (res.code === 0) {
        form.setFieldsValue({ ...res.data, FlowName: title });
      }
    });
  };
  // 提交表单
  const onFinish = () => {
    form.validateFields().then(validate => {
      if (validate) {
        let obj = {};
        console.log(modalType);
        if (modalType === 'add') {
          obj = validate;
        } else {
          obj = { ...validate, ID: msg.ID };
        }
        operateFlowNodeExtendPage(obj, flowNodeId)
          .then(res => {
            if (res.code === 0) {
              notification.success({
                message: '提示',
                duration: 3,
                description: '编辑成功',
              });
              onSubumit();
            } else {
              notification.error({
                message: '提示',
                duration: 3,
                description: res.msg,
              });
            }
          })
          .catch(() => {
            notification.error({
              message: '提示',
              duration: 3,
              description: '网络异常',
            });
          });
      }
    });
  };
  return (
    <Modal
      title="流程节点辅助视图配置"
      visible={visible}
      onOk={onFinish}
      onCancel={handleCancel}
      maskClosable={false}
      destroyOnClose
    >
      <Form
        form={form}
        labelCol={{ span: 5 }}
        wrapperCol={{ span: 18 }}
        initialValues={{ remember: true }}
      >
        <Form.Item label="流程节点" name="FlowName">
          <Input disabled />
        </Form.Item>
        <Form.Item label="前端标签" name="WebLabel">
          <Input placeholder="请输入前端标签" />
        </Form.Item>
        <Form.Item label="前端视图" name="WebPage">
          <Input placeholder="请输入前端视图" />
        </Form.Item>
        <Form.Item label="视图参数" name="WebParam">
          <Input placeholder="请输入视图参数" />
        </Form.Item>
        <Form.Item label="手持标签" name="MobileLabel">
          <Input placeholder="请输入手持标签" />
        </Form.Item>
        <Form.Item label="手持视图" name="MobilePage">
          <Input placeholder="请输入手持视图" />
        </Form.Item>
        <Form.Item label="手持参数" name="MobileParam">
          <Input placeholder="请输入手持参数" />
        </Form.Item>
      </Form>
    </Modal>
  );
};
export default ProcessConfig;
