<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" @click="handleCreate">
        新增
      </el-button>
    </div>
    <el-table
      :data="gameapihosts"
      border
      fit
      highlight-current-row
      style="width: 100%;"

      :empty-text="emptytext"
      height="480"
    >
      <el-table-column
        label="host(ip)"
        prop="gameapi_host"
      />
      <el-table-column
        label="port"
        prop="gameapi_port"
      />
      <el-table-column
        label="是否生效"
        prop="enable"
      >
        <template slot-scope="scope">
          <svg-icon v-if="scope.row.enable === 0" icon-class="no" style="font-size: 20px;" />
          <svg-icon v-else icon-class="ok" style="font-size: 20px;" />
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleUpdate(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="curpage"
      :page-size="page_size"
      :page-count="totalpage"
      layout="prev, pager, next"
      @current-change="handleCurrentChange"
    />
    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible" :close-on-click-modal="false" @close="handleDialogClose()">
      <el-form ref="form" :rules="rules" :model="form" label-position="left" label-width="130px" style="width: 400px; margin-left:50px;">
        <el-form-item label="host(ip)" prop="gameapi_host">
          <el-input v-model="form.gameapi_host" :readonly="dialogStatus!=='create'" />
        </el-form-item>
        <el-form-item label="port" prop="gameapi_port">
          <el-input v-model.number="form.gameapi_port" :readonly="dialogStatus!=='create'" />
        </el-form-item>
        <el-form-item label="是否生效" prop="enable">
          <el-switch
            v-model="form.enable"
            :active-value="1"
            :inactive-value="0"
            active-color="#13ce66"
            inactive-color="#ff4949"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = handleDialogClose()">
          Cancel
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          Confirm
        </el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
// import Pagination from '@/components/Pagination/index.vue'
import { addGameapi, getGameapiList, updateGameapi } from '@/api/workertool'
import { parseTime } from '@/utils'

export default {
  components: {

  },

  data() {
    return {
      form: {
        gameapi_host: '',
        gameapi_port: '',
        enable: 1
      },
      dialogFormVisible: false,
      dialogStatus: '',
      gameapihosts: [],
      dialogTitle: '新增',
      emptytext: ' ',
      totalpage: 0,
      curpage: 1,
      page_size: 8,
      rules: {
        gameapi_host: [{ required: true, message: 'required', trigger: 'blur' }],
        gameapi_port: [{ required: true, message: 'required', trigger: 'blur' }],
        type: [{ required: true, message: 'required', trigger: 'blur' }]
      },
      uploadUrl: '',
      uploadData: {
        file: ''
      },
      filelist: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    parseTime,
    handleDialogClose() {
      // this.getList()
    },
    handleUpdate(row) {
      this.form.enable = row.enable
      this.form.gameapi_host = row.gameapi_host
      this.form.gameapi_port = row.gameapi_port
      this.dialogStatus = 'update'
      this.dialogTitle = '编辑'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    /* handleDel(row) {
      this.$confirm('此操作将无法恢复, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delWhiteList({ gameapi_host: row.gameapi_host }).then(response => {
          if (response.code === 0) {
            if (this.gameapihosts.length === 1 && this.curpage === this.totalpage && this.curpage > 1) {
              this.curpage--
            }
            this.getList()
            this.$notify({
              title: 'Success',
              message: 'Deleted Successfully',
              type: 'success',
              duration: 2000
            })
          }
        })
      }).catch(() => {
      })
    }, */
    getList() {
      const data = {
        'page_dto': {
          'page': this.curpage,
          'pageSize': this.page_size
        }
      }
      getGameapiList(data).then(response => {
        this.gameapihosts = response.data
        this.totalpage = response.total_page
        this.curpage = response.cur_page
        if (this.gameapihosts === undefined || this.gameapihosts.length <= 0) {
          this.emptytext = 'No data'
        }
      })
    },
    handleCurrentChange(val) {
      this.curpage = val
      this.getList()
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogTitle = '新增'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    resetTemp() {
      this.form = {
        gameapi_host: '',
        gameapi_port: '',
        enable: 0
      }
    },
    createData() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          addGameapi(this.form).then(response => {
            if (response.code === 0) {
              this.dialogFormVisible = false
              this.getList()
              this.$notify({
                title: 'Success',
                message: 'Created Successfully',
                type: 'success',
                duration: 2000
              })
            }
          })
        }
      })
    },
    updateData() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          updateGameapi(this.form).then(response => {
            if (response.code === 0) {
              this.dialogFormVisible = false
              this.getList()
              this.$notify({
                title: 'Success',
                message: 'Updated Successfully',
                type: 'success',
                duration: 2000
              })
            }
          })
        }
      })
    }
  }
}
</script>

