<template>
  <section class="AiPayment">
    <el-tabs type="card" stretch v-model="search.module" @tab-click="getPayments">
      <el-tab-pane label="基础会员" name="0"/>
      <el-tab-pane label="金币充值" name="1"/>
    </el-tabs>
    <div class="content">
      <div v-if="search.module == '0' " style="grid-template-columns: 1fr 1fr" class="payments mb-16">
        <div class="card" v-for="pay in payments" :key="pay.id" :class="{active:pay.id==selected.id}"
             @click="getQrcode(pay)">
          <div v-text="pay.title"/>
          <div class="c-f00 mt-16" v-text="`￥${pay.price}`"/>
        </div>
      </div>
      <div v-if="search.module == '1' " class="payments mb-16">
        <div class="card" v-for="pay in payments" :key="pay.id" :class="{active:pay.id==selected.id}"
             @click="getQrcode(pay)">
          <div class="c-f00 mt-16" v-text="`￥${pay.price}`"/>
          <div class="c-999 mt-16" v-text="`${pay.coin}金币`"/>
        </div>
      </div>
      <el-row type="flex" align="middle">
        <ul class="fill" v-if="search.module == '0'">
          <li v-for="(desc,i) in descriptionsModule0" :key="i" v-text="desc"/>
        </ul>
        <ul class="fill" v-if="search.module == '1'">
          <li v-for="(desc,i) in descriptionsModule1" :key="i" v-text="desc"/>
        </ul>
        <div class="fill flex-center">
          <vue-qr v-if="qrcode" :text="qrcode" :size="120" :margin="8" :logoSrc="wechatLogo"/>
          <div v-else class="qrcode c-666">请选择<br>项目</div>
          <div class="c-999 ml-16">
            <div class="flex-center mb-16">
              应付金额：<div class="c-f00" style="font-size: 20px;">￥{{ selected.price }}</div>
            </div>
            <div class="wechat flex-center">
              微信扫码支付
            </div>
          </div>
        </div>
      </el-row>
      <div class="bottom flex-center">
        <el-button size="small">取消支付</el-button>
        <el-button size="small">已扫码支付</el-button>
      </div>
    </div>
  </section>
</template>
<script>
import VueQr from "vue-qr"

export default {
  name: "AiPayment",
  components: {VueQr},
  props: {},
  data() {
    return {
      search: {module: "0"},
      show: true,
      descriptionsModule0: ["抢仓发货", "数据下载", "复制商品", "会员服务"],
      descriptionsModule1: ["智能复制"],
      payments: [],
      qrcode: "",
      amount: 0,
      selected: {}
    }
  },
  computed: {
    wechatLogo: v => require("../assets/wechat_logo.png")
  },
  methods: {
    getPayments() {
      this.$http.post("/api/priceConfig/page", null, {
        params: {...this.search}
      }).then(res => {
        if (res?.data) {
          this.payments = res.data.records || []
        }
      })
    },
    getQrcode(item) {

      this.selected = item
      this.$http.post("/api/order/createOrder", null, {
        params: {priceConfigId: item.id}
      }).then(res => {
        if (res?.data?.id) {
          return res.data.id
        }
      }).then(id => this.$http.post("/api/order/createPrepayOrder", null, {
        params: {id}
      })).then(res => {
        if (res?.data) {
          this.qrcode = res.data.codeUrl
        }
      })
    }
  },
  created() {
    this.getPayments()
  }
}
</script>
<style scoped lang="scss">
.AiPayment {
  font-size: 16px;

  :deep(.el-tabs) {
    z-index: 4;

    .is-active {
      border-bottom-color: transparent;
    }

  }

  ul {
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-gap: 16px;

    li {
      list-style-type: circle;
    }
  }

  .payments {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr 1fr;
    grid-gap: 32px;

    .card {
      user-select: none;
      text-align: center;
      border: 1px solid #ddd;
      padding: 32px 64px;
      border-radius: 4px;

      &.active {
        border-color: #26f;
        position: relative;

        &:after {
          position: absolute;
          right: 8px;
          bottom: 8px;
          display: block;
          text-align: center;
          line-height: 20px;
          width: 20px;
          height: 20px;
          font-size: 16px;
          content: "✔";
          color: white;
          background: #26f;
          border-radius: 50%;
        }
      }
    }
  }

  .content {
    padding: 16px 32px;
    margin-top: -16px;
    border: 1px solid #ddd;
    z-index: 9;

    .qrcode {
      width: 120px;
      height: 120px;
      background-color: #eee;
      display: flex;
      align-items: center;
      justify-content: center;
      text-align: center;
    }
  }

  .wechat {
    line-height: 20px;
    padding-left: 28px;
    background-image: url("../assets/wechat_logo.png");
    background-repeat: no-repeat;
    background-position: left center;
    background-size: 20px 20px;
  }

  .bottom {
    justify-content: right;
  }
}
</style>
