<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.resourceName"
        :placeholder="$t('pages.sysResourcesManager.filter.resourceName')"
        style="width: 200px;"
        class="filter-item"
      />
      <el-select v-model="listQuery.method" :placeholder="$t('pages.sysResourcesManager.filter.method')" clearable style="width: 120px" class="filter-item">
        <el-option v-for="item in requestTypes" :key="item" :label="item" :value="item" />
      </el-select>
      <el-select v-model="listQuery.authType" :placeholder="$t('pages.sysResourcesManager.filter.authType')" clearable style="width: 120px" class="filter-item">
        <el-option v-for="(item,index) in grantTypes" :key="index" :label="$t(item.nameKey)" :value="item.value" />
      </el-select>
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        :disabled="listLoading"
        @click="handleSearch"
      >
        {{ $t('pages.sysResourcesManager.action.search') }}
      </el-button>
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-refresh-left"
        :loading="resetResourcesLoading"
        :disabled="resetResourcesLoading"
        @click="handleResetResouces"
      >
        {{ $t('pages.sysResourcesManager.action.refreshResources') }}
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      :element-loading-text="$t('pages.sysResourcesManager.loading')"
      fit
      highlight-current-row
    >
      <el-table-column prop="id" label="ID" align="center" width="95" />
      <el-table-column prop="resourceName" :label="$t('pages.sysResourcesManager.table.resourceName')" align="center" />
      <el-table-column prop="method" :label="$t('pages.sysResourcesManager.table.method')" align="center" />
      <el-table-column prop="mapping" :label="$t('pages.sysResourcesManager.table.mapping')" align="center" />
      <el-table-column prop="authType" :label="$t('pages.sysResourcesManager.table.authType')" align="center">
        <template slot-scope="scope">
          {{ getAuthTypeLabel(scope.row.authType) }}
        </template>
      </el-table-column>
      <el-table-column prop="perm" :label="$t('pages.sysResourcesManager.table.perm')" align="center" />
      <el-table-column prop="updateTime" :label="$t('pages.sysResourcesManager.table.updatedAt')" width="200" align="center">
        <template slot-scope="scope">
          {{ scope.row.createTime | dateFormat }}
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.cursor"
      :limit.sync="listQuery.limit"
      @pagination="renderData"
    />
  </div>
</template>

<script>
import { pageResources, resetResources } from '@/api/ops-system'
import Pagination from '@/components/Pagination'

export default {
  name: 'ResourcesManager',
  components: { Pagination },
  data() {
    return {
      resetResourcesLoading: false,
      requestTypes: ['GET', 'POST', 'PUT', 'DELETE'],
      grantTypes: [
        { value: '1', nameKey: 'pages.sysResourcesManager.authTypes.loginRequired' },
        { value: '2', nameKey: 'pages.sysResourcesManager.authTypes.noAuthRequired' },
        { value: '3', nameKey: 'pages.sysResourcesManager.authTypes.authRequired' }
      ],
      list: [],
      total: 0,
      listQuery: {
        cursor: 1,
        limit: 20,
        resourceName: '',
        authType: '',
        method: ''
      },
      listLoading: false
    }
  },
  created() {
    this.renderData(true)
  },
  methods: {
    renderData(isClean) {
      const that = this
      that.listLoading = true
      if (isClean === true) {
        that.listQuery.cursor = 1
      }
      pageResources(that.listQuery).then(res => {
        const { body } = res
        that.total = body.total || 0
        that.list = body.records
        that.listLoading = false
      })
    },
    handleSearch() {
      this.renderData(true)
    },
    getAuthTypeLabel(authType) {
      const matchedType = this.grantTypes.find(item => String(item.value) === String(authType))
      return matchedType ? this.$t(matchedType.nameKey) : authType
    },
    handleResetResouces() {
      const that = this
      that.resetResourcesLoading = true
      resetResources().then(res => {
        that.resetResourcesLoading = false
        that.$opsMessage.success()
      }).catch(er => {
        that.resetResourcesLoading = false
      })
    }
  }
}
</script>
