<template>
  <page-meta :page-style="themeColor"></page-meta>
  <view class="order-details">
    <!--商品信息-->
    <view class="top">
      <view class="left">
        <image class="top-image" :src="$util.img(info.goods_image)" mode="widthFix"></image>
      </view>
      <view class="right">
        <view class="goods-name">{{ info.goods_name }}</view>
        <view class="goods-status">订单状态：{{ status_text[info.status] }}</view>
        <view class="goods-type">
          订单类型：<text class="goods-type-text">采购订单</text>
        </view>
      </view>
    </view>
    <!--信息列表-->
    <view class="info-list">
      <view class="info-list-block">
        <view class="left">用户昵称</view>
        <view class="right">{{ info.seller_nickname }}</view>
      </view>
      <view class="info-list-block">
        <view class="left">订单金额</view>
        <view class="right right-price">￥{{ info.unit_price }}元</view>
      </view>
      <view class="info-list-block">
        <view class="left">订单编号<text class="copy-buttons" @click="$util.copy(info.order_no)">复制</text></view>
        <view class="right">{{ info.order_no }}</view>
      </view>
      <view class="info-list-block">
        <view class="left">购买时间</view>
        <view class="right">{{ $util.timeStampTurnTime(info.created_time) }}</view>
      </view>
      <view class="info-list-block">
        <view class="left">订单状态</view>
        <view class="right">{{ status_text[info.status] }}</view>
      </view>
      <view class="info-list-block" v-if="info.buyer_message">
        <view class="left">备注</view>
        <view class="right">{{ info.buyer_message }}</view>
      </view>
    </view>
    <!--操作按钮-->
    <view class="buttons">
      <view class="buttons-item buttons-item-pickup" @click="$refs.pickupConfirm.open()" v-if="parseInt(info.status) === 1">自提产品</view>
      <view class="buttons-item buttons-item-sale" @click="$refs.saleGoods.open()" v-if="parseInt(info.status) === 1">出售商品</view>
      <view class="buttons-item buttons-item-undercarriage" @click="$refs.undercarriageConfirm.open()" v-if="parseInt(info.status) === 2">下架商品</view>
    </view>
    <!-- 提货确认弹框 -->
    <uni-popup ref="pickupConfirm" type="dialog" class="order-remarks-content">
      <uni-popup-dialog mode="base" content="确认提货当前订单中的所有商品吗！" @close="$refs.pickupConfirm.close()" @confirm="pickup"></uni-popup-dialog>
    </uni-popup>
    <!-- 发布弹框 -->
    <uni-popup ref="saleGoods" type="center">
      <view class="transfer-price-content">
        <view class="transfer-price-top">
          <view class="top-left">详情</view>
          <view class="top-right">
            <i class="icondiy icon-system-guanbi" @click="$refs.saleGoods.close()"></i>
          </view>
        </view>
        <view class="transfer-price">
          <view>请输入转售价格：</view>
          <view>
            <input type="number" class="input" v-model="transfer_price"/>
          </view>
        </view>
        <view class="transfer-price-tips">转让价格必须大于{{ price_range.min_price }}并且小于等于{{ price_range.max_price }}！</view>
        <view class="transfer-price-button">
          <view class="transfer-buttons" @click="sale">确定出售</view>
        </view>
      </view>
    </uni-popup>
    <!-- 拆单确认弹框 -->
    <uni-popup ref="splitConfirm" type="dialog" class="order-remarks-content">
      <uni-popup-dialog mode="base" :content="split_message" @close="$refs.splitConfirm.close()" @confirm="splitConfirm()"></uni-popup-dialog>
    </uni-popup>
    <!-- 下架确认弹框 -->
    <uni-popup ref="undercarriageConfirm" type="dialog" class="order-remarks-content">
      <uni-popup-dialog mode="base" content="确认下架售卖中的商品吗！" @close="$refs.undercarriageConfirm.close()" @confirm="undercarriage"></uni-popup-dialog>
    </uni-popup>
    <!--登录弹框-->
    <ns-login ref="login"></ns-login>
    <!--加载动画-->
    <loading-cover ref="loadingCover"></loading-cover>
  </view>
</template>

<script>
import uniPopup from '@/components/uni-popup-new/uni-popup.vue';
import uniPopupDialog from '@/components/uni-popup-dialog/uni-popup-dialog.vue';

