<template>
  <view v-show="!isLoading" class="container" :style="appThemeStyle">
    <!-- 设备图片轮播 -->
    <SlideImage v-if="!isLoading" :video="equipment.video" :videoCover="equipment.videoCover" :images="equipment.equipment_images" />

    <!-- 设备信息 -->
    <view v-if="!isLoading" class="equipment-info m-top20">
      <!-- 价格、销量 -->
      <view class="info-item info-item__top dis-flex flex-x-between flex-y-end">
        <view class="block-left dis-flex flex-y-center">
          <!-- 设备售价 -->
          <text class="floor-price__samll">￥</text>
          <text class="floor-price">{{ equipment.equipment_price_min }}</text>
          <!-- 会员价标签 -->
          <view v-if="equipment.is_user_grade" class="user-grade">
            <text>会员价</text>
          </view>
          <!-- 划线价 -->
          <text v-if="equipment.line_price_min > 0" class="original-price">￥{{ equipment.line_price_min }}</text>
        </view>
        <view class="block-right dis-flex">
          <!-- 销量 -->
          <view class="equipment-sales">
            <text>已售{{ equipment.equipment_sales }}件</text>
          </view>
        </view>
      </view>
      <!-- 标题、分享 -->
      <view class="info-item info-item__name dis-flex flex-y-center">
        <view class="equipment-name flex-box">
          <text class="twoline-hide">{{ equipment.equipment_name }}</text>
        </view>
        <view class="equipment-share__line"></view>
        <view class="equipment-share">
          <button class="share-btn dis-flex flex-dir-column" @click="onShowShareSheet()">
            <text class="share__icon iconfont icon-fenxiang"></text>
            <text class="f-24">分享</text>
          </button>
        </view>
      </view>
      <!-- 设备卖点 -->
      <view v-if="equipment.selling_point" class="info-item info-item_selling-point">
        <text>{{ equipment.selling_point }}</text>
      </view>
    </view>

    <!-- 选择设备规格 -->
    <view v-if="equipment.spec_type == 20" class="equipment-choice m-top20 b-f" @click="onShowSkuPopup(1)">
      <view class="spec-list">
        <view class="flex-box">
          <text class="col-8">选择：</text>
          <text class="spec-name" v-for="(item, index) in equipment.specList" :key="index">{{ item.spec_name }}</text>
        </view>
        <view class="f-26 col-9 t-r">
          <text class="iconfont icon-arrow-right"></text>
        </view>
      </view>
    </view>

    <!-- 设备服务 -->
    <Service v-if="!isLoading" :equipment-id="equipmentId" />

    <!-- 设备SKU弹窗 -->
    <SkuPopup v-if="!isLoading" v-model="showSkuPopup" :skuMode="skuMode" :equipment="equipment" @addCart="onAddCart" />

    <!-- 设备评价 -->
    <Comment v-if="!isLoading" :equipment-id="equipmentId" :limit="2" />

    <!-- 设备描述 -->
    <view v-if="!isLoading" class="equipment-content m-top20">
      <view class="item-title b-f">
        <text>设备描述</text>
      </view>
      <block v-if="equipment.content != ''">
        <view class="equipment-content__detail b-f">
          <mp-html :content="equipment.content" />
        </view>
      </block>
      <empty v-else tips="亲，暂无设备描述" />
    </view>

    <!-- 设备推荐 -->
    <recommended />

    <!-- 底部选项卡 -->
    <view class="footer-fixed">
      <view class="footer-container">
        <!-- 导航图标 -->
        <view class="foo-item-fast">
          <!-- 首页 -->
          <!-- <view class="fast-item fast-item--home" @click="onTargetHome">
            <view class="fast-icon">
              <text class="iconfont icon-shouye"></text>
            </view>
            <view class="fast-text">
              <text>站点</text>
            </view>
          </view> -->
		  <!-- #ifdef MP-WEIXIN -->
		  <button class="btn-normal" @click="onCollection(equipment.equipment_id)">
		    <view class="fast-item">
		      <view class="fast-icon">
				<u-icon name="star-fill" color="#fd8008" v-if="equipment.is_collection"></u-icon>
				<u-icon name="star" v-else></u-icon>
		      </view>
		      <view class="fast-text">
		        <text>收藏</text>
		      </view>
		    </view>
		  </button>
		  <!-- #endif -->
          <!-- 客服 (仅微信小程序端显示) -->
          <!-- #ifdef MP-WEIXIN -->
          <!-- <button class="btn-normal" open-type="contact">
            <view class="fast-item">
              <view class="fast-icon">
                <text class="iconfont icon-kefu1"></text>
              </view>
              <view class="fast-text">
                <text>客服</text>
              </view>
            </view>
          </button> -->
          <!-- #endif -->
          <!-- 购物车 -->
          <!-- <view class="fast-item fast-item--cart" @click="onTargetCart">
            <view v-if="cartTotal > 0" class="fast-badge fast-badge--fixed">{{ cartTotal > 99 ? '99+' : cartTotal }}
            </view>
            <view class="fast-icon">
              <text class="iconfont icon-gouwuche"></text>
            </view>
            <view class="fast-text">
              <text>购物车</text>
            </view>
          </view> -->
        </view>
        <!-- 操作按钮 -->
        <view class="foo-item-btn">
          <view class="btn-wrapper">
            <!-- <view class="btn-item btn-item-deputy" @click="onShowSkuPopup(2)">
              <text>加入购物车</text>
            </view> -->
            <view class="btn-item btn-item-main" @click="onShowSkuPopup(3)">
              <text>立即租用</text>
            </view>
          </view>
        </view>
      </view>
    </view>

    <!-- 快捷导航 -->
    <!-- <shortcut bottom="120rpx" /> -->

    <!-- 分享菜单 -->
    <share-sheet v-model="showShareSheet" :shareTitle="equipment.equipment_name" :shareImageUrl="equipment.equipment_image"
      :posterApiCall="posterApiCall" :posterApiParam="{ equipmentId }" />

  </view>
