<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>
      <div class="gameRuleBox">{{ 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">奖品类型</th>
              <th class="tableHead">奖项设置</th>
              <th class="tableHead">奖品名称</th>
              <th class="tableHead">奖品图片</th>
              <th class="tableHead">奖品数量</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 != 0 ? 'invalid' : ''">
              <td class="cell">
                {{ prizeType[item.prizeType] }}
              </td>
              <td class="cell">
                <div v-if="item.prizeType == 2">
                  <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>
                  <span> {{ item.prizeName }}</span>
                </div>
                <div v-if="item.prizeType == 1">
                  {{ item.prizeNumber + '积分' }}
                </div>
                <div v-if="item.prizeType == 0">
                  - -
                </div>
              </td>
              <td class="cell">
                {{ item.prizeName }}
              </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">
                {{ item.prizeDesc }}
              </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="适用人群说明">{{ form.gameRuleInfo && form.gameRuleInfo.memberDesc }}</el-descriptions-item>
      </el-descriptions>
      <div v-if="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 class="section">
        <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="subTitle">
                <div class="line"></div>
                <div class="text">
                  <div class="space-between">
                    赠与免费游戏次数
                  </div>
                </div>
              </div>
              <p>{{ item.conditionJson.type == 21 ? '总赠送' : '每日赠送' }}: {{ item.conditionJson.value }}次游戏次数</p>
            </div>
            <div v-if="item.conditionType == 0" class="shareSection">
              <div class="subTitle">
                <div class="line"></div>
                <div class="text">
                  <div class="space-between">
                    消耗积分获得游戏次数
                  </div>
                </div>
              </div>
              <p>参与游戏每次消耗积分: {{ item.conditionJson.value }}积分</p>
              <p>提示客户获取积分路径： {{ item.conditionJson.mpUrlView.name }}</p>
            </div>
            <div v-if="item.conditionType == 1">
              <div class="subTitle">
                <div class="line"></div>
                <div class="text">
                  <div class="space-between">
                    邀请好友助力获得游戏次数
                  </div>
                </div>
              </div>
              <p>邀请机制: 每成功邀请{{ item.conditionJson.inviteNum }}人可获得{{ item.conditionJson.value }} 次游戏</p>
              <p>成功邀请条件:{{ item.conditionJson.type == 11 ? '注册新会员' : '首次关注服务号' }}</p>

              <div v-show="item.conditionJson.type == 11" 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">分享海报: <img :src="item.conditionJson.shareReport" alt="游戏背景" style="width: 100px;height: 178px;margin-left: 53px;" /></p>
              </div>
              <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>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { formatDateTimeByType } from '@/utils/index.js';

import gicNewMemberGroup from '@/components/dm-new-member-group/index.vue';
const music = new Audio();
export default {
  props: {
    data: {
      type: Object,
      default: () => {}
    },
    invalidCard: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      toggleTag: false,
      form: {
        gameRuleInfo: {},
        memberWeightView: {}
      },

      isPlay: false,
      prizeType: {
        0: '谢谢参与',
        1: '积分',
        2: '卡券'
      },
      isAdd: this.$route.meta.type === 'add',
      heightPx: 0
    };
  },
  mounted() {
    this.heightPx = document.body.clientHeight - 52;
  },
  components: {
    gicNewMemberGroup
  },
  computed: {
    storeParams() {
      return !this.isAdd ? { creatorId: this.form.creatorId } : {};
    }
  },
  watch: {
    data(val) {
      // const {
      //   gameRuleInfo, // 游戏规则信息
      //   gameRuleConditionList, // 游戏规则-参与门槛
      //   gamePrizeList, //游戏奖品信息
      //   gameExt,
      //   prizeNoticeStock,
      //   prizeNoticeFlag,
      //   adsFlag,
      //   bulletFlag,
      //   backMusicFlag,
      //   templateId,
      //   gameRule,
      //   startDate,
      //   endDate,
      //   gameName,
      //   memberWeightView,
      //   creatorId,
      //   gameId,
      //   status
      // } = val;
      this.form = { ...val };
      console.log(this.form);
      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;
        } else {
          item.status = 0;
        }
      });
      this.form.gamePrizeList = this.form.gamePrizeList.splice(0);
    }
  },
  methods: {
    // 取消
    cancelFilter() {
      this.$refs.peopleFilter.cancelSet();
    },

    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 = this.form.gameRuleInfo.winChance * (item.prizeStock / prizeNumTotal);
          item.prizeRate = prizeRate.toFixed(2);
        }
      });
      const rateTotal = this.form.gamePrizeList.map(item => (item.prizeType != 0 ? item.prizeRate : 0)).reduce((pre, current) => Number(pre) + Number(current));
      let prizeArr = this.form.gamePrizeList.filter(item => item.prizeType == 0);
      prizeArr.forEach(item => {
        const prizeRate = (100 - rateTotal) / 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();
    }
  }
};
</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;
}
.section {
  padding: 20px 0 18px 0;
}
.content {
  width: 100%;
  background: #f7f8fa;
  border-radius: 4px;
  margin-top: 10px;
  box-sizing: border-box;
  padding: 16px 16px;
  table {
    width: 100%;
  }
  .tableHead {
    text-align: left;
    font-weight: bold;
    line-height: 20px;
    color: #303133;
    font-size: 14px;
    line-height: 20px;
  }
  .cell {
    padding: 10px 0 0 0;
    line-height: 20px;
    vertical-align: middle;
    img {
      width: 60px;
      height: 60px;
      border-radius: 2px;
      border: 1px solid #dcdfe6;
    }
  }
}
.gameRuleBox {
  padding: 11px 13px;
  width: 100%;
  background: #f7f8fa;
  line-height: 20px;
}
.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>
