<template>
  <div class="phoneRecharge">
    <div class="p_head">
      <img src="../../assets/image/payrollback_left@2x.png" class="leftIcon" alt="" @click="$router.replace('/index')">
      <span>
          <span>
              手机充值
          </span>
      </span>
    </div>
    <div class="phoneCard">
      <!-- 选择手机卡 -->
      <div class="cardContent">
        <div class="selectCard">选择手机号</div>
        <el-form :model="phoneForm" :rules="phoneFormRule" ref="phoneForm">
          <el-form-item prop="phoneCard">
            <el-input v-model="phoneForm.phoneCard" placeholder="请输入11位手机号码" />
          </el-form-item>
        </el-form>
        <div class="alert">
          <img src="@/assets/image/cardAlert@2x.png" alt="">
          <span>请仔细核对手机号，避免造成不必要的损失。</span>
        </div>
      </div>
      <!-- 充值面额 -->
      <div class="denomination">
        <div style="font-family: PingFang SC;color:#333;">充值面额</div>
        <div>
          <div
            class="denominationCard"
            :class="{'activeBorder': idx == curAmountIdx}"
            v-for="(item, idx) in phone_amount" :key="item"
            @click="hPetrifaction(idx, item)">
            <span>{{item}}</span>
            <i>优惠价{{item * 0.995}}元</i>
            <div :class="{'active': idx == curAmountIdx}"></div>
          </div>
        </div>
      </div>
      <div class="recharfeBtn" @click="recharfeBtn">
        <img src="@/assets/image/rechargeBtn@2x.png" alt="">
      </div>
      <!-- 说明 -->
      <div class="explain">
        <div class="explainHead">
          <i></i>
          <span>充值说明</span>
          <i></i>
        </div>
        <ol>
          <li>1. 充值优惠来自充值平台补贴；</li>
          <li>2. 充值后1-10分钟左右到账，运营商将发充值成功短信至充值 手机；</li>
          <li>3. 如需开发票请至相应的手机运营商官方APP开取电子发票；</li>
        </ol>
      </div>
    </div>
  </div>
</template>

