<template>
  <div class="mainPage" v-if="show">
    <div class="banner"  v-if="!video">
      <img :src="data.Images[0].CardImageUrl" alt="" v-if="type === 'card'">
      <img :src="data.Images[0].CouponImageUrl" alt="" v-if="type === 'coupon'">
    </div>
    <div class="content">
      <div class="title" v-if="$route.query.type === 'card'">{{data.CardName}}</div>
      <div class="title" v-else>{{data.CouponName}}</div>
      <div class="video" v-if='video'>
        <video width="100%" height="100%" preload="auto" controls="controls" x5-playsinline="true" :poster="type === 'card' ? data.Images[0].CardImageUrl : data.Images[0].CouponImageUrl">
          <source :src="data.VideoUrl" type="video/mp4">
          <source :src="data.VideoUrl" type="video/avi">
          <source :src="data.VideoUrl" type="video/wma">
          <source :src="data.VideoUrl" type="video/rmvb">
          <source :src="data.VideoUrl" type="video/rm">
        </video>
      </div>
      <div class="text">
        <div>
          <i class="icon-shuoming iconfont"></i>
          <span>卡券使用说明</span>
        </div>
        <pre v-if="$route.query.type === 'card'">{{data.Share.CardUseInstruction}}</pre>
        <pre v-else>{{data.Share.UseInstruction}}</pre>
      </div>
      <div class="text">
        <div>
          <i class="icon-kaquan iconfont"></i>
          <span>卡券有效期</span>
        </div>
        <pre v-if="data.ValidDays>0">领取后{{data.ValidDays}}天有效，请在有效期结束前使用， 过期作废；</pre>
        <pre v-else>有效期至{{toolClass.dateFormat(data.EndDate,'yyyy-MM-dd')}}，请在有效期结束前使用， 过期作废；</pre>
      </div>
      <div class="text">
        <div>
          <i class="icon-dingwei iconfont"></i>
          <span>服务网点</span>
        </div>
        <pre>{{data.address}}</pre>
      </div>
      <div class="submit" v-if="button" @click="receive">确认领取</div>
      <div class="submit" v-else>点击右上角分享给客户</div>
    </div>
  </div>
</template>

<script>
import wxsdk from '../../../util/share'
import logo from '../../../common/icon/logoTop.png'
import { createNamespacedHelpers } from 'vuex'
const { mapState: mapShareState, mapActions: mapShareActions } = createNamespacedHelpers('share')
const { mapState: mapAppState, mapActions: mapAppActions } = createNamespacedHelpers('app')

export default {
  data () {
    return {
      logo,
      video: true,
      button: true,
      show: false,
      type: '',
      data: {}
    }
  },
  computed: {
    ...mapShareState({
      cardShare: x => x.cardShare,
      couponShare: x => x.couponShare
    }),
    ...mapAppState({
      caseList: x => x.CaseList
    })
  },
  created () {
    this.data = {}
    if (this.$route.params) {
      let that = this
      Object.keys(this.$route.params).forEach(function (key) {
        that.$route.query[key] = that.$route.params[key]
      })
    }

    this.type = this.$route.query.type

    if (this.$route.query.sharetype === 'share') {
      this.button = false
    }
    this.getCaseList().then(() => {
      this.init()
    })
  },
  methods: {
    ...mapShareActions(['getCardShare', 'getCouponShare', 'setCardShare', 'setCouponShare']),
    ...mapAppActions(['getCaseList']),
    init () {
      if (this.$route.query.type === 'card') {
        this.getCardShare({ id: this.$route.query.id }).then(() => {
          this.data = this.cardShare.Card
          this.data.address = this.caseList.filter(x => x.CaseId === this.data.CaseId)[0].CaseAddress
          if (!this.data.VideoUrl) {
            this.video = false
          }
          this.show = true
          this.sdk(this.data.Share.CardShareInfo)
        })
      } else {
        this.getCouponShare({ id: this.$route.query.id }).then(() => {
          this.data = this.couponShare.coupon
          this.data.address = this.caseList.filter(x => x.CaseId === this.data.CaseId)[0].CaseAddress
          if (!this.data.VideoUrl) {
            this.video = false
          }
          this.show = true
          this.sdk(this.data.Share.CouponShareInfo)
        })
      }
      if (this.$route.query.isAuto === 'auto') {
        this.receive()
      }
    },
    receive () {
      if (this.$route.query.type === 'card') {
        this.setCardShare({ id: this.$route.query.id, salesid: this.$route.query.salesid, serialcode: this.$route.query.random }).then(() => {
          this.$router.push({ name: 'receiveResults', query: { type: 'success', text: '卡券' } })
        }).catch((err) => {
          this.$router.push({ name: 'receiveResults', query: { type: 'fail' } })
          console.log(err)
        })
      } else {
        this.setCouponShare({ id: this.$route.query.id, salesid: this.$route.query.salesid, serialcode: this.$route.query.random }).then(() => {
          this.$router.push({ name: 'receiveResults', query: { type: 'success', text: '优惠券' } })
        }).catch((err) => {
          this.$router.push({ name: 'receiveResults', query: { type: 'fail' } })
          console.log(err)
        })
      }
    },
    sdk (info) {
      let logo, title
      if (this.$route.query.type === 'card') {
        logo = this.data.Images[0].CardImageUrl + '?x-oss-process=style/wxicon'
        title = this.data.CardName
      } else {
        logo = this.data.Images[0].CouponImageUrl + '?x-oss-process=style/wxicon'
        title = this.data.CouponName
      }
      wxsdk({ url: encodeURIComponent(window.location.href.split('#')[0]) }, {
        title: title,
        desc: info,
        link: `${window.location.origin}${window.location.pathname}#/receiveShared/${this.$route.query.id}/${this.$route.query.salesid}/${this.$route.query.type}/${this.$route.query.random}/receive`,
        thu_image: logo
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import 'page.scss';
</style>
