<template>
  <div class="obpm-common-tool">
    <div class="obpm-title">
      <div class="d-inline-block">
        <i class="el-icon-folder title-icon"></i>
        <div class="title-name d-inline-block ml-2">{{$t("roles.title")}}</div>
      </div>
      <div class="bar d-inline-block float-right">
        <el-button type="primary" size="small" @click="rolePermissionBtn(false)">{{$t("btns.resources_authorize")}}</el-button>
        <el-button type="primary" size="small" @click="createRoles">{{$t("btns.new")}}</el-button>
        <el-button type="danger" plain size="small" @click="showDeleteConfirm" :loading="btnLoading">{{$t("btns.delete")}}</el-button>
      </div>
    </div>
    <div class="obpm-content mt-2">
      <div class="search">
        <el-form ref="form" :inline="true" :model="params" class="demo-form-inline" size="mini" @submit.native.prevent>
          <el-form-item>
            <el-input v-model.trim="params.name"
              :placeholder="$t('placeholder.name')"
              @keyup.enter.native="getRoles"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="getRoles" type="primary" plain>{{$t("btns.query")}}</el-button>
            <el-button @click="resetName">{{$t("btns.reset")}}</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="obpm-content-table">
        <template>
          <el-table
            ref="tableList"
            :data="data.datas"
            @selection-change="handleSelectionChange"
            @row-click="onRowClick"
            :height="tableHeight"
            style="width: 100%"
            size='small'>
            <el-table-column
              type="selection"
              width="55">
            </el-table-column>
            <el-table-column
              prop="name"
              :label="$t('roles.name')">
            </el-table-column>
            <el-table-column :label="$t('roles.op')">
              <template slot-scope="scope">
                <el-button @click.stop="rolePermissionBtn(scope.row)" type="primary" plain size="mini" >{{$t('btns.authorize')}}</el-button>
              </template>
            </el-table-column>
          </el-table>
        </template>
        <el-pagination
          class="text-right pt-1"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="data.pageNo"
          :page-sizes="[10, 25, 50, 100]"
          :page-size="10"
          background
          layout="total, prev, pager, next, sizes"
          :total="data.rowCount">
        </el-pagination>
      </div>
    </div>
    <rolePermission :visible.sync="rolePermissionVisible" v-if="rolePermissionVisible"  :applicationId="params.appId" v-bind:roleId="roleId" :isBatch="isBatch"/>

  </div>
</template>
<script>
import { getRoles, deleteRoles } from "@/service/CommontoolsAPI.js";
import rolePermission from "@/components/Common/rolePermission.vue";
import {delMsg} from '@/utils/elementui'

export default {
  name: 'roleslist',
  components: { rolePermission },

  data(){
    return{
      params: {
        appId: "",
        id: "",               //角色id
        name: "",             //角色名称
        roleNo: "",           //编号
        status: "",           //激活状态
        defaultRole: "",       //系统默认角色
        pageNo: 1,
        linesPerPage: 10
      },
      data: {},
      multipleSelectionId: [],
      multipleSelectionArray: [],
      tableHeight: 100,
      isBatch:false,
      roleId:"",
      rolePermissionVisible:false,
      btnLoading: false, // 按钮的加载
    }
  },

  mounted(){
    this.getScollerHeight();
    if(this.$route.query.edit){
      let appId = sessionStorage.getItem("appId");
      let params = {
        ...this.params,
        appId: appId,
      }
      this.params = params;
      this.getRoles();
    }
  },

  methods:{
    //角色单个授权
    rolePermissionBtn(row){
      if(row){
        this.isBatch=false;
        this.roleId=row.id;
      }else{
        this.roleId="";
        this.isBatch=true;
      }
      this.rolePermissionVisible=true;
    },
    /**
     * 动态计算页面table高度
     */
    getScollerHeight() {
      setTimeout(() => {
        let clientHeight = document.documentElement.clientHeight || document.body.clientHeight;
        //60 头部， 32 面包屑， 41 第一层页签， 40 第二层页签， 50 列表标题， 71 查询， 20 父组件外边距, 40分页
        this.tableHeight = (clientHeight - 60 - 32 - 41 - 40 - 50 - 71 - 20 - 40) + 'px'
      }, 100)
    },
    
    /**
     * 获取角色列表
     */
    async getRoles(){
      let response = await getRoles(this.params);
      if (response.data.errcode == 0) {
        let data = response.data.data;
        this.data = data;
      }
    },

    /**
     * 重置查询名称值
     */
    resetName(){
      let params = {
        ...this.params,
        name: "",
      }
      this.params = params;
      this.getRoles();
    },

    /**
     * 勾选行时响应
     */
    handleSelectionChange(val) {
      let multipleSelectionId = [];
      if(val.length){
        val.map((e) => {
          multipleSelectionId.push(e.id)
        })
      }
      this.multipleSelectionArray = val;
      this.multipleSelectionId = multipleSelectionId;
    },

    /**
     * 新建角色
     */
    createRoles(){
      this.$store.dispatch("rightHelpPageParams", { title: this.$t('placeholder.role_info'), url: "application/application_info_generalTools_role_info_help.html" });
      this.$router.push({path: '/home/softwaresdetails/commontool/roles', query:{edit: false}});
    },

    /**
     * 删除角色
     */
    async showDeleteConfirm(){
      let params = this.params;
      let multipleSelectionId = this.multipleSelectionId;
      if(multipleSelectionId.length > 0){
        try{
          await delMsg(this.$t('msg.delConfirm'), this.$t('msg.tip'))
          this.btnLoading = true
          let response = await deleteRoles(multipleSelectionId, params.appId);
          if (response.data.errcode == 0) {
            this.btnLoading = false
            this.getRoles();
            this.$message.success(this.$t('msg.del_ss'))
          }
        }catch(e){
          console.log(e)
        }
      }else{
        this.$message.error(this.$t('msg.check_data'))
      }
    },

    /**
     * 选中数据源回显
     */
    onRowClick(row){
      this.$store.dispatch("rightHelpPageParams", { title: this.$t('placeholder.role_info'), url: "application/application_info_generalTools_role_info_help.html" });
      let params = {
        ...this.params,
        id: row.id,               //角色id
        name: row.name,             //角色名称
        roleNo: row.roleNo,           //编号
        status: row.status,           //激活状态
        defaultRole: row.defaultRole,       //系统默认角色
      }
      this.$router.push({path: '/home/softwaresdetails/commontool/roles', query:{edit: true, params: params}});
    },

    /**
     * 当前页
     */
    handleCurrentChange(val) {
      let params = {
        ...this.params,
        pageNo: val
      }
      this.params = params;
      this.getRoles()
    },

    /**
     * 切换分页，每页显示多少条
     */
    handleSizeChange(val){
      let params = {
        ...this.params,
        linesPerPage: val
      }
      this.params = params;
      this.getRoles()
    },

  },
  watch: {
    "$store.state.rightHelpPageParams": {
      handler() {
        if (this.$store.state.rightHelpFrame) {
          this.$store.state.rightHelpFrame.contentWindow.showHelpContentHtml(this.$store.state.rightHelpPageParams.title, this.$store.state.rightHelpPageParams.url);
        }
      },
      deep: true,
    },
  },
}
</script>