<template>
  <div class="order-wrapper" id="order-wrapper">
    <div class="com-mobile-header">
      <a @click="goLastPage"><i class="iconfont icon-fanhui"></i></a>
      <p>订单管理
      </p>
    </div>
    <div class="order-nav">
      <div :class="activeType === '' ? 'active': ''" @click="ChangeList('')"><span>全部</span></div>
      <div :class="activeType === 'tobeconfirmed' ? 'active' : ''" @click="ChangeList('tobeconfirmed')"><span>待付款</span></div>
      <div :class="activeType === 'comfirmed' ? 'active' : ''" @click="ChangeList('comfirmed')"><span>待发货</span></div>
      <div :class="activeType === 'inbound' ? 'active' : ''" @click="ChangeList('inbound')"><span>待收货</span></div>
    </div>
    <ul class="order-list-wrap">
      <li class="clearfix" v-for="item in orderList">
        <div class="list-wrap-title clearfix">
          <div class="pull-left">{{item.buyername}}&nbsp;|&nbsp;{{item.buyentername}}</div>
          <div class="pull-right">
            <template v-if="vendorType === 'buyer'">
              <span class="red">待卖家发货</span>
              <!--<span class="red">待付款</span>-->
              <!--<span class="red">待收货</span>-->
              <!--<span class="red">交易关闭</span>-->
            </template>
            <template v-else>
              <span class="red" v-if="item.status === 502 || item.status === 406">买家已付款</span>
              <!--<span class="red">待发货</span>-->
              <span class="red" v-else-if="item.status === 602 || item.status === 603 || item.status === 315 || item.status === 604 || item.status === 605 || item.status === 606">订单已取消</span>
              <span class="red" v-else-if="item.status === 501 || item.status === 524">待买家付款</span>
              <span class="red" v-else-if="item.status === 404 || item.status === 520 || item.status === 405 || item.status === 503 || item.status === 514">待买家收货</span>
              <span class="red" v-else-if="item.status === 405 || item.status === 514 || item.status === 503 || item.status === 506">待商城付款</span>
              <span class="red" v-else-if="item.status === 606">交易关闭</span>
              <span class="red" v-else-if="item.status === 520">交易完成</span>
              <span class="red" v-else-if="item.status === 525">卖家发起取消,待确认</span>
              <span class="red" v-else-if="item.status === 504">付款确认中</span>
            </template>
          </div>
        </div>
        <div class="list-wrap-content" v-for="details in item.purchaseDetails">
          <div class="list-item clearfix">
            <div class="list-wrap-content-brand clearfix pull-left">
              <div class="name pull-left">品牌：</div>
              <div class="pull-left">{{details.brName || '-'}}</div>
            </div>
            <div class="pull-right lab">
              自定义标签
            </div>
          </div>

          <div class="list-item clearfix">
            <div class="list-wrap-content-brand clearfix pull-left">
              <div class="name pull-left">类目：</div>
              <div class="pull-left">{{details.kiName || '-'}}</div>
            </div>
            <div class="pull-right pri">
              <span>¥</span>{{details.ensurePrice}}
            </div>
          </div>

          <div class="list-item clearfix">
            <div class="list-wrap-content-brand clearfix pull-left">
              <div class="name pull-left">型号：</div>
              <div class="pull-left">{{details.cmpCode || '-'}}</div>
            </div>
            <div class="pull-right lab">
              <span>x</span>{{details.number}}
            </div>
          </div>
        </div>
        <div class="list-all-info clearfix">
          <div>共<span>{{item.batchQty}}</span>件商品&nbsp;&nbsp;合计：
            <span class="pri"><a class="red">¥</a>{{item.ensurePrice }}</span>
            <span class="lab">(含运费：<a class="red">¥</a><a class="red">{{item.fare || '0.00'}}</a>)</span></div>
        </div>
        <div class="list-btn clearfix">
          <template v-if="vendorType === 'buyer'">
            <div class="pull-right sendGoods">确认收货</div>
            <!--<div class="pull-right sendGoods">确认付款</div>-->
            <!--<div class="pull-right">订单详情</div>-->
            <!--<div class="pull-right">取消订单</div>-->
            <!--<div class="pull-right">删除订单</div>-->
          </template>
          <template v-else>
            <div class="pull-right sendGoods" v-if="(item.status === 502 || item.status === 406) && !item.uasPurcid" @click="sendGoods(item)">点击发货</div>
            <div class="pull-right sendGoods" v-if="item.status === 404 && !item.uasPurcid">修改物流</div>
            <div class="pull-right">订单详情</div>
            <div class="pull-right" v-if="item.status === 602 || item.status === 603 || item.status === 315 || item.status === 604 || item.status === 605 || item.status === 606">
              删除订单</div>
            <div class="pull-right" v-if="item.status === 404 || item.status === 520 || item.status === 405 || item.status === 503 || item.status === 514">查看物流</div>
            <div class="pull-right" v-if="item.installmentId && ((item.installment.status === 503 && item.Overtime) || item.installment.status === 504) && item.status !== 606 && item.status !== 525">
              取消订单
            </div>
            <div @click="onMind('此订单为分期付款，请前往【PC】端进行相关操作')" class="pull-right" v-if="installmentDetailPaid(item) && item.status !== 525 && item.status !== 606">
              确认收款
            </div>
          </template>
        </div>
      </li>
    </ul>
    <div v-if="getOrderList && false"></div>
    <!-- 发货弹窗 -->
    <div class="mobile-modal" v-show="showSend">
      <div class="sendGoods_Alert">
        <div class="sendGoods_title">发货信息<span><i class="iconfont icon-guanbi1" @click="showSend = false"></i></span></div>
        <div class="sendGoods_buyer">
          <div class="sendGoods_buyer_top">
            <span class="name">{{sendGoodsInfo.area.name}}</span>
            <span class="tel">{{sendGoodsInfo.area.tel}}</span>
          </div>
          <div class="sendGoods_buyer_bottom">
            <img src="/images/order/address_icon.png"/>
            <span>{{sendGoodsInfo.area.area}}{{sendGoodsInfo.area.detailAddress}}</span>
          </div>
        </div>
        <div class="sendGoods_list clearfix">
          <div class="ti pull-left">配送方式</div>
          <div class="pull-right">
            {{sendGoodsInfo.sendType === 1301 ? '第三方配送' : (sendGoodsInfo.sendType === 1302 ? '卖家配送': '上门自提')}}
          </div>
        </div>
        <div class="sendGoods_list clearfix">
          <div class="ti pull-left">配送商</div>
          <div class="pull-right">请选择<img src="/images/mobile/user/icon-right.png"/></div>
        </div>
        <div class="sendGoods_list clearfix">
          <div class="ti pull-left">运单号</div>
          <div class="pull-right"><input type="number" placeholder="请输入运单号"/></div>
        </div>
        <div class="sendGoods_Btn">
          确定
        </div>
      </div>
    </div>
    <!-- /end 发货弹窗 -->
    <!-- 选择配送商 -->
    <div class="mobile-modal" v-show="peisongShow">
      <div class="peisong_Alert">
        <div class="com-mobile-header">
          <a @click="goLastPage"><i class="iconfont icon-fanhui"></i></a>
          <p>选择配送商
          </p>
        </div>
        <div class="search-content">
          <input type="text" v-model="seekKeyword" placeholder="请输入您要查找的配送商" @keyup.13="searchSeek" ref="searchSeekInput" @focus="inputGetFocus()" @blur="blur()">
          <span @click="searchSeek" >
          <i class="iconfont icon-sousuo"></i>
          </span>
        </div>
        <ul>
          <li class="clearfix">
            <div class="name pull-left">11111</div>
            <div class="pull-right target" :class="peisonChooseItem === 0 ? 'active' : ''"></div>
          </li>
        </ul>
      </div>
    </div>
    <!-- /end 选择配送商 -->
    <remind-box :title="collectResult" :timeoutCount="timeoutCount"></remind-box>
    <pull-up :fixId="'order-wrapper'"
             :searchMore="isSearchSearchingMore"
             :allPage="allPage"
             :page="page"
             @pullUpAction="getMoreSearch"></pull-up>
  </div>
