<template>
	<view class="achievement-page">
		<navbar title="读者成就设置" leftClick @leftClick="$utils.navigateBack" />
		<view class="achievement-card">
			<view class="card-title-row">
				<view class="card-title">阅读成就设置</view>
				<view v-if="statusText" class="status-tag" :class="statusClass">{{ statusText }}</view>
			</view>
			<view class="achievement-list">
				<view class="achievement-item"
				v-for="(item, index) in list"
				:key="index">
					<image class="badge-img"
						:src="item.image"
						mode="aspectFill" />
					<view class="">
						<view class="input-area">
							<view class="label-row">
								<text class="required">*</text>
								<text class="label">{{ item.title }}</text>
							</view>
							<input class="input" 
							v-model="form[keys[index] + 'Name']" 
							placeholder="请输入" 
							placeholder-class="input-placeholder"
							:disabled="!canEdit" />
						</view>
						<view class="input-area" v-if="form[keys[index] + 'Num']">
							<view class="label-row">
								<text class="label">达成人数</text>
							</view>
							<view class="Num">
								{{ form[keys[index] + 'Num'] || 0 }}
							</view>
						</view>
					</view>
				</view>
				<!-- <view class="divider"></view>
				<view class="achievement-item">
					<image class="badge-img"
						src="https://tse3-mm.cn.bing.net/th/id/OIP-C.wUsFZgl70iE4tI7b_HKaKgHaHa?w=166&h=180&c=7&r=0&o=5&dpr=1.1&pid=1.7"
						mode="aspectFill" />
					<view class="input-area">
						<view class="label-row">
							<text class="required">*</text>
							<text class="label">二级成就名称</text>
						</view>
						<input class="input" v-model="level2" placeholder="请输入" placeholder-class="input-placeholder" />
					</view>
				</view>
				<view class="divider"></view>
				<view class="achievement-item">
					<image class="badge-img"
						src="https://tse3-mm.cn.bing.net/th/id/OIP-C.wUsFZgl70iE4tI7b_HKaKgHaHa?w=166&h=180&c=7&r=0&o=5&dpr=1.1&pid=1.7"
						mode="aspectFill" />
					<view class="input-area">
						<view class="label-row">
							<text class="required">*</text>
							<text class="label">三级成就名称</text>
						</view>
						<input class="input" v-model="level3" placeholder="请输入" placeholder-class="input-placeholder" />
					</view>
				</view> -->
			</view>
		</view>
		<view class="bottom-btn-area">
			<button class="submit-btn" :disabled="!canEdit" 
            :class="{disabled: !canEdit}"
				@click="submit">
				{{ submitButtonText }}
			</button>
		</view>
	</view>
</template>

<script>
    import mixinsList from '@/mixins/list.js'
	export default {
        mixins: [mixinsList],
		data() {
			return {
                mixinsListApi : 'getAchievementList',
				keys : ['one', 'two', 'three'],
				form : {},
			}
		},
		computed: {
			// 判断是否可以编辑
			canEdit() {
				// status=0(审核中)不能编辑，status=1(已通过)、status=2(不通过)和第一次提交(没有form.id)可以编辑
				return this.form.status != 0
			},
			// 状态文本
			statusText() {
				// 第一次提交时不显示状态标签
				if (!this.form.id) return ''
				
				// 根据状态返回文本
				if (this.form.status == 0) return '审核中'
				if (this.form.status == 1) return '已通过'
				if (this.form.status == 2) return '不通过'
				
				return ''
			},
			// 状态样式类
			statusClass() {
				if (this.form.status == 0) return 'status-pending'
				if (this.form.status == 1) return 'status-approved'
				if (this.form.status == 2) return 'status-rejected'
				return ''
			},
			// 提交按钮文本
			submitButtonText() {
				if (!this.form.id) return '提交申请'
				if (this.form.status == 2) return '重新提交'
				return '设置'
			}
		},
		onLoad() {
			this.getDetail()
		},
		methods: {
			async getDetail(){
				const data = await this.$fetch('getAchievement') || {}
				// 使用展开运算符确保响应式更新
				this.form = { ...data }
			},
			async submit() {
				if(this.$utils.verificationAll(this.form, {
					oneName : '请填写' + this.list[0].title,
					twoName : '请填写' + this.list[1].title,
					threeName : '请填写' + this.list[2].title,
				})){
					return
				}

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

                if(this.form.id){
                    data.id = this.form.id
                }
				
				await this.$fetch('setAchievementName', data)
				
				uni.showToast({
					title: '提交成功',
					icon: 'none'
				})
				setTimeout(() => {
					uni.navigateBack()
				}, 1000)
			},
		}
	}
