<template>
  <el-dialog
    :visible="dialogVisible"
    width="40%"
    style="height: 98%"
    :before-close="handleClose"
    top="5vh"
    lock-scroll
  >
    <div class="title" slot="title">
      <div class="divider"></div>
      <div class="label">{{ title }}</div>
    </div>
    <div class="dialog-content">
      <el-form :model="dialogForm" class="basic-info">
        <el-form-item label="角色名称" :label-width="formLabelWidth">
          <el-input
            v-model="dialogForm.name"
            autocomplete="off"
            placeholder="请输入角色名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch v-model="status"> </el-switch>
        </el-form-item>
        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input
            type="textarea"
            placeholder="请输入备注"
            v-model="dialogForm.remark"
            maxlength="600"
            show-word-limit
          >
          </el-input>
        </el-form-item>
        <el-form-item label="权限" :label-width="formLabelWidth">
          <el-tree
            ref="tree"
            :data="dialogForm.permissionRespNodes"
            show-checkbox
            :props="props"
            node-key="id"
            @check-change="handleCheckChange"
          >
          </el-tree>
        </el-form-item>
      </el-form>
    </div>
    <div class="dialog-footer">
      <el-button @click="cancelForm">取 消</el-button>
      <el-button type="primary" @click="handleSubmit"
        >确定<i class="el-icon-s-promotion" style="margin-left: 5px"></i
      ></el-button>
    </div>
  </el-dialog>
</template> 

<script>
import {
  addRole,
  upadateRole,
  getPermissionTreeAll,
  getPermissionById,
} from "@/api/user";
export default {
  name: "InfoEditDialog",
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    form: {
      type: Object,
      default: () => ({}),
    },
  },
  computed: {
    title() {
      if (this.dialogForm.id) {
        return "修改角色信息";
      } else {
        return "新建角色";
      }
    },
  },
  watch: {
    form: {
      handler: function (value) {
        debugger
        let that = this;
        that.dialogForm = JSON.parse(JSON.stringify(value));
        // 编辑状态
        if (that.dialogForm.id) {
          // 回填状态
          that.status = Boolean(Number(that.dialogForm.status));
          that.setPer();
          // debugger
        } else {
          that.getDefaultTree();
        }
      },
      immediate: true,
      deep: true,
    },
    visible: {
      handler: function (value) {
        this.dialogVisible = value;
      },
      deep: true,
      immediate: true,
    },
  },
  data() {
    return {
      dialogForm: {},
      formLabelWidth: "100px",
      status: false,
      files: [], //文献文件
      dialogVisible: false,
      // permissionTree: [],
      props: {
        value: "id",
        label: "title",
        children: "children",
        checkStrictly: true,
      },
    };
  },
  // async created() {

  //   console.log(this.permissionTree);
  //   // console.log(res);
  // },
  methods: {
    handlePreview({ type, file }) {
      console.log(type, file);
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    // 取消编辑
    cancelForm() {
      this.$emit("handleClose");
    },

    async handleSubmit() {
      const { name, remark } = this.dialogForm;
      let status = this.status ? 1 : 0;
      let permissions = this.$refs.tree.getCheckedKeys();
      const params = {
        name,
        status,
        permissions,
        remark,
      };
      console.log(params);

      if (this.dialogForm.id) {
        params.id = this.dialogForm.id;
        let res = await upadateRole(params);
        if (res.code == 0) {
          this.$message.success("修改成功！");
          this.reload();
        }
      } else {
        let res = await addRole(params);
        if (res.code == 0) {
          this.$message.success("添加成功！");
          this.reload();
        }
      }
    },
    reload() {
      this.$emit("refresh");
      this.$emit("handleClose");
      this.files = [];
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
          this.$emit("handleClose");
          this.files = [];
        })
        .catch((_) => {});
    },

    handleCheckChange(data, checked, indeterminate) {
      console.log(data, checked, indeterminate);
    },

    setPer() {
      let checkedIds = [];
      getCheckedIds(this.dialogForm.permissionRespNodes);
      function getCheckedIds(arr) {
        if (arr.length > 0) {
          arr.map((item) => {
            if (item.checked) {
              checkedIds.push(item.id);
            } else if (item.children) {
              getCheckedIds(item.children);
            }
          });
        }
      }
      setTimeout(() => {
        if (checkedIds.length > 0) {
          this.$refs["tree"].setCheckedKeys(checkedIds);
        }
      }, 100);
    },

    async getDefaultTree() {
      let res = await getPermissionTreeAll();
      // this.permissionTree = res.data;
      this.$set(this.dialogForm, "permissionRespNodes", res.data);
      // debugger
    },
  },
};
</script>

<style lang='scss' scoped>
.title {
  display: flex;
  margin-bottom: 16px;
  .divider {
    width: 8px;
    border-left: 4px solid #409eff;
    margin-right: 8px;
  }
  .label {
    font-weight: bold;
  }
}
.dialog-content {
  padding: 0 32px;
  display: flex;
  .basic-info {
    flex: 1;
    margin-right: 48px;
  }
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
}
</style>