<template>
  <div class="app-container">
     <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
       <el-form-item label="模块名称" prop="modelType">
         <el-select v-model="queryParams.modelType" placeholder="请选择模块名称" clearable>
           <el-option v-for="dict in dict.type.model_type" :key="dict.value" :label="dict.label" :value="dict.value"/>
         </el-select>
       </el-form-item>
       <el-form-item label="菜单名称" prop="menuName">
         <el-input v-model="queryParams.menuName" placeholder="请输入菜单名称" clearable @keyup.enter.native="handleQuery"/>
       </el-form-item>
       <el-form-item label="菜单状态" prop="menuStatus">
         <el-select v-model="queryParams.menuStatus" placeholder="请选择菜单状态" clearable>
           <el-option v-for="dict in dict.type.sys_normal_disable" :key="dict.value" :label="dict.label" :value="dict.value"/>
         </el-select>
       </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="openSelectUser" v-hasPermi="['system:role:add']">添加授权</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-circle-close" size="mini" :disabled="multiple" @click="cancelAuthUserAll" v-hasPermi="['system:role:remove']">批量取消授权</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-close" size="mini" @click="handleClose">关闭</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="menuappList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="模块名称" align="center" prop="modelType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.model_type" :value="scope.row.modelType"/>
        </template>
      </el-table-column>
      <el-table-column label="菜单名称" align="center" prop="menuName" />
      <el-table-column label="菜单路径" align="center" prop="menuUrl" />
      <el-table-column label="菜单图标" align="center" prop="menuIcon" />
      <el-table-column label="显示顺序" align="center" prop="menuNum" />
      <el-table-column label="菜单状态" align="center" prop="menuStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.menuStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-circle-close" @click="cancelAuthUser(scope.row)" v-hasPermi="['system:role:remove']">取消授权</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>
    <select-app ref="select" :roleId="queryParams.roleId" @ok="handleQuery" />
  </div>
</template>

<script>

import { allocatedAppList, authAppCancel, authAppCancelAll } from "@/api/system/role"
import selectApp from "./selectApp"

export default {
  name: "AuthApp",
  dicts: ['model_type', 'sys_normal_disable'],
  components: { selectApp },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中用户组
      appIds: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      menuappList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        roleId: undefined,
        modelType: null,
        menuName: null,
        menuStatus: null,
      }
    }
  },
  created() {
    const roleId = this.$route.params && this.$route.params.roleId
    if (roleId) {
      this.queryParams.roleId = roleId
      this.getList()
    }
  },
  methods: {
    /** 查询授权用户列表 */
    getList() {
      this.loading = true
      allocatedAppList(this.queryParams).then(response => {
          this.menuappList = response.rows
          this.total = response.total
          this.loading = false
        }
      )
    },
    // 返回按钮
    handleClose() {
      const obj = { path: "/system/role" }
      this.$tab.closeOpenPage(obj)
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.appIds = selection.map(item => item.id)
      this.multiple = !selection.length
    },
    /** 打开授权用户表弹窗 */
    openSelectUser() {
      this.$refs.select.show()
    },
    /** 取消授权按钮操作 */
    cancelAuthUser(row) {
      const roleId = this.queryParams.roleId
      this.$modal.confirm('确认要取消该授权吗？').then(function() {
        return authAppCancel({ appId: row.id, roleId: roleId })
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("取消授权成功")
      }).catch(() => {})
    },
    /** 批量取消授权按钮操作 */
    cancelAuthUserAll(row) {
      const roleId = this.queryParams.roleId
      const appIds = this.appIds.join(",")
      this.$modal.confirm('是否取消选中菜单授权数据项？').then(function() {
        return authAppCancelAll({ roleId: roleId, appIds: appIds })
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("取消授权成功")
      }).catch(() => {})
    }
  }
}
</script>
