<template>
  <!--
    ss-search-button 搜索按钮组件
    基于原JSP中的mobileSearch-button样式封装
  -->
  <button
    class="ss-search-button"
    :style="buttonStyle"
    @click="handleClick"
    :disabled="disabled"
  >

    <!-- 前置图标插槽 -->
    <view v-if="preIcon || $slots.preIcon" class="ss-search-button__pre-icon">
      <slot name="preIcon">
        <Icon v-if="preIcon" :name="preIcon" :size="iconSize" :color="iconColor" />
      </slot>
    </view>

    <!-- 按钮文本 -->
    <text class="ss-search-button__text">{{ text }}</text>

    <!-- 后置图标插槽 -->
    <view v-if="suffixIcon || $slots.suffixIcon" class="ss-search-button__suffix-icon">
      <slot name="suffixIcon">
        <Icon v-if="suffixIcon" :name="suffixIcon" :size="iconSize" :color="iconColor" />
      </slot>
    </view>
  </button>
</template>

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

const props = defineProps({
  // 按钮文本
  text: {
    type: String,
    default: '增加'
  },
  // 是否禁用
  disabled: {
    type: Boolean,
    default: false
  },
  // 按钮高度
  height: {
    type: [String, Number],
    default: '36px'
  },
  // 前置图标名称
  preIcon: {
    type: String,
    default: ''
  },
  // 后置图标名称
  suffixIcon: {
    type: String,
    default: ''
  },
  // 图标大小
  iconSize: {
    type: [String, Number],
    default: '32'
  },
  // 图标颜色
  iconColor: {
    type: String,
    default: '#585d6e'
  },
  // 自定义按钮样式
  customStyle: {
    type: Object,
    default: () => ({})
  },
  // 跳转链接（兼容原JSP用法）
  href: {
    type: String,
    default: ''
  }
})

const emit = defineEmits(['click'])

// 按钮样式
const buttonStyle = computed(() => ({
  height: typeof props.height === 'number' ? `${props.height}px` : props.height,
  ...props.customStyle
}))


const handleClick = () => {
  // 触发点击事件
  emit('click')
}
</script>

<style lang="scss" scoped>
.ss-search-button {

  height: auto;
  padding: 0 1rem;
  border: 1px solid #eceded;
  outline: none;
  background-color: #fff;
  text-align: center;
  font-size: 1rem;
  color: #585d6e;
  letter-spacing: 0.1rem;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  -webkit-appearance: none;
  appearance: none;
  box-sizing: border-box;
  transition: all 0.2s ease;

  &:active {
    background-color: #eceded;
    color: #fff;
    border-color: #eceded;
    transform: scale(0.98);
    transition: all 0.1s ease;

    // active 状态下图标样式
    .ss-search-button__pre-icon,
    .ss-search-button__suffix-icon {
      :deep(.iconfont) {
        color: #fff !important;
      }
      :deep(text) {
        color: #fff !important;
      }
    }
  }

  &:disabled {
    opacity: 0.6;
    cursor: not-allowed;
  }

  &__pre-icon {
    margin-right: 0.3rem;
    display: flex;
    align-items: center;
  }

  &__suffix-icon {
    margin-left: 0.3rem;
    display: flex;
    align-items: center;
  }

  &__text {
    font-size: inherit;
    color: inherit;
  }
}

// 响应式适配
@media screen and (max-width: 750px) {
  .ss-search-button {
    font-size: 14px;
    padding: 0 0.8rem;
  }
}
</style>
