<template>
<view id="off-detail" v-if="init">
    <view class="coupon" @tap="changeShare">
        <view class="coupon-left" :class="is_coupon != 1 ? 'discount' : ''" :style="'background: ' + config.mainColor">
            <view class="text-wrap">
                <view class="text-box">
                    <view class="text">
                        <text class="money" v-if="detail.action_type.type == 'cash'">￥</text>
                        <sapn>{{ detail.action_type.value }}</sapn>

                        <text class="money" v-if="detail.action_type.type == 'discount'">折</text>
                    </view>
                    <text class="label text">{{ detail.label }}</text>
                </view>
            </view>
            <view class="dot-wrap">
                <view class="dot-item">

                </view>
                <view class="dot-item">

                </view>
                <view class="dot-item">

                </view>
                <view class="dot-item">

                </view>
                <view class="dot-item">

                </view>
                <view class="dot-item">

                </view>
                <view class="dot-item">

                </view>
                <view class="dot-item">

                </view>
            </view>
        </view>
        <view class="coupon-right">
            <view class="top">
                <span class="type">
                    <span v-if="detail.channel == 'ec'">商城</span>
                    <span v-else>门店</span>
                </span>
                <text class="info">{{detail.title}}</text>
            </view>
            <view class="bottom">
                <view class="bottom-use">
                    <view class="tiem-box">
                        <text>{{detail.use_start_time}}至{{detail.use_end_time}}</text>
                    </view>
                </view>
            </view>
            <view class="share mx-1px-top" v-if="detail.agent_code">
                <view class="text">
                    <i class="iconfont icon-fenxianganniu"></i>
                    <view>分享优惠券</view>
                </view>
                <view class="iconfont icon-jiantou">

                </view>
            </view>
        </view>
    </view>
    <view class="detail-item user-info" v-if="detail.intro">
        <view class="title">
            使用说明
        </view>
        <view class="info mx-1px-top">
            {{detail.intro}}
        </view>
    </view>
    <view class="qr_code" v-if="detail.qr_code_img">
        <image :src="detail.qr_code_img"></image>
    </view>

    <view class="button-box" v-if="is_coupon == 1 && detail.agent_code" :style="'background: ' + config.mainColor">
        <view class="btn" @tap="changeShare">
            点击分享
        </view>
    </view>

    <!--弹出分享部分-->
    <view class="maks" :class="show_share ? 'cur' : ''" @tap="changeShare">

    </view>

    <view class="share-box" :class="show_share ? 'cur' : ''">
        <!-- #ifdef MP-WEIXIN -->
        <button class="shaer-item" open-type="share">
            分享给好友
        </button>
        <!-- #endif -->
		<!-- #ifdef APP-PLUS -->
		<button class="shaer-item" @click="shareapp">
			分享给好友
		</button>
		<!-- #endif -->
        <view class="shaer-item mx-1px-top" @tap="getShearImg">
            生成海报
        </view>
        <view class="shaer-item clear" @tap="changeShare">
            取消
        </view>
    </view>
</view>
</template>
<script>
import {pageLogin, getUrl,config,is} from '@/common/js/utils.js';

export default {
  data() {
    return {
      detail: '',
      barcode: '',
      is_coupon: 1,
      // 用于判断是否为优惠券 1：优惠券 0：促销折扣
      init: false,
      show_share: false,
      config: ''
    };
  },

  onShareAppMessage() {
    this.setData({
      show_share: false
    });
    return {
      title: this.detail.title,
      path: '/pages/coupon/onDetail/onDetail?id=' + this.id + '&is_coupon=1&agent_code=' + this.detail.agent_code
    };
  },

  onLoad(e) {
    // 第三方平台配置颜色
    var gbConfig = this.$cookieStorage.get('globalConfig') || '';
    this.setData({
      config: gbConfig
    });
    var id = e.id;
    var coupon_id = e.coupon_id;
    this.setData({
      id: e.id
    });
    this.queryCouponDetail(id, coupon_id);
  },
  methods: {
	  // 分享给app的好友
	  shareapp(){
		 this.setData({
		   show_share: false
		 });
		  var title = this.detail.title;
		  var id  = this.id;
		  var agent_code = this.detail.agent_code;
	  		  uni.getProvider({
	  			  service:'share',
	  			  success:function(res){
	  				  uni.share({
	  				       provider: "weixin",
	  				       scene: "WXSceneSession",
	  				       type: 5,
	  				       title:title,
	  				       imageUrl:'https://cdn.guojiang.club/guojiang_logo.png',
	  				  			  miniProgram:{
	  				  				  id:'gh_72e65196278a',
	  				  				  path:'pages/coupon/onDetail/onDetail?id=' + id + '&is_coupon=1&agent_code=' + agent_code,
	  				  				  type:0,
	  				  				  webUrl: 'https://guojiang.club'
	  				  			  },
	  				       success: function (res) {
	  				           console.log("success:" + JSON.stringify(res));
	  				       },
	  				       fail: function (err) {
	  				           console.log("fail:" + JSON.stringify(err));
	  				       }
	  				   });
	  			  }
	  		  })
	  		  
	  		 
	  },
    changeShare() {
      this.setData({
        show_share: !this.show_share
      });
    },

    //生成海报
    getShearImg() {
      wx.navigateTo({
        url: '/pages/distribution/shareImg/shareImg?id=' + this.id + '&channel=' + this.detail.channel
      });
      this.changeShare();
    },

    // 查询优惠券详情
    queryCouponDetail(id, coupon_id) {
      wx.showLoading({
        title: "加载中",
        mask: true
      });
      var coupon_ids = coupon_id ? coupon_id : '';
      var token = this.$cookieStorage.get('user_token');
      this.$http.get({
        api: 'api/discount/' + id + '/detail',
        data: {
          is_agent: 1,
          coupon_id: coupon_ids
        },
        header: {
          Authorization: token
        }
      }).then(res => {
        if (res.statusCode == 200) {
          res = res.data;

          if (res.status) {
            this.setData({
              detail: res.data,
              init: true
            });

            if (!res.data.agent_code) {
              wx.showModal({
                content: '当前用户非分销员',
                showCancel: false,
                success: res => {
                  if (res.confirm || !res.cancel && !res.confirm) {
                    wx.navigateBack({
                      delta: 1
                    });
                  }
                }
              });
              this.setData({
                init: false
              });
              wx.hideShareMenu();
            }
          } else {
            wx.hideShareMenu();
            wx.showModal({
              content: res.message || '请求失败',
              showCancel: false
            });
          }
        } else {
          wx.hideShareMenu();
          wx.showModal({
            content: "请求失败",
            showCancel: false
          });
        }

        wx.hideLoading();
      }).catch(rej => {
        wx.hideShareMenu();
        wx.hideLoading();
        wx.showModal({
          content: '请求失败',
          showCancel: false
        });
      });
    },

    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  },
  computed: {},
  watch: {}
};
</script>
<style rel="stylesheet/less" lang="less">
    @import "offDetail";
</style>
