<template>
  <div class="rightForm" :style="`height:${heightPx}px`">
    <dm-sub-title type="line">基本信息</dm-sub-title>
    <div class="section">
      <el-descriptions column="3">
        <el-descriptions-item label="游戏名称"> {{ form.gameName }}</el-descriptions-item>
        <el-descriptions-item label="游戏时间">{{ form.gameTimeStr }}</el-descriptions-item>
      </el-descriptions>
      <el-descriptions column="3">
        <el-descriptions-item label="游戏说明"></el-descriptions-item>
      </el-descriptions>
      <el-input class="gameRuleBox" :disabled="true" :autosize="{ minRows: 5 }" v-model="form.gameRule" type="textarea" resize="none" />
      <!-- <div class="gameRuleBox" v-html="form.gameRule"></div> -->
    </div>
    <dm-sub-title type="line">展示信息配置</dm-sub-title>
    <div class="section">
      <el-descriptions column="3">
        <el-descriptions-item label="游戏模板"> {{ form.templateName }}</el-descriptions-item>
        <el-descriptions-item label="背景音乐">
          <div v-if="form.backMusicFlag == 1">
            {{ form.gameExt && form.gameExt.backMusicUrl.materialName }} <el-button type="text" @click="handleListen"> <i :class="['iconfont fz14', isPlay ? 'icon-zanting' : 'icon-kaiqi']"></i> 点击试听</el-button>
          </div>
          <div v-else>未开启</div>
        </el-descriptions-item>
        <el-descriptions-item label="氛围弹幕">{{ form.bulletFlag == 0 ? '未开启' : '已开启' }}</el-descriptions-item>
        <el-descriptions-item label="广告位">{{ form.adsFlag == 0 ? '未开启' : `已开启（小程序路径：${form.gameExt && JSON.parse(form.gameExt.mpUrlView.materialValue).name}）` }}</el-descriptions-item>
      </el-descriptions>
    </div>
    <dm-sub-title type="line">奖项配置</dm-sub-title>
    <div class="section">
      <el-descriptions column="3">
        <el-descriptions-item label="总中奖率"> {{ form.gameRuleInfo && form.gameRuleInfo.winChance }}%</el-descriptions-item>
        <el-descriptions-item label="库存提醒">
          {{ form.prizeNoticeFlag == 1 ? `开启（库存低于  ${form.prizeNoticeStock} 件奖品库发送短信提醒 ）` : '未开启' }}
          <el-popover placement="right" width="200" trigger="hover" content="您创建的游戏（游戏名称）中（奖品名称）的奖品数量已低于（奖品数量）件，请及时关注并补充！">
            <el-button slot="reference" type="text" class="ml16" v-show="form.prizeNoticeFlag == 1">查看短信内容 </el-button>
          </el-popover>
        </el-descriptions-item>
      </el-descriptions>
      <div class="content">
        <table>
          <thead>
            <tr>
              <th class="tableHead" style="min-width: 75px;">奖品类型</th>
              <th class="tableHead">奖项设置</th>
              <th class="tableHead">奖品名称</th>
              <th class="tableHead">奖品图片</th>
              <th class="tableHead" style="min-width: 75px;">奖品数量</th>
              <th class="tableHead">奖品说明</th>
              <th class="tableHead">中奖率</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="(item, index) in form.gamePrizeList" :key="index" :class="item.status && item.status != 0 ? 'invalid' : ''">
              <td class="cell">
                {{ prizeType[item.prizeType] }}
              </td>
              <td class="cell">
                <div v-if="item.prizeType == 2" class="flex">
                  <el-tag v-if="item.status == 1" size="mini" type="danger" class="tagIcon">删除</el-tag>
                  <el-tag v-if="item.status == 2" size="mini" type="danger" class="tagIcon">失效</el-tag>
                  <el-tooltip :content="item.cardName" placement="top">
                    <div class="ellips" :style="desW ? `width:${desW / 2}px` : ''">{{ item.cardName }}</div>
                  </el-tooltip>
                </div>
                <div v-if="item.prizeType == 1">
                  {{ item.prizeNumber + '积分' }}
                </div>
                <div v-if="item.prizeType == 0">
                  - -
                </div>
              </td>
              <td class="cell">
                <el-tooltip :content="item.prizeName" placement="top">
                  <div class="ellips" :style="desW ? `width:${desW / 2}px` : ''">{{ item.prizeName }}</div>
                </el-tooltip>
              </td>
              <td class="cell">
                <img :src="item.prizeImageUrl" v-if="item.prizeType != 0" />
                <span v-else>- -</span>
              </td>
              <td class="cell">
                <span v-if="item.prizeType != 0"> {{ item.prizeStock }}件</span>
                <span v-else>- -</span>
              </td>
              <td class="cell">
                <el-tooltip :content="item.prizeDesc" placement="top">
                  <div class="ellips" :style="desW ? `width:${desW / 2}px` : ''">{{ item.prizeDesc }}</div>
                </el-tooltip>
              </td>
              <td class="cell">{{ item.prizeRate }}%</td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>
    <dm-sub-title type="line">玩法配置</dm-sub-title>
    <div class="section">
      <el-descriptions column="3">
        <el-descriptions-item label="参与人群">{{ form.gameRuleInfo.memberType == 0 && !form.gameRuleInfo.filterJson ? '全部人群' : '' }} </el-descriptions-item>
        <el-descriptions-item label="适用人群说明" :labelStyle="labelStyle" v-if="form.gameRuleInfo">
          <el-tooltip :content="form.gameRuleInfo.memberDesc" placement="top">
            <div class="ellips" :style="desW ? `width:${desW}px` : ''">
              {{ form.gameRuleInfo.memberDesc }}
            </div>
          </el-tooltip>
        </el-descriptions-item>
      </el-descriptions>
      <div v-show="form.gameRuleInfo.memberType == 0 && form.gameRuleInfo.filterJson">
        <vue-gic-people projectName="marketing" :isAdd="false" :hasSearchData="form.gameRuleInfo.filterJson" :useId="form.memberWeightView.searchId" sceneValue="member" ref="peopleFilter" @editShow="toggleTag = true" @editHide="toggleTag = false" @hideBtn="toggleTag = false" />
        <div class="gic-people--button" v-show="toggleTag">
          <el-button size="small" type="primary" :disabled="true">确 定</el-button>
          <el-button size="small" @click="cancelFilter">取 消</el-button>
        </div>
      </div>
      <div v-if="form.gameRuleInfo.memberType == 1">
        <gic-new-member-group onlyRead :selectedIds="form.gameRuleInfo.filterJson" style="width:100%" />
      </div>
      <div>
        <el-descriptions column="3">
          <el-descriptions-item label="参与门槛">{{ form.gameRuleInfo.playConditionFlag == 0 ? '无门槛' : '' }} </el-descriptions-item>
          <el-descriptions-item label="每人最多可玩次数"> {{ form.gameRuleInfo && form.gameRuleInfo.playTimes }}次游戏次数</el-descriptions-item>
          <el-descriptions-item label="分享"> {{ form.gameRuleInfo.shareFlag == 1 ? '开启' : '未开启' }}</el-descriptions-item>
        </el-descriptions>
        <div class="shareBox" v-if="form.gameRuleInfo.playConditionFlag == 1">
          <div v-for="item in form.gameRuleConditionList" :key="item.conditionType">
            <div v-if="item.conditionType == 2" class="shareSection">
              <div class="mini-sub-title">
                <div class="line"></div>
                <div class="text">
                  <div class="space-between" style="font-weight:500">
                    赠与免费游戏次数
                  </div>
                </div>
              </div>
              <p>{{ item.conditionJson.type == 21 ? '每日赠送' : '总赠送' }}: {{ item.conditionJson.value }}次游戏次数</p>
            </div>
            <div v-if="item.conditionType == 0" class="shareSection">
              <div class="mini-sub-title">
                <div class="line"></div>
                <div class="text">
                  <div class="space-between" style="font-weight:500">
                    消耗积分获得游戏次数
                  </div>
                </div>
              </div>
              <p>参与游戏每次消耗积分: {{ item.conditionJson.value }}积分</p>
              <p>提示客户获取积分路径： {{ item.conditionJson.mpUrlView.name }}</p>
            </div>
            <div v-if="item.conditionType == 1">
              <div class="mini-sub-title">
                <div class="line"></div>
                <div class="text">
                  <div class="space-between" style="font-weight:500">
                    邀请好友助力获得游戏次数
                  </div>
                </div>
              </div>
              <p>邀请机制: 每成功邀请{{ item.conditionJson.inviteNum }}人可获得{{ item.conditionJson.value }} 次游戏</p>
              <p>成功邀请条件:{{ item.conditionJson.type == 11 ? '注册新会员' : '首次关注服务号' }}</p>
              <div v-show="item.conditionJson.type == 12">
                <el-descriptions column="1">
                  <el-descriptions-item label="关注回复"> {{ item.conditionJson.subscribeMsg }}</el-descriptions-item>
                </el-descriptions>
              </div>
              <div class="shareApp">
                <div class="shareTitle">邀请分享样式</div>
                <p>小程序卡片标题: {{ item.conditionJson.shareTitle }}</p>
                <p class="flex">小程序卡片图片: <img :src="item.conditionJson.shareCard" alt="游戏背景" style="width: 100px;height: 75px;margin-left: 10px;" /></p>
                <p class="flex" v-if="item.conditionJson.momentsCard">分享朋友圈: <img :src="item.conditionJson.momentsCard" alt="游戏背景" style="width: 100px;height: 178px;margin-left: 39px;" /></p>
                <p class="flex">分享海报: <img :src="item.conditionJson.shareReport" alt="游戏背景" style="width: 100px;height: 178px;margin-left: 53px;" /></p>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { formatDateTimeByType } from '@/utils/index.js';
