<template>
  <div class="p-15">
    <el-collapse v-model="activeNames" accordion>
      <template v-if="orderListArr.length != 0">
        <el-collapse-item class="mainFontColor" v-for="(item, index) in orderListArr" :key="index" :name="item.id">
          <template slot="title">
            <div class="clearB w-100">
              <div class="fb l w-30">
                订单ID：{{ item.id }}
              </div>
              <div class="r fb w-70 fr p-r-15">
                下单时间：{{ parseTime(item.addtime) }}
              </div>
            </div>
          </template>
          <el-descriptions direction=" vertical" :column="3" border>
            <el-descriptions-item label="状态">{{ orderState(item.back, item.status) }}
            </el-descriptions-item>
            <el-descriptions-item label="订单号">{{ item.order_sn }}</el-descriptions-item>
            <el-descriptions-item label="取餐号">{{ item.food_sn }}</el-descriptions-item>
            <el-descriptions-item label="收货人">{{ item.receiver }}</el-descriptions-item>
            <el-descriptions-item label="手机号">{{ item.tel }}</el-descriptions-item>
            <el-descriptions-item label="收货站点">{{ item.receive_site_name }}</el-descriptions-item>
            <el-descriptions-item label="总重量">{{ item.total_weight }}克</el-descriptions-item>
            <el-descriptions-item label="总价">{{ item.total_price }}元</el-descriptions-item>
            <el-descriptions-item label="货品总数">{{ item.total_num }}件</el-descriptions-item>
            <el-descriptions-item label="订单详情" span="3">
              <el-table v-for="spu, i in item.product_snapshot" :show-header="i === 0" :data="spu.sku_arr"
                style="width: 100%">
                <el-table-column prop="sku_name" label="名称">
                </el-table-column>
                <el-table-column label="编号">
                  <template slot-scope="scope">
                    {{ scope.row.sku_number }}
                  </template>
                </el-table-column>
                <el-table-column label="数量">
                  <template slot-scope="scope">
                    {{ scope.row.sku_totol }}{{ scope.row.sku_unit }}
                  </template>
                </el-table-column>
                <el-table-column label="详情" width="120">
                  <el-button type="primary" size="small">查看</el-button>
                </el-table-column>
              </el-table>
            </el-descriptions-item>
            <el-descriptions-item :label="item.back == 'normal' ? '客户备注' : '退货缘由'" span="3">
              {{ item.back == 'requested' ? (item.back_remark || '无') : (item.remark || '无') }}
            </el-descriptions-item>
            <el-descriptions-item label="操作" span="3">
              <el-button-group v-if="item.back == 'requested'">
                <el-button type="danger" icon="iconfont icon-zhengque">
                  <font class="m-l-5">同意退款</font>
                </el-button>
                <el-button @click="questAss(item.id, 'back', 'zero')" type="warning" icon="iconfont icon-cuowu">
                  <font class="m-l-5">拒绝退款</font>
                </el-button>
              </el-button-group>
              <el-button-group v-else-if="item.back == 'normal' && item.status == 'pending'">
                <el-button @click="questAss(item.id, 'quest', '1')" type="primary" icon="iconfont icon-chengjie">
                  <font class="m-l-5">确认备货</font>
                </el-button>
                <el-button type="danger" icon="iconfont icon-cuowu">
                  <font class="m-l-5">取消订单</font>
                </el-button>
                <el-button type="info" icon="iconfont icon-dayin">
                  <font class="m-l-5" @click="orderPrint(index)">打印小票</font>
                </el-button>
              </el-button-group>
              <el-button-group v-else-if="item.back == 'normal' && item.status == 'processing'">
                <el-button type="danger" icon="iconfont icon-cuowu">
                  <font class="m-l-5">取消订单</font>
                </el-button>
                <el-button type="info" icon="iconfont icon-dayin">
                  <font class="m-l-5" @click="orderPrint(index)">打印小票</font>
                </el-button>
              </el-button-group>
            </el-descriptions-item>
          </el-descriptions>
        </el-collapse-item>
      </template>
      <div v-else class="p-t-60 p-b-60 seatFontColor fc">
        <i class="iconfont icon-meiyoushuju f-s-100"></i>
        <div>空空如也</div>
      </div>
    </el-collapse>
  </div>
</template>

<script>
import { parseTime } from '@/utils/index'