</template>

<script>
  import { EncryptionFilter } from '~utils/tools.js'
  import { RemindBox, PullUp } from '~components/mobile/common'
  export default {
    name: 'order',
    layout: 'mobile',
    middleware: 'authenticated',
    fetch({store, route}) {
      return route.query.type === 'buyer' ? Promise.all([
      ]) : Promise.all([
        store.dispatch('order/getSellOrderList', {
          count: 5,
          page: 1,
          sorting: { createtime: 'DESC' },
          status: '',
          storeType: 'other'}
        )
      ])
    },
    data() {
      return {
        isSearchSearchingMore: false,
        page: 1,
        collectResult: '',
        timeoutCount: 0,
        activeType: '',
        orderList: [],
        $status: '',
        showSend: false,
        sendGoodsInfo: {
          area: {}
        },
        peisongShow: true,
        seekKeyword: '',
        peisonChooseItem: 0
      }
    },
    computed: {
      allPage() {
        return this.$store.state.order.order.orderList.data.totalPages
      },
      vendorType() {
        return this.$route.query.type
      },
      getOrderList() {
        this.orderList = this.$store.state.order.order.orderList.data.content
        return this.$store.state.order.order.orderList
      }
    },
    methods: {
      getMoreSearch() {
        this.page++
        this.isSearchSearchingMore = true
        let params = {
          count: 5,
          page: this.page,
          sorting: { createtime: 'DESC' },
          status: this.$status,
          storeType: 'other'
        }
        this.$http.get('/trade/purchase/status/createtime', {params}).then(res => {
          this.isSearchSearchingMore = false
          this.orderList = [...this.orderList, ...res.data.content]
        })
      },
      ChangeList(_tp) {
        this.activeType = _tp
        this.page = 1
        let status = ''
        if (_tp === 'tobeconfirmed') {
          status = '501-504-524-525'
        } else if (_tp === 'comfirmed') {
          status = '502-406'
        } else if (_tp === 'inbound') {
          status = '404-511'
        }
        this.$status = status
        this.$store.dispatch('order/getSellOrderList', {
          count: 5,
          page: 1,
          sorting: { createtime: 'DESC' },
          status: status,
          storeType: 'other'})
      },
      // 是否为分期付款
      installmentDetailPaid(item) {
        if (!item.installment) return false
        let _flag = false
        item.installment.installmentDetails.forEach(list => {
          if (list.detno === item.installment.currentNo && list.status === 504) {
            _flag = true
          }
        })
        return _flag
      },
      // 发货
      sendGoods(item) {
        let _id
        if (item.inid) {
          _id = EncryptionFilter(item.inid)
          this.$http.get(`/trade/inFpu/tobeshipped/${_id}`).then(res => {
            this.sendGoodsInfo = res.data[0]
            this.sendGoodsInfo.area = JSON.parse(res.data[0].jsonSpAddress)
            this.showSend = true
          })
        } else {
          this.$http.get(`/trade/purchase/vendor/tobeshiped/${item.id}`).then(res => {
            _id = EncryptionFilter(res.inid)
            this.$http.get(`/trade/inFpu/tobeshipped/${_id}`).then(res => {
              this.sendGoodsInfo = res.data[0]
              this.sendGoodsInfo.area = JSON.parse(res.data[0].jsonSpAddress)
              this.showSend = true
            })
          }, err => {
            this.onMind('转出货单失败' + err.data)
          })
        }
      },
      onMind(str) {
        this.collectResult = str
        this.timeoutCount++
      },
      // 搜索
      searchSeek() {
        this.seekKeyword
      },
      blur: function() {
        setTimeout(() => {
          this.$store.dispatch('mobile/SetInputGetFocus', false)
        }, 300)
      },
      inputGetFocus: function() {
        setTimeout(() => {
          this.$store.dispatch('mobile/SetInputGetFocus', true)
        }, 300)
      }
    },
    components: {
      RemindBox,
      PullUp
    }
  }
