<template>
    <div class="author-content">
        <div class="page-header">
            <div class="header-title">读者成就设置</div>
            <el-button type="primary" class="submit-btn" :disabled="isPending" @click="handleSubmit">
                {{ isPending ? '设置审核中' : '提交申请' }}
            </el-button>
        </div>

        <div class="readers-container">
            <div class="achievement-grid">
                <div v-for="(achievement, index) in achievements" :key="index" class="achievement-item">
                    <div class="achievement-icon">
                        <img :src="achievement.image" :alt="achievement.title" />
                    </div>
                    <div class="achievement-name">{{ achievement.title }}</div>
                    <div class="achievement-input">
                        <el-input 
                            v-model="form[keys[index] + 'Name']" 
                            placeholder="请输入成就名称"
                            :disabled="isPending"
                        />
                    </div>
                    <div class="achievement-stats" v-if="form[keys[index] + 'Num']">
                        <span class="label">达成人数：</span>
                        <span class="num">{{ form[keys[index] + 'Num'] || 0 }}</span>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import { defineComponent, ref, reactive, onMounted } from 'vue';
import { ElMessage } from 'element-plus';
import { achievementApi } from '@/api/bookshelf';

export default defineComponent({
    name: 'ReadersManagement',
    setup() {
        const isPending = ref(false);
        const keys = ['one', 'two', 'three'];
        const form = reactive({});
        const achievements = ref([]);

        // 获取成就列表
        const getAchievementList = async () => {
            try {
                const response = await achievementApi.getAchievementList();
                if (response.success) {
                    achievements.value = response.result || [];
                }
            } catch (error) {
                console.error('获取成就列表失败:', error);
                ElMessage.error('获取成就列表失败');
            }
        };

        // 获取成就设置详情
        const getAchievementDetail = async () => {
            try {
                const response = await achievementApi.getAchievement();
                if (response.success) {
                    Object.assign(form, response.result);
                    isPending.value = response.result.status === 0;
                }
            } catch (error) {
                console.error('获取成就设置失败:', error);
                ElMessage.error('获取成就设置失败');
            }
        };

        // 提交成就设置
        const handleSubmit = async () => {
            // 验证所有输入是否填写
            const emptyFields = keys.filter(key => !form[key + 'Name']?.trim());
            if (emptyFields.length > 0) {
                ElMessage.warning('请填写所有成就名称');
                return;
            }

            try {
                const response = await achievementApi.setAchievementName(form);
                if (response.success) {
                    ElMessage.success('提交成功');
                    isPending.value = true;
                } else {
                    ElMessage.error(response.message || '提交失败');
                }
            } catch (error) {
                console.error('提交成就设置失败:', error);
                ElMessage.error('提交失败，请稍后重试');
            }
        };

        onMounted(async () => {
            await getAchievementList();
            await getAchievementDetail();
        });

        return {
            achievements,
            form,
            keys,
            isPending,
            handleSubmit
        };
    }
});
</script>

<style lang="scss" scoped>
.author-content {
    flex: 1;
    
    .page-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 20px;
        background-color: #fff;
        padding: 16px 20px;
        border-radius: 8px;
        box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
        
        .header-title {
            font-size: 18px;
            font-weight: bold;
            color: #333;
        }
        
        .submit-btn {
            background-color: #0A2463;
            border-color: #0A2463;
            
            &:hover, &:focus {
                background-color: #1a3473;
                border-color: #1a3473;
            }

            &.is-disabled {
                background-color: #807a7a;
                border-color: #807a7a;
            }
        }
    }

    .readers-container {
        background-color: #fff;
        border-radius: 8px;
        box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
        padding: 40px;
        
        .achievement-grid {
            display: flex;
            flex-wrap: wrap;
            justify-content: space-around;
            gap: 40px;
            
            .achievement-item {
                display: flex;
                flex-direction: column;
                align-items: center;
                width: 180px;
                
                .achievement-icon {
                    width: 100px;
                    height: 100px;
                    margin-bottom: 16px;
                    
                    img {
                        width: 100%;
                        height: 100%;
                        object-fit: contain;
                    }
                }
                
                .achievement-name {
                    font-size: 14px;
                    color: #333;
                    margin-bottom: 12px;
                    text-align: center;
                }
                
                .achievement-input {
                    width: 100%;
                    margin-bottom: 8px;
                    
                    :deep(.el-input__wrapper) {
                        text-align: center;
                    }
                }

                .achievement-stats {
                    font-size: 12px;
                    color: #666;
                    text-align: center;

                    .label {
                        color: #999;
                    }

                    .num {
                        color: #333;
                        font-weight: bold;
                    }
                }
            }
        }
    }
}
</style> 