<template>
	<view class="container">
		<view class="header">
			<text class="title">提交作业</text>
		</view>

		<view class="form-container">
			<input v-model="title" class="input" placeholder="作业标题" disabled />
			<textarea v-model="content" class="textarea" placeholder="作业描述" disabled></textarea>
			<textarea v-model="homeworkContent" class="textarea" placeholder="请输入作业内容"></textarea>

			<button class="submit-button" @click="submitHomework">提交作业</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				homeworkId: null, // 作业 ID
				title: '', // 作业标题
				content: '', // 作业描述
				homeworkContent: '', //作业内容
				nickName: '',
			};
		},
		onLoad(options) {
			this.homeworkId = options.id; // 接收传递过来的作业 ID
			this.title = options.title; // 接收传递过来的作业 ID
			this.content = options.content; // 接收传递过来的作业 ID
			this.nickName = uni.getStorageSync("nickName")

		},
		methods: {

			submitHomework() {
				const token = uni.getStorageSync('token'); // 获取存储的 token
				if (!token) {
					uni.showToast({
						title: '未登录，请重新登录',
						icon: 'none'
					});
					return;
				}

				if (!this.title || !this.content) {
					uni.showToast({
						title: '请填写完整的作业信息',
						icon: 'none'
					});
					return;
				}

				uni.request({
					url: `${this.$baseUrl}/homework/studentHomework`,
					method: 'POST',
					header: {
						Authorization: `Bearer ${token}`
					},
					data: {
						homeworkTitle: this.title,
						homeworkContent: this.homeworkContent,
						studentUsername: this.nickName,
					},
					success: (res) => {
						if (res.data.code === 200) {
							uni.showToast({
								title: '提交成功',
								icon: 'success'
							});
							uni.navigateBack(); // 返回到上一页面
						} else {
							uni.showToast({
								title: res.data.message || '提交失败',
								icon: 'none'
							});
						}
					},
					fail: () => {
						uni.showToast({
							title: '网络错误，请稍后重试',
							icon: 'none'
						});
					}
				});
			}
		}
	};
</script>

<style>
	.container {
		display: flex;
		flex-direction: column;
		background-color: #f8f9fa;
		height: 100vh;
	}

	.header {
		background-color: #007bff;
		padding: 15px;
		text-align: center;
	}

	.title {
		color: #fff;
		font-size: 24px;
		font-weight: bold;
	}

	.form-container {
		padding: 20px;
	}

	.input,
	.textarea {
		width: 100%;
		margin-bottom: 20px;
		padding: 10px;
		font-size: 16px;
		border: 1px solid #ddd;
		border-radius: 5px;
	}

	.textarea {
		height: 100px;
		resize: none;
	}

	.submit-button {
		width: 100%;
		padding: 15px;
		background-color: #007bff;
		color: #fff;
		border: none;
		border-radius: 5px;
		font-size: 18px;
		text-align: center;
	}

	.submit-button:hover {
		background-color: #0056b3;
	}
</style>