<style>
  .post-form .tranbox .el-transfer-panel {
    width: 300px;
  }

  .post-form .tranbox .el-button {
    padding: 7px 10px;
  }

</style>
<template>
  <q-dialog v-model="persistent" content-class="bg-grey-1" persistent transition-show="scale" transition-hide="scale">
    <q-card style="width: 800px;max-width:900px;" class="post-form">
      <q-card-section>
        <div class="text-h6">{{objOption.MenuId==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="20" :dense="false" v-model="objOption.PostName" ref="PostName"
            class="col-6 q-pr-lg q-pb-lg" label="岗位名称" :rules="[val => !!val || '请填写岗位名称']" />
          <selectTree v-if="DeptTreeList&&DeptTreeList.length>0" :treeData='DeptTreeList' :defaultArray="returnString"
            nodeKey="DeptId" :multiple="false" labelKey="DeptName" childrenKey="ChildList" tipText="所属部门"
            @getChild="getChild" classStr="col-6 q-pr-lg q-pb-lg" style="display:none;"></selectTree>
          <div class="col-6 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" />
            <div class="text-grey-6 text-caption">注意：关闭后，岗位将无法正常使用.</div>
          </div>
        </div>
        <div class="text-caption q-mb-lg q-px-md text-grey-6">绑定角色</div>
        <div class="row wrap tranbox">
          <template>
            <el-transfer filterable :filter-method="filterMethod" filter-placeholder="请输入关键字"
              :props="{key: 'RoleId',label: 'RoleName'}" v-model="value" :titles="['可选角色', '已选角色']" :data="data">
            </el-transfer>
          </template>
        </div>
      </q-card-section>
      <q-separator />
      <q-card-actions align="right" class="bg-white">
        <q-btn label="取消" flat color="grey-10" style="font-weight:400 !important" @click="closeSaveForm" />
        <q-btn label="立即提交" color="accent q-px-md" style="font-weight:400 !important" :loading="saveLoading"
          @click="savePost" />
      </q-card-actions>
    </q-card>
  </q-dialog>
</template>

<script>
  import selectTree from '../common/select-tree'
  import {
    getDeptTree,
  } from '../../api/system/dept'
  import {
    getPostInfo,
    savePostInfo
  }
  from '../../api/system/post'
  //角色相关
  import {
    queryRoleDropdown
  }
  from '../../api/system/index'
  export default {
    props: {
      saveObj: {
        type: Object,
        default: null
      }
    },
    components: {
      selectTree
    },
    data() {
      return {
        persistent: true,
        objOption: {
          PostId: 0, //岗位编号
          PostName: "", //岗位名称
         // RB_Dept_Id: 0, //所属部门
          Status: 0, //状态(0-正常,1-禁用),
          PostRoleList: [], //岗位角色列表
        },
        optionTitle: "",
        returnString: [],
        //岗位树形列表
        DeptTreeList: [],
        saveLoading: false,
        RoleList: [], //角色列表
        data: [],
        value: [],
        filterMethod(query, item) { //根据名称搜索
          return item.RoleName.indexOf(query) > -1;
        }
      }
    },
    created() {
      this.getRoleList();
      this.queryDeptTree();
    },
    mounted() {
      this.initObj()
    },
    methods: {
      //获取角色列表
      getRoleList() {
        queryRoleDropdown({}).then(res => {
          this.RoleList = res.Data;
          this.data = res.Data;
        })
      },
      getChild(menuArray) {
        var tempStr = "";
        if (menuArray && menuArray != '') {
          tempStr = menuArray;
        }
        this.objOption.RB_Dept_Id = tempStr;
      },
      //获取岗位树形结构
      queryDeptTree() {
        getDeptTree({}).then(res => {
          if (res.Code == 1) {
            this.DeptTreeList = res.Data;
          }
        })
      },
      //初始化表单
      initObj() {
        if (this.saveObj && this.saveObj.PostId > 0) {
          getPostInfo({
            PostId: this.saveObj.PostId
          }).then(res => {
            this.objOption.PostId = res.Data.PostId;
            //this.objOption.RB_Dept_Id = res.Data.RB_Dept_Id.toString();
            //this.returnString.push(this.objOption.RB_Dept_Id);
            this.objOption.PostName = res.Data.PostName;
            this.objOption.Status = res.Data.Status;
            if (res.Data.PostRoleList.length > 0) {
              res.Data.PostRoleList.forEach(x => {
                this.value.push(x.RoleId)
              })
            }
          })
          this.optionTitle = "修改岗位信息"
        } else {
          this.optionTitle = "新增岗位"
          this.objOption.PostId = 0;
          //this.objOption.RB_Dept_Id = 0;
          this.objOption.PostName = "";
          this.objOption.Status = 0;
        }
      },
      //关闭弹窗
      closeSaveForm() {
        this.$emit('close')
        this.persistent = false
      },
      //保存岗位
      savePost() {
        var tempRole = [];
        if (this.RoleList && this.RoleList.length > 0) {
          this.RoleList.forEach(x => {
            this.value.forEach(j => {
              if (x.RoleId == j) {
                tempRole.push(x)
              }
            })
          })
        }
        if (this.objOption.RB_Dept_Id) {
          if (this.objOption.RB_Dept_Id == "") {
            this.objOption.RB_Dept_Id = 0;
          }
        } else {
          this.objOption.RB_Dept_Id = 0;
        }
        this.objOption.PostRoleList = tempRole;
        this.saveLoading = true;
        savePostInfo(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>
