<template>
  <div class="authority">
    <!-- 应用下拉框 -->
    <div class="authority__select">
      <p>应用：</p>
      <Select v-model="cur_app_key" style="width:200px">
        <Option
          v-for="item in appList"
          :value="item.app_key"
          class="authority__ioption"
          :key="item.id"
        >
          {{ item.app_name }}（app_key={{ item.app_key }}）
        </Option>
      </Select>
    </div>
    <!-- 用户模块 -->
    <div class="authority__user">

      <!-- 模块一 -->
      <div class="authority__user__header">

          <div class="authority__user__header-box" v-for="(item,index) in dataApis" :key="index">
              <p><Icon type="ios-unlock" /> {{item.title}}（{{item.items.length}}）</p>
              <my-checkbox v-for="(item2) in item.items" :key="item2.service" :service="item2.service" :title="item2.title" :is_allow="item2.is_allow" :app_key="cur_app_key"></my-checkbox>
          </div>
      </div>
  </div>
  </div>
</template>

<script>
import { GetAppDropdownList, GetAllAppApis } from '@/api/port'
import { getToken } from '@/libs/util'
import MyCheckbox from './checkboxs'
export default {
  components: {
    MyCheckbox
  },
  data () {
    return {
      cur_app_key: '', // 循环出来的下拉框的value
      appList: [],
      dataApis: [] // 全部接口
    }
  },
  // 获取应用管理数据
  created () {
    this.queryAppList()
  },
  watch: {
    cur_app_key (val, oldVal) {
      this.changeApp(val)
      this.displayAppRights()
    }
  },
  methods: {
    // 查询列表
    queryAppList () {
      const data = new FormData()
      data.append('access_token', getToken())
      GetAppDropdownList(data).then(res => {
        this.appList = [{ app_key: '', app_name: '空应用', id: 0 }]
        res.data.list.forEach(el => {
          this.appList.push(el)
        })

        // 切换应用
        this.changeApp(this.appList[0].app_key)
        this.displayAppRights()
      })
    },
    // 获取全部接口及权限列表
    displayAppRights () {
      // 这里通知页面刷新，未搞懂
      this.dataApis = []

      const formdata = new FormData()
      formdata.append('access_token', getToken())
      formdata.append('app_key', this.cur_app_key)
      GetAllAppApis(formdata).then(res => {
        this.dataApis = res.data.apis
      })
    },
    changeApp (ak) {
      this.cur_app_key = ak !== undefined ? ak : ''
    }
  }
}
</script>

<style lang="scss" scoped>
.authority {
  &__select {
    display: flex;
    align-items: center;
    margin-bottom: 30px;
    &__ioption {
      display: flex;
      justify-content: space-around;
    }
    &__input {
      margin: 30px;
    }
  }
  &__user {
    &__header {
      margin-bottom: 50px;
      &-box {
        display: flex;
        flex-wrap: wrap;
        p{
          width: 100%;
          margin-left: 20px;
        }
      }
      p {
        margin-right: 20px;
        font-size: 16px;
      }
      &-all{
        display: flex;
        flex-wrap: wrap;
        &-body{
          width: 15%;
          background: #e7e7e7;
          margin-right: 5%;
          &-h{
            display: flex;
            flex-wrap: wrap;
            margin-bottom: 20px;
            h5{
              margin-right: 20px;
            }
          }
        }
      }
    }
  }
}
</style>
