<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.paytype" placeholder="请选择下单支付方式">
              <el-option
                v-for="item in paytype"
                :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
              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>
    <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>
              <span>支付方式：{{returnStr(returnListItem(goodsOrderList, index).PayType)}}</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}}<span v-if="subItem.SpecName">（{{subItem.SpecName}}）</span></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" @click="printPdf(`item${(index + 1) / 2}`)">重打</el-button>
        </td>
        <div v-if="index%2 !== 0" class="print-area" :class="'item' + (index + 1) / 2" style="width:180px;left:0;position: absolute;opacity: 0;">
          <div style="padding:0px 10px 5px;border-bottom:2px solid #cccccc;">
            <div style="display:inline-block;">
              <img class="topIcon" style="display:block;width:60%;margin:0 0 20px 0px;" src="http://jingcheng-resourceplat.oss-cn-shanghai.aliyuncs.com/upload/111101940020.png" alt="">
              <span style="display:block;font-size:20px;font-weight:700;text-align: center;"><img :src="returnListItem(goodsOrderList, index).AreaIconWhite" width="30" style="display: inline-block" alt="">{{returnListItem(goodsOrderList, index).AreaName}}{{returnListItem(goodsOrderList, index).TableNo}}</span>
            </div>
          </div>
          <div style="padding:10px 10px 5px;border-bottom:1px solid #cccccc;" v-for="(subItem,subIndex) in returnListItem(goodsOrderList, index).Details" :key="subIndex">
            <div style="font-size:12px;"><span style="display:inline-block;width:75%;text-align:left;">{{subItem.GoodsName}}</span><span style="display:inline-block;width:22%;text-align:right;font-size:12px;" v-if='!subItem.SpecName'>× {{subItem.Number}}</span></div>
            <div style="margin-top:8px;" v-if='subItem.SpecName'>
              <span style="display:inline-block;width:75%;text-align:left;color:#cccccc;font-size:12px;">{{subItem.SpecName}}</span>
              <span style="display:inline-block;width:20%;text-align:right;font-size:12px;">× {{subItem.Number}}</span>
            </div>
          </div>
          <div style="padding:10px 10px 5px;border-bottom:1px solid #cccccc;">
            <div style="font-size:12px;">下单人：{{returnListItem(goodsOrderList, index).Name}}</div>
            <span style="display:block;margin-top:10px;font-size:12px;">下单时间：{{toolClass.dateFormat(returnListItem(goodsOrderList, index).CreateDate)}}</span>
          </div>
          <div style="padding:10px 10px;font-size:12px;white-space: normal;">备注：{{item.Remark}}</div>
          <div style="text-align: right;font-size: 12px;">城的空间.城咖啡</div>
          <div style="height:10px"></div>
        </div>
      </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 { getLodop } from '@/util/LodopFuncs'
import tableSearch from '@/components/tableSearch/index'
const { mapState: mapStaState, mapActions: mapStaActions } = createNamespacedHelpers('sta')
var LODOP
export default {
  name: '',
  data () {
    return {
      total: 0,
      postData: { // 表格搜索条件
        status: '', // 状态
        caseid: '', // 案场id
        usertype: '', // 用户类型
        begindate: '', // 起始日期
        enddate: '', // 截止日期
        paytype: '', // 支付方式
        page: 1, // 当前页码
        pagesize: 10, // 请求数据量
      },
      typeList: [{
        value: '',
        id: ''
      }, {
        value: '销售',
        id: 'sales'
      }, {
        value: '主管',
        id: 'manager'
      }, {
        value: '用户',
        id: 'customer'
      }],
      paytype: [{
        value: '',
        id: ''
      }, {
        value: '城币',
        id: 'cheng-coin'
      }, {
        value: '优惠券',
        id: 'coupon'
      }, {
        value: '案场',
        id: 'sys user'
      }, {
        value: '积分',
        id: 'points'
      }, {
        value: '城币+优惠券',
        id: 'coupon-coin'
      }],
      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 '已取消'
      }
    },
    returnStr (val) {
      return (this.paytype.filter(x => x.id === val)[0] || {}).value || ''
    },
    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.pagenum
      })
    },
    printPdf (cl) {
      this.CreateOneFormPage(cl)
      LODOP.PRINT()
      // LODOP.PREVIEW()
    },
    CreateOneFormPage (cl) {
      LODOP = getLodop()
      LODOP.PRINT_INIT('订单')
      LODOP.SET_PRINT_PAGESIZE(3, 480, 40, '')
      LODOP.ADD_PRINT_HTM(0, 0, document.getElementsByClassName(cl)[0].offsetWidth, document.getElementsByClassName(cl)[0].offsetHeight, document.getElementsByClassName(cl)[0].innerHTML)
    },
    exportExcel () {
      var url = '&status=' + this.postData.status + '&usertype=' + this.postData.usertype + '&begindate=' + this.postData.begindate + '&enddate=' + this.postData.enddate
      window.location.href = `${this.toolClass.ReplaceOrg(this.$api.statistics.goodsOrderExcel.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>
