<template>
<div class="app-container calendar-list-container">
  <!-- 筛选条件 -->
  <div class="filter-container">
    <el-button class="filter-item" type="success" icon="edit" @click="handleCreate">添加</el-button>
  </div>

  <!-- 列表数据 -->
  <Pagination uri="/venue/cards" :request-params="params" ref="pagination">
      <!-- 会员卡类型 -->
      <el-table-column align="center" label="会员卡">
        <template scope="scope">
          <span>{{scope.row.cardName}}</span>
        </template>
      </el-table-column>

      <!-- 金额 -->
      <el-table-column align="center" label="金额">
        <template scope="scope">
          <span>{{scope.row.price}}</span>
        </template>
      </el-table-column>

    <el-table-column label="操作" align="center" width="150">
      <template scope="scope">
        <el-button v-if="scope.row.status === 1" size="small" type="text" @click="handleUpdateStatus(scope.row, 0)">禁用</el-button>
        <el-button v-if="scope.row.status === 0" size="small" type="text" @click="handleUpdateStatus(scope.row, 1)">启用</el-button>
        <el-button size="small" type="text" @click="handleEdit(scope.row)">编辑</el-button>
        <el-button size="small" type="text" class="danger" @click="handleDelete(scope.row)">删除</el-button>
      </template>
    </el-table-column>
  </Pagination>
  <el-dialog title="会员卡信息" :visible.sync="dialogVisible" width="50%">
    <el-form label-position="right" label-width="120px" class="small-space">
        <el-form-item label="会员卡类型" >
          <el-select v-model="form.cardType" placeholder="请选择">
            <el-option
              v-for="item in cards"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>        
        <el-form-item label="价格">      
          <el-input-number v-model="form.price" :min="1" :max="1000000" :precision="2" label="价格"></el-input-number>    
        </el-form-item>
      </el-form>
    <span slot="footer" class="dialog-footer">      
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="handleSaveCard">确 定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
import { remove, updateStatus, findCards, save } from '@/api//venue/card'
import waves from '@/directive/waves.js'// 水波纹指令
import Pagination from '@/components/Pagination'
import Status from '@/components/Status'
import statusOptions from '@/utils/constants.js'

export default {
  name: 'venueCard_list',
  components: { Pagination, Status },
  directives: {
    waves
  },
  data() {
    return {
      dialogVisible: false,
      form: {
        id: undefined,
        cardType: 1,
        price: 1,
        venueId: '',
        venueType: ''
      },
      params: {
        venueId: '',
        venueType: '',
        search_eq_status: undefined
      },
      cards: [],
      options: {
        status: statusOptions
      }
    }
  },
  created() {
    this.params.venueId = this.$route.query.id
    this.params.venueType = this.$route.query.type
    this.form.venueId = this.$route.query.id
    this.form.venueType = this.$route.query.type
    findCards().then(response => {
      this.cards = response.cards
    })
  },
  methods: {
    /**
     * 搜索场馆会员卡
     */
    handleSearch() {
      this.$refs.pagination.handleSearch()
    },

    /**
     * 编辑场馆会员卡
     */
    handleEdit(row) {
      this.dialogVisible = true
      this.form.id = row.id
      this.form.cardType = row.cardType
      this.form.price = row.price
    },
    handleCreate() {
      this.form.id = undefined
      this.form.cardType = 1
      this.form.price = 1
      this.dialogVisible = true
    },
    /**
     * 创建场馆会员卡
     */
    handleSaveCard() {
      save(this.form).then(response => {
        this.dialogVisible = false
        this.$refs.pagination.pageRequest()
      })
    },

    /**
     * 更新场馆会员卡状态
     */
    handleUpdateStatus(row, status) {
      updateStatus(row.id, status).then(response => {
        this.$refs.pagination.pageRequest()
      })
    },

    /**
     * 删除场馆会员卡
     */
    handleDelete(row) {
      this.$confirm('确认删除该场馆会员卡？').then(_ => {
        remove(row.id).then(response => {
          this.$refs.pagination.removeItem(row)
        })
      }).catch(_ => {})
    }
  }
}
</script>

