<template>
  <!-- #ifdef H5 -->
  <zj-page-layout :hasFooter="true">
    <view class="all-container">
      <view class="common-title">订单信息</view>
      <view class="product-container card">
        <view class="it" v-for="(item, index) in goodsList" :key="index">
          <view class="name"
            >{{ item.goodsName }}<text>×{{ item.num }}{{ item.goodsSalesUnit }}</text></view
          >
          <view class="price">¥{{ (item.price * item.num) | priceFilter }}</view>
        </view>
        <view class="total">
          <view class="text">合计：</view>
          <view class="price">¥{{ totalPrice | priceFilter }}</view>
        </view>
      </view>

      <block v-if="websitData">
        <view class="common-title">提货信息</view>
        <view class="info-container card" v-if="websitData">
          <view class="top">
            <view class="left">{{ websitData.name }}</view>
            <view class="right">距离{{ (websitData.distance / 1000).toFixed(2) }}KM</view>
          </view>
          <view class="row">
            <view class="label">联系电话</view>
            <view class="value">{{ websitData.websitPhone }}</view>
          </view>
          <view class="row">
            <view class="label">提货地址</view>
            <view class="value">{{ websitData.address }}</view>
          </view>
        </view>
      </block>

      <view class="common-title">备注</view>
      <view class="remark-container card">
        <u--textarea v-model="remark" placeholder="请输入内容" border="none" autoHeight></u--textarea>
      </view>
    </view>

    <template slot="footer">
      <view class="bottom-container">
        <view class="left">
          <view class="text">共{{ totalNum }}件，合计：</view>
          <view class="price">¥{{ totalPrice | priceFilter }}</view>
        </view>
        <view class="btn">
          <u-button type="primary" text="确认订单" @click="submitData"></u-button>
        </view>
      </view>
    </template>
  </zj-page-layout>
  <!-- #endif -->

  <!-- #ifndef H5 -->
  <web-view
    :src="webViewHref(`/packageMaterial/pages/apply/order`, pam, crossPagePam)"
    @message="crossPage.$listener"
  ></web-view>
  <!-- #endif -->
</template>

<script>
// #ifdef H5
import wx from 'weixin-js-sdk'
export default {
  data() {
    return {
      type: 'M',
      applyName: '',
      websitId: null,
      websitName: null,
      goodsList: [],

      lat: '',
      lng: '',
      websitData: null,
      userInfo: {},
      remark: '',
    }
  },

  computed: {
    totalNum() {
      // let val = 0;
      // this.goodsList.forEach(item => {
      //   val = val + item.num;
      // })
      return this.goodsList.length;
    },
    totalPrice() {
      let val = 0;
      this.goodsList.forEach(item => {
        val = val + (item.num * (item.price * 100) / 100);
      })
      return  val;
    },
  },

  async onLoad() {
    let data = this.$getStorage('materialApplyData');
    if(data) {
      this.type = data.type;
      this.applyName = data.applyName;
      this.websitId = data.websitId;
      this.websitName = data.websitName;
      this.goodsList = data.goodsList;
    }
    this.$removeStorage('materialApplyData');

    this.userInfo = await this.$getUserInfo();
    var {latitude,longitude } = await this.$getLocation()
    this.lat = latitude;
    this.lng = longitude;
    this.getWebsitList();
  },

  methods: {
    getWebsitList() {
      this.$api.get('/user/apply/websit', {
        lat: this.lat,
        lng: this.lng,
      }).then(res => {
        this.websitData = res.data.find(o => o.websitId == this.websitId)
      })
    },

    submitData() {
      this.$api.postJson('/material/salses/add', {
        goodsType: this.type,
        remark: this.remark,
        workerId: this.userInfo.userId,
        workerName: this.userInfo.nickName,
        companyWechatId: this.userInfo.companyWechatId,
        websitId: this.websitId,
        websitName: this.websitName,
        websitBuyGoods: this.goodsList,
      }).then(res => {
        this.$navToPage(
            {
              url: `/packageMaterial/pages/stock/buyRecord`
            },
            'reLaunch'
          )
        // this.$navToPage({
        //   url: `/packageMaterial/pages/apply/pay?orderId=${res.data}&websitId=${this.websitId}&goodsType=${this.type}`
        // })
      })
    },
  },
}

// #endif
// #ifndef H5
export default {
  data() {
    return {
      pam: {},
    }
  },
  onLoad(pam) {
    this.pam = pam;
  }
}
// #endif
</script>

<style lang="scss" scoped>
.all-container {
  padding: 0 20rpx;
}
.common-title {
  font-size: 32rpx;
  font-weight: 600;
  margin-top: 30rpx;
}
.card {
  @include zj-card;
  margin-top: 20rpx;
  padding: 30rpx;
}
.product-container {
  .it {
    display: flex;
    align-items: flex-end;
    margin-bottom: 20rpx;
    .name {
      flex: 1;
      font-size: 28rpx;
      font-weight: 500;
      text {
        color: $sec-font;
        margin-left: 12rpx;
        font-weight: normal;
      }
    }
    .price {
      margin-left: 30rpx;
      font-size: 28rpx;
      font-weight: 500;
    }
  }
  .total {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    margin-top: 30rpx;
    .text {
      font-size: 28rpx;
      color: $sec-font;
    }
    .price {
      font-size: 28rpx;
      color: $minor-color;
      font-weight: 500;
    }
  }
}
.info-container {
  .top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    .left {
      font-weight: 500;
    }
    .right {
      font-size: 28rpx;
    }
  }
  .row {
    display: flex;
    align-items: center;
    margin-top: 20rpx;
    .label {
      font-size: 28rpx;
      color: $sec-font;
      margin-right: 20rpx;
    }
    .value {
      flex: 1;
      font-size: 28rpx;
      text {
        color: $minor-color;
      }
      &.price {
        color: $theme-color;
      }
    }
    .tag {
      font-size: 28rpx;
      color: $theme-color;
    }
  }
}
.remark-container {
  padding: 0;
  ::v-deep .u-textarea {
    padding: 20rpx 30rpx;
    min-height: 200rpx;
  }
}
.bottom-container {
  padding: 20rpx;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .left {
    display: flex;
    align-items: center;
    .text {
      font-size: 28rpx;
      color: $sec-font;
    }
    .price {
      font-size: 32rpx;
      color: $minor-color;
      font-weight: 500;
    }
  }
  .btn {
    width: 180rpx;
  }
}
</style>
