<template>
  <div class="subPage">
    <div class="system-table-search">
      <div class="flex-h">
        <div class="flex-item flex-h"></div>
        <ul>
          <li>
            <el-select v-model="postData.caseid" placeholder="请选择案场">
              <el-option
                v-for="item in cases"
                :key="item.CaseId"
                :label="item.CaseName"
                :value="item.CaseId">
              </el-option>
            </el-select>
          </li>
          <li>
            <el-select v-model="postData.ctype" placeholder="请选择卡券类型">
              <el-option
                v-for="item in typeList"
                :key="item.id"
                :label="item.value"
                :value="item.id">
              </el-option>
            </el-select>
          </li>
          <li>
            <el-input
              placeholder="请输入卡券名称"
              v-model="postData.name"
              clearable>
            </el-input>
          </li>
        </ul>
        <el-button
          size="mini"
          type="primary" @click="search">搜索</el-button>
        <el-button
          size="mini"
          type="success" @click="exportExcel">导出Excel</el-button>
      </div>
      <div class="moreFilter"></div>
    </div>
    <div class="system-table-box">
      <el-table
        :data="cardCouponList"
        stripe
        style="width: 100%">
        <el-table-column
          prop="CaseName"
          label="所属案场">
        </el-table-column>
        <el-table-column
          prop="CName"
          label="卡券名称">
        </el-table-column>
        <el-table-column
          prop="TypeName"
          label="卡券类型">
        </el-table-column>
        <el-table-column
          prop="SendType"
          label="发送类型">
          <template slot-scope="scope">
            {{scope.row.SendType === 'case' ? '案场' : scope.row.SendType === 'system' ? '系统' : '渠道'}}
          </template>
        </el-table-column>
        <el-table-column
          prop="Price"
          label="价格">
        </el-table-column>
        <el-table-column
          prop="SentCount"
          label="领取数">
        </el-table-column>
        <el-table-column
          prop="UsedCount"
          label="实际使用数">
        </el-table-column>
        <el-table-column
          prop="UsedCount"
          label="总费用">
          <template slot-scope="scope">
            {{(scope.row.Price - 0) * (scope.row.UsedCount - 0)}}
          </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="total">
    </el-pagination>
  </div>
</template>

<script>
import { mapState, createNamespacedHelpers } from 'vuex'
import tableSearch from '@/components/tableSearch/index'
const { mapState: mapStaState, mapActions: mapStaActions } = createNamespacedHelpers('sta')

export default {
  name: '',
  data () {
    return {
      total: 0,
      postData: { // 表格搜索条件
        caseid: '', // 案场id
        ctype: '', // 卡券类型
        name: '', // 卡券名称
        page: 1, // 当前页码
        pagesize: 10, // 请求数据量
      },
      typeList: [{
        value: '课程体验卡',
        id: 'card'
      }, {
        value: '商品优惠券',
        id: 'goodscoupon'
      }, {
        value: '课程优惠券',
        id: 'coursecoupon'
      }],
    }
  },
  computed: {
    ...mapState({
      cases: x => x.app.cases.list,
      defaultCaseId: x => x.app.cases.default
    }),
    ...mapStaState({
      cardCouponList: x => x.cardCoupon.list || [],
    }),
    CaseId: {
      get () {
        return this.postData.caseid || this.defaultCaseId
      },
      set (val) {
        this.postData.caseid = val
      }
    }
  },
  components: {
    tableSearch,
  },
  mounted () {
    this.$nextTick(function () {
      this.getList()
    })
  },
  methods: {
    ...mapStaActions([
      'getCardCouponList',
      'getCardCouponListExcel',
    ]),
    search () { // 搜索
      this.page = 1
      this.getList()
    },
    handleCurrentChange (val) { // 跳转到分页
      this.page = val
      this.getList()
    },
    getList () {
      this.getCardCouponList(this.postData).then((res) => {
        // console.log(JSON.stringify(res))
        this.postData.page = res.page
        this.total = res.pagenum
      })
    },
    exportExcel () {
      // console.log(`${this.toolClass.ReplaceOrg(this.$api.statistics.cardCouponListExcel.url)}?token=${localStorage.getItem('JWT')}&caseid=${this.CaseId === 'all' ? '' : this.CaseId}&ctype=${this.postData.ctype}&name=${this.postData.name}`)
      window.open(`${this.toolClass.ReplaceOrg(this.$api.statistics.cardCouponListExcel.url)}?token=${localStorage.getItem('JWT')}&caseid=${this.CaseId === 'all' ? '' : this.CaseId}&ctype=${this.postData.ctype}&name=${this.postData.name}`, '_blank')
    },
  }
}
</script>

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