<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.usertype" placeholder="请选择下单用户类型">
              <el-option
                v-for="item in typeList"
                :key="item.id"
                :label="item.value"
                :value="item.id">
              </el-option>
            </el-select>
          </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>
          <li style="white-space: nowrap;">
            <span>下单时间：</span>
            <el-date-picker
              v-model="postData.begindate"
              type="date"
              placeholder="选择起始日期">
            </el-date-picker>
            <span>至</span>
            <el-date-picker
              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="search">导出Excel</el-button>
      </div>
      <div class="moreFilter"></div>
    </div>
    <table>
      <tr v-for="(item,index) in goodsOrderList.length*2" :key="index">
        <td v-if="index%2 === 0" colspan="16" class="title">
          <div class="flex-h">
            <div class="flex-item" style="text-align:left;">
              <span>订单编号：{{returnListItem(goodsOrderList, index).OrdersNo}}</span>
              <span>案场：{{returnListItem(goodsOrderList, index).CaseName}}</span>
              <span>用户类型：{{returnListItem(goodsOrderList, index).UserType === 'sales' ? '销售' : returnListItem(goodsOrderList, index).UserType === 'manager' ? '主管' : '用户'}}</span>
              <span>下单时间：{{toolClass.dateFormat(returnListItem(goodsOrderList, index).CreateDate)}}</span>
            </div>
            <span>{{returnStatus(returnListItem(goodsOrderList, index).MakeStatus)}}</span>
          </div>
        </td>
        <td v-if="index%2 !== 0" colspan="4" class="noborder">
          <span v-for="(subItem,subIndex) in returnListItem(goodsOrderList, index).Details" :key="subIndex">{{subItem.GoodsName}}（{{subItem.SpecName}}）</span>
        </td>
        <td v-if="index%2 !== 0" colspan="1" class="noborder">
          <span v-for="(subItem,subIndex) in returnListItem(goodsOrderList, index).Details" :key="subIndex">x{{subItem.Number}}</span>
        </td>
        <td v-if="index%2 !== 0" colspan="1" class="noborder">
          <span v-for="(subItem,subIndex) in returnListItem(goodsOrderList, index).Details" :key="subIndex">￥{{subItem.Price}}</span>
        </td>
        <td v-if="index%2 !== 0" colspan="4">
          <span>{{returnListItem(goodsOrderList, index).AreaName}}{{returnListItem(goodsOrderList, index).TableNo}}</span>
        </td>
        <td v-if="index%2 !== 0" colspan="4">
          <span>{{returnListItem(goodsOrderList, index).Name}}/{{returnListItem(goodsOrderList, index).Phone}}</span>
        </td>
        <td v-if="index%2 !== 0" colspan="2">
          <el-button
          size="mini"
          type="success">重打</el-button>
        </td>
      </tr>
    </table>
    <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: { // 表格搜索条件
        status: '', // 状态
        caseid: '', // 案场id
        usertype: '', // 用户类型
        begindate: '', // 起始日期
        enddate: '', // 截止日期
        page: 1, // 当前页码
        pagesize: 10, // 请求数据量
      },
      typeList: [{
        value: '不限',
        id: ''
      }, {
        value: '销售',
        id: 'sales'
      }, {
        value: '主管',
        id: 'manager'
      }, {
        value: '用户',
        id: 'customer'
      }],
      statusList: [{
        value: '不限',
        id: ''
      }, {
        value: '已下单',
        id: 'not started'
      }, {
        value: '制作中',
        id: 'doing'
      }, {
        value: '已完成',
        id: 'finished'
      }, {
        value: '已取消',
        id: 'cancel'
      }],
    }
  },
  computed: {
    ...mapState({
      cases: x => x.app.cases.list,
      defaultCaseId: x => x.app.cases.default
    }),
    ...mapStaState({
      goodsOrderList: x => x.goodsOrder.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([
      'getGoodsOrderList',
    ]),
    returnStatus (val) {
      switch (val) {
        case 'not started':
          return '已下单'
        case 'doing':
          return '制作中'
        case 'finished':
          return '已完成'
        case 'cancel':
          return '已取消'
      }
    },
    returnListItem (target, index) {
      return target[Math.ceil((index + 1) / 2) - 1]
    },
    search () { // 搜索
      this.page = 1
      this.getList()
    },
    handleCurrentChange (val) { // 跳转到分页
      this.page = val
      this.getList()
    },
    getList () {
      this.getGoodsOrderList(this.postData).then((res) => {
        // console.log(JSON.stringify(res))
        this.postData.page = res.page
        this.total = res.total
      })
    },
  }
}
</script>

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