/*增加子账户上限*/
<template>
  <div>
    <transition name="marsk-fade">
      <div v-show="buyConfig.isShow" class="marsk" />
    </transition>
    <transition name="slide-fade">
      <div v-show="buyConfig.isShow" class="action-limit">
        <div class="title">
          <p>购买资源</p>
          <div class="operation">
            <i class="el-icon-circle-close" @click="close" />
          </div>
        </div>
        <p class="tips">
          剩余积分：<span>{{ buyConfig.point }}</span>
        </p>
        <div class="select">
          资源类型：
          <el-select
            v-model="buyConfig.resType"
            size="mini"
            placeholder="请选择"
            @change="getSelect"
          >
            <el-option
              v-for="item in typeList"
              :key="item.value"
              :value="item.value"
              :label="item.label"
            ></el-option>
          </el-select>
        </div>
        <div class="num-list">
          <div v-for="(item, index) in buyConfig.numList" :key="index">
            <el-radio
              v-if="item.saleUnit == 0"
              v-model="buyConfig.num"
              :label="item.returnGoods"
              @change="select"
            >
              <p>{{ item.returnGoods }}台资源</p>
              <span>{{ item.salePrice }}积分</span>
            </el-radio>
          </div>
        </div>
        <div class="desc">
          <p>
            购买时间：
            <el-input
              ref="times"
              v-model="buyConfig.times"
              style="width: 150px"
              type="number"
              size="mini"
              name="times"
              placeholder="购买时间"
            >
              <span slot="suffix" class="solt">天</span>
            </el-input>
          </p>
          <p>资源规格：{{ buyConfig.specs }}</p>
        </div>
        <div class="btn-group">
          <el-button
            type="danger"
            size="small"
            class="next-btn"
            @click.native.prevent="sure"
            >确认</el-button
          >
        </div>
      </div>
    </transition>
  </div>
</template>

<script>
import { request } from '@/utils/request'
export default {
  name: 'BuyResource',
  props: {
    buyConfig: {
      isShow: {
        type: Boolean,
        default: () => false,
      },
      num: {
        type: Number,
        default: () => 0,
      },
      intergal: {
        type: Number,
        default: () => 0,
      },
      times: {
        type: Number,
        default: () => 0,
      },
      specs: {
        type: String,
        default: () => '',
      },
      resType: {
        type: String,
        default: () => '',
      },
      numList: {
        type: Array,
        default: () => [],
      },
      selectGoodsCode: {
        type: String,
        default: () => '',
      },
      point: {
        type: String,
        default: () => '0',
      },
    },
  },
  data() {
    return {
      typeList: [
        {
          value: '0',
          label: '通用',
        },
      ],
    }
  },
  methods: {
    // 点击关闭
    close() {
      this.$emit('putBuy', false)
    },
    // 选择
    select() {
      this.buyConfig.numList.map((item) => {
        if (this.buyConfig.num == item.returnGoods) {
          this.buyConfig.intergal = item.salePrice
          this.buyConfig.spec = item.remarks
          this.buyConfig.selectGoodsCode = item.goodsCode
        }
      })
    },
    //获取选择类型
    getSelect(type) {
      //获取商品列表
      const params = {
        transCode: 'C00022',
        type: '4', // 类型(1-充值，2-增加子账户上限 4-购买资源)
        subType: type, // 资源类型(0-通用）
        saleUnit: '', // 出售方式(0-整卖 1-零卖)
      }
      request(params)
        .then((res) => {
          this.buyConfig = {
            isShow: true,
            intergal: res.rows.filter((tmp) => tmp.saleUnit == 0)[0].salePrice,
            num: res.rows.filter((tmp) => tmp.saleUnit == 0)[0].returnGoods,
            times: 30,
            specs: res.rows.filter((tmp) => tmp.saleUnit == 0)[0].remarks,
            resType: type,
            numList: res.rows.filter((tmp) => tmp.saleUnit == 0),
            selectGoodsCode: res.rows.filter((tmp) => tmp.saleUnit == 0)[0]
              .goodsCode,
          }
        })
        .catch((err) => {})
    },
    // 点击确定
    sure() {
      if (this.buyConfig.times == '')
        return this.$message.warning('请输入购买时间！')
      if (
        !/(^[1-9]\d*$)/.test(this.buyConfig.times) ||
        this.buyConfig.times.toString().indexOf('.') != -1
      )
        return this.$message.warning('购买时间只能是正整数!')
      const params = {
        transCode: 'C00028',
        resCount: this.buyConfig.num,
        resType: this.buyConfig.resType,
        resGoodsCode: this.buyConfig.selectGoodsCode,
        resPeriod: this.buyConfig.times.toString(),
      }
      request(params)
        .then((res) => {
          this.$message.success(`恭喜你，成功购买 ${this.buyConfig.num} 台资源`)
          this.$emit('putBuy', this.buyConfig)
        })
        .catch((err) => {})
    },
  },
}
</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(4, 112, 204, 0.05);
}

.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-limit {
  @include position(absolute, 50%, auto, 55%, auto, 1001);
  @include w_h(450px);
  padding: 30px 40px;
  transform: translate(-50%, -50%);
  background: $color_f;
  box-shadow: 0 0 7px 7px rgba(0, 0, 0, 0.04);
  .title {
    @include flex();
    font-size: 18px;
    font-weight: 700;
    color: $color_3;
    .operation {
      font-size: 24px;
      color: $color_9;
      &:hover {
        cursor: pointer;
        color: $color_b;
      }
    }
  }
  .tips {
    margin-top: 10px;
    font-size: 12px;
    color: $color_9;
    span {
      font-size: 14px;
      color: $color_on;
    }
  }
  .num-list {
    @include w_h();
    @include flex(flex-start);
    flex-wrap: wrap;
    margin: 30px auto 10px;
  }
  .select {
    margin-top: 20px;
    font-size: 12px;
    color: $color_6;
  }
  .desc {
    font-size: 12px;
    color: $color_6;
    p {
      line-height: 40px;
    }
  }
  .btn-group {
    @include w_h();
    margin: 30px auto;
    .next-btn {
      @include w_h();
    }
  }
}
</style>

<style lang="scss">
@import '@/styles/variables.scss';
@import '@/styles/mixin.scss';
.num-list {
  .el-radio {
    margin: 0 10px 10px 0;
    padding: 8px 10px;
    min-width: 90px;
    line-height: 20px;
    font-size: 12px;
    border: 1px solid $color_b;
    border-radius: 4px;
    &.is-checked {
      border: 1px solid #0470cc;
      background: #e7f3fb;
    }
    &__input {
      display: none;
    }
    &__label {
      padding: 0;
    }
    &__label p,
    &__input.is-checked + &__label p {
      color: $color_3;
    }
    &__label span,
    &__input.is-checked + &__label span {
      color: #0470cc;
    }
  }
}

@supports (-webkit-mask: none) and (not (cater-color: $color_9)) {
  .el-input input {
    font-size: 12px;
    color: $color_6;
  }
}

.el-input {
  &__inner {
    padding: 0 5px;
  }
  &__inner:focus {
    border-color: $color_9;
  }
  &--suffix {
    line-height: 28px;
  }
  &--suffix .el-input__inner {
    padding-right: 20px;
  }
}
</style>