export default {
  components: {
    uniPopup,
    uniPopupDialog
  },
  data() {
    return {
      futures_id: 0,
      info: {},
      status_text: {
        1 : '库存',
        2 : '正在销售',
        3 : '已售出',
        4 : '提货中',
        5 : '提货完成',
        6 : '待支付',
        7 : '捡漏',
      },
      price_range: {},
      transfer_price: 0.00,
      split_message: '',
    };
  },
  mixins: [],
  onLoad(option) {
    this.futures_id = option.futures_id || 0;
    this.getDetail();
  },
  onShow() {},
  onReady(){
    // 判断是否登录
    if (!uni.getStorageSync('token')) this.$refs.login.open('/pages_promotion/futures/list');
  },
  methods: {
    // 获取订单详情
    getDetail() {
      this.$api.sendRequest({
        url: '/futures/api/futures/myDetail',
        data: {
          id: this.futures_id
        },
        success: res => {
          if (parseInt(res.code) === 0) {
            this.info = res.data;
            this.getSet();
          }

          if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
        },
        fail: res => {
          if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
        }
      });
    },
    // 获取基本设置
    getSet(){
      this.$api.sendRequest({
        url: '/futures/api/futures/getBasicsConfig',
        success: res => {
          if (parseInt(res.code) === 0) {
            this.price_range = res.data.price_range;
            //获取最小值 和 最大值
            let unitPrice = this.info.unit_price;
            let minPrice = unitPrice * (parseFloat(this.price_range.min) / 100);
            let maxPrice = unitPrice * (parseFloat(this.price_range.max) / 100);
            this.price_range.min_price = (parseFloat(unitPrice) + parseFloat(minPrice)).toFixed(2);
            this.price_range.max_price = (parseFloat(unitPrice) + parseFloat(maxPrice)).toFixed(2);

            this.transfer_price = this.price_range.max_price;
          }
        }
      });
    },
    // 提货
    pickup(){
      this.$refs.pickupConfirm.close();
      let _this = this;
      _this.$api.sendRequest({
        url: '/futures/api/futures/pickUp',
        data: {
          id: _this.futures_id
        },
        success: res => {
          _this.$util.showToast({
            title: res.message,
            mask: true,
            duration: 2000,
            success: function(){
              _this.getDetail();
              // setTimeout(() => {
              //   this.$util.redirectTo('/pages/index/index');
              // }, 1500);
            }
          });
        }
      });
    },
    // 发布
    sale(agree_split_order = 0){
      let _this = this;
      // 判断价格
      if(_this.transfer_price <= _this.price_range.min_price){
        _this.$util.showToast({
          title: '转售价格必须大于'+ _this.price_range.min_price,
          mask: true,
          duration: 1500,
        });
        return false;
      }
      if(_this.transfer_price > _this.price_range.max_price){
        _this.$util.showToast({
          title: '转售价格必须小于等于'+ _this.price_range.max_price,
          mask: true,
          duration: 1500,
        });
        return false;
      }
      // 请求发布
      _this.$api.sendRequest({
        url: '/futures/api/futures/release',
        data: {
          id: _this.futures_id,
          price: _this.transfer_price,
          agree_split_order: agree_split_order,
        },
        success: res => {
          // 关闭弹框
          if(_this.$refs.saleGoods) _this.$refs.saleGoods.close();
          // 返回结果处理
          if(parseInt(res.code) === -800){
            // 需要拆单  确认后再次请求发布
            this.split_message = res.message;
            _this.$refs.splitConfirm.open();
          }
          else if(parseInt(res.code) === 0){
            // 发布成功  跳转到已售出列表
            _this.$util.showToast({
              title: res.message,
              mask: true,
              duration: 2000,
              success: function () {
                setTimeout(() => {
                  _this.$util.redirectTo('/pages_promotion/futures/order', { status: 'release' });
                }, 1000);
              }
            });
          }
          else{
            // 失败
            _this.$util.showToast({
              title: res.message,
              mask: true,
              duration: 2000,
              success: function () {
                _this.getDetail();
              }
            });
          }
        }
      });
    },
    // 发布 - 确认拆单
    splitConfirm(){
      this.$refs.splitConfirm.close();
      this.sale(1);
    },
    // 下架
    undercarriage(){
      this.$refs.undercarriageConfirm.close();
      let _this = this;
      _this.$api.sendRequest({
        url: '/futures/api/futures/stock',
        data: {
          id: _this.futures_id
        },
        success: res => {
          _this.$util.showToast({
            title: res.message,
            mask: true,
            duration: 2000,
            success: function(){
              _this.getDetail();
              // setTimeout(() => {
              //   this.$util.redirectTo('/pages/index/index');
              // }, 1500);
            }
          });
        }
      });
    }
  },
  onBackPress(options) {
    if (options.from === 'navigateBack') return false;
    this.$util.redirectTo('/pages/member/index');
    return true;
  }
};
</script>

