<template>
  <equipment-sku-popup :value="value" @input="onChangeValue" border-radius="20" :localdata="equipmentInfo" :mode="skuMode" :maskCloseAble="true"
    :priceColor="appTheme.mainBg" :buyNowBackgroundColor="appTheme.mainBg" :addCartColor="appTheme.viceText" :addCartBackgroundColor="appTheme.viceBg"
    :activedStyle="{ color: appTheme.mainBg, borderColor: appTheme.mainBg, backgroundColor: activedBtnBackgroundColor }"
    @open="openSkuPopup" @close="closeSkuPopup" @add-cart="addCart" @buy-now="buyNow" buyNowText="立即租用" />
</template>

<script>
  import { setCartTotalNum } from '@/core/app'
  import { hex2rgba } from '@/utils/color'
  import * as CartApi from '@/api/cart'
  import EquipmentSkuPopup from '@/components/equipment-sku-popup'

  export default {
    components: {
      EquipmentSkuPopup
    },
    model: {
      prop: 'value',
      event: 'input'
    },
    props: {
      // true 组件显示 false 组件隐藏
      value: { 
        Type: Boolean,
        default: false
      },
      // 模式 1:都显示 2:只显示购物车 3:只显示立即购买
      skuMode: {
        type: Number,
        default: 1
      },
      // 商品详情信息
      equipment: {
        type: Object,
        default: {}
      }
    },
    data() {
      return {
        equipmentInfo: {}
      }
    },
    computed: {
      // 规格按钮选中时的背景色
      activedBtnBackgroundColor() {
        return hex2rgba(this.appTheme.mainBg, 0.1)
      }
    },
    created() {
      const app = this
      const { equipment } = app
      app.equipmentInfo = {
        _id: equipment.equipment_id,
        name: equipment.equipment_name,
        equipment_thumb: equipment.equipment_image,
        sku_list: app.getSkuList(),
        spec_list: app.getSpecList()
      }
    },

    methods: {

      // 监听组件显示隐藏
      onChangeValue(val) {
        this.$emit('input', val)
      },

      // 整理商品SKU列表
      getSkuList() {
        const app = this
        const { equipment: { equipment_name, equipment_image, skuList } } = app
        const skuData = []
        skuList.forEach(item => {
          skuData.push({
            _id: item.id,
            equipment_sku_id: item.equipment_sku_id,
            equipment_id: item.equipment_id,
            equipment_name: equipment_name,
            image: item.image_url ? item.image_url : equipment_image,
            price: item.equipment_price * 100,
            stock: item.stock_num,
            spec_value_ids: item.spec_value_ids,
            sku_name_arr: app.getSkuNameArr(item.spec_value_ids)
          })
        })
        return skuData
      },

      // 获取sku记录的规格值列表
      getSkuNameArr(specValueIds) {
        const app = this
        const defaultData = ['默认']
        const skuNameArr = []
        if (specValueIds) {
          specValueIds.forEach((valueId, groupIndex) => {
            const specValueName = app.getSpecValueName(valueId, groupIndex)
            skuNameArr.push(specValueName)
          })
        }
        return skuNameArr.length ? skuNameArr : defaultData
      },

      // 获取指定的规格值名称
      getSpecValueName(valueId, groupIndex) {
        const app = this
        const { equipment: { specList } } = app
        const res = specList[groupIndex].valueList.find(specValue => {
          return specValue.spec_value_id == valueId
        })
        return res.spec_value
      },

      // 整理规格数据
      getSpecList() {
        const { equipment: { specList } } = this
        const defaultData = [{ name: '默认', list: [{ name: '默认' }] }]
        const specData = []
        specList.forEach(group => {
          const children = []
          group.valueList.forEach(specValue => {
            children.push({ name: specValue.spec_value })
          })
          specData.push({
            name: group.spec_name,
            list: children
          })
        })
        return specData.length ? specData : defaultData
      },

      // sku组件 开始-----------------------------------------------------------
      openSkuPopup() {
        // console.log("监听 - 打开sku组件")
      },

      closeSkuPopup() {
        // console.log("监听 - 关闭sku组件")
      },

      // 加入购物车按钮
      addCart(selectShop) {
        const app = this
        const { equipment_id, equipment_sku_id, buy_num } = selectShop
        CartApi.add(equipment_id, equipment_sku_id, buy_num)
          .then(result => {
            // 显示成功
            app.$toast(result.message)
            // 隐藏当前弹窗
            app.onChangeValue(false)
            // 购物车商品总数量
            const cartTotal = result.data.cartTotal
            // 缓存购物车数量
            setCartTotalNum(cartTotal)
            // 传递给父级
            app.$emit('addCart', cartTotal)
          })
      },

      // 立即购买
      buyNow(selectShop) {
        // 跳转到订单结算页
        this.$navTo('package/equipment/cashier/index', {
          mode: 'buyNow',
          equipmentId: selectShop.equipment_id,
          equipmentSkuId: selectShop.equipment_sku_id,
          equipmentNum: selectShop.buy_num
        })
        // 隐藏当前弹窗
        this.onChangeValue(false)
      }

    }
  }
</script>

<style lang="scss" scoped>

</style>
