<template>
  <view
    class="uni-list-item"
    :class="{ 'uni-list-item--hover': hoverClass, 'uni-list-item--disabled': disabled }"
    @click="onClick"
  >
    <view class="uni-list-item__container">
      <!-- 左侧图标/插槽 -->
      <view v-show="showHeader" class="uni-list-item__header">
        <slot name="header">
          <view v-if="thumb" class="uni-list-item__icon">
            <image :src="thumb" class="uni-list-item__thumb" mode="aspectFill" />
          </view>
          <view v-else-if="icon" class="uni-list-item__icon">
            <text class="uni-icons text-gray-500" :class="icon"></text>
          </view>
        </slot>
      </view>

      <!-- 内容区 -->
      <view class="uni-list-item__content">
        <slot>
          <text v-if="title" class="uni-list-item__title">{{ title }}</text>
          <text v-if="note" class="uni-list-item__note">{{ note }}</text>
        </slot>
      </view>

      <!-- 右侧 -->
      <view class="uni-list-item__extra">
        <slot name="footer">
          <text v-if="rightText" class="uni-list-item__right-text">{{ rightText }}</text>
        </slot>
        <text v-if="showArrow" class="uni-icons uniui-arrowright text-gray-400 ml-2"></text>
      </view>
    </view>

    <!-- 分割线 -->
    <view v-if="border" class="uni-list-item__border"></view>

    <!-- 左滑操作区 -->
    <view v-if="showSwipe" class="uni-list-item__swipe">
      <slot name="swipe">
        <view class="uni-list-item__swipe-actions">
          <view
            v-for="(action, index) in swipeActions"
            :key="index"
            class="uni-list-item__swipe-btn"
            :style="{ backgroundColor: action.color }"
            @click.stop="onSwipeClick(action)"
          >
            <text class="text-white text-sm">{{ action.text }}</text>
          </view>
        </view>
      </slot>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted, getCurrentInstance } from 'vue'

const instance = getCurrentInstance()
const showHeader = ref(true)

onMounted(() => {
  const slots = instance?.slots || {}
  showHeader.value = !!(props.showArrow || props.thumb || props.icon || slots.header)
})

interface SwipeAction {
  text: string
  color: string
  key?: string
}

interface Props {
  title?: string
  note?: string
  thumb?: string
  icon?: string
  rightText?: string
  showArrow?: boolean
  border?: boolean
  hoverClass?: boolean
  disabled?: boolean
  showSwipe?: boolean
  swipeActions?: SwipeAction[]
  clickable?: boolean
}

const props = withDefaults(defineProps<Props>(), {
  border: true,
  showArrow: false,
  hoverClass: true,
  disabled: false,
  showSwipe: false,
  clickable: true,
  swipeActions: () => []
})

const emit = defineEmits<{
  click: []
  swipeClick: [action: SwipeAction]
}>()

function onClick() {
  if (props.disabled || !props.clickable) return
  emit('click')
}

function onSwipeClick(action: SwipeAction) {
  emit('swipeClick', action)
}
</script>

<style scoped>
.uni-list-item {
  position: relative;
  overflow: hidden;
  background-color: #fff;
}
.uni-list-item__container {
  display: flex;
  align-items: center;
  padding: 12px 15px;
  position: relative;
  background-color: #fff;
  z-index: 1;
  transition: transform 0.3s;
}
.uni-list-item--hover:active .uni-list-item__container {
  background-color: #f5f5f5;
}
.uni-list-item--disabled {
  opacity: 0.6;
}
.uni-list-item__header {
  display: flex;
  align-items: center;
  margin-right: 10px;
}
.uni-list-item__thumb {
  width: 40px;
  height: 40px;
  border-radius: 6px;
}
.uni-list-item__icon {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f3f4f6;
  border-radius: 10px;
}
.uni-list-item__content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.uni-list-item__title {
  font-size: 15px;
  color: #1f2937;
  line-height: 1.5;
}
.uni-list-item__note {
  font-size: 12px;
  color: #9ca3af;
  margin-top: 4px;
  line-height: 1.4;
}
.uni-list-item__extra {
  display: flex;
  align-items: center;
  margin-left: 10px;
}
.uni-list-item__right-text {
  font-size: 14px;
  color: #6b7280;
}
.uni-list-item__border {
  position: absolute;
  bottom: 0;
  right: 0;
  left: 15px;
  height: 1px;
  background-color: #e5e7eb;
  transform: scaleY(0.5);
}
.uni-list-item__swipe {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  z-index: 0;
}
.uni-list-item__swipe-actions {
  display: flex;
  height: 100%;
}
.uni-list-item__swipe-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 20px;
  height: 100%;
}
</style>
