<template>
  <button
    :class="['gradient-button', { 'gradient-button--disabled': disabled, 'gradient-button--loading': loading }]"
    :disabled="disabled || loading"
    @click="handleClick"
  >
    <span v-if="loading" class="gradient-button__loading">
      <a-spin size="small" />
      <span v-if="loadingText" class="gradient-button__loading-text">{{ loadingText }}</span>
    </span>
    <span v-else class="gradient-button__content">
      <slot name="icon">
        <span v-if="icon" class="gradient-button__icon">
          <GmIcon :name="icon" :size="iconSize" />
        </span>
      </slot>
      <span class="gradient-button__text">
        <slot>{{ text }}</slot>
      </span>
    </span>
  </button>
</template>

<script setup>
import { computed } from 'vue'
import GmIcon from '@/components/icons/Icon.vue'

const props = defineProps({
  // 按钮文本
  text: {
    type: String,
    default: ''
  },
  // 是否禁用
  disabled: {
    type: Boolean,
    default: false
  },
  // 是否加载中
  loading: {
    type: Boolean,
    default: false
  },
  // 加载中的文本
  loadingText: {
    type: String,
    default: ''
  },
  // 图标名称
  icon: {
    type: String,
    default: ''
  },
  // 图标大小
  iconSize: {
    type: Number,
    default: 16
  },
  // 按钮大小
  size: {
    type: String,
    default: 'large', // large, middle, small
    validator: (value) => ['large', 'middle', 'small'].includes(value)
  },
  // 是否块级按钮
  block: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['click'])

const handleClick = (event) => {
  if (!props.disabled && !props.loading) {
    emit('click', event)
  }
}

const buttonClass = computed(() => {
  return [
    `gradient-button--${props.size}`,
    {
      'gradient-button--block': props.block
    }
  ]
})
</script>

<style scoped>
.gradient-button {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 8px 24px;
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 500;
  color: #ffffff;
  cursor: pointer;
  transition: all 0.2s ease;
  background: var(--color-slate-900);
  user-select: none;
}

.gradient-button:hover {
  background: var(--color-slate-800);
}

.gradient-button:active {
  background: var(--color-slate-800);
  transform: translateY(0);
  box-shadow: 0 4px 6px -2px rgba(59, 130, 246, 0.15);
}

.gradient-button__content,
.gradient-button__loading {
  position: relative;
  z-index: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

.gradient-button__icon {
  display: inline-flex;
  align-items: center;
}

.gradient-button__text {
  line-height: 1;
}

.gradient-button__loading-text {
  margin-left: 4px;
}

/* 尺寸变体 */
.gradient-button--large {
  padding: 10px 20px;
  font-size: 15px;
  min-height: 36px;
}

.gradient-button--middle {
  padding: 8px 16px;
  font-size: 14px;
  min-height: 32px;
}

.gradient-button--small {
  padding: 4px 12px;
  font-size: 13px;
  min-height: 24px;
}

/* 块级按钮 */
.gradient-button--block {
  width: 100%;
  display: flex;
}

/* 禁用状态 */
.gradient-button--disabled,
.gradient-button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  background: var(--color-slate-700);
  box-shadow: none;
}

.gradient-button--disabled:hover,
.gradient-button:disabled:hover {
  background: var(--color-slate-700);
  transform: none;
  box-shadow: none;
}

/* 加载状态 */
.gradient-button--loading {
  cursor: wait;
}

.gradient-button--loading:hover {
  transform: none;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .gradient-button--large {
    padding: 8px 16px;
    font-size: 14px;
    min-height: 32px;
  }
}
</style>
