<template>
  <section class="AiPayment">
    <el-tabs type="card" stretch v-model="search.module" @tab-click="getPayments">
      <el-tab-pane label="激活码兑换" name="2"/>
      <el-tab-pane label="基础会员" name="0"/>
      <el-tab-pane label="金币充值" name="1"/>
    </el-tabs>
    <div class="content">
      <div v-if="search.module == '0'" class="mb-16">
        <div style="grid-template-columns: 1fr 1fr" class="payments ">
          <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>
          <el-form :model="vipForm" label-position="top" ref="vipForm" label-width="100px" class="form">
            <el-form-item
              prop="mallName"
              label="当前绑定店铺"
              :rules="[{ required: true, message: '请先登录拼多多跨境卖家中心', trigger: 'blur' }]">
              <el-input readonly placeholder="请先登录拼多多跨境卖家中心" v-model="vipForm.mallName"></el-input>
            </el-form-item>
          </el-form>
        </div>
      </div>
      <div v-else-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="mt-16 flex-base center">
            <div class="c-f00 f-20 mr-4" v-text="`￥${pay.price}`"/>
            <div class="c-999 through" v-text="`￥${pay.originPrice}`"/>
          </div>
          <div class="c-999 mt-16" v-text="`${pay.coin}金币`"/>
        </div>
      </div>
      <div v-else-if="search.module == '2'"  style="grid-template-columns: 1fr" class="payments mb-16">
        <el-form :model="form" label-position="top" ref="form" label-width="100px" class="form">
          <el-form-item
            prop="mallName"
            label="当前绑定店铺"
            :rules="[{ required: true, message: '请先登录拼多多跨境卖家中心', trigger: 'blur' }]">
            <el-input readonly placeholder="请先登录拼多多跨境卖家中心" v-model="form.mallName"></el-input>
          </el-form-item>
          <el-form-item
            prop="mallId"
            v-show="false"
            :rules="[{ message: '请输入店铺ID', trigger: 'blur' }]">
            <el-input placeholder="请输入店铺ID" v-model="form.mallId"></el-input>
          </el-form-item>
          <el-form-item
            prop="code"
            label="激活码"
            :rules="[{ required: true, message: '请输入激活码', trigger: 'blur' }]">
            <el-input placeholder="请输入激活码" v-model="form.code"></el-input>
          </el-form-item>
        </el-form>
      </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" v-if="search.module != '2'">
          <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" v-if="search.module != '2'">
        <el-button size="small" @click="$store.commit('setActiveDlgShow', false)">取消支付</el-button>
        <el-button size="small" @click="paied">已扫码支付</el-button>
      </div>
      <div class="bottom flex-center" v-else>
        <el-button size="small" @click="$store.commit('setActiveDlgShow', false)">取消</el-button>
        <el-button size="small" @click="active">确定</el-button>
      </div>
    </div>
  </section>
</template>
<script>
import { Message } from "element-ui"
import VueQr from "vue-qr"

export default {
  name: "AiPayment",
  components: {VueQr},
  props: {},
  data() {
    return {
      form: {
        mallId: this.$store.state.mallId,
        mallName: this.$store.state.mallName,
        code: ''
      },
      vipForm: {
        mallId: this.$store.state.mallId,
        mallName: this.$store.state.mallName
      },
      vipType: ["体验会员","年会员","年会员多店通用"],

      search: {module: "0"},
      show: true,
      descriptionsModule0: ["抢仓发货", "数据下载", "复制商品", "会员服务"],
      descriptionsModule1: ["商品采集", "店铺跟踪", "关键字跟踪"],
      payments: [],
      qrcode: "",
      amount: 0,
      selected: {}
    }
  },
  computed: {
    wechatLogo: () => require("../assets/wechat_logo.png")
  },
  methods: {
    getPayments() {
      if (this.search.module == '2') return
      this.$http.post("/api/priceConfig/page", null, {
        params: {...this.search}
      }).then(res => {
        if (res?.data) {
          this.payments = res.data.records || []
        }
      })
    },
    getMessage(type) {
      return `你使用的是“${this.vipType[type]}”兑换券，确定兑换？`;
    },
    getQrcode(item) {
      if (item.module == '0' && !this.vipForm.mallId) {
        Message.error("请先登录拼多多垮境卖家中心")
        return
      }
      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
        }
      })
    },
    paied() {
      this.$store.dispatch('getUserInfo')
      this.$store.commit('setActiveDlgShow', false)
    },
    active() {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.$http.post(`/api/coupon/getDetail`, null, {
              params: {
                code: this.form.code
              }
            }).then(res => {
              if (res.code == 0) {
                let msg = this.getMessage(res.data.type);
                this.$confirm(msg, '温馨提示', {
                  confirmButtonText: '确定',
                  cancelButtonText: '取消',
                  type: 'info'
                }).then(() => {
                  this.$http.post(`/api/order/upgradeByCode`, null, {
                    params: {
                      ...this.form
                    }
                  }).then(res => {
                    if (res.code == 0) {
                      this.$message.success('激活成功')
                      this.$store.dispatch('getUserInfo')
                      this.$store.commit('setActiveDlgShow', false)
                    }
                  })
                })
              }
            });

          }
        })
      }
  },
  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;
      margin-left: 32px;
    }
  }

  .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 16px;
      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>
