<template>
  <div class="app-container">

    <div class="filter-container">
      <el-input v-model="listQuery.title" placeholder="游戏名称" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">
        新增
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
    <el-table-column label="游戏名称" min-width="150px" align="center">
      <template slot-scope="scope">
        <span>{{scope.row.gameName}}</span>
      </template>
    </el-table-column>
    <el-table-column label="渠道ID" min-width="150px" align="center">
      <template slot-scope="scope">
        <span>{{scope.row.channelID}}</span>
      </template>
    </el-table-column>
    <el-table-column label="推广员" min-width="150px" align="center">
      <template slot-scope="scope">
        <span>{{scope.row.promoters}}</span>
      </template>
    </el-table-column>
    <el-table-column label="操作" min-width="150px" align="center">
      <template slot-scope="scope">
        <span>{{scope.row.operation}}</span>
      </template>
    </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="100px" style="width: 400px; margin-left:30px;">
        <el-form-item label="游戏名称" prop="name">
          <el-select v-model="temp.name" class="filter-item" placeholder="请选择">
            <el-option v-for="item in gameNameList" :key="item" :label="item" :value="item" />
          </el-select>
        </el-form-item>
        <el-form-item label="推广员名称" prop="extensionName">
          <el-select v-model="temp.extensionName" class="filter-item" placeholder="请选择">
            <el-option v-for="item in extensionList" :key="item" :label="item" :value="item" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { access } from 'fs'
import Pagination from '@/components/Pagination'


export default {
  name:'channels',
  components: { Pagination },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: false,
      listQuery:{
        page: 1,
        limit: 20,
        title: undefined,
      },
      textMap: {
        update: '编辑',
        create: '创建'
      },
      dialogPvVisible: false,
      dialogFormVisible:false,
      rules:{
        name:[{required:true, message:"游戏名称是必需的", trigger:'blur'}],
        extensionName:[{required:true, message:'推广员名称是必需的',trigger:'blur'}]
      },
      temp:{
        id:undefined,
        name:'',
        cpName:'',
        extensionName:''
      },
      dialogStatus:'',
      gameNameList:['火影','海贼','超人','柯南'],
      extensionList:['推广员1','推广员2','推广员3'],
      
    }
  },
  created(){
    this.getList();
  },
  methods: {
    handleFilter(){
      console.log("查询游戏")
    },
    
    getList(){
      // this.listLoading = true
    },
    handleCreate(){
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      // this.$nextTick(() => {
      //   this.$refs['dataForm'].clearValidate()
      // })
    },
    createData(){
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          
          createChannel(this.temp).then(() => {
            this.dialogFormVisible = false
            this.$notify({
              title: 'Success',
              message: 'Created Successfully',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    updateData(){
      this.$refs['dataForm'].validate((valid)=>{
        console.log("------", this.temp)
        if(valid){
          const tempData = Object.assign({}, this.temp)
          console.log("------",tempData)
          updateChannel(tempData).then(()=>{
            for(const v of this.list){
              if(v.id === this.temp.id){
                const index = this.list.indexOf(v)
                this.list.splice(index,1, this.temp)
                break
              }
            }
            this.dialogFormVisible = false
            this.$notify({
              title: 'Success',
              message: 'Update Successfully',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    }
  },
}
</script>

<style lang="scss" scoped>
.filter-container {
  padding-bottom: 10px;

  .filter-item {
    display: inline-block;
    vertical-align: middle;
    margin-bottom: 10px;
  }
}
</style>
