<template>
  <div>
    <div>
      <el-form :inline="true" class="orderform" size="mini">
        <el-row>
          <el-form-item>
            <el-input v-model="tag" style="width: 250px" placeholder="请输入应用名称" class="search_tag" clearable/>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="btn_search()"><i class="el-icon-search"/> 搜索</el-button>
            <el-button @click="btn_clear()"><i class="el-icon-remove-outline"/> 重置</el-button>
            <!--el-button type="primary" @click="addFormVisible = true"><i class="el-icon-plus"/> 添加</el-button-->
          </el-form-item>
        </el-row>
      </el-form>
      <el-table
        ref="multipleTable"
        :data="tableData"
        :highlight-current-row="true"
        :header-cell-style="{background:'#E0E0E0',color:'#666666'}"
        border>
        <el-table-column
          prop="id"
          label="AppId"/>
        <el-table-column
          prop="name"
          label="应用名称"/>
        <el-table-column
          prop="user.name"
          label="开发者"/>
        <el-table-column
          prop="apiKey"
          label="API Key"/>
        <el-table-column
          prop="secretKey"
          label="Secret Key"/>
        <el-table-column
          :formatter="formatStatus"
          prop="status"
          label="应用状态"/>
        <el-table-column
          prop="createTime"
          label="创建时间"/>
        <el-table-column
          fixed="right"
          label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click="handleViewAuth(scope.row)">查看接口权限</el-button>
            <el-button type="text" @click="handleDisable(scope.row)">审核</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="mydiv">
        <el-pagination
          :current-page.sync="pageNo"
          :page-sizes="[10, 50, 100, 300,1000]"
          :page-size="pageSize"
          :total="totalDataNumber"
          layout="sizes,total, prev, pager, next"
          class="table-pagination"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"/>
      </div>

    </div>

    <el-dialog
      :visible.sync="disableFormVisible"
      title="审核提示"
      width="30%">
      <span>审核应用</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="submitDisable(0)">拒 绝</el-button>
        <el-button type="primary" @click="submitDisable(1)">通 过</el-button>
      </span>
    </el-dialog>

    <el-dialog
      :visible.sync="showAppApis">
      <p>应用接口权限列表:</p>
      <el-table
        :data="authData"
        :highlight-current-row="true"
        :header-cell-style="{background:'#E0E0E0',color:'#666666'}"
        border>
        <el-table-column
          prop="api.name"
          label="API"/>
        <el-table-column
          :formatter="formatStatus"
          prop="status"
          label="状态"/>
        <el-table-column
          prop="api.url"
          label="请求地址"/>
        <el-table-column
          prop="callLimit"
          label="调用限制量"/>
        <el-table-column
          prop="callLimit"
          label="QPS限制"/>
        <el-table-column
          label="申请状态">
          <template slot-scope="scope">
            <div v-if="scope.row.qpsApply.applyStatus === 2">
              <span>审核中</span>
            </div>
            <div v-else-if="scope.row.qpsApply.applyStatus === 1">
              <span>更高配额 {{ scope.row.qpsApply.maxQps }} </span>
            </div>
            <div v-else-if="scope.row.qpsApply.applyStatus === 3">
              <span>审核拒绝 </span>
            </div>
            <div v-else>
              <span>默认配额</span>
            </div>

          </template>
        </el-table-column>
      </el-table>
      <div>
        <el-pagination
          :current-page.sync="pageAuthNo"
          :page-sizes="[10, 50, 100, 300,1000]"
          :page-size="pageAuthSize"
          :total="totalAuthDataNumber"
          layout="sizes,total, prev, pager, next"
          class="table-pagination"
          @size-change="handleAuthSizeChange"
          @current-change="handleAuthCurrentChange"/>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import axios from 'axios'