<style lang="scss" scoped>
.order-details{
  background: #fafcff;//fafcff
  min-height: calc(100vh - (30rpx * 2));
  padding: 30rpx;

  .top{
    --top-height--: 150rpx;
    width: calc(100% - (20rpx * 2));
    height: var(--top-height--);
    padding: 20rpx;
    display: inline-flex;
    flex-direction: row;
    flex-wrap: nowrap;
    align-items: center;
    justify-content: flex-start;
    background-color: #FFFFFF;
    .left{
      width: var(--top-height--);
      height: var(--top-height--);
      border-radius: 20rpx;
      .top-image{
        width: 100% !important;
        height: 100% !important;
      }
    }
    .right{
      padding-left: 20rpx;
      width: calc(100% - var(--top-height--) - 20rpx);
      .goods-name{
        height: calc(var(--top-height--) / 2);
        line-height:  calc(var(--top-height--) / 4);
        font-size: 30rpx;
        font-weight: bold;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-box-orient: vertical; /*设置对齐模式*/
        -webkit-line-clamp: 2; /*设置多行的行数*/
        color: #404040;
      }
      .goods-status{
        height: calc(var(--top-height--) / 4);
        line-height:  calc(var(--top-height--) / 4);
        font-size: 26rpx;
        color: #7e7e7e;
      }
      .goods-type{
        height: calc(var(--top-height--) / 4);
        line-height:  calc(var(--top-height--) / 4);
        font-size: 26rpx;
        color: #7e7e7e;
        .goods-type-text{
          color: #fc6b64;
        }
      }
    }
  }
  .info-list{

    .info-list-block{
      background: #ffffff;
      margin-top: 20rpx;
      border-radius: 10rpx;
      padding: 20rpx;
      width: calc(100% - (20rpx * 2));
      display: inline-flex;
      flex-direction: row;
      flex-wrap: nowrap;
      justify-content: space-between;
      align-items: center;
      align-content: center;
      .left{
        font-size: 26rpx;
        font-weight: bold;
        color: #2d2d2d;
        .copy-buttons{
          font-size: 22rpx;
          text-decoration: underline;
          margin-left: 5px;
          color: #868686;
        }
      }
      .right{
        font-size: 26rpx;
        color: #898989;
        max-width: calc(100% - 160rpx);
      }
      .right-price{
        color: #fb5e56;
      }
    }
  }
  .buttons{
    position: absolute;
    bottom: 70rpx;
    left: 0;
    width: 100%;
    display: inline-flex;
    flex-direction: row;
    flex-wrap: nowrap;
    justify-content: space-evenly;
    align-items: center;
    .buttons-item{
      background: red;
      height: 60rpx;
      line-height: 60rpx;
      font-size: 30rpx;
      padding: 0 50rpx;
      border-radius: 100rpx;
    }
    .buttons-item-pickup{
      color: #fef9eb;
      background: #f8ce2e;
    }
    .buttons-item-sale{
      color: #fee6e6;
      background: #ff4445;
    }
    .buttons-item-undercarriage{
      color: #fee6e6;
      background: #ff4445;
    }
  }
  .transfer-price-content{
    width: calc(90vw - (30rpx * 2));
    padding: 30rpx;
    background-color: #FFFFFF;
    border-radius: 30rpx;

    .transfer-price-top{
      display: inline-flex;
      width: 100%;
      flex-direction: row;
      flex-wrap: nowrap;
      align-items: center;
      justify-content: space-between;
      align-content: center;
      height: 50rpx;
      .top-right{
        i{
          font-size: 40rpx!important;
        }
      }
    }
    .transfer-price{
      display: inline-flex;
      width: 100%;
      flex-direction: row;
      flex-wrap: nowrap;
      align-items: center;
      justify-content: flex-start;
      margin-top: 20rpx;
      font-size: 30rpx;
      .input{
        font-size: 30rpx;
        border-bottom: 2rpx solid #e1dfe0;
        height: 60rpx;
      }
    }
    .transfer-price-tips{
      font-size: 24rpx;
      height: 40rpx;
      line-height: 30rpx;
      color: #b5b2b3;
    }
    .transfer-price-button{
      width: 100%;
      display: inline-flex;
      flex-direction: row;
      flex-wrap: nowrap;
      align-items: center;
      justify-content: center;
      margin: 60rpx 0 20rpx 0;
      .transfer-buttons{
        width: 60%;
        text-align: center;
        height: 70rpx;
        line-height: 70rpx;
        font-size: 30rpx;
        background: #0282ff;
        color: #99d9e9;
        border-radius: 100rpx;
      }
    }
  }
}
</style>
