<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">服务费</view>
        <view class="right right-price">{{ info.service_price }}</view>
      </view>
      <view class="info-list-block">
        <view class="left">实际收入</view>
        <view class="right right-price">{{ info.real_income }}</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" v-if="info.release_time > 0">
        <view class="left">上架时间</view>
        <view class="right">{{ $util.timeStampTurnTime(info.release_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-info">
          <view class="transfer-info-block">
            <view class="info-title">服务费</view>
            <view class="info-content">{{ service_charge }}</view>
          </view>
          <view class="transfer-info-block">
            <view class="info-title">实际收入</view>
            <view class="info-content">{{ real_income }}</view>
          </view>
          <view class="transfer-info-block">
            <view class="info-title">建议售价</view>
            <view class="info-content">{{ price_range.max_price }}</view>
          </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: '',// 拆单确认信息
      service_charge: 0.00, // 手续费
      real_income: 0.00,// 实际收入
    };
  },
  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;// 默认 当前售价为最高价格
            // 计算手续费
            this.service_charge = (this.transfer_price * (parseFloat(res.data.service_price) / 100)).toFixed(2);
            // 计算实际收入
            this.real_income = (parseFloat(this.transfer_price) - parseFloat(this.service_charge)).toFixed(2);
          }
        }
      });
    },
    // 提货
    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(parseFloat(_this.transfer_price) <= parseFloat(_this.price_range.min_price)){
        _this.$util.showToast({
          title: '转售价格必须大于'+ _this.price_range.min_price,
          mask: true,
          duration: 1500,
        });
        return false;
      }
      if(parseFloat(_this.transfer_price) > parseFloat(_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-info{
      width: 100%;
      padding: 10px 0;
      display: inline-flex;
      flex-direction: row;
      flex-wrap: nowrap;
      align-content: center;
      justify-content: space-around;
      align-items: center;
      .transfer-info-block{
        width: calc(100% / 3);
        display: inline-flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        flex-wrap: nowrap;
        align-content: center;
        .info-title{
          font-size: 30rpx;
          color: #9f9d9e;
        }
        .info-content{
          font-size: 30rpx;
          font-weight: bold;
        }
      }
      .transfer-info-block:nth-of-type(1){
        .info-content{
          color: #ac736e;
        }
      }
      .transfer-info-block:nth-of-type(2){
        .info-content{
          color: #ac736e;
        }
      }
      .transfer-info-block:nth-of-type(3){
        .info-content{
          color: #d39a63;
        }
      }
    }
    .transfer-price-tips{
      font-size: 24rpx;
      height: 40rpx;
      line-height: 30rpx;
      color: #b5b2b3;
    }
    .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-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>
