<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-select v-model="postData.typeid" placeholder="请选择课程类别">
              <el-option
                v-for="item in courseTypeList"
                :key="item.LocationId"
                :label="item.LocationName"
                :value="item.LocationId">
              </el-option>
            </el-select>
          </li>
          <li>
            <el-input
              placeholder="请输入课程活动名称"
              v-model="postData.name"
              clearable>
            </el-input>
          </li>
          <li style="white-space: nowrap;">
            <span>下单时间：</span>
            <el-date-picker
              value-format="yyyy-MM-dd"
              v-model="postData.begindate"
              type="date"
              placeholder="选择起始日期">
            </el-date-picker>
            <span>至</span>
            <el-date-picker
              value-format="yyyy-MM-dd"
              v-model="postData.enddate"
              type="date"
              placeholder="选择截止日期">
            </el-date-picker>
          </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="courseOrderList"
        stripe
        style="width: 100%">
        <el-table-column
          prop="OrdersNo"
          label="订单编号">
        </el-table-column>
        <el-table-column
          prop="CourseName"
          label="课程名称">
        </el-table-column>
        <el-table-column
          prop="CaseName"
          label="所属案场">
        </el-table-column>
        <el-table-column
          prop="LocationName"
          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>
    </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')
const { mapState: mapCmsState, mapActions: mapCmsActions } = createNamespacedHelpers('cms')

export default {
  name: '',
  data () {
    return {
      total: 0,
      postData: { // 表格搜索条件
        caseid: '', // 案场id
        typeid: '', // 获取方式
        begindate: '', // 起始日期
        enddate: '', // 截止日期
        name: '', // 卡券名称
        page: 1, // 当前页码
        pagesize: 10, // 请求数据量
      },
      currentList: [],
      typeList: [],
      statusList: [],
    }
  },
  computed: {
    ...mapState({
      cases: x => x.app.cases.list,
      defaultCaseId: x => x.app.cases.default
    }),
    ...mapStaState({
      courseOrderList: x => x.courseOrder.list || [],
    }),
    ...mapCmsState({
      courseTypeList: x => x.location || [],
    }),
    CaseId: {
      get () {
        return this.postData.caseid || this.defaultCaseId
      },
      set (val) {
        this.postData.caseid = val
      }
    }
  },
  components: {
    tableSearch,
  },
  mounted () {
    this.$nextTick(function () {
      this.updateLocationInfo().then((res) => {
        // console.log(JSON.stringify(res))
      })
      this.getList()
    })
  },
  methods: {
    ...mapCmsActions([
      'updateLocationInfo',
    ]),
    ...mapStaActions([
      'getCourseOrderList',
    ]),
    search () { // 搜索
      this.page = 1
      this.getList()
    },
    handleCurrentChange (val) { // 跳转到分页
      this.page = val
      this.getList()
    },
    getList () {
      this.getCourseOrderList(this.postData).then((res) => {
        // console.log(JSON.stringify(res))
        this.postData.page = res.page
        this.total = res.pagenum
      })
    },
    exportExcel () {
      var url = '&typeid=' + this.postData.typeid + '&begindate=' + this.postData.begindate + '&enddate=' + this.postData.enddate + '&name=' + this.postData.name
      window.location.href = `${this.toolClass.ReplaceOrg(this.$api.statistics.courseOrderExcel.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>
