<template>
  <div class="subPage">
    <div class="system-table-search">
      <div class="flex-h">
        <div class="flex-item"></div>
        <ul>
          <li>
            <el-select v-model="CaseId" placeholder="请选择">
              <el-option
                key="all"
                label="所有案场"
                value="all">
              </el-option>
              <el-option
                v-for="item in cases"
                :key="item.CaseId"
                :label="item.CaseName"
                :value="item.CaseId">
              </el-option>
            </el-select>
          </li>
          <li>
            <!-- <span>VIP卡号：</span> -->
            <el-input
              placeholder="请输入VIP卡号"
              v-model="postData.cardNo"
              clearable>
            </el-input>
          </li>
          <li>
            <!-- <span>销售：</span> -->
            <el-input
              placeholder="请输入销售姓名"
              v-model="postData.sellerName"
              clearable>
            </el-input>
          </li>
          <li>
            <!-- <span>用户：</span> -->
            <el-input
              placeholder="请输入用户姓名"
              v-model="postData.userName"
              clearable>
            </el-input>
          </li>
        </ul>
        <el-button
          size="mini"
          type="primary" @click="search">搜索</el-button>
      </div>
      <div class="moreFilter"></div>
      <div class="flex-h">
        <div class="flex-item flex-h" style="margin-top: 20px;">
          <el-button size="mini" type="success" @click='addVIP'>新增VIP</el-button>
          <el-button size="mini" type="success" @click='activateVip'>激活VIP</el-button>
          <el-button size="mini" type="warning" @click='exportExcel'>导出Excel</el-button>
        </div>
      </div>
    </div>
    <div class="system-table-box">
      <el-table
        :data="vips.list"
        stripe
        style="width: 100%">
        <el-table-column
          prop="VipCardChildCode"
          label="卡号">
        </el-table-column>
        <el-table-column
          prop="Amount"
          label="价格">
        </el-table-column>
        <el-table-column
          prop="Status"
          label="状态">
          <template slot-scope="scope">
            <span>{{scope.row.Status===1?'已激活':'未激活'}}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="SalesName"
          label="销售">
        </el-table-column>
        <el-table-column
          prop="CaseName"
          label="案场">
        </el-table-column>
        <el-table-column
          prop="CustomerName"
          label="用户">
        </el-table-column>
        <el-table-column
          prop="ActiveDate"
          label="激活时间">
          <template slot-scope="scope">
            <span>{{toolClass.dateFormat(scope.row.ActiveDate, 'yyyy-MM-dd')==='1-01-01'?'-':toolClass.dateFormat(scope.row.ActiveDate, 'yyyy-MM-dd hh:mm')}}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="BeginDate"
          label="有效期">
          <template slot-scope="scope">
            <span style="display:block">{{toolClass.dateFormat(scope.row.BeginDate, 'yyyy-MM-dd')==='1-01-01'?'':toolClass.dateFormat(scope.row.BeginDate, 'yyyy-MM-dd')}}</span>
            <span v-if="toolClass.dateFormat(scope.row.BeginDate, 'yyyy-MM-dd')!=='1-01-01'">至</span>
            <span v-else>-</span>
            <span style="display:block">{{toolClass.dateFormat(scope.row.EndDate, 'yyyy-MM-dd')==='1-01-01'?'':toolClass.dateFormat(scope.row.EndDate, 'yyyy-MM-dd')}}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="CreateDate"
          label="创建时间">
          <template slot-scope="scope">
            <span v-if="scope.row.CreateDate !== '0001-01-01T00:00:00Z'">{{toolClass.dateFormat(scope.row.CreateDate, 'yyyy-MM-dd')}}</span>
            <span v-else>-</span>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page.sync="postData.page"
      :page-size="postData.pagesize"
      layout="prev, pager, next, jumper"
      :total="vips.pagenum">
    </el-pagination>
  </div>
</template>

<script>
import { createNamespacedHelpers, mapState } from 'vuex'

const { mapState: mapVipState, mapActions: mapVipActions } = createNamespacedHelpers('vip')

export default {
  name: '',
  data () {
    return {
      total: 0,
      postData: { // 表格搜索条件
        caseid: '', // 案场id
        page: 1, // 当前页码
        pagesize: 10, // 请求数据量
        cardNo: '', // vip卡号
        sellerName: '', // 销售姓名
        userName: '', // 用户名
      },
    }
  },
  mounted () {
    this.$nextTick(function () {
      this.getList()
    })
  },
  computed: {
    ...mapVipState({
      vips: x => x.vipList,
    }),
    ...mapState({
      cases: x => x.app.cases.list,
      defaultCaseId: x => x.app.cases.default
    }),
    CaseId: {
      get () {
        return this.postData.caseid || this.defaultCaseId
      },
      set (val) {
        this.postData.caseid = val
      }
    }
  },
  methods: {
    ...mapVipActions([
      'GetVipList',
    ]),
    search () { // 搜索
      this.postData.page = 1
      this.currentList = []
      this.getList()
    },
    getList () { // 获取列表
      this.GetVipList({ ...this.postData, caseid: this.CaseId === 'all' ? '' : this.CaseId })
    },
    handleCurrentChange (val) { // 跳转到分页
      this.postData.page = val
      this.getList()
    },
    addVIP () {
      this.$router.push({ name: 'addVip' })
    },
    activateVip () {
      this.$router.push({ name: 'activateVip' })
    },
    exportExcel () {
      window.open(`${this.toolClass.ReplaceOrg(this.$api.cardManager.vipcardExcel.url)}?token=${localStorage.getItem('JWT')}&cardNo=${this.$data.postData.cardNo}&sellerName=${this.$data.postData.sellerName}&userName=${this.$data.postData.userName}&caseid=${this.CaseId === 'all' ? '' : this.CaseId}`, '_blank')
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>
@import "page.scss";
</style>
