<template>
  <!-- 分配菜单 -->
  <el-dialog
    title="分配菜单"
    :visible.sync="dialogVisible"
    width="50%"
    top="10vh"
    append-to-body
  >
    <el-row>
      <el-col>
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          :disabled="distribution"
          @click="distMenuAuthBtns"
        >
          批量分配</el-button>
      </el-col>
    </el-row>
    <el-table
      ref="multipleTable"
      v-loading="loading"
      :data="addMenuAuthList"
      row-key="id"
      @selection-change="handelAddMenuAuth"
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
        :reserve-selection="true"
      />
      <el-table-column
        label="接口名称"
        prop="permissionUrl"
        :show-overflow-tooltip="true"
        align="center"
      />
      <el-table-column
        label="备注"
        prop="permissionName"
        :show-overflow-tooltip="true"
        align="center"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            type="primary"
            plain
            icon="el-icon-circle-plus"
            size="mini"
            :disabled="!distribution"
            @click="distMenuAuthBtn(scope.row)"
          >分配菜单
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="addTotal > 0"
      :total="addTotal"
      :page.sync="allAuthQuery.page"
      :limit.sync="allAuthQuery.limit"
      @pagination="getAllAuthApi"
    />
  </el-dialog>
</template>

<script>
import { getAllAuthApi, distMenuAuth } from '@/api/system/menu'
export default {
  data() {
    return {
      // 所有权限接口查询参数
      allAuthQuery: {
        page: 1,
        limit: 10
      },
      // 分配菜单权限参数
      menuAuthObj: {
        id: null,
        permissionList: []
      },
      // 添加菜单权限数据
      addMenuAuthList: [],
      // 弹出框表格总数
      addTotal: 0,
      // 添加菜单权限按钮禁用
      distribution: true,
      // 开关弹框
      dialogVisible: false,
      // 加载状态
      loading: true
    }
  },
  created() {
    if (this.$route.params.userId) {
      this.menuAuthObj.id = this.$route.params.userId
    }
  },
  methods: {
    /** 查看所有权限接口 */
    getAllAuthApi() {
      getAllAuthApi(this.allAuthQuery).then(res => {
        const data = res.data
        this.addMenuAuthList = data.permissionList
        this.addTotal = data.count
        this.loading = false
      })
    },
    /** 添加菜单权限多选框选中数据 */
    handelAddMenuAuth(selection) {
      let arr = []
      selection.forEach(item => {
        arr.push({ id: item.id })
      })
      arr = this._.uniqBy(arr, 'id')
      this.menuAuthObj.permissionList = arr
      this.distribution = !selection.length
    },
    /** 分配按钮操作 */
    distMenuAuthBtn(row) {
      this.$confirm('是否分配接口权限?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        cancelButtonClass: 'btn_custom_cancel',
        type: 'warning'
      }).then(() => {
        this.menuAuthObj.permissionList = []
        this.menuAuthObj.permissionList.push({ id: row.id })
        this.distMenuAuth()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消操作'
        })
      })
    },
    /** 批量分配按钮操作 */
    distMenuAuthBtns() {
      this.$confirm('是否批量分配接口权限?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        cancelButtonClass: 'btn_custom_cancel',
        type: 'warning'
      }).then(() => {
        this.distMenuAuth()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消操作'
        })
      })
    },
    /** 分配菜单权限接口 */
    distMenuAuth() {
      distMenuAuth(this.menuAuthObj).then(res => {
        if (res.code === 200) {
          this.$message({
            type: 'success',
            message: '已分配权限'
          })
          this.$emit('handelUpdate')
        }
      })
    },
    /** 显示弹框 */
    show() {
      this.allAuthQuery.page = 1
      this.menuAuthObj.permissionList = []
      this.getAllAuthApi()
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.multipleTable.clearSelection()
      })
    }
  }
}
</script>