<script>
import Http from '@/extend/Http';
import {Message} from "element-ui";
export default {
  name: 'phoneRecharge',
  data() {
    var validPhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('手机号不能为空'));
      } else if (!/^1[3-9]\d{9}$/.test(value)) {
        return callback(new Error('请输入合法手机号'));
      } else {
        callback();
      }
    };
    return {
      // 手机号
      phoneForm: {
        phoneCard: '',
      },
      phoneFormRule: {
        phoneCard: [
          { validator: validPhone, trigger: 'blur' }
        ]
      },
      // 充值面额
      phone_amount: [],
      curAmountIdx: null,
      // 当前面额
      curAmount: ''
    }
  },
  created() {
    this.getDenomination()
  },
  watch: {
    '$route': {
      handler(newVal) {
        if (newVal.path == '/phoneRecharge' && !localStorage.getItem('access_token')) {
            router.replace('/login')
        }
      }
    }
  },
  methods: {
    handleClick(tab, event) {
      // console.log(tab, event);
    },
    // 获取充值面额
    async getDenomination() {
      let res = await Http.getInstance().getDenomination();
      console.log('手机面额', res);
      if (res && res.msg == '请求成功') {
        this.phone_amount = res.data.phone_amount
      }
    },
    // 选择石化充值面额
    hPetrifaction(idx, item) {
      this.curAmountIdx = idx
      this.curAmount = item
    },
    // 充值按钮
    recharfeBtn() {
      if (!this.curAmount) {
        Message({
          message: '请选择充值金额', type: "warning"
        });
        return
      }
      this.$refs.phoneForm.validate(async valid => {
        if (valid) {
          let res = await Http.getInstance().RechargeOrder({
            cardno: this.phoneForm.phoneCard,
            amount: this.curAmount
          });
          console.log('石化充值', res);
          if (res && res.msg == '请求成功') {
            localStorage.setItem('order_paying', JSON.stringify(res.data));
            this.$router.replace({
              name: 'opayment',
              params: {
                order_sn: res.data.order_sn,
                type: '1',
                recharfe: 'false'
              }
            });
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
  .phoneRecharge {
    background-color: #EDF0F5;
    position: absolute;
    width: 100%;
    height: 100%;
    box-sizing: border-box;
    overflow: auto;
  }
  // 顶部导航
  .p_head{
    text-align: center;
    position: relative;
    top: 1.5rem;
    font-size: 2rem;
    // font-family: 'PingFang SC';
    margin-bottom: 3.667rem;
    // font-weight: bold;
    line-height: 1.5rem;
    .leftIcon{
      position: absolute;
      left: 1.667rem;
      top: 0.7rem;
      width: 0.667rem;
    }
    span {
      vertical-align: middle;
    }
  }
  .phoneCard {
    padding: 0 1.667rem 7.5rem;
  }
  .cardContent {
    height: 14rem;
    background-color: #fff;
    padding: 15px 20px;
    font-size: 1.167rem; 
    border-radius: 1.667rem;
    font-family: PingFang SC;
    color: #333;
    .selectCard {
      margin-bottom: 1.667rem;
    }
    /deep/.el-input__inner {
      border: 0;
      border-bottom: 1px solid #BFBFBF;
      border-radius: 0;
      // margin-bottom: 1rem;
      padding-left: 0;
    }
    /deep/.el-form-item {
      margin-bottom: 10px;
    }
    .alert {
      font-size: 1.2rem;
      color: #F58520;
      padding-left: 0;
      padding-right: 0;
      img {
        width: 1.2rem;
        margin-right: 0.6rem;
      }
      span {
        vertical-align: middle;
      }
    }
  }
  .denomination {
    width: 100%;
    margin-top: 1.667rem;
    background-color: #fff;
    border-radius: 1.667rem;
    padding: 15px 20px;
  }
  .denominationCard {
    // width: 7.917rem;
    width: 29.3%;
    height: 5rem;
    border: 1px solid #E5E5E5;
    margin-right: 1.667rem;
    margin-top: 1.667rem;
    display: inline-block;
    // padding-left: 1rem;
    position: relative;
    span {
      display: block;
      font-size: 1.167rem;
      font-family: PingFang SC;
      font-weight: bold;
      color: #F58520;
      margin-top: 0.5rem;
      margin-bottom: 0.3rem;
      text-align: center;
    }
    i {
      display: block;
      text-align: center;
      font-style: normal;
      font-size: 1rem;
      font-family: PingFang SC;
      font-weight: 500;
      color: #666666;
    }
  }
  .denominationCard.activeBorder {
    border: 2px solid #F58520;
  }
  .denominationCard:nth-child(3n) {
    margin-right: 0;
  }
  .active {
    background-color: #F58520;
    -moz-opacity: 0.2;
    opacity:.20;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
  }
  .recharfeBtn {
    margin: 2.5rem 0;
    img {
      width: 100%;
    }
  }
  .explainHead {
    // padding: 0 4.667rem;
    text-align: center;
    margin-bottom: 2.083rem;
    i {
      display: inline-block;
      font-style: normal;
      width: 6rem;
      height: 1px;
      background-color: #999;
      vertical-align: middle;
    }
    span {
      display: inline-block;
      font-size: 1.5rem;
      font-family: PingFang SC;
      font-weight: 500;
      color: #666666;
      margin: 0 1.25rem;
    }
  }
  ol li {
    font-size: 1.1rem;
    font-family: PingFang SC;
    font-weight: 500;
    color: #666666;
    margin-bottom: 1.667rem;
  }
  @media screen and (max-width: 411px) {
    .cardContent .alert {
      font-size: 1.1rem;
    }
    .denominationCard {
      margin-right: 1.3rem;
    }
    .explainHead i {
      width: 4rem;
    }
  }
  @media screen and (max-width: 360px) {
    .cardContent .alert {
      font-size: 1rem;
      padding-right: 0;
    }
    .denominationCard {
      margin-right: 1rem;
      width: 30%;
      i {
        -webkit-transform: scale(0.9);
      }
    }
    .explainHead i {
      width: 4rem;
    }
  }
  @media screen and (max-width: 350px) {
    .denominationCard {
      margin-right: 0.4rem;
      width: 31.8%;
    }
    .explainHead i {
      width: 3rem;
    }
  }
  @media screen and (max-width: 335px) {
    .denominationCard {
      width: 32%;
    }
  }
  @media screen and (max-width: 333px) {
    .cardContent .alert {
      font-size: 1rem;
    }
    .denominationCard {
      width: 34.5%;
      margin-right: 1.3rem;
    }
    .explainHead i {
      width: 2.5rem;
    }
    /deep/.el-tabs--border-card > .el-tabs__content {
      height: 14rem;
    }
    /deep/#tab-petrifaction,
    /deep/#tab-petroleum {
      padding-left: 2rem;
      padding-right: 0;
    }
    .cardContent .el-input__inner {
      font-size: 1rem;
    }
    .denominationCard:nth-child(3n) {
      margin-right: 1.667rem;
    }
  }
</style>