// component/skuPopup/skuPopup.js
// import { ReportModel } from '../../utils/reportModel'
const wxService = require('../../utils/wxService')

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    currentHasUserInfo:{
      type: Boolean,
      type: false
    },
    disableNum: { // 不能改变的数量
      type: Number,
      observer: '_disableNumChange',
      value: 0
    },
    productName: {
      type: String,
      value: ''
    },
    exchangePrice: {
      type: Number,
      value: 0
    },
    isAllowArrive: {
      type: Number,
      value: 0
    },
    isNoticeStatus: {
      type: Boolean
      // value: false
    },
    integralRole: {
      type: Number,
      value: 0
    },
    priceNoChange: {
      type: Boolean,
      value: false
    },
    showPrice: { // 指定显示的价格
      type: Number,
      observer: '_showPriceChange',
      value: 0
    },
    quantityAverage: {
      type: String,
      value: '0'
    },
    btnText: {
      type: String,
      value: '确定'
    },
    imageDomain: {
      type: String,
      value: ''
    },
    specItem: { // 所有规格
      type: Object,
      value: []
    },
    // specValues: { // 所有规格值
    //   type: Object,
    //   value: []
    // },
    proNum: { // 初始化的数量
      type: Number,
      value: 1
    },
    goodType: { // 虚拟
      type: Number,
      value: 1
    },
    initSku: { // 初始化的sku
      type: Object,
      value: null
    },
    skuInfos: { // 所有sku信息
      type: Object,
      value: [],
      observer: '_skuInfosChange'
    },
    indexImg: { // 商品主图
      type: String,
      value: ''
    },
    show: { // 显示标识
      type: Boolean,
      value: false
    },
    productStock:{
      type: Number,
      value: 0
    }
  },

  attached () {
    // let price = this.data.showPrice
    // if (!price) {
    //   if (this.data.curSku.promotion_price) {
    //     price = this._setExchangePrice(this.data.curSku.promotion_price)
    //   }
    // }
    // console.log(this.data)
    // this.setData({
    //   price
    // })

  },

  /**
   * 组件的初始数据
   */
  data: {
    curSpec: [], // 当前选中的规格值
    curSku: {}, // 当前选中的sku信息
    // price: 0, // 页面上展示的价格
    skuText: '',
    skuStock: 0,
    skuId:'',
    btnText: '确定', // 确定 缺货 缺货订阅
    isOutStock: false, // 背景色
    clickDataStock: 0,
    salePrice:0,
    picUrl:''
  },

  detached: function () {
    this._disableNumChange = null
    this._showPriceChange = null
    this._skuInfosChange = null
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // _skuInfosChange () {
    //   var curSpec = []
    //   if (this.data.initSku) {
    //     console.log(this.data.initSku)
    //     curSpec = this.data.initSku.spec.split('###')
    //   }
    //   this.setData({
    //     curSpec: curSpec, // 当前选中的规格值
    //     curSku: {}, // 当前选中的sku信息
    //     skuText: '',
    //     skuStock: 0,
    //     salePrice:0
    //   })
    //   this._setCurSku()
    // },
    // _showPriceChange (newV, oldV) {
    //   if (newV !== oldV) {
    //     this.setData({
    //       showPrice: newV,
    //       price: newV
    //     })
    //   }
    // },
    confirm() {
      if (this.data.curSpec.length < 1){
        wx.showToast({
          title: '请选择规格',
          icon: 'none'
        })
        return false
      }else{
        for (var i = 0; i < this.data.curSpec.length; i++) {
          if (!this.data.curSpec[i]) {
            wx.showToast({
              title: '请选择规格',
              icon: 'none'
            })
            return false
          }
        }
      }

      this.triggerEvent('skuselect', {
        skuId: this.data.skuId,
        count: this.data.proNum,
      })
      // this.triggerEvent('skuselect', {
      //   selectSku: this.data.curSku,
      //   counts: this.data.proNum,
      //   isAllowArrive: this.data.isAllowArrive,
      //   clickDataStock: this.data.clickDataStock
      // })
    },
    inputNum: function (e) { // 输入数量
      var newNum = e.detail.value
      if (newNum === '' || newNum === '0') {
        newNum = 1
      } else {
        newNum = parseInt(newNum)
        if (this.data.quantityAverage == '0') {
          if (newNum > this.data.curSku.inventory_num) {
            newNum = this.data.curSku.inventory_num
            wx.showToast({
              title: '不能超过库存',
              icon: 'none'
            })
          }
        } else {
          if (newNum > this.data.quantityAverage) {
            newNum = this.data.quantityAverage
            wx.showToast({
              title: '每人限购' + this.data.quantityAverage + '件',
              icon: 'none'
            })
          }
        }
      }
      this.setData({
        proNum: newNum
      })
    },
    minusNum: function () { // 减少数量
      var proNum = this.data.proNum
      if (proNum > 1) {
        proNum--
      } else {
        wx.showToast({
          title: '数量不能为0',
          icon: 'none'
        })
      }
      this.setData({
        proNum: proNum
      })
    },
    addNum: function () { // 增加数量
      let proNum = this.data.proNum
      const curSku = this.data.curSku
      proNum ++
      this.setData({
        proNum
      })
     
    },
    hidePopup: function () { // 隐藏弹出框
      this.setData({
        show: false
      })
    },
    selectSku: function (event) { // 点击规格值，设置选中状态
      var clickData = event.target.dataset
      var curSpec = this.data.curSpec
      if (!clickData.disable) {
        curSpec[clickData.index] = {
          id: clickData.id,
          name: clickData.name
        }
        console.log(event)
        // curSpec.push({
        //   id: clickData.id,
        //   name: clickData.name
        // })
        this.setData({
          curSpec: curSpec
        }, this._setCurSku())
      } else {
        return false
      }
      console.log('........', clickData)
      // 有库存
      let btnText = '确定'
      let isOutStock = false
      // 没库存
      if (!this.data.skuStock) {
        isOutStock = true
        btnText = "商品暂无库存" 
      }
      this.setData({
        btnText, isOutStock, clickDataStock: clickData.stock
      })
    },

    _disableNumChange (newV, oldV) {
      if (newV !== oldV) {
        this.setData({
          disableNum: newV
        })
      }
    },

    // _setExchangePrice (price) {
    //   let exprice = price
    //   if (this.data.exchangePrice > 0) {
    //     let exchangePrice = this.data.exchangePrice// 积分数
    //     let bwtPrice = ((exchangePrice * this.data.integralRole - price) / 100)// 差额
    //     exprice = bwtPrice < 0 ? ((exchangePrice) + '积分+￥' + Math.abs(bwtPrice)) : (exchangePrice + '积分')
    //   }
    //   return exprice
    // },

    _setCurSku: function () { // 显示选中的sku信息
      if (!this.data.skuInfos) {
        return false
      }
      var proNum = this.data.proNum
      var curSpec = this.data.curSpec
      for (var i = 0; i < this.data.skuInfos.length; i++){
        var skuNameArray = this.data.skuInfos[i].specValue.split('###')
        // console.log(skuNameArray)
        var flag = true
        for (var j = 0; j < skuNameArray.length; j++){
          if (curSpec[j] && i !== j && curSpec[j] !== skuNameArray[j]) {
            flag = false
          }
        }
        if (flag) {
          this.setData({
            skuStock: this.data.skuInfos[i].stock,
            salePrice: this.data.skuInfos[i].salePrice,
            picUrl: this.data.skuInfos[i].picUrl,
            skuId: this.data.skuInfos[i].skuId
          })
        }
      }
      console.log('00000',this.data.skuStock)

      // 根据当前选中的规格，设置头部的规格信息
      var skuText = []
      if (curSpec.join('') === '') {
        skuText = ['请选择规格']
      } else {
        for (let i = 0; i < curSpec.length; i++) {
          if (!curSpec[i]) {
            skuText.push('请选择' + this.data.specItem[i].name)
          }else{
            skuText.push(curSpec[i].name)
          }
        }
      }
      var skuStock = 0
      var skuId = ''
      var salePrice = 0
      var picUrl = ''
      var curSku = 0
      var curSkus = this.data.skuInfos.filter(function (item) {
        curSku = item
        var flag = true
        for (let i = 0; i < curSpec.length; i++) {
          if (curSpec[i] && curSpec[i].id !== item.specValue.split('###')[i]) {
            flag = false
          }
          
        }
        if (flag) {
          skuStock = item.stock
          skuId = item.skuId
          salePrice = item.salePrice
          picUrl = item.picUrl
        }
        console.log(flag)
        
        return flag
      })
     
      if (proNum > curSku.stock) { // 选择数量超过库存时将数量修改为库存数
        proNum = curSku.stock
      }
      if (proNum == 0) {
        proNum = 1
      }
      // let price = curSku.promotion_price

      // price = this._setExchangePrice(price)

      let updateData = {
        curSpec: curSpec,
        curSku: curSku,
        skuStock: skuStock,
        skuText: skuText,
        proNum: proNum,
        skuId: skuId,
        salePrice: salePrice,
        picUrl: picUrl
      }
      // if (!this.data.priceNoChange) {
      //   updateData.price = price// 拼团规格没有价格信息
      // }
      this.setData(updateData)
      console.log('666', updateData.skuStock)
    },
    _getUserInfo(res = {}) {
      this.triggerEvent('getAuth', res);
    }
  }
})
