<template>
    <view class="active-box">
        <!-- 热门标签 -->
        <view v-if="config.hot" class="btn-tag-star">
            <uni-icons type="star-filled" size="16" color="#ffd700"></uni-icons>
        </view>

        <!-- 图片区域 -->
        <view class="img crm-cursor" @click="handleClick">
            <image :src="imageSrc" mode="aspectFill" />
            <view v-if="config.imageTitle" class="imgTitle">{{ t(config.imageTitle) }}</view>
        </view>

        <!-- 内容区域 -->
        <view class="content">
            <view class="title">
                <span class="name crm-cursor crm-one-font" @click="handleClick">
                    {{ t(config.title) }}
                </span>
                <span v-if="config.time" class="time">{{ config.time }}</span>
            </view>

            <view v-if="config.description" class="des crm-one-font">
                {{ t(config.description) }}
            </view>

            <!-- 按钮区域 -->
            <view class="bottom">
                <template v-for="(btn, index) in config.buttons" :key="index">
                    <span v-if="shouldShowButton(btn)" :class="['btn', getButtonType(btn)]" @click="handleButtonClick(btn)">
                        {{ t(btn.text) }}
                        <template v-if="btn.suffix && state[btn.suffix]">
                            {{ state[btn.suffix] }}
                        </template>
                        <template v-if="btn.suffixText">
                            {{ t(btn.suffixText) }}
                        </template>
                    </span>
                    <span v-else-if="btn.elseType && btn.condition" :class="['btn', btn.elseType]">
                        {{ t(btn.text) }}
                    </span>
                </template>
            </view>
        </view>
    </view>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useI18n } from 'vue-i18n'

const { t, locale} = useI18n()

const props = defineProps<{
    config: any
    state?: Record<string, any>
}>()

const emit = defineEmits(['action'])

// 图片源
const imageSrc = computed(() => {
    if (typeof props.config.image === 'object') {
        const lang = locale.value || 'en'
        return props.config.image[lang] || props.config.image.default
    }
    return props.config.image
})

// 判断按钮是否显示
const shouldShowButton = (btn: any): boolean => {
    if (!btn.condition) return true
    if (!props.state) return false
    
    try {
        const conditionStr = btn.condition
        const evalStr = conditionStr.replace(/([a-zA-Z_][a-zA-Z0-9_.]*)/g, (match) => {
            if (match.includes('.')) {
                const parts = match.split('.')
                let value: any = props.state
                for (const part of parts) {
                    value = value?.[part]
                }
                return JSON.stringify(value)
            }
            return JSON.stringify(props.state[match])
        })
        const fn = new Function(`return ${evalStr}`)
        return fn()
    } catch (error) {
        console.error('按钮条件评估失败:', error, btn.condition)
        return false
    }
}

// 获取按钮类型
const getButtonType = (btn: any): string => {
    if (btn.type === 'dynamic') {
        return shouldShowButton(btn) ? 'red' : 'gray'
    }
    return btn.type
}

// 处理点击事件
const handleClick = () => {
    if (props.config.onClick) {
        emit('action', {
            type: props.config.onClick,
            params: props.config.onClickParams || []
        })
    }
}

// 处理按钮点击
const handleButtonClick = (btn: any) => {
    if (btn.action && btn.action !== 'disabled') {
        emit('action', {
            type: btn.action,
            params: btn.params || []
        })
    }
}
</script>

<style scoped lang="scss">
.active-box {
    position: relative;
    display: flex;
    background-color: #fff;
    border-radius: 16rpx;
    padding: 30rpx;
    margin-bottom: 20rpx;
    box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.1);

    .btn-tag-star {
        position: absolute;
        top: 0;
        left: 0;
        z-index: 1;
    }

    .img {
        width: 200rpx;
        height: 200rpx;
        margin-right: 30rpx;
        border-radius: 12rpx;
        overflow: hidden;
        position: relative;

        image {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }

        .imgTitle {
            position: absolute;
            bottom: 0;
            left: 0;
            right: 0;
            background: rgba(0, 0, 0, 0.5);
            color: #fff;
            font-size: 24rpx;
            padding: 8rpx;
            text-align: center;
        }
    }

    .content {
        flex: 1;
        display: flex;
        flex-direction: column;

        .title {
            display: flex;
            align-items: center;
            justify-content: space-between;
            margin-bottom: 10rpx;

            .name {
                font-size: 32rpx;
                font-weight: bold;
                color: #333;
                flex: 1;
                overflow: hidden;
                text-overflow: ellipsis;
                white-space: nowrap;
            }

            .time {
                font-size: 24rpx;
                color: #999;
                margin-left: 16rpx;
                white-space: nowrap;
            }
        }

        .des {
            font-size: 28rpx;
            color: #666;
            margin-bottom: 20rpx;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
            overflow: hidden;
            line-height: 1.4;
        }

        .bottom {
            display: flex;
            flex-wrap: wrap;
            gap: 16rpx;

            .btn {
                padding: 10rpx 24rpx;
                border-radius: 40rpx;
                font-size: 24rpx;
                font-weight: 500;
                transition: all 0.3s;
                display: inline-flex;
                align-items: center;
                justify-content: center;
                white-space: nowrap;

                &.red {
                    background-color: #ff4d4f;
                    color: #fff;
                    border: 1rpx solid #ff4d4f;

                    &:active {
                        opacity: 0.8;
                    }
                }

                &.gray {
                    background-color: #f5f5f5;
                    color: #999;
                    border: 1rpx solid #e5e5e5;
                }

                &.check {
                    background-color: #e6f7ff;
                    color: #1890ff;
                    border: 1rpx solid #91d5ff;

                    &:active {
                        opacity: 0.8;
                    }
                }
            }
        }
    }
}
</style>