<template>
  <div class="send-preview" :class="{ visible }">
    <!-- <transition name="slide-fade"> -->
      <div class="block">
        <div style="height:100%;overflow-y:auto;width:100%;">
          <div style="width:486px;position:relative;margin: 0 auto;z-index:9;transform:scale(0.79)">
            <div class="content" v-if="data.giveImage">
              <div v-if="active==='word'">
                <el-avatar shape="square" :size="35" icon="el-icon-user-solid" class="avatar"></el-avatar>
                <div class="blk">
                  <div class="card">
                    <el-avatar shape="circle" :size="35" class="mr15 shrink"><i class="el-icon-user-solid fz18"></i></el-avatar>
                    <p class="shrink ellipsis-80">{{ data.cardName }}</p>
                  </div>
                  <div style="max-height: 400px;overflow: auto;">
                    <div class="ellipsis-l2 mt5 mb5">{{ data.giveContent }}</div>
                    <dm-el-image-view draggable="false" style="width: 100%;" :src="data.giveImage" fit="cover"></dm-el-image-view>
                  </div>
                  <p class="mt10 gray03"><i class="iconfont-market4 icon-xiaochengxu1" style="color: #24AF41;margin-right: 5px;"></i>小程序</p>
                </div>
              </div>
              <div class="page" v-if="active==='page'">
                <div class="page-card">
                  <img :src="data.giveBannerImage" draggable="false" width="100%" height="100px" />
                  <p class="title">你的好友xx，赠送给你一张卡券</p>
                  <div class="card-content">
                    <div class="dl" :style="`background:linear-gradient(137deg,rgba(${colorRGB2Hex(data.color)},0.8) 0%,rgba(${colorRGB2Hex(data.color)},1) 100%);`">
                      <p v-if="data.cardType === 1">{{ data.denomination }}<span class="ml5">折</span></p>
                      <img v-else-if="data.cardType === 2" :src="require('@/assets/images/exchange-coupon-icon.png')" draggable="false" class="exchange" />
                      <p v-else><span>￥</span>{{ data.denomination }}</p>
                    </div>
                    <div class="card-d">
                      <p class="card-d-title ellipsis">
                        <span :style="`background:${data.color}`">{{ data.cardType | dct }}</span>{{ data.cardName }}
                      </p>
                      <p class="card-d-time" v-if="data.cardEffectiveMode == 0">{{ time }}</p>
                      <p class="card-d-time ellipsis" v-if="data.cardEffectiveMode == 1">
                        领取后第{{ data.effectTimeCount }}{{ data.effectTimeMode ? '月' : '天' }} - 第{{ data.expireTimeCount + data.effectTimeCount - 1 }}天
                      </p>
                    </div>
                  </div>
                  <dl class="give-rule mt40">
                    <dd>领 取 记 录</dd>
                    <dt class="gray02">--<span class="fr gray03">2021.01.01 00:00:00</span></dt>
                  </dl>
                  <dl class="give-rule">
                    <dd>转 赠 规 则</dd>
                    <dt style="max-height:370px;overflow-y:auto;">
                      <pre style="white-space: pre-wrap;word-wrap: break-word;">{{ data.giveRule }}</pre>
                      <!-- <div>{{ data.giveRule.slice(0, 12) }}</div>
                      <div>{{ data.giveRule.slice(12, 24) }}</div> -->
                    </dt>
                  </dl>
                </div>
              </div>
              <ul class="btn">
                <li :class="{ active: active === 'word' }" @click="active = 'word'">转赠对话框</li>
                <li :class="{ active: active === 'page' }" @click="active = 'page'">转赠页面</li>
              </ul>
            </div>
            <img src="../../../../assets/images/iphone-bg.png" draggable="false" />
          </div>
        </div>
        <div class="bg" @click="close"></div>
      </div>
    <!-- </transition> -->
  </div>
</template>

<script>
import { formatDateTimeByType } from '@/utils/index';
import { cardType as _cardType } from '@/config/mapping/gic-card';
export default {
  name: 'SendPreview',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    data: {
      type: Object,
      default() {
        return {};
      }
    },
    type: {
      type: String,
      default: 'form' // form info
    }
  },
  data() {
    return {
      active: 'word', // word page
    };
  },
  methods: {
    // 16进制颜色转换为rgb颜色
    colorRGB2Hex(color) {
      let sColor = color.toLowerCase();
      const reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
      if (sColor && reg.test(sColor)) {
        if (sColor.length === 4) {
          let sColorNew = '#';
          for (let i = 1; i < 4; i += 1) {
            sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1));
          }
          sColor = sColorNew;
        }
        // 处理六位的颜色值
        let sColorChange = [];
        for (let i = 1; i < 7; i += 2) {
          sColorChange.push(parseInt('0x' + sColor.slice(i, i + 2)));
        }
        return sColorChange.join(',');
      } else {
        return sColor;
      }
    },
    close() {
      this.active = 'word';
      this.$emit('update:visible', false);
    }
  },
  filters: {
    dct(val) {
      return _cardType[val].label || '--';
    }
  },
  computed: {
    time() {
      if(this.type === 'info') {
        return `${formatDateTimeByType(this.data.startDate, 'yyyy-MM-dd')} - ${formatDateTimeByType(this.data.expireDate, 'yyyy-MM-dd')}`;
      } else {
        if(this.dateTime && this.data.dateTime[0] && this.data.dateTime[1]) {
          return `${formatDateTimeByType(this.data.dateTime[0], 'yyyy-MM-dd')} - ${formatDateTimeByType(this.data.dateTime[1], 'yyyy-MM-dd')}`;
        } else {
          return '--';
        }
      }
    }
  },
};
</script>

