<template>
  <div class="subPage">
    <div class="system-table-search">
      <div class="flex-h">
        <ul class="searchFilterLine" style="white-space: normal;">
          <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-input
              placeholder="请输入课程活动名称"
              v-model="postData.name"
              clearable>
            </el-input>
          </li>
          <li>
            <el-input
              placeholder="请输入手机号"
              v-model="postData.tel"
              clearable>
            </el-input>
          </li>
          <li>
            <el-select v-model="postData.status" placeholder="请选择状态">
              <el-option
                v-for="item in statusList"
                :key="item.id"
                :label="item.value"
                :value="item.id">
              </el-option>
            </el-select>
          </li>
        </ul>
      </div>
      <div class="flex-h">
        <div class="flex-item flex-h"></div>
        <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="courseVerifyList"
        stripe
        style="width: 100%">
        <el-table-column
          prop="CaseName"
          label="所属案场">
        </el-table-column>
        <el-table-column
          prop="CourseName"
          label="课程名称">
        </el-table-column>
        <el-table-column
          prop="BeginDate"
          label="课程时间"
          width="140px">
          <template slot-scope="scope">
            {{scope.row.BeginDate === '0001-01-01T00:00:00Z' ? '-' : toolClass.dateFormat(scope.row.BeginDate)}}<br>
            -<br>
            {{scope.row.EndDate === '0001-01-01T00:00:00Z' ? '-' : toolClass.dateFormat(scope.row.EndDate)}}
          </template>
        </el-table-column>
        <el-table-column
          prop="CustomerName"
          label="用户姓名">
        </el-table-column>
        <el-table-column
          prop="Name"
          label="微信昵称">
        </el-table-column>
        <el-table-column
          prop="Phone"
          label="手机号">
        </el-table-column>
        <el-table-column
          prop="CreateDate"
          label="下单时间"
          width="140px">
          <template slot-scope="scope">
            {{scope.row.CreateDate === '0001-01-01T00:00:00Z' ? '-' : toolClass.dateFormat(scope.row.CreateDate)}}
          </template>
        </el-table-column>
        <el-table-column
          prop="VerifyDate"
          label="核销时间"
          width="140px">
          <template slot-scope="scope">
            {{scope.row.VerifyDate === '0001-01-01T00:00:00Z' ? '-' : toolClass.dateFormat(scope.row.VerifyDate)}}
          </template>
        </el-table-column>
        <el-table-column
          prop="Status"
          label="状态">
          <template slot-scope="scope">
            {{scope.row.VerifyStatus === 'useable' ? '未核销' : scope.row.VerifyStatus === 'used' ? '已核销' : scope.row.VerifyStatus === 'late' ? '逾期核销' : ''}}
          </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
        tel: '', // 手机号
        status: '', // 状态
        name: '', // 卡券名称
        page: 1, // 当前页码
        pagesize: 10, // 请求数据量
      },
      statusList: [{
        value: '',
        id: ''
      }, {
        value: '未核销',
        id: 'useable'
      }, {
        value: '已核销',
        id: 'used'
      }, {
        value: '逾期核销',
        id: 'late'
      }],
    }
  },
  computed: {
    ...mapState({
      cases: x => x.app.cases.list,
      defaultCaseId: x => x.app.cases.default
    }),
    ...mapStaState({
      courseVerifyList: x => x.courseVerifyList.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([
      'getCourseVerifyList',
    ]),
    search () { // 搜索
      this.page = 1
      this.getList()
    },
    handleCurrentChange (val) { // 跳转到分页
      this.page = val
      this.getList()
    },
    getList () {
      this.getCourseVerifyList(this.postData).then((res) => {
        // console.log(JSON.stringify(res))
        this.postData.page = res.page
        this.total = res.pagenum
      })
    },
    exportExcel () {
      var url = '&tel=' + this.postData.tel + '&status=' + this.postData.status + '&name=' + this.postData.name
      console.log(`${this.toolClass.ReplaceOrg(this.$api.statistics.courseVerifyListExcel.url)}?token=${localStorage.getItem('JWT')}&caseid=${this.CaseId === 'all' ? '' : this.CaseId}${url}`)
      window.location.href = `${this.toolClass.ReplaceOrg(this.$api.statistics.courseVerifyListExcel.url)}?token=${localStorage.getItem('JWT')}&caseid=${this.CaseId === 'all' ? '' : this.CaseId}${url}`
    },
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>
@import "page.scss";
.searchFilterLine {
  > li {
    margin-bottom: 20px;
  }
}
</style>
