<template>
  <view class="demo-container">
    <view class="demo-header">
      <text class="demo-title">RefundPopup 退款弹窗组件演示</text>
    </view>

    <view class="demo-content">
      <!-- 场景选择 -->
      <view class="demo-section">
        <text class="section-title">退款场景</text>
        <view class="scenario-buttons">
          <button 
            class="scenario-btn" 
            :class="{ active: currentScenario === 'no_refund' }"
            @click="setScenario('no_refund')"
          >
            不予退款
          </button>
          <button 
            class="scenario-btn" 
            :class="{ active: currentScenario === 'free_cancel' }"
            @click="setScenario('free_cancel')"
          >
            免费取消
          </button>
          <button 
            class="scenario-btn" 
            :class="{ active: currentScenario === 'partial_refund' }"
            @click="setScenario('partial_refund')"
          >
            部分退款
          </button>
          <button 
            class="scenario-btn" 
            :class="{ active: currentScenario === 'anytime_refund' }"
            @click="setScenario('anytime_refund')"
          >
            随时可退
          </button>
        </view>
      </view>

      <!-- 金额设置 -->
      <view class="demo-section">
        <text class="section-title">退款金额</text>
        <view class="amount-input">
          <input 
            class="amount-field" 
            type="number" 
            v-model="refundAmount" 
            placeholder="请输入退款金额"
          />
          <text class="amount-unit">元</text>
        </view>
      </view>

      <!-- 操作按钮 -->
      <view class="demo-section">
        <button class="demo-btn primary" @click="showPopup">
          显示退款弹窗
        </button>
      </view>

      <!-- 事件日志 -->
      <view class="demo-section">
        <text class="section-title">事件日志</text>
        <view class="event-log">
          <view 
            class="log-item" 
            v-for="(log, index) in eventLogs" 
            :key="index"
          >
            <text class="log-time">{{ log.time }}</text>
            <text class="log-event">{{ log.event }}</text>
          </view>
          <view class="log-empty" v-if="eventLogs.length === 0">
            暂无事件日志
          </view>
        </view>
        <button class="demo-btn secondary" @click="clearLogs">
          清空日志
        </button>
      </view>
    </view>

    <!-- RefundPopup 组件 -->
    <RefundPopup
      v-model="popupVisible"
      :refund-type="currentScenario"
      :refund-amount="refundAmount"
      :refund-rules="customRules"
      @policy-click="handlePolicyClick"
      @confirm-click="handleConfirmClick"
      @close="handleClose"
    />
  </view>
</template>

<script setup>
import { ref, reactive } from 'vue'
import RefundPopup from './index.vue'

// 响应式数据
const popupVisible = ref(false)
const currentScenario = ref('no_refund')
const refundAmount = ref(399)
const eventLogs = ref([])

// 自定义退款规则（可选）
const customRules = ref([])

// 方法定义
const setScenario = (scenario) => {
  currentScenario.value = scenario
  addLog(`切换到场景: ${getScenarioName(scenario)}`)
  
  // 根据场景设置默认金额
  switch (scenario) {
    case 'no_refund':
      refundAmount.value = 0
      break
    case 'free_cancel':
      refundAmount.value = 399
      break
    case 'partial_refund':
      refundAmount.value = 199
      break
    case 'anytime_refund':
      refundAmount.value = 399
      break
  }
}

const getScenarioName = (scenario) => {
  const names = {
    'no_refund': '不予退款',
    'free_cancel': '免费取消',
    'partial_refund': '部分退款',
    'anytime_refund': '随时可退'
  }
  return names[scenario] || scenario
}

const showPopup = () => {
  popupVisible.value = true
  addLog('显示退款弹窗')
}

const handlePolicyClick = () => {
  addLog('点击了退订政策按钮')
  // 这里可以跳转到政策详情页面
  uni.showToast({
    title: '查看退订政策',
    icon: 'none'
  })
}