<style lang="scss" scoped>
.send-preview {
  transition: all 0.3s ease;
  opacity: 0;
  visibility: hidden;
  z-index: 1000;
  position: fixed;
  width: 100%;
  height: 100%;
  overflow-y: hidden;
  left: 0;
  top: 0;
  &.visible {
    visibility: visible;
    opacity: 1;
  }
  div.block {
    margin: 0 auto;
    z-index: 3;
    position: relative;
    top: 0;
    height: 100%;
    // overflow-y: auto;
    width: 100%;
    img {
      width: 100%;
    }
  }
  ul {
    position: absolute;
    bottom: 10px;
  }
  .content {
    height: 100%;
    width: 100%;
    position: absolute;
    left: 0;
    top: 0;
    .avatar {
      position: absolute;
      right: 75.5px;
      top: 119px;
      /deep/ i {
        font-size: 18px;
        line-height: 35px;
      }
    }
    .blk {
      position: absolute;
      padding: 22px 15px 13px 15px;
      right: 129.5px;
      top: 119px;
      width: 224px;
      background: white;
      -webkit-box-shadow: 0px 2px 8px 0px rgba(0, 0, 0, 0.1);
      box-shadow: 0px 2px 8px 0px rgba(0, 0, 0, 0.1);
      border-radius: 10px;
      .card {
        i {
          position: relative;
          top: 2px;
        }
        display: flex;
        align-items: center;
      }
      pre {
        margin: 15px auto;
        line-height:18px;
      }
    }
  }
  .bg {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: calc(100%);
    z-index: 1;
    display: block;
    background: rgba(0,0,0,0.5);
  }
  .btn {
    position: absolute;
    height: 80px;
    width: 368px;
    top: 773px;
    left: 63px;
    background: white;
    overflow: hidden;
    border-radius: 0 0 50px 50px;
    li {
      float: left;
      width: 50%;
      line-height: 80px;
      color: $gray02;
      text-align: center;
      font-size:18px;
      &.active {
        color: $blue;
      }
    }
  }
}
.page {
  width: 308px;
  position: absolute;
  top: 98px;
  left: 50%;
  margin-left: -154px;
  .title {
    color: #335782;
    font-size: 16px;
    margin: 16px 0;
    text-align: center;
  }
  .page-card {
    background: white;
    padding: 20px;
    height: 650px;
    border-radius: 4px;
  }
  .give-rule {
    margin-top: 70px;
    dd {
      text-align: center;
      color: #2C3038;
      font-size: 18px;
      margin-bottom: 20px;
      padding-bottom: 20px;
      border-bottom: 1px solid #E6E6E6;
    }
    dt {
      font-size: 14px;
      line-height: 24px;
    }
  }
  .card {
    &-content {
      box-shadow: 0px 2px 8px 0px rgba(136, 148, 156, 0.15);
      overflow: hidden;
      position: relative;
      height: 70px;
      padding-left: 91px;
      background: white;
      img.exchange {
        width: 50px;
      }
      .dl {
        justify-content: center;
        display: flex;
        align-items: center;
        width: 91px;
        color: white;
        height: 100%;
        text-align: center;
        position: absolute;
        left: 0;
        top: 0;
        background: #3970B5;
        &::after {
          content: '';
          width: 6px;
          height: 100%;
          background: url('../../../../assets/images/semi-circle-icon.png') repeat-y;
          position: absolute;
          left: -3px;
          top: 0;
        }
        p {
          text-overflow: ellipsis;
          overflow: hidden;
          white-space: nowrap;
          font-size: 28px;
          font-weight: bold;
          line-height: 100px;
          height: 100px;
          text-align: center;
          span {
            margin-right: 2px;
            font-size: 18px;
          }
        }
      }
    }
    &-d {
      padding: 10px 10px 5px 10px;
      position: relative;
      height: 100%;
      &-title {
        height: 18px;
        font-size: 13px;
        font-weight: 500;
        color: rgba(44, 48, 56, 1);
        line-height: 18px;
        margin-bottom: 11px;
        position: relative;
        margin-top: 2px;
        padding-left: 48px;
        span {
          position: absolute;
          left: -20px;
          bottom: -7px;
          width: 84px;
          text-align: center;
          padding: 2px 0 2px;
          border-radius: 1px;
          text-align: center;
          display: inline-block;
          color: white;
          line-height: 28px;
          font-weight: normal;
          font-size: 20px;
          display: inline-block;
          transform: scale(0.5);
        }
      }
      &-time {
        height: 15px;
        font-size: 12px;
        font-weight: 400;
        color: rgba(44, 48, 56, 1);
        line-height: 15px;
      }
      &-option {
        width: calc(200% - 48px);
        position: absolute;
        bottom: 0px;
        left: -40%;
        font-size: 22px;
        font-weight: 400;
        color: rgba(44, 48, 56, 1);
        line-height: 32px;
        transform: scale(0.5);
        display: flex;
        align-items: center;
        img {
          width: 24px;
          height: 24px;
        }
        .right {
          margin-left: auto;
        }
      }
    }
  }
}
.slide-fade-enter-active {
  transition: all .3s ease;
}
.slide-fade-leave-active {
  transition: all .8s cubic-bezier(1.0, 0.5, 0.8, 1.0);
}
.slide-fade-enter, .slide-fade-leave-to
/* .slide-fade-leave-active for below version 2.1.8 */ {
  transform: translateX(10px);
  opacity: 0;
}
</style>
