<template>
  <BaseContainer>
    <NavBar title="考试详情" />
    <view class="question-index-page">
      <view class="panel">
        <view class="name">{{ question.title }}</view>
        <view class="list">
          <view class="item">
            <view>
              <text>{{ question.txamination_time }}</text>分钟
            </view>
            <view>建议用时</view>
          </view>
          <view class="item">
            <view>
              <text>{{ question.total_score }}</text>分
            </view>
            <view>试卷总分</view>
          </view>
        </view>
        <view class="desc">
          {{ question.answer }}人已答题
          <view>
            试题难度：<span v-for="star in 5" :key="star" :class="{ on: star <= question.difficulty }"
              class="iconfont iconxing"></span>
          </view>
        </view>
        <view>
          <view v-if="question.isPay" @click="againQuestion(id)" class="again">
            <view v-if="!status">开始做题</view>
            <view v-if="status === 1">再次练习</view>
            <view v-if="status === 2">继续练习</view>
          </view>
          <view v-else class="pay-btn" @click="payDialogOpen = true">
            <view>
              <view>
                ￥<span>{{ question.money }}</span>
              </view>
              <view>会员价:￥{{ question.member_money }}</view>
            </view>
            <view>支付答题</view>
          </view>
        </view>
      </view>
      <view class="intro">
        <view>题型介绍：</view>
        <view class="list">
          <view class="item">单选题（选项中只有1个正确答案)；</view>
          <view class="item">多选题（选项中至少有2个正确答案)；</view>
          <view class="item">判断题（选项中只有1个正确答案)。</view>
        </view>
      </view>
      <view class="intro">
        <view>考前必读：</view>
        <view class="list">
          <view class="item">答题者准备好后，可点击“开始做题”按钮开始考试；</view>
          <view class="item">做题前可看题目前的标签，明白此题是单选题、多选题还是判断题；</view>
          <view class="item">作答完所有的题目点击“提交考试”按钮，即可查看自己的考试结果。</view>
        </view>
      </view>
    </view>
    <!-- 支付弹窗 -->
    <PayDialog :open.sync="payDialogOpen" :money="Number(isMember ? question.member_money : question.money)"
      :now_money="nowMoney" :pay_type_num="pay_type_num" :special_id="id" :template-id="templateId"
      :is-wechat="isWechat" :wxpay-h5="wxpayH5" :is-alipay="isAlipay" :is-balance="isBalance" @change="onChange"
      :isMember="!!isMember" :isMembership="true" :memberMoney="question.member_money" />
  </BaseContainer>
</template>

<script>
  import { getSpecialTemplateIds, aliPayApp, aliPayWap } from "@/api/special";
  import { getTestPaperDetails, situationRecord } from "@/api/topic";
  import PayDialog from "@/components/PayDialog/index.vue";
  import { getAuthInfo } from "@/api/auth";

  export default {
    components: {
      PayDialog,
    },
    data() {
      return {
        id: 0,
        question: {
          title: "--",
          txamination_time: "--",
          total_score: "--",
          answer: "--",
          isPay: true,
        },
        isMember: false,
        isWechat: false,
        templateId: "",
        payDialogOpen: false, // 是否显示支付弹窗
        money: 0,
        nowMoney: 0,
        pay_type_num: 60,
        special_id: 0,
        orderId: "",
        wxpayH5: false,
        isAlipay: false,
        isBalance: false,
        isOpenWeixing: false,
        status: -1,
				is_analysis: 0,
				special_id: 0,
      };
    },
    onLoad({ id, isMember, is_analysis, special_id }) {
      this.id = Number(id);
      this.isMember = isMember;
			this.special_id = special_id || '';
      this.getDetail();
      this.getAuthInfo();
			if (is_analysis && parseInt(is_analysis)) {
				this.is_analysis = parseInt(is_analysis);
			} else {
				this.is_analysis = 0;
			}
    },
    onShow() {
      if (this.isOpenWeixing && this.orderId) {
        this.$util.wxh5PayIsSuccess(this.orderId, 4, this)
        this.isOpenWeixing = false
      }
      this.getsituationRecord();
    },
    methods: {
      getsituationRecord() {
        situationRecord({ id: this.id }).then(res => {
          this.status = res.data;
        }).catch(err => {
        })
      },
      againQuestion(id) {
        if (this.status == 1) this.$util.setStorage(`exam_time${id}`, 0);
        console.log(this.$util.getStorage(`exam_time${id}`));
        uni.navigateTo({
          url: `/pages/topic/question_detail?test_id=${id}&is_analysis=${this.is_analysis}&special_id=${this.special_id}`
        });
      },
      async getAuthInfo() {
        const res = await getAuthInfo();
        const { nowMoney, isWechat, isAlipay, isBalance, wxpayH5 } = res.data
        Object.assign(this, {
          nowMoney,
          isWechat,
          isAlipay,
          isBalance,
          wxpayH5
        })
      },
      getDetail() {
        getTestPaperDetails(this.id).then(({ data }) => {
          this.question = data;
        });
      },
      subscribe() {
        getSpecialTemplateIds(this.pay_type_num, this.id).then(({ msg }) => {
          this.templateId = msg;
        });
      },
      onChange(obj) {
        if (typeof obj != "object") {
          obj = {};
        }
        var action = obj.action || "";
        var value = obj.value || "";
        this[action] && this[action](value);
      },
      pay_order(data) {
        this.orderId = data.data.result.orderId || "";
        switch (data.data.status) {
          case "WECHAT_PAY":
            this.wechatPay(data.data.result.jsConfig);
            break;
          case "WECHAT_ROUTINE_PAY":
            this.$util.wechatRoutinePay(data.data.result.jsConfig, this);
            break;
          case "WECHAT_H5_PAY":
            this.payDialogOpen = false;
            this.$util.wechatH5Pay(data.data.result.jsConfig, this);
            break;
          case "ZHIFUBAO_PAY":
            this.aliPay(data.data.result, "testpaper")
            break;
          case "SUCCESS":
            this.successOrder(data.msg);
            break;
          case 'TOUTIAO_PAY':
            this.$util.toutiaoPay(data.data.result.jsConfig, this);
            break;
          case 'KUAISHOU_PAY':
            this.$util.kuaishouPay(data.data.result.jsConfig, this);
            break
          default:
            this.extendOrder(data.msg);
            break;
        }
      },
      aliPay(msn, type) {
        this.$util.aliPay(msn, type, this)
      },
      wechatPay(config) {
        this.$util.weixinpay(config, this)
      },
      successOrder(msg) {
        this.$util.showMsg(msg ? msg : "支付成功");
        this.payDialogOpen = false;
        this.question.isPay = true;
      },
      extendOrder(msg) {
        if (typeof msg === "object") {
          if (msg.errMsg === "chooseWXPay:cancel") {
            msg = "微信支付取消";
          } else {
            msg = "支付失败";
          }
        } else {
          msg = "支付失败";
        }
        this.payDialogOpen = false
        this.$util.showMsg(msg)
      },
    },
  };