const handleConfirmClick = () => {
  addLog(`确认操作 - 场景: ${getScenarioName(currentScenario.value)}, 金额: ¥${refundAmount.value}`)
  
  // 根据不同场景执行不同操作
  if (currentScenario.value === 'no_refund') {
    uni.showToast({
      title: '已知晓退款政策',
      icon: 'success'
    })
  } else {
    uni.showToast({
      title: '退款申请已提交',
      icon: 'success'
    })
  }
}

const handleClose = () => {
  addLog('关闭退款弹窗')
}

const addLog = (event) => {
  const now = new Date()
  const time = `${now.getHours().toString().padStart(2, '0')}:${now.getMinutes().toString().padStart(2, '0')}:${now.getSeconds().toString().padStart(2, '0')}`
  
  eventLogs.value.unshift({
    time,
    event
  })
  
  // 限制日志数量
  if (eventLogs.value.length > 10) {
    eventLogs.value = eventLogs.value.slice(0, 10)
  }
}

const clearLogs = () => {
  eventLogs.value = []
  addLog('清空事件日志')
}

// 初始化
addLog('演示页面已加载')
</script>

<style lang="scss" scoped>
.demo-container {
  padding: 20px;
  background: #f5f5f5;
  min-height: 100vh;
}

.demo-header {
  text-align: center;
  margin-bottom: 30px;
  
  .demo-title {
    font-size: 20px;
    font-weight: 600;
    color: #333333;
  }
}

.demo-content {
  .demo-section {
    background: #ffffff;
    border-radius: 12px;
    padding: 20px;
    margin-bottom: 20px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    
    .section-title {
      font-size: 16px;
      font-weight: 600;
      color: #333333;
      margin-bottom: 16px;
      display: block;
    }
  }
}

.scenario-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  
  .scenario-btn {
    flex: 1;
    min-width: 80px;
    height: 40px;
    border: 2px solid #e0e0e0;
    border-radius: 20px;
    background: #ffffff;
    color: #666666;
    font-size: 14px;
    font-weight: 500;
    transition: all 0.3s ease;
    
    &.active {
      border-color: #007aff;
      background: #007aff;
      color: #ffffff;
    }
    
    &:active {
      transform: scale(0.95);
    }
  }
}

.amount-input {
  display: flex;
  align-items: center;
  gap: 8px;
  
  .amount-field {
    flex: 1;
    height: 44px;
    border: 2px solid #e0e0e0;
    border-radius: 8px;
    padding: 0 16px;
    font-size: 16px;
    background: #ffffff;
    
    &:focus {
      border-color: #007aff;
      outline: none;
    }
  }
  
  .amount-unit {
    font-size: 16px;
    color: #666666;
    font-weight: 500;
  }
}

.demo-btn {
  width: 100%;
  height: 48px;
  border: none;
  border-radius: 24px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  
  &.primary {
    background: #007aff;
    color: #ffffff;
    
    &:active {
      background: #0056cc;
      transform: scale(0.98);
    }
  }
  
  &.secondary {
    background: #f0f0f0;
    color: #666666;
    margin-top: 12px;
    
    &:active {
      background: #e0e0e0;
      transform: scale(0.98);
    }
  }
}

.event-log {
  max-height: 200px;
  overflow-y: auto;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  padding: 12px;
  background: #f9f9f9;
  
  .log-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 4px 0;
    border-bottom: 1px solid #f0f0f0;
    
    &:last-child {
      border-bottom: none;
    }
    
    .log-time {
      font-size: 12px;
      color: #999999;
      font-family: monospace;
      min-width: 60px;
    }
    
    .log-event {
      font-size: 14px;
      color: #333333;
      flex: 1;
    }
  }
  
  .log-empty {
    text-align: center;
    color: #999999;
    font-size: 14px;
    padding: 20px 0;
  }
}

// 响应式适配
@media screen and (max-width: 375px) {
  .demo-container {
    padding: 16px;
  }
  
  .demo-content {
    .demo-section {
      padding: 16px;
      margin-bottom: 16px;
    }
  }
  
  .scenario-buttons {
    .scenario-btn {
      min-width: 70px;
      height: 36px;
      font-size: 13px;
    }
  }
}
</style>