<!--
 * @Descripttion: 
 * @version: 
 * @Author: 王三华
 * @Date: 2023-05-26 09:39:11
-->
<template>
  <view class="content">
    <backIcon></backIcon>
    <!-- 轮播图 -->
    <u-swiper :list="imgUrlList"
              height="266"
              circular
              :autoplay="false"
              indicator
              indicatorMode="dot"></u-swiper>
    <view class="contxt">
      <view class="kuang">
        <view class="disFlex">
          <view class="t_red">{{ goodDetail.goodsPrice | numberToCurrency | isLocal }}</view>
          <view class="t_hui">{{ goodDetail.goodsAchive | numberToCurrency | isLocal }}</view>
        </view>
        <view class="disFlex">
          <view class="bao"
                v-if="goodDetail.prefixLabelTarget">[{{ goodDetail.prefixLabelTarget.label }}]
          </view>
          <view class="t_hei">{{ goodDetail.waresName }}</view>
        </view>
      </view>
      <view class="kuang t_hei">
        {{ '急速下单即享优先发货' }}
      </view>
      <view class="kuang">
        <view class="heng"
              @tap="selSku = true">
          <view class="disFlex">
            <view class="label">{{ '规格' }}</view>
            <view class="label1">{{ '已选' }}: {{ skuString }}</view>
          </view>
          <u-icon name="arrow-right"></u-icon>
        </view>
        <view class="heng">
          <view class="disFlex">
            <view class="label">{{ '服务承诺' }}</view>
            <view class="label1"
                  v-for="item in goodDetail.ensureLabelList"
                  :key="item.pkId">{{ item.label }}</view>
          </view>
        </view>
      </view>
      <view class="bTit">
        <view>{{ '商品详情' }}</view>
        <view class="btm"></view>
      </view>
      <u-parse :content="goodDetail.details"></u-parse>
    </view>

    <!-- 占空 -->
    <view style="height:150rpx"></view>
    <!-- 底部 -->
    <view class="footer_f">
      <view class="footer">
        <view class="footer_l">
          <view class="footer_li footer_ll">
            <u-icon size="18"
                    name="server-fill"></u-icon>
            <view @click="openKf">{{ '客服' }}</view>
          </view>
        </view>
        <view class="footer_l">
          <u-button type="success"
                    class="uBtn"
                    @tap="goBuy"
                    shape="circle"
                    color="linear-gradient(to right, #fb3024, #fb3024 )">{{'立即购买'}} </u-button>

        </view>
      </view>
    </view>
    <!-- 选择规格 -->
    <u-popup :show="selSku"
             mode="bottom"
             closeable
             @close="close">
      <view class="pop_a">
        <view class="pop_tt">
          <view class="pop_t">
            <img :src="imgUrlList[1]"
                 alt="">
            <view>{{ goodDetail.goodsPrice | numberToCurrency | isLocal }}</view>
          </view>
        </view>
        <view class="zhan"></view>
        <view class="pop">
          <view v-for="item,index in goodDetail.productParams"
                :key="item.productId"
                class="pop_c">
            <view class="ggTit">{{ item.productName }}*{{ item.quantity }}</view>
            <view v-for="ctem,cndex in item.waresItemsParamsList"
                  :key="ctem.pkSkuId"
                  class="pop_ci">
              <view class="pop_cl">
                <img :src="ctem.specCover"
                     alt="">
                <view class="gui_iit">{{ ctem.specsNameDian}}</view>
              </view>
              <u-number-box :min="0"
                            :max="ctem.maxNum"
                            @change="getNum(index,cndex,$event)"
                            v-model="ctem.quantity">
                <view slot="minus"
                      :class="ctem.quantity==0?'hui':'minus'">
                  <u-icon name="minus"
                          color="#FFFFFF"
                          size="12"></u-icon>
                </view>
                <text slot="input"
                      class="input">{{ctem.quantity}}</text>
                <view slot="plus"
                      :class="ctem.quantity==0?'hui':'plus'">
                  <u-icon name="plus"
                          color="#FFFFFF"
                          size="12"></u-icon>
                </view>
              </u-number-box>
            </view>
          </view>
        </view>
        <view class="zhan1"></view>
        <view class="pop_b">
          <view class="pop_bb">
            <u-button type="success"
                      class="uBtn"
                      shape="circle"
                      color="linear-gradient(to right, #fb3024, #fb3024 )"
                      @click="close">{{ '确认' }}</u-button>
          </view>
        </view>
      </view>
    </u-popup>
  </view>