</script>

<style>
  page {
    background-color: #f5f5f5;
  }
</style>

<style scoped lang="scss">
  /* 试题详情 */
  .question-index-page .panel {
    padding: 35rpx 30rpx 50rpx;
    border-radius: 12rpx;
    margin: 70rpx 55rpx 50rpx;
    background-color: #ffffff;

    box-shadow: 0 3rpx 20rpx rgba(0, 0, 0, 0.05);
  }

  .question-index-page .panel .name {
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    font-size: 32rpx;
    text-align: center;
    color: #282828;
  }

  .question-index-page .panel .list {
    display: flex;
  }

  .question-index-page .panel .item {
    flex: 1;
    padding-top: 35rpx;
    padding-bottom: 40rpx;
    font-size: 24rpx;
    text-align: center;
    color: #999999;
  }

  .question-index-page .panel .item view:first-child {
    font-size: 22rpx;
    color: #2c8eff;
  }

  .question-index-page .panel .item text {
    margin-right: 5rpx;
    font-weight: 600;
    font-size: 44rpx;
  }

  .question-index-page .panel .desc {
    border-top: 1px solid #f5f5f5;
    padding-top: 30rpx;
    padding-bottom: 70rpx;
    font-size: 26rpx;
    text-align: center;
    color: #999999;
  }

  .question-index-page .panel .desc view {
    display: inline;
    margin-left: 50rpx;
  }

  .question-index-page .panel .desc+view {
    padding-right: 20rpx;
    padding-left: 20rpx;
  }

  .question-index-page .panel .desc .iconfont {
    font-size: 22rpx;
    color: #fff0e5;
  }

  .question-index-page .panel .desc .iconfont .iconfont {
    margin-left: 6rpx;
  }

  .question-index-page .panel .desc .iconfont.on {
    color: #ff6b00;
  }

  .question-index-page .panel .again {
    display: block;
    height: 74rpx;
    border-radius: 37rpx;
    background-color: #2c8eff;
    font-family: inherit;
    font-weight: normal;
    font-size: 30rpx;
    line-height: 74rpx;
    text-align: center;
    color: #ffffff;
  }

  .question-index-page .intro {
    margin-right: 75rpx;
    margin-left: 75rpx;
    font-size: 22rpx;
    line-height: 44rpx;
    text-align: justify;
    color: #999999;
  }

  .question-index-page .intro+.intro {
    margin-top: 40rpx;
  }

  .question-index-page .intro .list {
    margin-left: 1em;
    list-style: decimal;

    .item {
      display: list-item;
    }
  }

  .question-index-page .panel .pay-btn {
    display: flex;
    padding-left: 21rpx;
    border-radius: 37rpx;
    background-color: rgba(232, 243, 255, 1);
    overflow: hidden;
    color: #2c8eff;
  }

  .question-index-page .panel .pay-btn>view:first-child {
    -webkit-box-flex: 1;
    flex: 1;
    display: -webkit-box;
    display: flex;
    min-width: 0;
  }

  .question-index-page .panel .pay-btn>view>view:first-child {
    font-weight: bold;
    font-size: 24rpx;
  }

  .question-index-page .panel .pay-btn>view>view:first-child span {
    font-size: 40rpx;
  }

  .question-index-page .panel .pay-btn>view>view:last-child {
    margin-left: 10rpx;
    font-size: 20rpx;
    display: flex;
    align-items: center
  }

  .question-index-page .panel .pay-btn>view:last-child {
    position: relative;
    width: 147rpx;
    padding-right: 20rpx;
    background-color: #2c8eff;
    font-size: 28rpx;
    color: #ffffff;
    display: flex;
    align-items: center;
  }

  .question-index-page .panel .pay-btn>view:last-child::before {
    content: "";
    position: absolute;
    top: 0;
    right: 100%;
    bottom: 0;
    width: 20rpx;
    border-width: 37rpx 10rpx 37rpx 10rpx;
    border-style: solid;
    border-color: transparent #2c8eff #2c8eff transparent;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
  }
</style>