<template>
    <div class="author-content">
        <div class="page-header">
            <div class="header-title">读者成就设置</div>
            <div class="status-info" v-if="form.status !== undefined">
                <el-tag 
                    :type="getStatusType(form.status)" 
                    :class="'status-tag status-' + form.status"
                >
                    {{ getStatusText(form.status) }}
                </el-tag>
            </div>
            <el-button 
                type="primary" 
                class="submit-btn" 
                :disabled="!canSubmit" 
                @click="handleSubmit"
            >
                {{ getSubmitButtonText() }}
            </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="!canEdit"
                        />
                    </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 v-if="form.status == 2 && form.rejectReason" class="reject-reason">
                <el-alert
                    title="驳回原因"
                    :description="form.rejectReason"
                    type="error"
                    show-icon
                    :closable="false"
                />
            </div>
        </div>
    </div>
</template>

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

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

        // 计算属性：是否可以编辑
        const canEdit = computed(() => {
            return form.status == undefined || form.status == 2; // 未设置或驳回状态可编辑
        });

        // 计算属性：是否可以提交
        const canSubmit = computed(() => {
            return canEdit.value && !isSubmitting.value;
        });

        const isSubmitting = ref(false);

        // 获取状态类型
        const getStatusType = (status) => {
            if (status == 0) return 'warning'; // 待审核
            if (status == 1) return 'success'; // 已通过
            if (status == 2) return 'danger';  // 驳回
            return 'info';
        };

        // 获取状态文本
        const getStatusText = (status) => {
            if (status == 0) return '待审核';
            if (status == 1) return '已通过';
            if (status == 2) return '已驳回';
            return '未设置';
        };

        // 获取提交按钮文本
        const getSubmitButtonText = () => {
            if (isSubmitting.value) {
                return '提交中...';
            }
            if (form.status == 0) {
                return '审核中';
            }
            if (form.status == 1) {
                return '已通过';
            }
            if (form.status == 2) {
                return '重新提交';
            }
            return '提交申请';
        };

        // 获取成就列表
        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 && response.result) {
                    // 确保状态是数字类型
                    const result = {
                        ...response.result,
                        status: parseInt(response.result.status) || 0
                    };
                    Object.assign(form, result);
                }
            } 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;
            }

            // 如果状态是已通过，不允许重复提交
            if (form.status == 1) {
                ElMessage.warning('成就设置已通过，无需重复提交');
                return;
            }

            try {
                isSubmitting.value = true;

                let data = {
                    oneName: form.oneName,
                    twoName: form.twoName,
                    threeName: form.threeName,
                }

                if(form.id){
                    data.id = form.id;
                }

                const response = await achievementApi.setAchievementName(data);
                if (response.success) {
                    ElMessage.success('提交成功，请等待审核');
                    // 等待一小段时间后重新获取详情以更新状态
                    setTimeout(async () => {
                        await getAchievementDetail();
                    }, 500);
                } else {
                    ElMessage.error(response.message || '提交失败');
                }
            } catch (error) {
                console.error('提交成就设置失败:', error);
                ElMessage.error('提交失败，请稍后重试');
            } finally {
                isSubmitting.value = false;
            }
        };

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

        return {
            achievements,
            form,
            keys,
            canEdit,
            canSubmit,
            isSubmitting,
            getStatusType,
            getStatusText,
            getSubmitButtonText,
            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;
        }

        .status-info {
            .status-tag {
                font-size: 14px;
                padding: 6px 12px;
                
                &.status-0 {
                    background-color: #fdf6ec;
                    color: #e6a23c;
                    border-color: #f5dab1;
                }
                
                &.status-1 {
                    background-color: #f0f9ff;
                    color: #67c23a;
                    border-color: #b3e19d;
                }
                
                &.status-2 {
                    background-color: #fef0f0;
                    color: #f56c6c;
                    border-color: #fbc4c4;
                }
            }
        }
        
        .submit-btn {
            background-color: #0A2463;
            border-color: #0A2463;
            
            &:hover, &:focus {
                background-color: #1a3473;
                border-color: #1a3473;
            }

            &.is-disabled {
                background-color: #c0c4cc;
                border-color: #c0c4cc;
                color: #fff;
            }
        }
    }

    .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;
            margin-bottom: 20px;
            
            .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;
                    }
                }
            }
        }

        .reject-reason {
            margin-top: 20px;
            padding: 16px;
            background-color: #fef0f0;
            border-radius: 6px;
            border-left: 4px solid #f56c6c;
        }
    }
}
</style> 