<template>
  <el-dialog
    :visible="dialogVisible"
    width="30%"
    style="height: 98%"
    :before-close="handleClose"
    top="5vh"
    lock-scroll
  >
    <div class="title" slot="title">
      <div class="divider"></div>
      <div class="label">修改角色数据权限</div>
    </div>
    <div class="dialog-content">
      <el-form :model="dialogForm" class="basic-info">
        <el-form-item label="数据范围" :label-width="formLabelWidth">
          <el-select
            v-model="dataScopeData"
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="选择自定义部门"
          :label-width="formLabelWidth"
          v-if="dataScopeData == 2"
        >
          <el-cascader
            style="width: 100%"
            v-model="depts"
            :options="orgTreeData"
            :props="optionProps"
            placeholder="请选择部门"
          >
          </el-cascader>
        </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-circle-check" style="margin-left: 5px"></i
      ></el-button>
    </div>
  </el-dialog>
</template> 

<script>
import { bindDept } from "@/api/user";
export default {
  name: "DataEditDialog",
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    id: {
      type: String,
    },
    dataScope: {
      type: Number,
    },
  },
  watch: {
    visible: {
      handler: function (value) {
        this.dialogVisible = value;
        this.dataScopeData = this.dataScope;
      },
      deep: true,
      immediate: true,
    },
    dataScopeData(value) {
      // 除了自定义部门之外，其余的自定义部门都给清空
      if (value != 2) {
        this.depts = [];
      }
    },
  },
  data() {
    return {
      dialogForm: {},
      formLabelWidth: "120px",
      dialogVisible: false,
      options: [
        { label: "所有", value: 1 },
        // { label: "自定义部门", value: 2 },
        { label: "本部门及以下部门", value: 3 },
        { label: "本部门", value: 4 },
        { label: "自己", value: 5 },
      ],
      orgTreeData: [],
      optionProps: {
        multiple: true,
        value: "id",
        label: "name",
        children: "children",
        checkStrictly: true,
      },
      depts: [], //自定义部门
      dataScopeData: null,
    };
  },

  created() {
    this.$store.dispatch("org/getMuseumTreeData", false).then((res) => {
      // this.orgTreeData = res[0].children; //去掉根节点的文旅厅
      this.orgTreeData =res
    });
  },
  methods: {
    // 取消编辑
    cancelForm() {
      this.$emit("handleCloseDataEdit");
    },

    async handleSubmit() {
      console.log(this.id);
      //   return;
      //   let checkedIds =this.$refs.tree.getCheckedKeys()
      let depts = [];
      this.depts.map((item) => {
        depts = [...item, ...depts];
      });
      const params = {
        id: this.id,
        dataScope: this.dataScopeData,
        depts,
      };
      //   return;
      let res = await bindDept(params);
      if (res.code == 0) {
        this.$message.success("保存成功！");
        this.reload();
      }
    },
    reload() {
      this.$emit("refresh");
      this.$emit("handleCloseDataEdit");
      this.dataScopeData = null;
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
          this.$emit("handleCloseDataEdit");
        })
        .catch((_) => {});
    },
  },
};
</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>