</template>

<script>
  import { getSceneData } from '@/core/app'
  import * as EquipmentApi from '@/api/equipment'
  import * as CartApi from '@/api/cart'
  import Recommended from '@/components/recommended'
  import ShareSheet from '@/components/share-sheet'
  import SlideImage from './components/SlideImage'
  import SkuPopup from './components/SkuPopup'
  import Comment from './components/Comment'
  import Service from './components/Service'

  export default {
    components: {
      Recommended,
      ShareSheet,
      SlideImage,
      SkuPopup,
      Comment,
      Service
    },
    data() {
      return {
        // 正在加载
        isLoading: true,
        // 当前设备ID
        equipmentId: null,
        // 设备详情
        equipment: {},
        // 购物车总数量
        cartTotal: 0,
        // 显示/隐藏SKU弹窗
        showSkuPopup: false,
        // 模式 1:都显示 2:只显示购物车 3:只显示立即购买
        skuMode: 1,
        // 显示/隐藏分享菜单
        showShareSheet: false,
        // 获取设备海报图api方法
        posterApiCall: EquipmentApi.poster
      }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
      // 记录query参数
      this.onRecordQuery(options)
      // 加载页面数据
      this.onRefreshPage()
	  // 浏览历史
	  this.saveFootMarkList(options.equipmentId)
    },

    methods: {
		// 浏览历史记录
	  saveFootMarkList(equipmentId){
		  const userId = uni.getStorageSync('userId')
		  const arr = uni.getStorageSync(`FOOTMARKLIST_${userId}`) || []
		  arr.unshift(Number(equipmentId))
		  return uni.setStorageSync(`FOOTMARKLIST_${userId}`,arr)
	  },
      // 记录query参数
      onRecordQuery(query) {
		  const scene = getSceneData(query)
		  this.equipmentId = query.equipmentId ? parseInt(query.equipmentId) : parseInt(scene.gid)
      },

      // 刷新页面数据
      onRefreshPage() {
        const app = this
        app.isLoading = true
        Promise.all([app.getEquipmentDetail(), app.getCartTotal()])
          .finally(() => app.isLoading = false)
      },

      // 获取设备信息
      getEquipmentDetail() {
		  const app = this
        return new Promise((resolve, reject) => {
          EquipmentApi.detail(app.equipmentId)
            .then(result => {
              app.equipment = result.data.detail
              resolve(result)
            })
            .catch(reject)
        })
      },

      // 获取购物车总数量
      getCartTotal() {
        const app = this
        return new Promise((resolve, reject) => {
          CartApi.total()
            .then(result => {
              app.cartTotal = result.data.cartTotal
              resolve(result)
            })
            .catch(reject)
        })
      },

      // 更新购物车数量
      onAddCart(total) {
        this.cartTotal = total
      },

      /**
       * 显示/隐藏SKU弹窗
       * @param {skuMode} 模式 1:都显示 2:只显示购物车 3:只显示立即购买
       */
      onShowSkuPopup(skuMode = 1) {
		  const shopInfo = uni.getStorageSync('shopInfo')
		  const clerkInfo = uni.getStorageSync('clerkInfo')
		  if(shopInfo || clerkInfo){
			  return this.$error('请使用会员账号购买')
		  }
        this.skuMode = skuMode
        this.showSkuPopup = !this.showSkuPopup
      },

      // 显示隐藏分享菜单
      onShowShareSheet() {
        this.showShareSheet = !this.showShareSheet
      },

      // 跳转到首页
      onTargetHome(e) {
		  const { equipment } = this
		console.log("this.equipment: ",equipment);
        this.$navTo('package/shops/detail/index',equipment)
      },
	  
	  // 加入收藏
	  onCollection(equipmentId){
		  EquipmentApi.collection({equipmentId})
		  this.equipment.is_collection = !this.equipment.is_collection
	  },

      // 跳转到购物车页
      onTargetCart() {
        this.$navTo('pages/cart/index')
      },

    },

    /**
     * 分享当前页面
     */
    onShareAppMessage() {
      const app = this
      // 构建页面参数
      const params = app.$getShareUrlParams({
        equipmentId: app.equipmentId,
      })
      return {
        title: app.equipment.equipment_name,
        path: `/pages/equipment/detail?${params}`
      }
    },

    /**
     * 分享到朋友圈
     * 本接口为 Beta 版本，暂只在 Android 平台支持，详见分享到朋友圈 (Beta)
     * https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/share-timeline.html
     */
    onShareTimeline() {
      const app = this
      // 构建页面参数
      const params = app.$getShareUrlParams({
        equipmentId: app.equipmentId,
      })
      return {
        title: app.equipment.equipment_name,
        path: `/pages/equipment/detail?${params}`
      }
    }
  }
</script>

<style>
  page {
    background: #fafafa;
  }
</style>
<style lang="scss" scoped>
  @import "./detail.scss";
</style>

