/*积分充值*/
<template>
  <div>
    <transition name="marsk-fade">
      <div v-show="chargeConfig.isShow" class="marsk" />
    </transition>
    <transition name="slide-fade">
      <div v-show="chargeConfig.isShow" class="action-charge">
        <div class="container">
          <div class="header">
            <p>
              积分充值
              <span>(当前账户：{{ chargeConfig.account }})</span>
            </p>
            <div class="operation">
              <i class="el-icon-circle-close" @click="close" />
            </div>
          </div>
          <div class="content">
            <div class="content-list align">
              <div class="lable">充值金额</div>
              <div class="price-list">
                <div
                  v-for="(item, index) in chargeConfig.ruleList"
                  :key="index"
                >
                  <el-radio
                    v-if="item.saleUnit == 0"
                    v-model="chargeConfig.amount"
                    :label="item.salePrice"
                    @change="select"
                  >
                    <p>到账积分{{ item.returnGoods }}</p>
                    <p class="price">
                      ￥{{ item.salePrice }}
                      <del v-if="item.isDiscount == 1">￥{{ item.price }}</del>
                    </p>
                    <div v-if="item.isDiscount == 1" class="discount">
                      超值推荐
                    </div>
                  </el-radio>
                </div>
              </div>
            </div>
            <div class="content-list">
              <div class="lable">自定义充值</div>
              <div class="custom">
                <div class="custom-input">
                  <el-input
                    ref="price"
                    v-model="price"
                    type="number"
                    size="small"
                    style="width: 180px"
                    name="price"
                    placeholder="请输入充值积分数"
                    @keyup.native="checkNum"
                  >
                    <span slot="suffix" class="solt">积分</span>
                  </el-input>
                  <el-button
                    type="primary"
                    size="small"
                    @click.native.prevent="sure"
                    >确定</el-button
                  >
                </div>
                <div class="tips">
                  ※注：{{ chargeConfig.description }}
                  <!-- <span v-if="price"
                    >，{{ chargeConfig.amount }}元={{
                      chargeConfig.intergal
                    }}积分</span
                  > -->
                </div>
              </div>
            </div>
            <div class="content-list">
              <div class="lable">支付方式</div>
              <div class="pay-type">
                <el-radio v-model="chargeConfig.payType" label="WeChat">
                  <el-image
                    class="image-pay"
                    :src="wechat"
                    fit="cover"
                    alt="wechat"
                  />微信支付
                </el-radio>
                <el-radio v-model="chargeConfig.payType" label="Alipay">
                  <el-image
                    class="image-pay"
                    :src="alipay"
                    fit="cover"
                    alt="alipay"
                  />支付宝支付
                </el-radio>
              </div>
            </div>
          </div>
        </div>
        <div class="footer">
          <p>
            实付金额：
            <span>￥{{ chargeConfig.amount }}</span>
          </p>
          <el-button type="danger" size="small" @click.native.prevent="submit"
            >提交订单</el-button
          >
        </div>
      </div>
    </transition>
  </div>
</template>

<script>
import wechat from '@/assets/img/wechat.png'
import alipay from '@/assets/img/alipay.png'
import { request } from '@/utils/request'
export default {
  name: 'Charge',
  props: {
    chargeConfig: {
      isShow: {
        type: Boolean,
        default: () => false,
      },
      account: {
        type: String,
        default: () => '',
      },
      payType: {
        type: String,
        default: () => 'WeChat',
      },
      intergal: {
        type: String,
        default: () => '0',
      },
      amount: {
        type: String,
        default: () => '0',
      },
      ruleList: {
        type: Array,
        default: () => [],
      },
      selectGoodsCode: {
        type: String,
        default: () => '',
      },
      defineGoodsCode: {
        type: String,
        default: () => '',
      },
      description: {
        type: String,
        default: () => '',
      },
    },
  },
  data() {
    return {
      wechat: wechat,
      alipay: alipay,
      price: '',
    }
  },
  methods: {
    // 点击关闭
    close() {
      this.price = ''
      this.$emit('putCharge', false)
    },
    // 选择
    select() {
      this.chargeConfig.ruleList.map((item) => {
        if (this.chargeConfig.amount == item.salePrice) {
          this.chargeConfig.intergal = item.returnGoods
          this.chargeConfig.selectGoodsCode = item.goodsCode
          this.price = ''
        }
      })
    },
    checkNum() {
      let num = this.price
      if (num == '') return this.$message.warning('请输入充值积分数!')
      if (!/(^[1-9]\d*$)/.test(num) || num.toString().indexOf('.') != -1)
        return this.$message.warning('积分只能是正整数!')
      return true
    },
    // 确定
    sure() {
      if (this.checkNum()) {
        //获取商品列表
        const params = {
          transCode: 'C00023',
          type: '1', //1-充值
          count: this.price,
          goodsCode: this.chargeConfig.defineGoodsCode,
        }
        request(params)
          .then((res) => {
            this.chargeConfig.amount = res.salePrice
            this.chargeConfig.intergal = res.returnGoods
            this.chargeConfig.selectGoodsCode = this.chargeConfig.defineGoodsCode
          })
          .catch((err) => {})
      }
    },
    // 提交订单
    submit() {
      if (this.chargeConfig.amount == 0)
        return this.$message.warning('请选择或输入充值积分数!')
      this.price = ''
      this.$emit('putCharge', this.chargeConfig)
    },
  },
}
</script>

