<template>

<view id="order-search">
    <view class="search-box">
        <view class="input-box">
            <i class="iconfont icon-sousuo " type="search" size="20"></i>

            <input type="text" :value="text" @input="sureSearch" confirm-type="search" bindconfirm="searchKeywords" />

            <!-- #ifdef MP-WEIXIN -->
            <icon type="clear" size="20" class="clear" :hidden="clear" @tap="clearF"></icon>
            <!-- #endif -->
        </view>
        <view class="text" @tap="searchKeywords">
            搜索
        </view>
    </view>
    <view class="tab-panel">
        <view class="tab-content">
            <view class="order-box">
                <view  v-for="(item, idx) in dataList" :key="idx">
                    <view class="order-item" :data-no="order.order_no" v-for="(order, index) in dataList[idx]" :key="index"  @tap="jump">
                        <view class="item-top">
                            <view class="indent mx-1px-bottom">
                                <view class="order-num">
                                    <span>{{order.from}}</span> | 订单编号：{{order.order_no}}
                                </view>
                                <view class="order-type">
                                    {{typeList[order.status]}}
                                </view>
                            </view>
                        </view>
                        <view class="item-middle">
                            <view class="middle-item mx-1px-bottom" v-for="(good, index) in order.items" :key="index" >
                                <image :src="good.item_meta.image"></image>
                                <view class="text">
                                    <view class="names">
                                        {{good.item_name}}
                                    </view>
                                    <view class="model">
                                        {{good.item_meta.specs_text}}
                                    </view>
                                </view>
                                <view class="money-box">
                                    <view>
                                        {{good.quantity}}件
                                    </view>
                                    <view>
                                        ￥{{good.total_yuan}}
                                    </view>
                                </view>
                            </view>
                        </view>
                        <view class="item-bottom">
                            <view class="all-money">
                                {{order.count}}件, 共计 ￥{{order.total_yuan}}
                            </view>
                            <view class="button-box" v-if="order.status === 1" :data-no="order.order_no" @tap.stop="pay">
                                立即付款
                            </view>
                            <view class="button-box" v-if="order.status === 3" :data-no="order.order_no" @tap.stop="submit">
                                确认收货
                            </view>
                            <view class="button-box" v-if="order.status === 6" :data-no="order.order_no" @tap.stop="deleteF">
                                删除订单
                            </view>
                        </view>
                    </view>
                </view>
                <!--<view class="loadingbox" :hidden="!tabList[0].show">
                    {{showText}}
                </view>-->
            </view>
        </view>
    </view>
</view>


</template>
<script>
import {pageLogin, getUrl,config,is} from '@/common/js/utils.js';

export default {
  data() {
    return {
      dataList: [],
      text: '',
      clear: true,
      meta: '',
      typeList: {
        0: '临时订单',
        1: '待付款',
        2: '付款成功',
        3: '已发货',
        4: '已完成',
        5: '已完成',
        6: '已取消',
        7: '已退款',
        8: '已作废',
        9: '已删除',
        31: '部分已发货'
      }
    };
  },

  onReachBottom() {
    // debugger
    var hasMore = this.meta.pagination.total_pages > this.meta.pagination.current_page;

    if (hasMore) {
      var page = this.meta.pagination.current_page + 1;
      this.orderList(this.text, page);
    } else {
      wx.showToast({
        image: '../../../static/error.png',
        title: '再拉也没有啦'
      });
    }
  },
  methods: {
    sureSearch(e) {
      this.setData({
        text: e.detail.value,
        clear: e.detail.value <= 0
      });
    },

    clearF() {
      this.setData({
        text: '',
        clear: true
      });
    },

    // 搜索
    searchKeywords() {
      var keyword = this.text;
      if (!keyword || !keyword.length) return;
      this.orderList(keyword);
    },

    jump(e) {
      wx.navigateTo({
        url: '/pages/order/detail/detail?no=' + e.currentTarget.dataset.no
      });
    },

    pay(e) {
      var order_no = e.currentTarget.dataset.no;
      wx.navigateTo({
        url: '/pages/store/payment/payment?order_no=' + order_no
      });
    },

    deleteF(e) {
      wx.showModal({
        title: '',
        content: '是否删除该订单',
        success: res => {
          if (res.confirm) {
            this.deleteOrder(e.currentTarget.dataset.no);
          }
        }
      });
    },

    submit(e) {
      wx.showModal({
        title: '',
        content: '是否确认收货',
        success: res => {
          if (res.confirm) {
            this.receiveOrder(e.currentTarget.dataset.no);
          }
        }
      });
    },

    // 确认收货
    receiveOrder(orderNo) {
      var token = this.$cookieStorage.get('user_token');
      this.$http.post({
        api: 'api/shopping/order/received',
        header: {
          Authorization: token
        },
        data: {
          order_no: orderNo
        }
      }).then(res => {
        if (res.statusCode == 200) {
          res = res.data;
          wx.showModal({
            title: '',
            content: res.message,
            showCancel: false,
            success: res => {
              if (res.confirm) {
                this.orderList(0, this.activeIndex);
              }
            }
          });
        } else {
          wx.showModal({
            title: '',
            content: '取消订单失败, 请检查您的网络状态',
            showCancel: false
          });
        }
      }).catch(rej => {
        wx.showModal({
          title: '',
          content: '取消订单失败, 请检查您的网络状态',
          showCancel: false
        });
      });
    },

    // 删除订单
    deleteOrder(orderNo) {
      var token = this.$cookieStorage.get('user_token');
      this.$http.post({
        api: 'api/shopping/order/delete',
        header: {
          Authorization: token
        },
        data: {
          'order_no': orderNo
        }
      }).then(res => {
        if (res.statusCode == 200) {
          wx.showToast({
            title: res.data.message
          });
          this.orderList(0, this.activeIndex);
        } else {
          wx.showModal({
            title: '',
            content: '删除订单失败, 请检查您的网络状态',
            showCancel: false
          });
        }
      }).catch(rej => {
        wx.showModal({
          title: '',
          content: '删除订单失败, 请检查您的网络状态',
          showCancel: false
        });
      });
    },

    // 获取订单列表
    orderList(status, page = 1) {
      console.log(status);
      var token = this.$cookieStorage.get('user_token');
      var params = {
        criteria: status,
        page: page
      };
      wx.showLoading({
        title: '加载中',
        mask: true
      });
      this.$http.get({
        api: 'api/order/list',
        header: {
          Authorization: token
        },
        data: params
      }).then(res => {
        if (res.statusCode == 200) {
          res = res.data;

          if (res.data.length) {
            this.setData({
              [`dataList.${page - 1}`]: res.data,
              meta: res.meta
            });
          } else {
            wx.showToast({
              image: '../../../static/error.png',
              title: '没有查询到'
            });
          }
        } else {
          wx.showModal({
            title: '',
            content: '请求失败',
            showCancel: false
          });
        }

        wx.hideLoading();
      }).catch(rej => {
        wx.showToast({
          title: "请求失败",
          image: '../../../static/error.png'
        });
        wx.hideLoading();
      });
    },

    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  },
  computed: {},
  watch: {}
};
</script>
<style rel="stylesheet/less" lang="less">
    @import "search";
</style>