<template>
    <uni-col :xs="24" :sm="24" :md="12" :lg="8" :xl="8">
        <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="widthFix" />
            </view>
            <!-- 内容区域 -->
            <view class="content">
                <view class="content-box">
                    <view v-if="config.time" class="time">{{ config.time }}</view>
                    <view class="title" @click="handleClick">{{ t(config.title) }}</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">
                            <text 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>
                            </text>
                            <text v-else-if="btn.elseType && btn.condition" :class="['btn', btn.elseType]">
                                {{ t(btn.text) }}
                            </text>
                        </template>
                    </view>
                </view>
            </view>
        </view>
    </uni-col>
</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 shouldClassCondition = (btn: any): boolean => {
    if (!btn.classCondition) return true
    if (!props.state) return false

    try {
        const conditionStr = btn.classCondition
        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) && shouldClassCondition(btn) ? 'red' : '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">
@import "@/uni.scss";

.active-box {
    width: 100%;
    position: relative;
    margin-bottom: px2rpx(20);
    padding: 0;
    aspect-ratio: 3 / 4;

    .btn-tag-star {
        position: absolute;
        top: px2rpx(30);
        left: px2rpx(30);
        z-index: 2;
        background: linear-gradient(135deg, #ffd700 0%, #ffb347 100%);
        padding: px2rpx(12) px2rpx(28);
        border-radius: px2rpx(40);
        font-size: px2rpx(26);
        font-weight: bold;
        color: #0f1423;
        display: flex;
        align-items: center;
        gap: px2rpx(8);
        box-shadow: 0 px2rpx(4) px2rpx(12) rgba(255, 215, 0, 0.3);
        letter-spacing: px2rpx(1);
        text-transform: uppercase;

        .uni-icons {
            font-size: px2rpx(28) !important;
        }
    }

    .img {
        width: 100%;
        position: relative;
        overflow: hidden;

        image {
            width: 100%;
            object-fit: cover;
            transition: transform 0.6s ease;
        }
    }

    .content {
        width: 100%;
        padding: 0 px2rpx(14);
        box-sizing: border-box;
        margin-top: px2rpx(-20);


        .content-box {
            width: 100%;
            height: 100%;
            display: flex;
            flex-direction: column;
            padding: 0 px2rpx(14);
            box-sizing: border-box;
            background-color: var(--color-white);
            z-index: 1;
            position: relative;
        }

        .title {
            width: 100%;
            margin: px2rpx(20) 0;
            font-size: px2rpx(24);
            font-weight: 700;
            color: var(--color-error);
            line-height: 1.3;
            display: block;
            margin-bottom: px2rpx(12);
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }

        .time {
            margin-top: px2rpx(20);
            width: fit-content;
            font-size: px2rpx(12);
            color: var(--color-error);
            background-color:
                color-mix(in oklab, var(--color-error) 10%, transparent);
            line-height: px2rpx(20);
            padding: px2rpx(5) px2rpx(7);
            border-radius: px2rpx(2);
        }

        .des {
            width: 100%;
            font-size: px2rpx(14);
            color: var(--color-slate-900);
            margin-bottom: px2rpx(40);
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
            overflow: hidden;
            line-height: 1.2;
        }

        .bottom {
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            gap: px2rpx(20);

            .btn {
                color: var(--color-white);
                padding: px2rpx(16);
                font-size: px2rpx(14);
                font-weight: 600;
                transition: all 0.3s ease;
                display: inline-flex;
                align-items: center;
                justify-content: center;
                white-space: nowrap;
                letter-spacing: px2rpx(1);
                text-transform: uppercase;
                padding: px2rpx(8) px2rpx(16);
                box-sizing: border-box;
                cursor: pointer;

                &.red {
                    width: 100%;
                    background-color: var(--color-secondary);
                }

                &.check {
                    font-size: px2rpx(11);
                    background-color: var(--color-error);
                }

                &.gray {
                    cursor: not-allowed;
                    background-color: var(--color-navy-200);
                }
            }
        }
    }

    // 列表项样式（用于底部链接）
    .link-list {
        display: flex;
        gap: px2rpx(30);
        flex-wrap: wrap;
        margin-top: px2rpx(30);
        padding-top: px2rpx(30);
        border-top: px2rpx(1) solid rgba(255, 215, 0, 0.2);

        .link-item {
            color: #8e9aaf;
            font-size: px2rpx(26);
            transition: color 0.3s;

            &:active {
                color: #ffd700;
            }

            &::before {
                content: '•';
                color: #ffd700;
                margin-right: px2rpx(8);
            }
        }
    }
}

// 针对不同屏幕尺寸的响应式调整
@media screen and (max-width: 767px) {
    .active-box {

        .title {
            width: px2rpx(200) !important;
        }
    }
}

// 添加悬停效果（仅H5）
/* #ifdef H5 */
.active-box:hover {
    // transform: translateY(-6rpx);
    // box-shadow: 0 p 60rpx rgba(0, 0, 0, 0.5);
    // transition: all 0.3s ease;

    .img image {
        transform: scale(1.05);
    }
}

/* #endif */
</style>