<template>
	<view class="page">
		<view class="form">
			<u--form :labelStyle="{color:'#000000',fontSize:'34rpx'}" labelWidth="80">
				<u-form-item label="主题" borderBottom required>
					<u--input
						v-model="title"
						name="title"
						placeholder="请填写主题"
						fontSize="17"
						color="#000000"
						inputAlign="right"
						border="none"
					/>
				</u-form-item>
				<u-form-item label="内容" labelPosition="top" borderBottom>
					<textarea v-model="content" name="content" auto-height placeholder="请填写内容" />
				</u-form-item>
				<u-form-item label="图片(最多5张)" labelPosition="top" labelWidth="120" borderBottom>
					<view class="item">
						<u-upload
							:fileList="images"
							uploadIcon="photo"
							multiple
							maxCount="5"
							previewFullImage
							@afterRead="uploadImage"
							@delete="deleteImage"
						/>
					</view>
				</u-form-item>
				<u-form-item label="附件" labelPosition="top" borderBottom>
					<view class="item">
						<u-upload
							:fileList="files"
							accept="file"
							uploadIcon="plus"
							maxCount="1"
							@afterRead="uploadFile"
							@delete="deleteFile"
						/>
					</view>
				</u-form-item>
				<u-form-item v-if="reason" label="未通过原因" labelPosition="top" labelWidth="auto" borderBottom>
					<u-text type="warning" :text="reason" margin="30rpx 0" />
				</u-form-item>
				<u-form-item>
					<button class="btn-danger" @click="submit">提交</button>
				</u-form-item>
			</u--form>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				id: '',
				type: '',
				title: '',
				content: '',
				images: [],
				files: [],
				reason: ''
			}
		},
		onLoad(e) {
			if (e.type) {
				this.type = e.type
			}
			if (e.id) {
				this.id = e.id
				uni.setNavigationBarTitle({
					title: '修改'
				})
				this.$ajax.get(`/api/user/score/${this.id}`).then(res => {
					if (res.status == 0) {
						this.title = res.data.title
						this.content = res.data.content
						if (res.data.images) {
							this.images = res.data.images.map((value) => {
								return {url: value}
							})
						}
						if (res.data.file) {
							this.files = [{ url: res.data.file }]
						}
						if (res.data.check_status == 2) {
							this.reason = res.data.check_remarks
						}
					}
				})
			}
		},
		methods: {
			deleteImage(event) {
				this.images.splice(event.index, 1)
			},
			async uploadImage(event) {
				let lists = [].concat(event.file)
				let fileListLen = this.images.length
				lists.map((item) => {
					this.images.push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.$ajax.upload('/api/web/upload', {filePath: lists[i].url, name: 'file'})
					let item = this.images[fileListLen]
					this.images.splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result.data.file
					}))
					fileListLen++
				}
			},
			uploadFile(event) {
				const file = event.file
				file.status = 'uploading'
				file.message = '上传中'
				this.$ajax.upload('/api/web/upload', {filePath: file.url, name: 'file'}).then(res => {
					if (res.status == 0) {
						file.status = 'success'
						file.message = ''
						file.url = res.data.file
						this.files.push(file)
					}
				})
			},
			deleteFile(event) {
				this.files.splice(event.index, 1)
			},
			submit() {
				if (!this.title) {
					return uni.showToast({
						title: '主题必填',
						icon: 'error'
					})
				}
				const params = {
					type_id: this.type,
					title: this.title,
					content: this.content,
					images: this.images.length > 0 ? this.images.map(item => { return item.url }) : null,
					file: this.files.length > 0 ? this.files[0].url : null
				}
				this.$ajax.post('/api/user/score', params, { custom: {loading: true} }).then(res => {
					if (res.status == 0) {
						uni.showToast({
							title: '提交成功',
							icon: 'success'
						})
						setTimeout(() => {
							uni.navigateBack()
						}, 800)
					}
				})
			}
		}
	}
</script>

<style>
	.page {
		padding-top: 20rpx;
	}

	.form {
		background-color: white;
		padding: 0 30rpx;
	}
	.item {
		margin-top: 30rpx;
	}
	::v-deep uni-textarea {
		min-height: 110rpx;
		background-color: #F5F5F5;
		width: 100%;
		margin-top: 20rpx;
		padding: 10rpx 15rpx;
	}
	.btn-danger {
		width: 100%;
	}
</style>