</template>

<script>
import backIcon from '@/components/backIcon.vue'
import * as api from '@/config/goods'
import { mapGetters } from 'vuex'

export default {
  components: {
    backIcon,
  },
  data() {
    return {
     
      waresCode: '',
      specialArea: '',
      goodDetail: '',
      imgUrlList: [],
      selSku: false,
      skuString: '',
    }
  },
  computed: {
    ...mapGetters(['user']),
  },
  onLoad(options) {
    this.waresCode = options.waresCode
    this.specialArea = options.specialArea
    this.getDetails()
  },
  methods: {
    openKf() {
      let userInfo = uni.getStorageSync('User')
      let src =
        'https://im1c5366d.7x24cc.com/phone_webChat.html?accountId=N000000033467&chatId=302384fb-eda1-436e-a5e9-c03a2dbd6e97&visitorId=' +
        userInfo.memberCode +
        '&nickName=' +
        userInfo.memberCode
      window.open(src, '_blank')
    },
    close() {
      this.selSku = false
    },
    goBuy() {
      uni.navigateTo({
        url:
          '/pages/shareArea/orderShare?allData=' +
          JSON.stringify(this.goodDetail),
      })
    },
    getDetails() {
      api
        .waresDetail({
          specialArea: this.specialArea,
          waresCode: this.waresCode,
        })
        .then((res) => {
          this.goodDetail = res.data
          // let sl = this.goodDetail.sales + ''
          // if (sl >= 10000) {
          //   sl = sl.substring(0, sl.length - 4) + '万+'
          // }
          // this.goodDetail.sales = sl
          // this.mainImgUrl = res.data.videoUrl
          this.imgUrlList = [
            res.data.videoUrl,
            res.data.cover,
            res.data.cover1,
            res.data.cover2,
            res.data.cover3,
            res.data.cover4,
            res.data.cover5,
            res.data.cover6,
          ]
          this.imgUrlList = this.imgUrlList.filter(
            (item) => item && item.trim()
          )
          this.goodDetail.productParams.forEach((item, index) => {
            item.waresItemsParamsList.forEach((ctem, cndex) => {
              ctem.quantity = 0
              ctem.maxNum = 0
              if (ctem.specsName.length > 15) {
                ctem.specsNameDian = ctem.specsName.substring(0, 15) + '...'
              } else {
                ctem.specsNameDian = ctem.specsName
              }
            })
          })
          // 赋初值
          this.goodDetail.productParams.forEach((item, index) => {
            item.waresItemsParamsList.forEach((ctem, cndex) => {
              if (cndex == 0) {
                this.$set(ctem, 'quantity', item.quantity)
                this.$set(ctem, 'maxNum', item.quantity)
                // 初始总价格业绩
                this.$set(item, 'priceTotal', ctem.priceTotal)
                this.$set(item, 'achieveTotal', ctem.achieveTotal)
                this.$set(item, 'assAchieveTotal', ctem.assAchieveTotal)
              }
            })
          })
          this.$set(this.goodDetail, 'goodsPrice', 0)
          this.$set(this.goodDetail, 'goodsAchive', 0)
          this.$set(this.goodDetail, 'goodsAssAchive', 0)
          this.goodDetail.productParams.forEach((item) => {
            this.goodDetail.goodsPrice += parseFloat(item.priceTotal)
            this.goodDetail.goodsAchive += parseFloat(item.achieveTotal)
            this.goodDetail.goodsAssAchive += parseFloat(item.assAchieveTotal)
          })
          this.ggString()
        })
    },
    // 拼接已选
    ggString() {
      this.goodDetail.productParams.forEach((item) => {
        item.skuString = ''
        item.waresItemsParamsList.forEach((ctem, cndex) => {
          if (ctem.quantity > 0) {
            item.skuString += ctem.specsName + '*' + ctem.quantity
          }
        })
      })
      let skuString = ''
      this.goodDetail.productParams.forEach((item) => {
        skuString += item.productName + item.skuString
      })
      if (skuString.length > 19) {
        this.skuString = skuString.substring(0, 19) + '...'
      } else {
        this.skuString = skuString
      }
    },
    getNum(index, cndex, { value }) {
      this.goodDetail.productParams[index].waresItemsParamsList[
        cndex
      ].quantity = value
      let ggNum = 0
      this.goodDetail.productParams[index].waresItemsParamsList.forEach(
        (item, iindex) => {
          if (iindex == cndex) {
            ggNum += value
          } else {
            ggNum += item.quantity
          }
        }
      )
      if (this.goodDetail.productParams[index].quantity - value >= 0) {
        this.goodDetail.productParams[index].waresItemsParamsList.forEach(
          (stem, sndex) => {
            if (sndex != cndex) {
              this.$set(
                stem,
                'maxNum',
                this.goodDetail.productParams[index].quantity -
                  (ggNum - stem.quantity)
              )
            }
          }
        )
      }
      let that = this
      setTimeout(() => {
        that.calPrice()
      }, 50)
    },
    calPrice() {
      if (this.user.pkCountry == 1) {
        this.goodDetail.productParams.forEach((item) => {
          item.waresItemsParamsList.forEach((ctem) => {
            ctem.priceTotal = ctem.price * ctem.quantity
            ctem.assAchieveTotal = ctem.assAchieve * ctem.quantity
            ctem.achieveTotal =
              ctem.achieve * ctem.quantity * this.goodDetail.inExchangeRate
          })
        })
      } else {
        this.goodDetail.productParams.forEach((item) => {
          item.waresItemsParamsList.forEach((ctem) => {
            ctem.priceTotal = ctem.price * ctem.quantity
            ctem.achieveTotal = ctem.achieve * ctem.quantity
            ctem.assAchieveTotal = ctem.assAchieve * ctem.quantity
          })
        })
      }
      let price = 0
      let achieve = 0
      let assAchieve = 0
      this.goodDetail.productParams.forEach((item) => {
        item.waresItemsParamsList.forEach((ctem) => {
          price += ctem.priceTotal
          achieve += ctem.achieveTotal
          assAchieve += ctem.assAchieveTotal
        })
      })
      this.goodDetail.goodsPrice = price
      this.goodDetail.goodsAchive = achieve
      this.goodDetail.goodsAssAchive = assAchieve

      this.ggString()
    },
  },
}
</script>

