<template>
  <el-dialog :title="!dataForm.id ? '新建' : '编辑'" :close-on-click-modal="false"
    :visible.sync="visible" :append-to-body="true" class="JNPF-dialog JNPF-dialog_center"
    lock-scroll width="600px">
    <el-form ref="dataForm" :model="dataForm" :rules="dataRule" v-loading="formLoading"
      label-width="80px">
      <el-form-item label="上级" prop="parentId">
        <JnpfTreeSelect v-model="dataForm.parentId" :options="treeData" :props="{
            value: 'id',
            label: 'name',
            children: 'children',
            disabled: function (data) {
              return data.disabled;
            },
          }" placeholder="选择项目上级" />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="输入名称" />
      </el-form-item>
      <el-form-item label="审核用户" prop="checkUserId">
        <JnpfUserSelect v-model="dataForm.checkUserId" placeholder="请选择审核用户" selectType="all"
          :style='{"width":"100%"}'>
        </JnpfUserSelect>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{ $t("common.cancelButton") }}</el-button>
      <el-button type="primary" :loading="btnLoading" @click="dataFormSubmit()">
        {{ $t("common.confirmButton") }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {
  getKnowledgeBase,
  getKnowledgeBaseInfo,
  createKnowledgeBase,
  updateKnowledgeBase,
} from "@/api/governmentCloud/knowledgeBase/knowledgeBase";

export default {
  data() {
    return {
      visible: false,
      treeDisabled: false,
      dataForm: {
        id: "",
        parentId: "",
        name: "",
        checkUserId: "",
      },
      formLoading: false,
      btnLoading: false,
      treeData: [],
      dataRule: {
        name: [
          { required: true, message: "请输入名称", trigger: "blur" },
          { max: 10, message: "名称最多为10个字符！", trigger: "blur" },
        ],
        checkUserId: [
          { required: true, message: "请选择审核用户", trigger: "change" },
        ],
      },
    };
  },
  methods: {
    init(id) {
      this.visible = true;
      this.dataForm.id = id || "";
      this.formLoading = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        getKnowledgeBase(id || 0).then((res) => {
          if (this.dataForm.id !== "") {
            let list = res.data;
            this.recursion(list, this.dataForm.id);
            this.treeData = list;
          } else {
            this.treeData = res.data || [];
          }
        });
        if (this.dataForm.id) {
          this.treeDisabled = true;
          getKnowledgeBaseInfo(this.dataForm.id).then((res) => {
            this.dataForm = res.data;
          });
        } else {
          this.treeDisabled = false;
        }
        this.formLoading = false;
      });
    },
    recursion(list, id) {
      for (let i = 0; i < list.length; i++) {
        if (list[i].id === id) {
          list.splice(i, 1);
          break;
        }
        if (list[i].children.length) {
          this.recursion(list[i].children, id);
        }
      }
    },
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.btnLoading = true;
          const formMethod = this.dataForm.id ? updateKnowledgeBase : createKnowledgeBase;
          if (this.dataForm.parentId == "") {
            this.dataForm.parentId = "0";
          }
          formMethod(this.dataForm)
            .then((res) => {
              this.$message({
                message: res.msg,
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.btnLoading = false
                  this.$emit('refreshDataList')
                }
              });
            })
            .catch(() => {
              this.btnLoading = false;
            });
        }
      });
    },
  },
};
</script>
