<template>
  <div>
    <el-dialog
      :close-on-click-modal="false"
      :title="title"
      :visible.sync="dialogVisible"
      width="600px"
      :before-close="handleClose"
    >
      <el-row :gutter="20">
        <el-col :span="11"
          ><div class="grid-content bg-purple">
            <el-tree
              :data="data"
              show-checkbox
              node-key="id"
              default-expand-all
              :props="defaultProps"
              ref="tree"
            >
            </el-tree></div
        ></el-col>
        <el-col :span="13"
          ><el-form ref="form" :rules="rules" :model="form" label-width="60px">
            <el-form-item label="名称:" prop="name">
              <el-input
                v-model.trim="form.name"
                placeholder="请输入名称"
              ></el-input>
            </el-form-item>
            <el-form-item label="描述:" prop="text">
              <el-input
                type="textarea"
                :rows="2"
                placeholder="请输入描述"
                v-model="form.text"
              >
              </el-input>
            </el-form-item> </el-form
        ></el-col>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="determine">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  components: {},
  props: {
    title: {
      type: String,
      default: undefined,
    },
    dialogVisible: {
      type: Boolean,
      default: false,
    },
    TemplateTree: {
      type: [Object, Array],
      default: false,
    },
  },
  data() {
    return {
      form: {
        name: "",
        text: "",
      },
      rules: {
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        text: [{ required: true, message: "描述不能为空", trigger: "blur" }],
      },
      data: [
        {
          id: 1,
          label: "一级 1",
          children: [
            {
              id: 4,
              label: "二级 1-1",
              children: [
                {
                  id: 9,
                  label: "三级 1-1-1",
                },
                {
                  id: 10,
                  label: "三级 1-1-2",
                },
              ],
            },
          ],
        },
        {
          id: 2,
          label: "一级 2",
          children: [
            {
              id: 5,
              label: "二级 2-1",
            },
            {
              id: 6,
              label: "二级 2-2",
            },
          ],
        },
        {
          id: 3,
          label: "一级 3",
          children: [
            {
              id: 7,
              label: "二级 3-1",
            },
            {
              id: 8,
              label: "二级 3-2",
            },
          ],
        },
      ],
      defaultProps: {
        children: "children",
        label: "label",
      },
    };
  },
  created() {},

  methods: {
    // 确定
    determine() {
      let list = [];
      this.$refs.tree.getCheckedNodes().forEach((item) => {
        if (item.children == undefined) {
          list.push(item.id);
        }
      });
      // console.log("🚀 ~ file: Template.vue ~ line 137 ~ determine ~ list", list)

      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.handleClose();
        } else {
        }
      });
    },
    // 关闭
    handleClose() {
      this.$emit("close");
    },
    // 树多选
    handleCheckChange(data, checked, indeterminate) {
      // 获取当前选中的所有节点ID
      // console.log(this.$refs.tree.getCheckedKeys());
    },
  },

  computed: {},

  watch: {},
};
</script>
<style lang="scss" scoped>
::v-deep.el-tree {
  height: 400px;
  overflow-y: auto;
}
::v-deep textarea {
  resize: none;
}
::v-deep .el-form-item--small.el-form-item {
  margin-bottom: 15px;
}
</style>