</script>

<style scoped lang="scss">
	.achievement-page {
		min-height: 100vh;
		background: #f7f8fa;
		display: flex;
		flex-direction: column;
	}

	.achievement-card {
		background: #fff;
		border-radius: 20rpx;
		margin: 40rpx 32rpx 0 32rpx;
		padding: 32rpx 24rpx;
		box-shadow: 0 4rpx 24rpx 0 rgba(0, 0, 0, 0.04);
	}

	.card-title-row {
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.card-title {
		font-size: 32rpx;
		font-weight: bold;
		margin-bottom: 32rpx;
	}

	.status-tag {
		font-size: 24rpx;
		border-radius: 10rpx;
		padding: 6rpx 24rpx;
		margin-left: 20rpx;
		margin-bottom: 32rpx;
	}

	.status-pending {
		background: #ff9500;
		color: #fff;
	}

	.status-approved {
		background: #07c160;
		color: #fff;
	}

	.status-rejected {
		background: #e23d3d;
		color: #fff;
	}

	.achievement-list {
		display: flex;
		flex-direction: column;
		gap: 0;
	}

	.achievement-item {
		display: flex;
		align-items: flex-start;
		padding: 18rpx 0 10rpx 0;
	}

	.badge-img {
		width: 72rpx;
		height: 72rpx;
		margin-right: 20rpx;
		border-radius: 50%;
		background: #f5f5f5;
		object-fit: cover;
		border: 2rpx solid #fff;
		box-shadow: 0 2rpx 8rpx 0 rgba(0, 0, 0, 0.04);
	}

	.input-area {
		margin-top: 15rpx;
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: flex-start;
	}

	.label-row {
		display: flex;
		align-items: center;
		margin-bottom: 6rpx;
	}

	.required {
		color: #e23d3d;
		font-size: 28rpx;
		margin-right: 4rpx;
	}

	.label {
		font-size: 24rpx;
		color: #999;
		font-weight: bold;
	}
	
	.Num{
	}

	.input {
		width: 100%;
		border: none;
		border-bottom: 1.5rpx solid #ececec;
		font-size: 28rpx;
		background: transparent;
		padding: 16rpx 0;
		margin-bottom: 2rpx;
	}

	.input:disabled {
		background: #f5f5f5;
		color: #999;
	}

	.input-placeholder {
		color: #d2d2d2;
		font-size: 26rpx;
	}

	.divider {
		height: 1rpx;
		background: #f3f3f3;
		margin: 0 0 0 92rpx;
	}

	.bottom-btn-area {
		margin-top: auto;
		padding: 48rpx 32rpx 32rpx 32rpx;
		background: transparent;
	}

	.submit-btn {
		width: 100%;
		height: 88rpx;
		background: #0a2e6d;
		color: #fff;
		font-size: 32rpx;
		border-radius: 44rpx;
		font-weight: bold;
		letter-spacing: 2rpx;
		transition: background 0.2s;
	}

	.submit-btn.disabled {
		background: #aaa;
		color: #fff;
	}

	.submit-btn.pending {
		background: #0a226d;
		color: #fff;
	}
</style>