<template>
  <div class="table-box">
    <div
      class="btn-wrap-left"
      style="padding:10px 24px">
      <el-button
        type="primary"
        size="mini"
        @click="showDialog('add')">
        新增售票点
      </el-button>
    </div>

    <div class="block-title">
      售票点列表
    </div>
    <el-table
      border
      stripe
      v-loading="loading"
      :data="tableData">
      <el-table-column
        type="index"
        label="#"
        width="60">
      </el-table-column>
      <el-table-column
        prop="name"
        label="名称">
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        width="160">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="showDialog('edit',scope.row)">
            编辑
          </el-button>
          <el-button
            type="text"
            size="small"
            @click="deleteItem(scope.row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 弹框 -->
    <ElDialog
      :title="dialogType === 'add' ? '添加售票点' : '编辑售票点'"
      width="660px"
      v-model="dialogVisible">
      <EditDialog
        :dialog-type="dialogType"
        :current-item="currentItem"
        @updateList="getList"></EditDialog>
    </ElDialog>
    <el-pagination
      background
      :page-size="10"
      :current-page.sync="params.pageNum"
      @current-change="getList()"
      layout="prev, pager, next"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
import { getTicketSpotList, deleteTicketSpot } from '@/api/ticketSpot'
import ElDialog from '@/components/Dialog'
import EditDialog from './ticketSpot/Dialog'
export default {
  components: {
    ElDialog,
    EditDialog
  },
  created () {
    this.getList()
  },
  data () {
    return {
      loading: false,
      dialogType: 'add',
      dialogVisible: false,
      tableData: [],
      total: 0,
      params: {
        pageNum: 1,
        pageSize: 10
      },
      currentItem: {}
    }
  },
  methods: {
    showDialog (type, item) {
      this.dialogType = type
      this.currentItem = item
      this.dialogVisible = true
    },
    getList () {
      this.loading = true
      getTicketSpotList(this.params).then(res => {
        this.tableData = res.list
        this.total = res.total
      }).finally(() => {
        this.loading = false
      })
    },
    deleteItem (item) {
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteTicketSpot(item).then(res => {
          this.$message.success('删除成功')
          this.getList()
        })
      })
    }
  }
}
</script>
