import React, { useState, useEffect } from 'react';
import { Form, Input, Button, Row, Col } from 'antd';
import classnames from 'classnames';
import styles from './addForm.less';
import PicturesWall from '@/components/Upload/index';
import CheckList from './checkBox';

const { Item } = Form;
const EditForm = props => {
  const { submitCallback, nodeType, info, valueCallback = () => {} } = props;
  const [form] = Form.useForm();
  const [otherForm] = Form.useForm();
  const layout = {
    layout: 'horizontal',
    labelCol: { span: 2, offset: 0 },
    wrapperCol: { span: 20 },
  };
  // 回显表单
  useEffect(() => {
    form.resetFields();
    otherForm.resetFields();
  }, [info]);
  useEffect(() => {
    if (JSON.stringify(info) != '{}') {
      let data = info.data;
      if (nodeType === 1 || nodeType === 2) {
        let arr = Object.keys(form.getFieldsValue());
        let obj = {};
        arr.map(i => {
          obj[i] = data[i];
        });
        form.setFieldsValue({ ...obj, shortName: data.menuShortName });
      }
      if (nodeType === 3 || nodeType === 4) {
        let arr = Object.keys(otherForm.getFieldsValue());
        let obj = {};
        arr.map(i => {
          obj[i] = data[i];
        });
        otherForm.setFieldsValue({ ...obj, shortName: data.menuShortName });
      }
    }
  }, [info]);

  const submit = () => {
    if (nodeType === 1 || nodeType === 2) {
      let obj = form.getFieldsValue();
      console.log(obj, '1,2');
      submitCallback(obj);
    }
    if (nodeType === 3 || nodeType === 4) {
      let obj = otherForm.getFieldsValue();
      console.log(obj, '3,4');
      submitCallback(obj);
    }
  };
  const onFinish = () => {
    submit();
  };
  return (
    <div className={classnames({ [styles.divbox]: true })}>
      {(nodeType === 1 || nodeType === 2) && (
        <Form form={form} name="editGroup" {...layout} onFinish={onFinish}>
          <Item
            label="菜单组名称"
            name="menuName"
            rules={[
              {
                required: true,
                message: '请输入菜单组名称',
              },
            ]}
          >
            <Input placeholder="请输入菜单组名称" />
          </Item>
          <Item label="菜单组别名" name="shortName">
            <Input placeholder="请输入菜单组别名" />
          </Item>
          {nodeType === 1 && (
            <Item
              label="在线图标"
              name="imageUrl"
              rules={[
                {
                  required: true,
                  message: '请选择在线图标',
                },
              ]}
            >
              <PicturesWall picType="androidMenu" />
            </Item>
          )}
          {nodeType === 1 && (
            <Item
              label="离线图标"
              name="offlineImgUrl"
              rules={[
                {
                  required: true,
                  message: '请选择离线图标',
                },
              ]}
            >
              <PicturesWall picType="androidMenu" />
            </Item>
          )}

          {nodeType === 2 && (
            <Item
              label="菜单组图标"
              name="imageUrl"
              rules={[
                {
                  required: true,
                  message: '请选择菜单组图标',
                },
              ]}
            >
              <PicturesWall picType="androidMenu" />
            </Item>
          )}
          <Item label="功能参数" name="funParam">
            <Input />
          </Item>
          <Item wrapperCol={{ offset: 10 }} style={{ marginTop: '40px' }}>
            <Button type="primary" htmlType="submit">
              提交
            </Button>
          </Item>
        </Form>
      )}

      {(nodeType === 3 || nodeType === 4) && (
        <Form form={otherForm} name="editMenu" {...layout} onFinish={onFinish}>
          <Item
            label="菜单名称"
            name="menuName"
            rules={[
              {
                required: true,
                message: '请输入菜单名称',
              },
            ]}
          >
            <Input />
          </Item>
          <Item label="菜单别名" name="shortName">
            <Input />
          </Item>
          {nodeType === 3 && (
            <Item
              label="在线图标"
              name="imageUrl"
              rules={[
                {
                  required: true,
                  message: '请选择在线图标',
                },
              ]}
            >
              <PicturesWall picType="androidMenu" />
            </Item>
          )}
          {nodeType === 3 && (
            <Item
              label="离线图标"
              name="offlineImgUrl"
              rules={[
                {
                  required: true,
                  message: '请选择离线图标',
                },
              ]}
            >
              <PicturesWall picType="androidMenu" />
            </Item>
          )}
          {nodeType === 4 && (
            <Item
              label="菜单图标"
              name="imageUrl"
              rules={[
                {
                  required: true,
                  message: '请选择菜单图标',
                },
              ]}
            >
              <PicturesWall picType="androidMenu" />
            </Item>
          )}
          <Item
            label="功能路径"
            name="pageUrl"
            rules={[
              {
                required: true,
                message: '请输入功能路径',
              },
            ]}
          >
            <Input />
          </Item>
          <Item label="功能参数" name="funParam">
            <Input />
          </Item>
          <CheckList
            info={info.data ? info.data : {}}
            nodeType={nodeType}
            valueCallback={valueCallback}
          />
          <Item wrapperCol={{ offset: 10 }}>
            <Button type="primary" htmlType="submit">
              提交
            </Button>
          </Item>
        </Form>
      )}
    </div>
  );
};
export default EditForm;
