<template>
  <view v-show="!isLoading" class="container" :style="appThemeStyle">
    <!-- 商品图片轮播 -->
    <SlideImage v-if="!isLoading" :video="goods.video" :videoCover="goods.videoCover" :images="goods.goods_images" />

    <!-- 商品信息 -->
    <view v-if="!isLoading" class="goods-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">
          <view class="active-tag">
            <text>{{ goods.active_type != ActiveTypeEnum.NORMAL.value ? ActiveTypeEnum[goods.active_type].name2 : '多人拼团' }}</text>
          </view>
          <!-- 拼团价 -->
          <text class="floor-price__samll">￥</text>
          <text class="floor-price">{{ goods.groupon_price }}</text>
          <!-- 商品原价 -->
          <text class="original-price">￥{{ goods.original_price }}</text>
        </view>
        <view class="block-right dis-flex">
          <!-- 销量 -->
          <view class="goods-sales">
            <text>已抢{{ goods.active_sales }}件</text>
          </view>
        </view>
      </view>
      <!-- 标题、分享 -->
      <view class="info-item info-item__name dis-flex flex-y-center">
        <view class="goods-name flex-box">
          <text class="twoline-hide">{{ goods.goods_name }}</text>
        </view>
        <view class="goods-share__line"></view>
        <view class="goods-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="goods.selling_point" class="info-item info-item_selling-point">
        <text>{{ goods.selling_point }}</text>
      </view>
      <!-- 活动倒计时 -->
      <view v-if="goods.active_status != ActiveStatusEnum.STATE_END.value" class="info-item info-item_status info-item_countdown dis-flex flex-y-center">
        <text class="countdown-icon iconfont icon-naozhong"></text>
        <text>距离拼团活动{{ goods.active_status == ActiveStatusEnum.STATE_SOON.value ? '开始' : '结束' }}</text>
        <text class="m-r-10">还剩</text>
        <count-down :date="goods.end_time" separator="zh" theme="text" />
      </view>
      <!-- 活动已结束 -->
      <view v-else class="info-item info-item_status info-item_end">
        <text class="countdown-icon iconfont icon-naozhong"></text>
        <text>拼团活动已结束，下次记得早点来哦~</text>
      </view>
    </view>

    <!-- 凑团信息 -->
    <TaskList v-if="!isLoading" :grouponGoodsId="goods.groupon_goods_id" :list="goods.taskQuickJoinList" />

    <!-- 选择商品规格 -->
    <view v-if="goods.spec_type == 20" class="goods-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 goods.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>

    <!-- 拼团玩法 -->
    <view class="rule-nav m-top20 b-f" @click="handleShowRules()">
      <view class="top-nav dis-flex flex-x-between">
        <text>拼团玩法</text>
        <text class="f-25 col-9">查看规则</text>
      </view>
      <!-- 拼团步骤 -->
      <view class="rule-simple dis-flex flex-x-around">
        <view class="simple-item dis-flex flex-dir-column flex-y-center">
          <view class="i-number dis-flex flex-x-center flex-y-center">
            <text class="f-30">1</text>
          </view>
          <view class="i-text f-28">选择商品</view>
        </view>
        <view class="simple-item dis-flex flex-dir-column flex-y-center">
          <view class="i-number dis-flex flex-x-center flex-y-center">
            <text class="f-30">2</text>
          </view>
          <view class="i-text f-28">开团/参团</view>
        </view>
        <view class="simple-item dis-flex flex-dir-column flex-y-center">
          <view class="i-number dis-flex flex-x-center flex-y-center">
            <text class="f-30">3</text>
          </view>
          <view class="i-text f-28">邀请好友</view>
        </view>
        <view class="simple-item dis-flex flex-dir-column flex-y-center">
          <view class="i-number dis-flex flex-x-center flex-y-center">
            <text class="f-30">4</text>
          </view>
          <view class="i-text f-28">人满成团</view>
        </view>
      </view>
    </view>

    <!-- 商品SKU弹窗 -->
    <SkuPopup v-if="!isLoading" v-model="showSkuPopup" :skuMode="skuMode" :goods="goods" :buyMode="buyMode" />

    <!-- 商品评价 -->
    <Comment v-if="!isLoading" :goods-id="goods.goods_id" :limit="2" />

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

    <!-- 底部选项卡 -->
    <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>
          <!-- 客服 -->
          <customer-btn v-if="isShowCustomerBtn">
            <view class="fast-item">
              <view class="fast-icon">
                <text class="iconfont icon-kefu1"></text>
              </view>
              <view class="fast-text">
                <text>客服</text>
              </view>
            </view>
          </customer-btn>
          <!-- 购物车 (客服按钮不显示时) -->
          <view v-if="!isShowCustomerBtn" 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">
            <block v-if="goods.active_status == ActiveStatusEnum.STATE_BEGIN.value">
              <view v-if="goods.is_alone_buy" class="btn-item btn-item-deputy" @click="onShowSkuPopup(2)">
                <view class="price">￥{{ goods.original_price }}</view>
                <view>单独购买</view>
              </view>
              <view class="btn-item btn-item-main" @click="onShowSkuPopup(1)">
                <view class="price">￥{{ goods.groupon_price }}</view>
                <view>发起拼团</view>
              </view>
            </block>
            <view v-else class="btn-item btn-item-gray">
              <text>{{ goods.active_status == ActiveStatusEnum.STATE_SOON.value ? '活动未开始' : '活动已结束' }}</text>
            </view>
          </view>
        </view>
      </view>
    </view>

    <!-- 分享菜单 -->
    <share-sheet v-model="showShareSheet" :shareTitle="goods.goods_name" :shareImageUrl="goods.goods_image" :posterApiCall="posterApiCall" :posterApiParam="{ grouponGoodsId }" />

    <!-- 拼团规则弹窗 -->
    <u-modal v-if="!isLoading" v-model="showRules" title="拼团规则">
      <scroll-view style="height: 610rpx; touch-action: none;" :scroll-y="true">
        <view class="pops-content">
          <text>{{ setting.ruleDetail }}</text>
        </view>
      </scroll-view>
    </u-modal>

  </view>
