<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 label="审核状态:">
            <el-select v-model="select_value" clearable placeholder="请选择审核状态" size="mini">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"/>
            </el-select>
          </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-form-item>
        </el-row>
      </el-form>
      <el-table
        ref="multipleTable"
        :data="tableData"
        :highlight-current-row="true"
        :header-cell-style="{color:APP_CONFIG.TABLE_DOMINANT_COLOR}"
        :cell-style="{color:APP_CONFIG.TABLE_ASSISTANT_COLOR}"
        border>
        <el-table-column
          prop="id"
          label="序号"/>
        <el-table-column
          prop="apiName"
          label="接口名称"/>
        <el-table-column
          prop="maxQps"
          label="预估最高QPS"/>
        <el-table-column
          prop="callTimes"
          label="预估每日调用量"/>
        <!--el-table-column
          prop="areas"
          label="接入地域比例"/-->
        <el-table-column
          prop="companyName"
          label="公司名称"/>
        <el-table-column
          prop="phone"
          label="联系电话"/>
        <el-table-column
          prop="email"
          label="联系邮箱"/>
        <el-table-column
          prop="description"
          label="应用介绍"/>
        <el-table-column
          prop="applyReason"
          label="提升配额原因"/>
        <el-table-column
          prop="appName"
          label="应用名称"/>
        <el-table-column
          :formatter="formatStatus"
          prop="applyStatus"
          label="审核状态"/>
        <el-table-column
          fixed="right"
          label="操作">
          <template slot-scope="scope">
            <el-button :disabled="scope.row.applyStatus !== 2" 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>审核接口更高QPS申请</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="submitDisable(3)">拒 绝</el-button>
        <el-button type="primary" @click="submitDisable(1)">通 过</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios'
import APP_CONFIG from "../../../public/config";

export default {
  name: 'Index',
  computed: {
    APP_CONFIG() {
      return APP_CONFIG
    }
  },
  data() {
    return {
      tag: '',
      select_value: '', // 用户状态
      options: [{
        value: 1,
        label: '审核通过'
      }, {
        value: 2,
        label: '审核中'
      }, {
        value: 3,
        label: '审核拒绝'
      }],
      allData: [], // 所有前端分页数据
      tableData: [], // 表格数据 仅展示当前页的数据
      pageNo: 1, // 当前页数
      totalDataNumber: 0, // 总记录数
      pageSize: 10,
      formLabelWidth: '100px', // 弹出框form宽
      disableFormVisible: false,
      addFormVisible: false,
      editFormVisible: 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.applyStatus === 1) {
        return '审核通过'
      } else if (ob.applyStatus === 0) {
        return '默认配额'
      } else if (ob.applyStatus === 2) {
        return '审核中'
      } else if (ob.applyStatus === 3) {
        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 + '/appapi/listqpsapply', {
        params: {
          tag: this.tag,
          status: this.select_value
        }
      }).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)
    },
    handleDisable(row) {
      this.id = row.id
      this.disableFormVisible = true
    },
    submitDisable(status) {
      var vm = this
      var params = new URLSearchParams()
      params.append('qpsId', vm.id)
      params.append('status', status)
      axios.post(process.env.BASE_API + '/appapi/approveqps', 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
    }
  }
}
</script>

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

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