<template>
<view>
<!--pages/activity/describe/index.wxml-->
<!--<import src="../../../wxParse/wxParse.wxml"></import>-->
<view class="describe-box">
  <view class="activity-title">{{activityDetail.name}}</view>
  <view class="time-box">
    <text class="name">{{activityDetail.publisherVo.name}}</text>
    <text>{{activityDetail.updateTime}} 发布</text>
  </view>
  <view class="content">
    <!-- <rich-text nodes="{{activityDetail.content}}"></rich-text> -->
    <mp-html :content="article"></mp-html>
  </view>
  <view class="handle-box acea-row row-middle row-between">
    <view class="acea-row">
      <view class="call-box acea-row row-middle" @tap="call">
        <image src="/static/images/phone.png"></image>
        <text>拨号</text>
      </view>
      <view class="call-box  acea-row row-middle" @tap="collect">
        <image src="/static/images/collect.png"></image>
        <text>{{activityDetail.collect ? '已收藏':'收藏'}}</text>
      </view>
    </view>
    <view class="handle-btn-box">
      <view class="cancel-btn" v-if="activityDetail.status == 2">报名截止</view>
      <view class="cancel-btn" v-if="activityDetail.status == 3">活动结束</view>
      <view class="cancel-btn" v-if="activityDetail.status == 0">等待报名</view>
      <view class="cancel-btn signIn-btn" @tap="toSignPage" v-if="activityDetail.status == 1">立即报名</view>
    </view>
  </view>
</view>
</view>
</template>

<script>
// pages/activity/describe/index.js
const app = getApp();

import util from '../../../utils/util';

export default {
  data() {
    return {
      activityId: '',
      article: "",
      activityDetail: ""
    };
  },

  components: {},
  props: {},
  onLoad: function (options) {
    console.log(options);

    if (options.id) {
      this.setData({
        activityId: options.id
      });
      this.getDetail();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  methods: {
    getDetail() {
      var that = this;
      app.http('GET', 'activity/activityInfo/' + this.activityId).then(res => {
        console.log(res);

        if (res.data.success) {
          this.setData({
            activityDetail: res.data.data
          });
          let article = res.data.data.content;
          //WxParse.wxParse('article', 'html', article, that, 5)
          that.article = that.escape2Html(article);
        } else {
          uni.showToast({
            title: res.data.msg,
            icon: 'none'
          });
        }
      });
    },

    call() {
      uni.makePhoneCall({
        phoneNumber: this.activityDetail.publisherVo.phone,
        fail: function () {
          console.log('取消');
        }
      });
    },

    collect() {
      let data = {};
      data.id = this.activityDetail.id;
      data.type = 'collect';
      data.status = this.activityDetail.collect ? false : true;

      if (uni.getStorageSync('userInfo') != '') {
        this.requestCollect(data);
      } else {
        uni.getUserProfile({
          desc: '展示用户信息',
          // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
          success: res => {
            console.log(res);
            var iv = res.iv;
            var encryptedData = res.encryptedData;
            var userInfo = res.userInfo;
            uni.login({
              success: request => {
                // 发送 res.code 到后台换取 openId, sessionKey, unionId
                app.http('POST', 'wxapp/auth', {
                  code: request.code,
                  iv: iv,
                  encryptedData: encryptedData
                }).then(response => {
                  if (response.data.success) {
                    uni.setStorageSync('token', 'Bearer ' + response.data.data.token);
                    uni.setStorageSync('userInfo', userInfo);
                    this.requestCollect(data);
                  } else {
                    uni.showToast({
                      title: response.data.msg,
                      icon: 'none'
                    });
                  }
                });
              }
            });
          }
        });
      }
    },

    requestCollect(data) {
      app.http('POST', 'activity/collect', data).then(res => {
        if (res.data.success) {
          uni.showToast({
            title: '操作成功！'
          });
          this.getDetail();
        } else {
          uni.showToast({
            title: res.data.msg,
            icon: 'none'
          });
        }
      });
    },

    toSignPage() {
      if (uni.getStorageSync('userInfo') != '') {
        uni.navigateTo({
          url: '../signIn/index?id=' + this.activityId
        });
      } else {
        uni.getUserProfile({
          desc: '展示用户信息',
          // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
          success: res => {
            console.log(res);
            var iv = res.iv;
            var encryptedData = res.encryptedData;
            var userInfo = res.userInfo;
            uni.login({
              success: request => {
                // 发送 res.code 到后台换取 openId, sessionKey, unionId
                app.http('POST', 'wxapp/auth', {
                  code: request.code,
                  iv: iv,
                  encryptedData: encryptedData
                }).then(response => {
                  if (response.data.success) {
                    uni.setStorageSync('token', 'Bearer ' + response.data.data.token);
                    uni.setStorageSync('userInfo', userInfo);
                    uni.navigateTo({
                      url: '../signIn/index?id=' + this.activityId
                    });
                  } else {
                    uni.showToast({
                      title: response.data.msg,
                      icon: 'none'
                    });
                  }
                });
              }
            });
          }
        });
      }
    }

  }
};
</script>
<style>
/* pages/activity/describe/index.wxss */
.describe-box{
  min-height: 100%;
  background: #f2f2f2;
  padding: 32rpx;
  box-sizing: border-box;
}
.activity-title{
  color: #000000;
  font-size: 36rpx;
  line-height: 50rpx;
  margin-bottom: 24rpx;
}
.name{
  display: inline-block;
  margin-right: 64rpx;
  color: #EBB672;
}
.time-box{
  color: #7D7D7D;
  font-size: 24rpx;
}
.content{
  margin-top: 38rpx;
  padding-bottom: 130rpx;
}
.handle-box {
  width: 100%;
  position: fixed;
  bottom: 0;
  left: 0;
  background: #fff;
  padding: 42rpx 32rpx 80rpx;
  box-sizing: border-box;
  box-shadow: 0px -2px 8px 0px rgba(0,0,0,0.1);
}
.call-box{
  margin-right: 54rpx;
}
.call-box image{
  width: 37rpx;
  height: 37rpx;
  font-size: 28rpx;
  color: #333333;
  margin-right: 12rpx;
}
.cancel-btn{
  width: 234rpx;
  height: 60rpx;
  text-align: center;
  line-height: 60rpx;
  background: #F4F4F4;
  border-radius: 30rpx;
  color: #868686;
  font-size: 32rpx;
}
.signIn-btn{
  background: #EBB672;
  color: #fff;
}
.wxParse-p image{
  width: 100% !important;
  height: auto;
}
</style>