<template>
  <div class="oilRecharge">
    <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="oilCard">
      <!-- 选择油卡 -->
      <el-tabs v-model="activeName" type="border-card" @tab-click="handleClick">
        <el-tab-pane label="中国石化" name="petrifaction">
           <span slot="label">
             <img src="@/assets/image/petrifaction@2x.png" alt="">
              中国石化
            </span>
            <div class="cardContent">
              <div class="selectCard">选择油卡</div>
              <el-form :model="petrifaction" :rules="petrifactionRule" ref="petrifaction">
                <el-form-item prop="petrifactionCard">
                 <el-input v-model="petrifaction.petrifactionCard" placeholder="请输入以1开头的19位中石化加油卡号" />
                </el-form-item>
              </el-form>
              <div class="alert">
                <img src="@/assets/image/cardAlert@2x.png" alt="">
                <span>请仔细核对卡号，避免造成不必要的损失。</span>
              </div>
            </div>
        </el-tab-pane>
        <el-tab-pane label="中国石油" name="petroleum">
          <span slot="label">
            <img src="@/assets/image/petroleum@2x.png" alt="">
            中国石油
          </span>
          <div class="cardContent">
            <div class="selectCard">选择油卡</div>
            <el-form :model="petroleum" :rules="petroleumRule" ref="petroleum">
              <el-form-item prop="petroleumCard">
                <el-input v-model="petroleum.petroleumCard" placeholder="请输入以9开头的16位中石油加油卡号" />
              </el-form-item>
            </el-form>
            <div class="alert">
              <img src="@/assets/image/cardAlert@2x.png" alt="">
              <span>请仔细核对卡号，避免造成不必要的损失。</span>
            </div>
          </div>
        </el-tab-pane>
      </el-tabs>
      <!-- 充值面额 -->
      <div class="denomination">
        <div>充值面额</div>
        <div
          class="denominationCard"
          :class="{'activeBorder': idx == curAmountIdx}"
          v-for="(item, idx) in oil_amount" :key="item"
          @click="hPetrifaction(idx, item)">
          <span>{{item}}</span>
          <i>优惠价{{item * 0.995}}元</i>
          <div :class="{'active': idx == curAmountIdx}"></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. 本服务为全国加油卡代充服务、故<span>不提供充值发票</span>;</li>
          <li>4. 支付成功后，需收到充值成功通知短信后，方可到<span>加油站圈存</span>后加油使用;</li>
        </ol>
      </div>
    </div>
  </div>
</template>

<script>
import Http from '@/extend/Http';
import {Message} from "element-ui";
export default {
  name: 'oilRecharge',
  data() {
    var validPetrifaction = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('油卡不能为空'));
      } else if (!/^1\d{18}$/.test(value)) {
        return callback(new Error('请输入合法油卡'));
      } else {
        callback();
      }
    };
    var validPetroleum = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('油卡不能为空'));
      } else if (!/^9\d{15}$/.test(value)) {
        return callback(new Error('请输入合法油卡'));
      } else {
        callback();
      }
    };
    return {
      activeName: 'petrifaction',
      // 石化
      petrifaction: {
        petrifactionCard: '',
      },
      petrifactionRule: {
        petrifactionCard: [
          { validator: validPetrifaction, trigger: 'blur' }
        ],
      },
      // 石油
      petroleum: {
        petroleumCard: ''
      },
      petroleumRule: {
        petroleumCard: [
          { validator: validPetroleum, trigger: 'blur' }
        ],
      },
      // 充值面额
      oil_amount: [],
      // 当前面额
      curAmountIdx: null,
      curAmount: ''
    }
  },
  created() {
    this.getDenomination()
  },
  watch: {
    '$route': {
      handler(newVal) {
        if (newVal.path == '/oilRecharge' && !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.oil_amount = res.data.oil_amount
      }
    },
    // 选择石化充值面额
    hPetrifaction(idx, item) {
      this.curAmountIdx = idx
      this.curAmount = item
    },
    // 充值按钮
    recharfeBtn() {
      if (!this.curAmount) {
        Message({
          message: '请选择充值金额', type: "warning"
        });
        return
      }
      if (this.activeName === 'petrifaction') {
        this.$refs.petrifaction.validate(async valid => {
          if (valid) {
            let res = await Http.getInstance().RechargeOrder({
              cardno: this.petrifaction.petrifactionCard,
              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'
                }
              });
            }
          }
        })
      } else if (this.activeName === 'petroleum') {
        this.$refs.petroleum.validate(async valid => {
          if (valid) {
            let res = await Http.getInstance().RechargeOrder({
              cardno: this.petroleum.petroleumCard,
              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>
  .oilRecharge {
    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;
    }
  }
  .oilCard {
    padding: 0 1.667rem 7.5rem;
  }
  .el-tabs img {
    display: inline-block;
    width: 2.5rem;
  }
  /deep/.el-tabs--border-card {
    border-radius: 1.667rem;
    border: 0;
    box-shadow: none;
  }
  /deep/.el-tabs--border-card>.el-tabs__header {
    border-top-left-radius: 1.667rem;
    border-top-right-radius: 1.667rem;
    overflow: hidden;
    border:0;
  }
  /deep/.el-tabs__nav {
    width: 100%;
    height: 3.667rem;
  }
  /deep/#tab-petrifaction,
  /deep/#tab-petroleum {
    width: 50%;
    height: 100%;
    padding-left: 3rem;
    font-size: 1.4rem;
    border:0;
  }
  /deep/#tab-petroleum {
    width: 51%;
  }
  /deep/.el-tabs--border-card>.el-tabs__header .el-tabs__item:not(.is-disabled):hover,
  /deep/.el-tabs--border-card>.el-tabs__header .el-tabs__item.is-active  {
    color: #F47D53;
  }
  /deep/.el-tabs--border-card>.el-tabs__header .el-tabs__item {
    color: #666;
    background-color: #EDF0F5;
  }
  /deep/.el-tabs--border-card>.el-tabs__header .el-tabs__item.is-active {
    background-color: #fff;
  }
  /deep/.el-tabs--border-card>.el-tabs__content {
    height: 13rem;
  }
  /deep/.el-tabs--border-card>.el-tabs__content {
    padding: 15px 20px;
  }
  .cardContent {
    font-size: 1.167rem; 
    font-family: PingFang SC;
    color: #333;
    .selectCard {
      margin-bottom: 1.667rem;
    }
    /deep/.el-form-item {
      margin-bottom: 10px;
    }
    /deep/.el-input__inner {
      border: 0;
      border-bottom: 1px solid #BFBFBF;
      border-radius: 0;
      // margin-bottom: 1rem;
      padding-left: 0;
    }
    .alert {
      font-size: 1.2rem;
      color: #F58520;
      padding-left: 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(4n) {
    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;
    span {
      color: #F58520;
    }
  }
  @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: 334px) {
    .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: 0;
    }
    .denominationCard:nth-child(4n) {
      margin-right: 1.667rem;
    }
  }
</style>