<style lang="scss" scoped>
.content {
  background: #f8f8f8;
  min-height: 100vh;
}
.contxt {
  padding: 0 24rpx 20rpx 24rpx;
}
.kuang {
  margin-top: 20rpx;
  padding: 20rpx;
  background: #fff;
  box-shadow: 0px 2px 10px 0px rgba(204, 204, 204, 0.5);
  border-radius: 10px 10px 10px 10px;
  .disFlex {
    display: flex;
    align-items: center;
  }
  .t_red {
    font-size: 16px;
    font-family: Arial-Bold, Arial;
    font-weight: bold;
    color: #b42b2a;
    margin-right: 20px;
  }
  .t_hui {
    font-size: 11px;
    font-family: Arial-Bold, Arial;
    font-weight: bold;
    color: #999;
  }
  .bao {
    font-size: 13px;
    color: #de0214;
    font-weight: 600;
  }
}
.t_hei {
  font-size: 13px;
  font-family: PingFang SC-Semibold, PingFang SC;
  font-weight: 600;
  color: #333;
}
.heng {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  font-family: PingFang SC-Medium, PingFang SC;
  font-weight: 500;
  color: #999999;
  margin-bottom: 10rpx;
}
.label {
  margin-right: 40rpx;
}
.label1 {
  font-size: 13px;
  font-family: PingFang SC-Semibold, PingFang SC;
  font-weight: 600;
  color: #333333;
}
.bTit {
  text-align: center;
  margin: 30rpx 0;
  font-size: 16px;
  font-family: PingFang SC-Semibold, PingFang SC;
  font-weight: 600;
  color: #333333;
  .btm {
    width: 26px;
    height: 2px;
    background: #15b84b;
    border-radius: 2px 2px 2px 2px;
    margin: 10rpx auto;
  }
}
.pop_a {
  background: #f7f7f7;
  max-height: 80vh;
  overflow-y: auto;
}
.pop_tt {
  padding: 0 10rpx;
  margin: 0rpx 10rpx;
  height: 172rpx;
  background: #fff;
  position: fixed;
  width: calc(100% - 40rpx);
  display: flex;
  align-items: center;
  z-index: 1;
  .pop_t {
    display: flex;
    align-items: center;
    img {
      width: 58px;
      height: 58px;
      border-radius: 8px 8px 8px 8px;
      margin-right: 40rpx;
    }
    font-size: 28px;
    font-family: Arial-Bold, Arial;
    font-weight: bold;
    color: #b42b2a;
  }
}
.zhan {
  height: 172rpx;
}
.zhan1 {
  height: 130rpx;
}
.pop {
  padding: 10rpx 20rpx;
  margin: 20rpx 10rpx;
  box-shadow: 0px 2px 10px 0px rgba(204, 204, 204, 0.5);
  border-radius: 8px 8px 8px 8px;
  background: #fff;
  .pop_c {
    border-bottom: 8rpx solid rgba(0, 0, 0, 0.05);
    padding: 10rpx 0 20rpx 0;
    .ggTit {
      font-size: 12px;
      font-family: PingFang SC-Medium, PingFang SC;
      font-weight: 500;
      color: #333333;
      margin: 20rpx 0;
    }
    .pop_ci {
      display: flex;
      align-items: center;
      justify-content: space-between;
      background: #eeeeee;
      border-radius: 4px 4px 4px 4px;
      opacity: 1;
      margin-bottom: 20rpx;
      padding: 4rpx 12rpx;
      .pop_cl {
        display: flex;
        align-items: center;
        .gui_iit {
          margin-left: 20rpx;
        }
      }
    }
    img {
      width: 48rpx;
      height: 48rpx;
      border-radius: 4px 4px 4px 4px;
    }
  }
}
::v-deep .u-popup__content__close {
  z-index: 2;
}
.hui {
  width: 22px;
  height: 22px;
  background: #dbdbdb;
  border-radius: 3px 3px 3px 3px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.minus {
  width: 22px;
  height: 22px;
  background: rgba(249, 2, 0, 0.48);
  border-radius: 3px 3px 3px 3px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.plus {
  width: 22px;
  height: 22px;
  background: #e40301;
  border-radius: 3px 3px 3px 3px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.input {
  width: 50px;
  text-align: center;
  background: #fff;
  margin: 0 10rpx;
  border-radius: 3px 3px 3px 3px;
}
.pop_b {
  position: fixed;
  bottom: 0;
  height: 166rpx;
  background: #ffffff;
  width: 100%;
}
.pop_bb {
  padding: 20rpx 40rpx;
}
.footer_f {
  position: fixed;
  bottom: 0;
  width: 100%;
  z-index: 10;
  box-shadow: 0px -2px 20px 0px rgba(204, 204, 204, 0.5);
}
.footer {
  display: flex;
  justify-content: space-between;
  padding: 20rpx 24rpx 40rpx 60rpx;
  background: #fff;
  .footer_l {
    flex: 1;
    display: flex;
    align-items: center;
    font-size: 20rpx;
    font-weight: 500;
    color: #333333;
  }
  .footer_li {
    // position: relative;
    // text-align: center;
    display: flex;
    flex-direction: column;
    align-items: center;
  }
  .flout {
    position: absolute;
    top: -4px;
    right: -10px;
    background: #f56c6c;
    border-radius: 50%;
    width: 32rpx;
    height: 32rpx;
    color: #fff;
    text-align: center;
  }
  .footer_ll {
    margin-right: 84rpx;
  }
  .btns {
    border-radius: 18px;
    border: 1px solid #fbb61e;
    padding: 12rpx 0;
    width: 176rpx;
    text-align: center;
    font-size: 12px;
    font-weight: 500;
  }
  .btn3 {
    background: rgba(255, 255, 255, 0);
    color: #fbb61e;
    margin-right: 20rpx;
  }
  .btn4 {
    background: #fbb61e;
    color: #fff;
  }
}
</style>