<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="gameSwitchList"
      border
      fit
      highlight-current-row
      style="width: 100%;"

      :empty-text="emptytext"
      height="480"
    >
      <el-table-column
        label="功能"
        prop="switch_name"
      />
      <el-table-column
        prop="remark"
        label="功能描述"
      />
      <el-table-column
        label="是否生效"

        align=" "
      >
        <template slot-scope="scope">
          <svg-icon v-if="scope.row.is_open === 1" icon-class="ok" style="font-size: 20px;" />
          <svg-icon v-else icon-class="no" style="font-size: 20px;" />
        </template>
      </el-table-column>
      <el-table-column
        prop="createtime"
        label="创建时间"
      >
        <template slot-scope="{row}">
          <span>
            {{ parseTime(row.createtime) }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        prop="modifytime"
        label="修改时间"
      >
        <template slot-scope="{row}">
          <span>
            {{ parseTime(row.modifytime) }}
          </span>
        </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>
          <el-button type="text" size="small" @click="handleDel(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="90px" style="width: 400px; margin-left:50px;">
        <el-form-item label="功能" prop="switch_name">
          <el-input v-model="form.switch_name" />
        </el-form-item>
        <el-form-item label="功能描述" prop="remark">
          <el-input v-model="form.remark" />
        </el-form-item>
        <el-form-item label="是否启用">
          <el-switch
            v-model="form.is_open"
            :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 { addGameSwitch, updatGameSwitch, getGameSwitchList, delGameSwitch } from '@/api/gameswitch'
import { parseTime } from '@/utils'

export default {
  components: {

  },

  data() {
    return {
      form: {
        switch_name: '',
        remark: '',
        is_open: 1
      },
      dialogFormVisible: false,
      dialogStatus: '',
      listLoading: true,
      gameSwitchList: [],
      dialogTitle: '新增',
      emptytext: ' ',
      totalpage: 0,
      curpage: 1,
      page_size: 8,
      rules: {
        switch_name: [{ required: true, message: 'required', trigger: 'blur' }],
        remark: [{ required: true, message: 'required', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    parseTime,
    handleDialogClose() {
      // this.getList()
    },
    handleUpdate(row) {
      console.log(row)
      this.form.is_open = row.is_open
      this.form.switch_name = row.switch_name
      this.form.remark = row.remark
      this.dialogStatus = 'update'
      this.dialogTitle = '编辑'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    handleDel(row) {
      this.$confirm('此操作将无法恢复, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delGameSwitch({ switch_name: row.switch_name }).then(response => {
          if (response.code === 0) {
            if (this.gameSwitchList.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() {
      getGameSwitchList(this.page_size, this.curpage).then(response => {
        console.log(response)
        this.gameSwitchList = response.data
        this.totalpage = response.total_page
        this.curpage = response.cur_page
        // this.pager = response.cur_page
        if (this.gameSwitchList === undefined || this.gameSwitchList.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 = {
        switch_name: '',
        is_open: 0,
        remark: ''
      }
    },
    createData() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          addGameSwitch(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) {
          updatGameSwitch(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>
