<template>
  <view class="popup-mask" v-if="visible" @click="handleCancel">
    <view class="popup-content" @click.stop>
      <text class="popup-title">{{ t('giftPoints.title') }}</text>
      <view class="popup-body">
        <input
          class="popup-input"
          type="text"
          v-model="uid"
          :placeholder="t('giftPoints.uidPlaceholder')"
        />
        <input
          class="popup-input"
          type="number"
          v-model="amount"
          :placeholder="t('giftPoints.pointsPlaceholder')"
        />
      </view>
      <view class="popup-actions">
        <view class="btn cancel-btn" @click="handleCancel">
          <text class="btn-text cancel-text">{{ t('common.cancel') }}</text>
        </view>
        <view class="btn confirm-btn" @click="handleGift">
          <text class="btn-text confirm-text">{{ t('giftPoints.giftBtn') }}</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, watch } from 'vue'
import { useI18n } from 'vue-i18n'
import { giftPoints } from '../api/points.js'

const { t } = useI18n()

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['close', 'success'])

const uid = ref('')
const amount = ref('')

// 弹窗关闭时重置输入
watch(() => props.visible, (val) => {
  if (!val) {
    uid.value = ''
    amount.value = ''
  }
})

function handleCancel() {
  emit('close')
}

async function handleGift() {
  if (!uid.value || !amount.value) return
  try {
    await giftPoints(uid.value, Number(amount.value))
    uni.showToast({ title: t('mine.giftSuccess'), icon: 'none' })
    emit('success')
    emit('close')
  } catch (e) {
    // 错误提示已由请求模块统一处理
  }
}
</script>

<style scoped>
.popup-mask {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 999;
}

.popup-content {
  width: 600rpx;
  background-color: #ffffff;
  border-radius: 24rpx;
  padding: 40rpx;
}

.popup-title {
  font-size: 34rpx;
  font-weight: 600;
  color: #333;
  text-align: center;
  display: block;
  margin-bottom: 30rpx;
}

.popup-body {
  margin-bottom: 40rpx;
}

.popup-input {
  width: 100%;
  height: 80rpx;
  border: 1rpx solid #e0e0e0;
  border-radius: 12rpx;
  padding: 0 24rpx;
  font-size: 28rpx;
  margin-bottom: 20rpx;
  box-sizing: border-box;
}

.popup-actions {
  display: flex;
  gap: 20rpx;
}

.btn {
  flex: 1;
  height: 80rpx;
  border-radius: 40rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.cancel-btn {
  background-color: #f5f5f5;
}

.confirm-btn {
  background-color: #007aff;
}

.btn-text {
  font-size: 28rpx;
}

.cancel-text {
  color: #666;
}

.confirm-text {
  color: #ffffff;
}
</style>
