/*
 * @Author: yuananting
 * @Date: 2021-02-22 17:51:28
 * @LastEditors: yuananting
 * @LastEditTime: 2021-03-04 11:47:20
 * @Description: 助学工具-题库-题库新建或编辑题库分类模态框
 * @Copyrigh: © 2020 杭州杰竞科技有限公司 版权所有
 */
import React, { Component } from "react";
import { Modal, Form, Input, message } from "antd";

class NewEditQuestionBankCategory extends Component {
  formRef = React.createRef();
  constructor(props) {
    super(props);
    this.state = {
      categoryName: null, // 分类名称
    };
  }

  componentDidMount() {
    // this.getSameLevelNodes(this.props.treeData);
  }

  // 确定新增或编辑
  confirmOperate = async () => {
    try {
      await this.formRef.current.validateFields();
      // TODO save
      this.props.close();
    } catch (e) {
      console.log(e);
    }
  };

  getEqualLevelNodes = (data, parentId) => {
    let nodes = [];
    data.forEach((item) => {
      if (item.parentId === parentId) {
        nodes.push(item);
      }
      if (item.children) {
        nodes.push(...this.getEqualLevelNodes(item.children, parentId))
      }
    })
    return nodes;
  }

  getChildLevelNodes = (data, id) => {
    let nodes = [];
    data.forEach((item) => {
      if(item.id === id && item.children) {
        nodes.push(...item.children);
      }
      if (item.children) {
        nodes.push(...this.getChildLevelNodes(item.children, id))
      }
    })
    return nodes;
  }

  getSameLevelNodes = (data, type) => {
    let sameLevelNodes = [];
    const { id, parentId } = this.props;
    if (type === "equal") {
      sameLevelNodes = this.getEqualLevelNodes(data, parentId);
    } else {
      sameLevelNodes = this.getChildLevelNodes(data, id);
    }
    return sameLevelNodes;
  };

  // 查询是否重名
  checkExist = (sameLevelNodes, categoryName) => {
    var result = null;
    sameLevelNodes.forEach((item) => {
      if (result != null) {
        return result;
      }
      if (item.categoryName === categoryName) {
        result = item;
      }
    });
    return result;
  };

  render() {
    const { title, label, treeData, addLevelType } = this.props;
    const { categoryName } = this.state;
    const _that = this;
    return (
      <Modal
        visible={true}
        title={title}
        onOk={this.confirmOperate}
        onCancel={() => this.props.close()}
      >
        <Form ref={this.formRef}>
          <Form.Item
            name="categoryName"
            label={label}
            required
            rules={[
              {
                required: true,
                message: `请输入${label}`,
              },
              ({ getFieldValue }) => ({
                validator(_, value) {
                  let sameLevelNodes = _that.getSameLevelNodes(treeData, addLevelType);
                  if (_that.checkExist(sameLevelNodes, value)) {
                    return Promise.reject("此分类名称已存在");
                  } else {
                    return Promise.resolve();
                  }
                },
              }),
            ]}
          >
            <Input
              value={categoryName}
              placeholder={`请输入${title}，最多10个字`}
              maxLength={10}
              onChange={(e) => {
                this.setState({
                  categoryName: e.target.value,
                });
              }}
            />
          </Form.Item>
        </Form>
      </Modal>
    );
  }
}
export default NewEditQuestionBankCategory;