</template>

<script>
  import { getSceneData } from '@/core/app'
  import ShareSheet from '@/components/share-sheet'
  import CustomerBtn from '@/components/customer-btn'
  import SkuPopup from './components/SkuPopup'
  import TaskList from './components/TaskList'
  import SlideImage from '../../goods/components/SlideImage'
  import Comment from '../../goods/components/Comment'
  import CountDown from '@/components/countdown'
  import * as GrouponGoodsApi from '@/api/groupon/goods'
  import * as CartApi from '@/api/cart'
  import SettingModel from '@/common/model/Setting'
  import { ActiveTypeEnum, ActiveStatusEnum } from '@/common/enum/groupon'

  export default {
    components: {
      ShareSheet,
      CustomerBtn,
      SlideImage,
      TaskList,
      SkuPopup,
      Comment,
      CountDown
    },
    data() {
      return {
        // 正在加载
        isLoading: true,
        // 枚举类
        ActiveTypeEnum,
        ActiveStatusEnum,
        // 显示/隐藏SKU弹窗
        showSkuPopup: false,
        // 按钮模式 1:都显示 2:只显示购物车 3:只显示立即购买
        skuMode: 3,
        // 购买模式 1:拼团购买 2:单独购买
        buyMode: 1,
        // 显示/隐藏分享菜单
        showShareSheet: false,
        // 获取商品海报图api方法
        posterApiCall: GrouponGoodsApi.poster,
        // 显示拼团规则
        showRules: false,
        // 拼团规则内容
        setting: {},
        // 当前拼团商品ID
        grouponGoodsId: null,
        // 拼团商品详情
        goods: {},
        // 购物车总数量
        cartTotal: 0,
        // 是否显示在线客服按钮
        isShowCustomerBtn: false
      }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    async onLoad(options) {
      // 记录query参数
      this.onRecordQuery(options)
      // 加载页面数据
      this.onRefreshPage()
      // 是否显示在线客服按钮
      this.isShowCustomerBtn = await SettingModel.isShowCustomerBtn()
    },

    methods: {

      // 记录query参数
      onRecordQuery(query) {
        const scene = getSceneData(query)
        this.grouponGoodsId = query.grouponGoodsId ? parseInt(query.grouponGoodsId) : parseInt(scene.gid)
      },

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

      // 获取拼团活动详情
      getActiveDetail() {
        const app = this
        return new Promise((resolve, reject) => {
          GrouponGoodsApi.detail(app.grouponGoodsId)
            .then(result => {
              app.goods = result.data.detail
              app.setting = result.data.setting
              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)
        })
      },

      // 显示拼团规则
      handleShowRules() {
        this.showRules = true
      },

      // 显示/隐藏SKU弹窗
      onShowSkuPopup(buyMode = 1) {
        this.buyMode = buyMode
        this.showSkuPopup = !this.showSkuPopup
      },

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

      // 跳转到首页
      onTargetHome(e) {
        this.$navTo('pages/index/index')
      },

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

    },

    /**
     * 分享当前页面
     */
    onShareAppMessage() {
      // 构建页面参数
      const app = this
      const params = app.$getShareUrlParams({
        grouponGoodsId: app.grouponGoodsId
      })
      return {
        title: app.goods.goods_name,
        path: `/pages/groupon/goods/index?${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({
        grouponGoodsId: app.grouponGoodsId
      })
      return {
        title: app.goods.goods_name,
        path: `/pages/groupon/goods/index?${params}`
      }
    },

  }
</script>

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