<template>
  <q-dialog v-model="persistent" persistent transition-show="scale" transition-hide="scale">
    <q-card style="width: 800px;max-width:900px;">
      <q-card-section>
        <div class="text-h6">{{objOption.RoleId==0?'新增角色信息':'修改角色信息'}}</div>
      </q-card-section>
      <q-card-section class="q-pt-none scroll" style="max-height: 70vh">
        <div class="text-caption q-mb-lg q-px-md text-grey-6">角色信息</div>
        <div class="row wrap">
          <q-input filled stack-label maxlength="50" :dense="false" v-model="objOption.RoleName" ref="RoleName"
            class="col-6 q-pr-lg q-pb-lg" label="角色名称" :rules="[val => !!val || '请填写角色名称']" />
          <q-input filled stack-label maxlength="100" :dense="false" v-model="objOption.RoleIntro" ref="RoleIntro"
            class="col-6 q-pr-lg q-pb-lg" label="角色介绍" />
          <div class="col-12 q-pr-lg q-pb-lg q-pt-lg">
            <q-toggle size="md" label="状态" color="primary" :false-value="1" :true-value="0" v-model="objOption.Status"
              title="注意：关闭后，角色将无法正常使用." />
          </div>
        </div>
        <div class="text-caption q-mb-lg q-px-md text-grey-6">权限设置</div>
        <div class="row wrap">
          <selectTree v-if="TreeMenuList&&TreeMenuList.length>0" :treeData='TreeMenuList' :defaultArray="returnString"
            nodeKey="MenuId" :multiple="true" labelKey="MenuName" childrenKey="SubList" tipText="选择菜单"
            @getChild="getChild" classStr="col-6 q-pr-lg q-pb-lg"></selectTree>
        </div>
      </q-card-section>
      <q-separator />
      <q-card-actions align="right" class="bg-white">
        <q-btn label="取消" color="dark" style="font-weight:400 !important" @click="closeSaveForm" />
        <q-btn label="立即提交" color="accent q-px-md" style="font-weight:400 !important" :loading="saveLoading"
          @click="saveRole" />
      </q-card-actions>
    </q-card>
  </q-dialog>
</template>

<script>
  import {
    queryTreeMenu,
    queryRoleInfo,
    saveRoleInfo,
  } from '../../api/system/index'
  import selectTree from '../common/select-tree'
  export default {
    props: {
      saveObj: {
        type: Object,
        default: null
      }
    },
    components: {
      selectTree
    },
    data() {
      return {
        persistent: true,
        objOption: {
          RoleId: 0, //角色编号
          RoleName: "", //角色名称
          RoleIntro: "", //角色介绍
          RoleAuth: "", //角色权限
          Status: 0, //状态(0-正常,1-禁用)},
        },
        returnString: [],
        optionTitle: "",
        //菜单列表
        TreeMenuList: [],
        saveLoading: false,
        //选中的节点
        checkMenuArray: [],
      }
    },
    mounted() {
      this.queryMenuList();
      this.initObj()
    },
    methods: {
      getChild(menuArray) {
        var tempStr = "";
        if (menuArray && menuArray != '') {
          tempStr = menuArray;
        }
        this.objOption.RoleAuth = tempStr;
      },
      //获取菜单列表
      queryMenuList() {
        this.TreeMenuList = [];
        var qMsg = {}
        queryTreeMenu(qMsg).then(res => {
          this.TreeMenuList = res.Data;
        })
      },
      //初始化表单
      initObj() {
        this.returnString = [];
        if (this.saveObj && this.saveObj.RoleId > 0) {
          queryRoleInfo({
            RoleId: this.saveObj.RoleId
          }).then(res => {
            this.objOption.RoleId = res.Data.RoleId;
            this.objOption.RoleName = res.Data.RoleName;
            this.objOption.RoleIntro = res.Data.RoleIntro;
            this.objOption.RoleAuth = res.Data.RoleAuth;
            this.objOption.Status = res.Data.Status;
            this.returnString = res.Data.CheckMenuList;
          })
          this.optionTitle = "修改角色信息"
        } else {
          this.optionTitle = "新增角色"
          this.objOption.RoleId = 0;
          this.objOption.RoleName = "";
          this.objOption.RoleIntro = "";
          this.objOption.RoleAuth = "";
          this.objOption.Status = 0;
        }
      },
      //关闭弹窗
      closeSaveForm() {
        this.$emit('close')
        this.persistent = false
      },
      //保存菜单
      saveRole() {
        this.saveLoading = true
        saveRoleInfo(this.objOption).then(res => {
          this.saveLoading = false
          this.$q.notify({
            icon: 'iconfont icon-chenggong',
            color: 'accent',
            timeout: 2000,
            message: '数据保存成功！',
            position: 'top'
          })
          this.$emit("success")
          this.closeSaveForm()
        }).catch(() => {
          this.saveLoading = false
        })
      }
    },
  }

</script>