export default {
  name: 'Index',
  data() {
    return {
      tag: '',
      select_value: '', // 用户状态
      options: [{
        value: 1,
        label: '正常'
      }, {
        value: 0,
        label: '已停用'
      }],
      allData: [], // 所有前端分页数据
      tableData: [], // 表格数据 仅展示当前页的数据
      pageNo: 1, // 当前页数
      totalDataNumber: 0, // 总记录数
      pageSize: 10,
      allAuthData: [], // app接口权限
      authData: [], // app接口权限
      pageAuthNo: 1, // app接口权限
      totalAuthDataNumber: 0, // app接口权限
      pageAuthSize: 10, // app接口权限
      formLabelWidth: '100px', // 弹出框form宽
      disableFormVisible: false,
      addFormVisible: false,
      editFormVisible: false,
      showAppApis: false,
      addForm: {
        name: '',
        url: '',
        callLimit: '',
        qpsLimit: '',
        apiTypeId: '',
        description: ''
      },
      editForm: {
        id: '',
        name: '',
        apiTypeId: '',
        description: ''
      },
      status: '',
      id: '',
      apiTypes: []
    }
  },
  created() {
    this.searchAllApiTypes()
    this.btn_search()
  },
  mounted() {
  },
  methods: {
    formatStatus(ob) {
      if (ob.status === 1) {
        return '正常'
      } else if (ob.status === 0) {
        return '异常/停用'
      } else if (ob.status === 2) {
        return '审核中'
      } else {
        return '未知'
      }
    },
    searchAllApiTypes() {
      const vm = this
      axios.get(process.env.BASE_API + '/apitype/list', {
      }).then(function(response) {
        var obj = response.data
        if (obj.code !== 10000) {
          vm.$message.error(obj.message)
          vm.totalDataNumber = 0
          vm.apiTypes = []
        } else {
          vm.totalDataNumber = parseInt(obj.total)
          if (vm.totalDataNumber > 0) {
            vm.apiTypes = obj.data
          } else {
            vm.apiTypes = []
          }
        }
      }).catch(function(error) {
        alert(error)
      })
    },
    btn_search() {
      const vm = this
      axios.get(process.env.BASE_API + '/app/list', {
        params: {
          tag: this.tag,
          status: 2
        }
      }).then(function(response) {
        var obj = response.data
        if (obj.code !== 10000) {
          vm.$message.error(obj.message)
          vm.totalDataNumber = 0
          vm.allData = []
          vm.tableData = []
        } else {
          vm.totalDataNumber = parseInt(obj.total)
          if (vm.totalDataNumber > 0) {
            vm.allData = obj.data
            vm.tableData = obj.data.slice(0, vm.pageSize)
          } else {
            vm.allData = []
            vm.tableData = []
          }
        }
      }).catch(function(error) {
        alert(error)
      })
    },
    btn_clear() {
      this.tag = ''
      this.select_value = ''
      this.btn_search()
    },
    handleCurrentChange() {
      this.tableData = this.allData.slice((this.pageNo - 1) * this.pageSize, this.pageNo * this.pageSize)
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.tableData = this.allData.slice((this.pageNo - 1) * this.pageSize, this.pageNo * this.pageSize)
    },
    handleAuthCurrentChange() {
      this.authData = this.allAuthData.slice((this.pageAuthNo - 1) * this.pageAuthSize, this.pageAuthNo * this.pageAuthSize)
    },
    handleAuthSizeChange(val) {
      this.pageAuthSize = val
      this.authData = this.allAuthData.slice((this.pageAuthNo - 1) * this.pageAuthSize, this.pageAuthNo * this.pageAuthSize)
    },
    btnAddCancel() {
      this.addFormVisible = false
      this.addForm.name = ''
      this.addForm.description = ''
    },
    handleEdit(row) {
      this.editForm.id = row.id
      this.editForm.name = row.name
      this.editForm.url = row.url
      this.editForm.callLimit = row.callLimit
      this.editForm.qpsLimit = row.qpsLimit
      this.editForm.apiTypeId = row.apiType.name
      this.editForm.description = row.description
      this.editFormVisible = true
    },
    btnUpdateCancel() {
      this.editFormVisible = false
      this.editForm.name = ''
      this.editForm.description = ''
    },
    btnUpdateSubmit() {
      if (this.editForm.name === '' || this.editForm.name === undefined) {
        this.$message.info('请输入接口类型名称，不能为空')
        return
      }
      var vm = this
      var params = new URLSearchParams()
      params.append('id', vm.editForm.id)
      params.append('name', vm.editForm.name)
      params.append('description', vm.editForm.description)
      axios.post(process.env.BASE_API + '/apimngr/update', params).then(function(response) {
        var obj = response.data
        if (obj.code !== 10000) {
          vm.$message.error(obj.message)
        } else {
          vm.$message(obj.message)
          vm.editForm.name = ''
          vm.editForm.description = ''
          vm.editFormVisible = false
          vm.btn_search()
        }
      }).catch(function(error) {
        alert(error)
      })
    },
    handleDisable(row) {
      this.id = row.id
      this.disableFormVisible = true
    },
    submitDisable(status) {
      var vm = this
      var params = new URLSearchParams()
      params.append('id', vm.id)
      params.append('status', status)
      axios.post(process.env.BASE_API + '/app/approve', params).then(function(response) {
        var obj = response.data
        if (obj.code !== 10000) {
          vm.$message.error(obj.message)
        } else {
          vm.$message(obj.message)
          vm.btn_search()
        }
      }).catch(function(error) {
        alert(error)
      })
      this.disableFormVisible = false
    },
    handleViewAuth(row) {
      this.id = row.id
      this.getAppApi()
      this.showAppApis = true
    },
    getAppApi() {
      const vm = this
      axios.get(process.env.BASE_API + '/appapi/listbyappid', {
        params: {
          appId: vm.id
        }
      }).then(function(response) {
        var obj = response.data
        if (obj.code !== 10000) {
          vm.$message.error(obj.message)
          vm.totalAuthDataNumber = 0
          vm.allAuthData = []
          vm.authData = []
        } else {
          vm.totalAuthDataNumber = parseInt(obj.total)
          if (vm.totalAuthDataNumber > 0) {
            vm.allAuthData = obj.data
            vm.authData = obj.data.slice(0, vm.pageAuthSize)
          } else {
            vm.allAuthData = []
            vm.authData = []
          }
        }
      }).catch(function(error) {
        alert(error)
      })
    }
  }
}
</script>

<style scoped>
  .orderform {
    padding-top: 20px;
    padding-left: 10px;
    padding-right: 10px;
  }

  .mydiv {
    float: right;
    margin-right: 10px;
  }
</style>