</script>

<style scoped lang="scss">
  @mixin overFlowHidden {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  @mixin lineHeight($value) {
    height: $value;
    line-height: $value;
  }
.order-wrapper {
  background: #f1f3f6;
  margin: 0.88rem 0 0.98rem 0;
  height: calc(100vh - 0.88rem - 0.98rem);
  overflow-y: scroll;
  padding-bottom: 0.2rem;
  .order-nav {
    background: #fff;
    div {
      height: 0.82rem;
      line-height: 0.82rem;
      display: inline-block;
      width: 25%;
      text-align: center;
      font-size: .28rem;
      color: #666;
      &.active span{
        color: #3f84f6;
        border-bottom: 0.03rem solid #3f84f6;
        padding-bottom: 0.2rem;
      }
    }
  }
  .order-list-wrap {
    li {
      margin-top: 0.2rem;
    }
    .list-wrap-title{
       padding: 0 0.2rem;
       background: #fff;
       @include lineHeight(0.88rem);
       font-size: 0.28rem;
       color: #333;
       .pull-left {
         width: 5rem;
         @include overFlowHidden()
       }
       .pull-right {
         .red {
           color: #f21c1c;
         }
       }
    }
    .list-wrap-content {
      background: #f9f9f8;
      padding: 0.3rem 0.2rem;
      border-bottom: 1px solid #e4e4e4;
      .list-item {
        font-size: 0.28rem;
        color: #333;
        line-height: 0.6rem;
        .list-wrap-content-brand {
          width: 4.8rem;
          @include overFlowHidden();
        }
        .name {
          color: #666;
        }
        .lab {
          color: #999;
          font-size: 0.24rem;
          span {
            font-size: 0.16rem;
          }
        }
        .pri {
          font-size: 0.32rem;
          color: #f43938;
          span {
            font-size: 0.24rem;
          }
        }
      }
    }
    .list-all-info{
      text-align: right;
      background: #fff;
      padding: 0 0.2rem;
      @include overFlowHidden();
      @include lineHeight(0.66rem);
      font-size: 0.26rem;
      color: #333;
      border-bottom: 1px solid #e4e4e4;
      .pri {
        font-size: 0.32rem;
        color: #f43938;
      }
      .red {
        color: #f43938;
        font-size: 0.24rem;
      }
      .lab {
        color: #999;
        font-size: 0.24rem;
      }
    }
    .list-btn {
      background: #fff;
      @include lineHeight(0.8rem);
      padding: 0 0.2rem;
      div {
        display: inline-block;
        width: 1.5rem;
        @include lineHeight(0.56rem);
        border: 0.01rem solid #333;
        font-size: 0.26rem;
        color: #333;
        text-align: center;
        margin-top: 0.11rem;
        margin-left: 0.2rem;
        border-radius: 3px;
        &.sendGoods {
          color: #f43938;
          border: 0.01rem solid #f43938;
        }
      }
    }
  }
  .sendGoods_Alert {
    background: #fff;
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    border-top-left-radius: 5px;
    border-top-right-radius: 5px;
    overflow: hidden;
    .sendGoods_title {
      color: #333;
      font-size: 0.42rem;
      text-align: center;
      line-height: 1rem;
      font-weight: 600;
      position: relative;
      i {
        position: absolute;
        font-size: 16px;
        right: 0.2rem;
      }
    }
    .sendGoods_buyer {
      padding: 0.3rem 0.2rem;
      border-bottom: 1px solid #d9d9d9;
      border-top: 1px solid #d9d9d9;
      .sendGoods_buyer_top {
        color: #333;
        font-size: 0.3rem;
        font-weight: bold;
        .name {
          margin-right: 0.3rem;
          margin-left: 0.32rem;
        }
      }
      .sendGoods_buyer_bottom {
        color: #333;
        font-size: 0.26rem;
        margin-top: 0.2rem;
        img {
          width: 0.22rem;
          height: 0.29rem;
          margin-right: 0.1rem;
          margin-top: -0.1rem;
        }
      }
    }
    .sendGoods_list {
      padding: 0 0.2rem;
      @include lineHeight(0.8rem);
      border-bottom: 1px solid #d9d9d9;
      font-size: 0.32rem;
      color: #333;
      .ti {
        font-weight: bold;
      }
      .pull-right {
        font-size: 0.28rem;
        color: #999;
        img {
          width: 0.14rem;
          height: 0.24rem;
          vertical-align: middle;
          margin-top: -0.02rem;
          margin-left: 0.1rem;
        }
      }
      input {
        width: 4rem;
        text-align: right;
        color: #333;
      }
    }
    .sendGoods_Btn {
      @include lineHeight(1rem);
      text-align: center;
      color: #fff;
      font-size: 0.36rem;
      background: #3f84f6;
    }
  }
  .search-content {
    margin-top: 0.88rem;
    margin-bottom: 0.2rem;
    text-align: center;
    padding: .25rem 0 0 0;
    input {
      border: 1px solid #376ff3;
    }
    span {
      height: .46rem;
      line-height: .46rem;
    }
  }
  .peisong_Alert {
    background: #f1f3f6;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    top: 0;
    z-index: 11111;
    ul {
      li {
        line-height: 1rem;
        background: #fff;
        padding: 0 0.2rem;
        font-size: 0.28rem;
        color: #333;
        border-bottom: 1px solid #e0e1e2;
        div.target {
          width: 0.36rem;
          height: 0.36rem;
          border-radius: 50%;
          overflow: hidden;
          border: 1px solid #a4a4a4;
          margin-top: 0.3rem;
          &.active {
            background-image: url('/images/order/getChoose_icon.png');
            background-size: 100% 100%;
          }
        }
      }
    }
  }
}
</style>