const music = new Audio();
export default {
  props: {
    data: {
      type: Object,
      default: () => {}
    },
    invalidCard: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      toggleTag: false,
      form: {
        gameRuleInfo: {},
        memberWeightView: {}
      },
      desW: 0,
      isPlay: false,
      prizeType: {
        0: '谢谢参与',
        1: '积分',
        2: '卡券'
      },
      isAdd: this.$route.meta.type === 'add',
      heightPx: 0,
      labelStyle: {
        minWidth: '88px'
      }
    };
  },
  mounted() {
    this.heightPx = document.body.clientHeight - 52;
    music.addEventListener('ended', () => {
      music.currentTime = 0;
      this.isPlay = false;
    });
    const width = document.documentElement.clientWidth;
    if (width < 1750) {
      this.desW = 300;
    }
  },

  computed: {
    storeParams() {
      return !this.isAdd ? { creatorId: this.form.creatorId } : {};
    }
  },
  watch: {
    data(val) {
      this.form = { ...val };
      if (this.form.gameRuleInfo.playConditionFlag == 1) {
        this.form.gameRuleConditionList.forEach(item => {
          item.conditionJson = JSON.parse(item.conditionJson);
        });
      }
      let startDateStr = formatDateTimeByType(this.form.startDate, 'yyyy-MM-dd');
      let endDateStr = formatDateTimeByType(this.form.endDate, 'yyyy-MM-dd');
      this.form.gameTimeStr = startDateStr + ' 至 ' + endDateStr;
      this.calculateRate();
    },
    invalidCard(val) {
      this.form.gamePrizeList.forEach(item => {
        if (item.prizeType == 2) {
          item.status = val[item.prizeRelationId].invalid;
          item.cardName = val[item.prizeRelationId].name;
        } else {
          item.status = 0;
        }
      });
      this.form.gamePrizeList = this.form.gamePrizeList.splice(0);
    }
  },
  methods: {
    // 取消
    cancelFilter() {
      this.$refs.peopleFilter.cancelSet();
    },
    stopMusic() {
      if (this.isPlay) {
        this.isPlay = false;
        music.pause();
      }
    },
    calculateRate() {
      const prizeNumTotal = this.form.gamePrizeList.map(item => (item.prizeType != 0 ? item.prizeStock : 0)).reduce((pre, current) => pre + current);
      this.form.gamePrizeList.forEach(item => {
        if (item.prizeType != 0) {
          const prizeRate = prizeNumTotal ? this.form.gameRuleInfo.winChance * (item.prizeStock / prizeNumTotal) : 0;
          item.prizeRate = prizeRate.toFixed(2);
        }
      });
      let prizeArr = this.form.gamePrizeList.filter(item => item.prizeType == 0);

      if (!prizeNumTotal) {
        prizeArr.forEach(item => {
          const prizeRate = 100 / prizeArr.length || 0;
          item.prizeRate = prizeRate.toFixed(2);
        });
      } else {
        prizeArr.forEach(item => {
          const prizeRate = (100 - this.form.gameRuleInfo.winChance) / prizeArr.length || 0;
          item.prizeRate = prizeRate.toFixed(2);
        });
      }
      this.form.gamePrizeList = this.form.gamePrizeList.splice(0);
    },
    handleListen() {
      this.isPlay = !this.isPlay;
      music.src = this.form.gameExt.backMusicUrl.materialValue;
      this.isPlay ? music.play() : music.pause();
    }
  },
  destroyed() {
    this.stopMusic();
  }
};
</script>

