<!-- 分销命欢喜 - 充值 -->
<template>
  <el-dialog title="充值" width="400px" v-model="visible">
    <el-form ref="formRef" :model="form" :rules="rules" label-width="100" label-position="right">
      <el-form-item label="选择金额">
        <div class="num_list">
          <div class="btn" v-for="(item, index) in numList" :key="item.value" @click="numHandle(index)">
            <el-button :type="numActive == index ? 'primary' : 'default'">{{ item.value }}元</el-button>
          </div>
        </div>
      </el-form-item>
      <el-form-item label="自定义金额" prop="amount">
        <el-input v-model="form.amount" placeholder="请输入金额" @input="amountInput">
          <template #append>元</template>
        </el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <div class="btn">
          <el-button type="primary" @click="submitHandle" :loading="confirmLoading" style="width: 100%;">充 值</el-button>
        </div>
        <div class="btn">
          <el-button @click="visible = false" style="width: 100%;">取 消</el-button>
        </div>
      </div>
    </template>
  </el-dialog>
  <el-dialog title="请使用微信扫码充值" width="400px" v-model="showCodeDialog">
    <el-image :src="codeUrl" style="width: 100%; height: auto"></el-image>
    <div class="amount_wrap">充值￥{{ multiplyAndFormat(form.amount || 0) }}</div>
  </el-dialog>
</template>

<script setup>
import { ref } from 'vue'
import { filterNumberInput, multiplyAndFormat } from '@/utils'
import { distributionRechargeQrCode } from "@/api/coupon";

const visible = ref(false)
const numList = ref([
  {
    value: 500
  },
  {
    value: 800
  },
  {
    value: 1000
  },
  {
    value: 2000
  }
])
const form = ref({
  amount: ''
})

const rules = ref({
  amount: [
    {
      required: true,
      message: '请输入或选择金额',
      trigger: 'change'
    }
  ]
})

const numActive = ref(-1)
function numHandle(index) {
  numActive.value = index
  form.value.amount = numList.value[index].value
}

function amountInput(e) {
  form.value.amount = filterNumberInput(e)
  numActive.value = -1
}

// 提交
const formRef = ref(null)
const confirmLoading = ref(false)
const showCodeDialog = ref(false)
const codeUrl = ref('')
function submitHandle() {
  formRef.value.validate(async vaild => {
    try {
      if (vaild) {
        confirmLoading.value = true
        const res = await distributionRechargeQrCode(form.value)
        showCodeDialog.value = true
        codeUrl.value = res
      }
    } catch (error) {
      console.log(error);
    }
    setTimeout(() => {
      confirmLoading.value = false
    }, 300);
  })
}

function show() {
  visible.value = true
  form.value.amount = ''
  numActive.value = -1
}

defineExpose({
  show
})
</script>

<style scoped lang="scss">
.num_list {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
}

.dialog-footer {
  display: flex;
  gap: 14px;

  .btn {
    flex: 1;
  }
}

.amount_wrap {
  display: flex;
  justify-content: center;
  font-size: 16px;
  font-weight: bold;
}
</style>