<template>
	<view class="container">
		<view class="message-form">
			<u-cell-group :border="false">
				<u-field type="textarea" placeholder="请输入留言" v-model="formData.leaMsgContent" label-width="0"></u-field>
			</u-cell-group>
			<u-upload :action="uploadUrl" :form-data="uploadData" @on-success="uploadSuccess" @on-error="uploadError" @on-remove="uploadRemove"></u-upload>
		</view>
		<view class="form-handle">
			<u-button type="success" shape="circle" :ripple="true" @click="submitData" class="handle-custom">发布</u-button>
		</view>
		<u-top-tips ref="uTips"></u-top-tips>
	</view>
</template>

<script>
	const NET = require('@/utils/request')
	const API = require('@/config/api')
	export default {
		data() {
			return {
				formData: {
					orderId: '',
					tenantCode: '',
					leaMsgContent: '',
				},
				uploadData: {
					folderId: 0,
				},
				uploadUrl: '',
				fileList: []
			}
		},
		onLoad(options) {
			this.uploadUrl = API.uploadFile
			this.formData.orderId = options.orderId
			this.formData.tenantCode = options.tenantCode
		},
		methods: {
			//  文件上传成功回调
			uploadSuccess(res, index, lists, name) {
				this.fileList.push(res.data.url)
				this.$refs.uTips.show({
					title: '文件上传成功',
					type: 'success',
				})
				return true
			},
			//  文件上传失败回调
			uploadError(res, index, lists, name) {
				this.$refs.uTips.show({
					title: '文件上传失败',
					type: 'warning',
				})
			},
			//  移除文件回调
			uploadRemove(index, lists, name) {
				this.fileList.splice(index, 1)
			},
			//  提交数据
			submitData() {
				NET.request(API.addMessage, {
					...this.formData,
					leaMsgType: 2,
					mid: uni.getStorageSync("userData").userId,
					orderLeaImgResVOs: this.fileList.map((site, index) => {
						return {
							fileType: 1,
							imgUrl: site,
							sortOrder: index + 1,
						}
					})
				}, 'POST').then(res => {
					this.$refs.uTips.show({
						title: '留言成功',
						type: 'success',
					})
					setTimeout(() => {
						uni.navigateBack()
					}, 1000)
				}).catch(error => {
					console.log(error)
					this.$refs.uTips.show({
						title: error.data.msg,
						type: 'warning',
					})
				})
			},
		},
	}
</script>

<style lang="less" scoped>
	page {
		width: 100%;
		height: 100%;
	}

	.container {
		width: 100%;
		height: 100%;
		float: left;
		box-sizing: border-box;
		background-color: #FFFFFF;
		padding-bottom: 70px;
		overflow-y: auto;

		.message-form {
			width: 100%;
			float: left;
			box-sizing: border-box;
			padding: 0 15px;
			background-color: #ffffff;

			/deep/.u-field {
				padding-left: 0px;
				padding-right: 0px;
			}

			/deep/.u-cell {
				padding-left: 0px;
				padding-right: 0px;
			}
		}

		.form-handle {
			width: calc(100% - 30px);
			height: 40px;
			position: fixed;
			bottom: 20px;
			left: 15px;

			.handle-custom {
				background-color: #51A539;
			}
		}
	}
</style>