<style lang="scss" scoped>
@import '@/styles/variables.scss';
@import '@/styles/mixin.scss';

.marsk-fade-enter-active,
.marsk-fade-leave-active {
  transition: all 0.5s;
}
.marsk-fade-enter,
.marsk-fade-leave-to {
  opacity: 0;
}
.marsk {
  @include position(fixed, 0, 0, 0, 0, 1000);
  background: rgba(255, 255, 255, 0.98);
}

.slide-fade-enter-active {
  transition: all 0.3s ease;
}
.slide-fade-leave-active {
  transition: all 0.3s cubic-bezier(1, 0.5, 0.8, 1);
}
.slide-fade-enter,
.slide-fade-leave-to {
  transform: translateY(7.48rem);
}
.action-charge {
  @include position(absolute, 0, 0, 0, 0, 1001);
  @include w_h(100%, 100%);
  padding: 50px 12% 0;
  min-width: 1000px;
  .container {
    @include w_h();
    .header {
      @include flex();
      position: relative;
      p {
        font-size: 24px;
        color: $color_3;
        span {
          font-size: 12px;
          color: $color_9;
        }
      }
      .operation {
        @include position(absolute, 5px, auto, auto, 5px, 1);
        font-size: 24px;
        color: $color_9;
      }
      .operation:hover {
        cursor: pointer;
        color: $color_b;
      }
    }
    .content {
      @include w_h();
      padding: 50px 0 0;
      font-size: 14px;
      color: $color_3;
      .content-list {
        @include flex(flex-start, baseline);
        margin-bottom: 30px;
        .lable {
          width: 80px;
        }
        .price-list {
          @include w_h(calc(100% - 80px));
          @include flex(flex-start);
        }
        .custom {
          &-input {
            @include flex(flex-start);
          }
          .tips {
            font-size: 12px;
            line-height: 30px;
            color: $color_9;
          }
        }
        .pay-type {
          @include flex(flex-start);
        }
      }
      .align {
        align-items: flex-start;
      }
    }
  }
  .footer {
    @include position(fixed, auto, 0, 0, 0, 1001);
    @include flex();
    @include w_h(100%, 60px);
    line-height: 60px;
    padding: 0 12%;
    background: $color_f;
    box-shadow: 0px -1px 7px 0px rgba(0, 0, 0, 0.04);
    p {
      font-size: 12px;
      color: $color_3;
      span {
        font-size: 18px;
        color: $color_on;
      }
    }
  }
}
</style>

<style lang="scss">
@import '@/styles/variables.scss';
@import '@/styles/mixin.scss';

.action-charge {
  .el-button {
    &--mini,
    &--mini.is-round {
      padding: 5px 15px;
    }
  }

  .el-radio {
    margin-right: 15px;
    font-size: 14px;
    border: 1px solid $color_b;
    border-radius: 4px;
    &.is-checked {
      border: 1px solid #0470cc;
      background: #e7f3fb;
    }
    &__input {
      display: none;
    }
    &__label {
      padding: 0;
    }
  }
  .price-list {
    .el-radio {
      padding: 15px 10px;
      // min-width: 125px;
      line-height: 20px;
      &__label p,
      &__input.is-checked + &__label p {
        color: $color_3;
      }
      &__label p.price,
      &__input.is-checked + &__label p.price {
        color: #0470cc;
      }
      &__label p del,
      &__input.is-checked + &__label p del {
        font-size: 12px;
        color: $color_9;
      }
      &__label .discount {
        @include position(absolute, -10px, auto, auto, -10px, 1001);
        padding: 3px 10px;
        font-size: 12px;
        color: $color_f;
        background: $color_on;
        transform: scale(0.8);
        border-radius: 0 10px 0 10px;
      }
    }
  }
  .custom {
    .el-input {
      margin-right: 15px;
      &__inner {
        margin-right: 20px;
        border: 1px solid $color_b;
        border-radius: 0;
      }
      &__suffix-inner {
        color: $color_3;
        line-height: 30px;
      }
      &--suffix .el-input__inner {
        padding-right: 30px;
      }
    }
  }
  .pay-type {
    .el-radio {
      padding: 10px 15px;
      &__input.is-checked + &__label {
        color: $color_9;
      }
      &__label {
        @include flex();
      }
      .el-image__inner {
        @include w_h(14px);
        margin-right: 5px;
      }
    }
  }
}
</style>