export default {
  data () {
    return {
      activeNames: []
    }
  },
  props: {
    fil: Number,
    shop_id: String
  },
  computed: {
    /**
     * @description: 获取订单列表
     */
    orderList () {
      return this.$store.state.orderList
    },
    /**
 * @description: 获取订单列表
 */
    orderListArr () {
      let list = this.orderList // 初始化list变量为this.orderList数组
      if (list.length > 0) {
        if (this.fil === 2) { // 过滤“申请退款”的订单
          list = list.filter(item => item.shop_id === this.shop_id && item.back === 'requested')
        } else if (this.fil === 0) { // 过滤“待处理”的订单
          list = list.filter(item => item.shop_id === this.shop_id && item.back === 'normal' && item.status === 'pending')
        } else if (this.fil === 1) { // 过滤“备货中”的订单
          list = list.filter(item => item.shop_id === this.shop_id && item.back === 'normal' && item.status === 'processing')
        }
        return list
      }
      return []
    }
  },
  methods: {
    parseTime, // 时间戳格式化
    orderState (back, status) {
      if (back === 'requested') {
        return '申请退款'
      } else {
        if (status === 'pending') {
          return '处理中'
        } else {
          return '备货中'
        }
      }
    },
    /**
     * @description: 通过蓝牙打印机并打印小票
     * @param {number} index 订单列表下标索引
     */
    orderPrint (index) {
      // 订单文本编辑
      // console.log(this.list[index])
      // let con
      // con = '无人机外卖送餐#' + this.list[index].food_sn + '\n\n'
      // this.btPrint(con, '10', '1')
      // con = ''
      // con += '------------客户信息------------\n'
      // con += '昵称：' + this.list[index].name + '\n'
      // const tel = this.list[index].tel.substr(0, 3) + '****' + this.list[index].tel.substr(7)
      // con += '电话：' + tel + '\n\n'
      // con += '------------订单信息------------\n'
      // this.btPrint(con, '0', '0')
      // con = ''
      // con += '取餐号：' + this.list[index].food_sn + '\n'
      // this.btPrint(con, '31', '0')
      // con = ''
      // con += '单号：' + this.list[index].order_sn + '\n'
      // con += '收货站点：' + this.list[index].receive_site_name + '\n'
      // con += '下单时间：' + this.parseTime(this.list[index].addtime) + '\n\n'
      // if (this.list[index].remark !== '') {
      //   con += '------------客户备注------------\n'
      //   con += this.list[index].remark + '\n\n'
      // }
      // con += '--------------商品--------------\n'
      // this.list[index].products.forEach(element => {
      //   con += 'ID：' + element.pid + '\n'
      //   con += '品名：' + element.name + '\n'
      //   if (element.pro_buff !== '') {
      //     con += '规格: ' + element.pro_buff + '\n'
      //   }
      //   con += '数量：' + element.num + '\n'
      //   con += '单价：' + element.price + '\n'
      //   con += '················\n'
      // })
      // con += '货品总数：' + this.list[index].product_num + '\n'
      // con += '货品总额：' + this.list[index].price + '\n\n'
      // con += '---------加盟无人机送餐---------\n'
      // this.btPrint(con, '0', '0')
      // con = ''
      // var data = 'https://flicube.com'
      // var align = 1
      // var model = 49
      // var size = 32
      // var eclevel = 50
      // BTPrinter.printQRCode(function (data) {
      //   console.log('Success')
      //   console.log(data)
      // }, function (err) {
      //   console.log('Error')
      //   console.log(err)
      // }, data, align, model, size, eclevel)
      // con += '\n请扫上列二维码关注我们\n\n\n'
      // this.btPrint(con, '0', '1')
    },
    /**
     * @description: cordova 打印api接口
     * @param {string} str 内容
     * @param {string} size 字体大小预设
     * @param {string} algin 对齐
     */
    btPrint (str, size = '0', align = '0') {
      // BTPrinter.printTitle(function (data) {
      //   console.log('Success')
      //   console.log(data)
      // }, function (err) {
      //   console.log('Error')
      //   console.log(err)
      // }, str, size, align)// string, size, align
    }
  },
  watch: {
  },
  created () {
  }
}
</script>

<style lang="scss" scoped>
.orterTit {
  display: flex;
  justify-content: space-between;
}
</style>
