<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.typeid" placeholder="请选择商品分类">
              <el-option
                v-for="item in goodsTypeList"
                :key="item.TypeId"
                :label="item.TypeName"
                :value="item.TypeId">
              </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="goodsList"
        stripe
        style="width: 100%">
        <el-table-column
          prop="CaseName"
          label="所属案场">
        </el-table-column>
        <el-table-column
          prop="GoodsName"
          label="商品名称">
        </el-table-column>
        <el-table-column
          prop="TypeName"
          label="商品分类">
        </el-table-column>
        <el-table-column
          prop="Price"
          label="价格">
        </el-table-column>
        <el-table-column
          prop="Total"
          label="下单数">
        </el-table-column>
        <el-table-column
          prop="FinishTotal"
          label="完成数">
        </el-table-column>
        <el-table-column
          prop="Amount"
          label="总费用">
        </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
        typeid: '', // 卡券类型
        name: '', // 卡券名称
        page: 1, // 当前页码
        pagesize: 10, // 请求数据量
      },
      currentList: [],
      typeList: [],
    }
  },
  computed: {
    ...mapState({
      cases: x => x.app.cases.list,
      defaultCaseId: x => x.app.cases.default
    }),
    ...mapStaState({
      goodsList: x => x.goods.list,
      goodsTypeList: x => x.goodsType.list,
    }),
    CaseId: {
      get () {
        return this.postData.caseid || this.defaultCaseId
      },
      set (val) {
        this.postData.caseid = val
      }
    }
  },
  components: {
    tableSearch,
  },
  mounted () {
    this.$nextTick(function () {
      this.getGoodsTypeList({
        page: 1,
        pagesize: 10000,
        caseid: ''
      }).then((res) => {
        // console.log(JSON.stringify(res))
        this.getList()
      })
    })
  },
  methods: {
    ...mapStaActions([
      'GetGoods',
      'getGoodsTypeList',
    ]),
    search () { // 搜索
      this.postData.page = 1
      this.getList()
    },
    handleCurrentChange (val) { // 跳转到分页
      this.postData.page = val
      this.getList()
    },
    getList () { // 获取列表
      this.GetGoods(this.postData).then((res) => {
        this.postData.page = res.page
        this.total = res.pagenum
      })
    },
    exportExcel () {
      var url = '&typeid=' + this.postData.typeid + '&name=' + this.postData.name
      window.location.href = `${this.toolClass.ReplaceOrg(this.$api.statistics.goodsExcel.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";
</style>