<style lang="scss" scoped>
.rightForm {
  padding: 30px 20px 0 30px;
  box-sizing: border-box;
  width: calc(100vw - 520px);
  overflow-y: auto;
  overflow-x: hidden;
  background: #ffffff;
}

.invalid {
  color: #909399;
}
.tagIcon {
  margin-right: 4px;
}
.ellips {
  display: inline-block;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.section {
  padding: 20px 0 40px 0;
}
.content {
  width: 100%;
  background: #f7f8fa;
  border-radius: 4px;
  margin-top: 10px;
  box-sizing: border-box;
  padding: 16px 16px;
  overflow-x: auto;
  table {
    width: 100%;
  }
  .tableHead {
    text-align: left;
    font-weight: bold;
    line-height: 20px;
    color: #303133;
    font-size: 14px;
    line-height: 20px;
  }
  .cell {
    padding: 10px 10px 0 0;
    line-height: 20px;
    vertical-align: middle;
    img {
      width: 60px;
      height: 60px;
      border-radius: 2px;
      border: 1px solid #dcdfe6;
    }
    &:last-child {
      padding: 10px 0 0 0;
    }
    &:first-child {
      padding: 10px 0 0 0;
    }
  }
}
.gameRuleBox {
  width: 100%;
  line-height: 20px;
  /deep/ .el-textarea__inner {
    border: none;
    color: #303133;
    padding: 11px 13px;
  }
}
.cardBox {
  width: 100%;
  height: 32px;
  border-radius: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding-left: 10px;
}
.shareBox {
  width: 100%;
  padding: 16px 16px;
  box-sizing: border-box;
  background: #f7f8fa;
  /deep/ .el-descriptions__body {
    background: #f7f8fa;
  }
  p {
    line-height: 20px;
  }
  p + p {
    margin-top: 6px;
  }
  .subTitle {
    height: 20px;
  }
}
.shareTitle {
  font-size: 14px;
  font-weight: 500;
  margin: 10px 0;
  line-height: 20px;
}
.shareApp {
  p + p {
    margin-top: 10px;
  }
}

.shareSection {
  p:last-child {
    margin-bottom: 16px;
  }
}
</style>
