<template>
	<view class="content">
		<u-navbar back-text="返回" title="建议反馈" back-icon-color="#ffffff" :back-text-style="{color: '#ffffff'}" title-color="#ffffff"
		 :background="{backgroundColor: mainColor}"></u-navbar>
		<u-form :model="form" ref="form" label-width="140">
			<u-form-item label="建议反馈" prop="content" required>
				<u-input v-model="form.content" placeholder="请描述您的建议反馈" type="textarea" auto-height :height="180" />
			</u-form-item>
			<u-form-item label="上传附件" label-position="top">
				<u-upload max-count="1" :multiple="false" :action="uploadUrl" :header="uploadHeader" @on-success="uploadSuccess"
				 @on-error="uploadError" @on-remove="uploadRemove"></u-upload>
			</u-form-item>
		</u-form>
		<view class="handle-fix-box">
			<u-button type="primary" :ripple="true" :custom-style="customStyle" @click="submitForm()">提交</u-button>
		</view>
		<u-top-tips ref="uTips" :navbar-height="statusBarHeight + navbarHeight"></u-top-tips>
	</view>
</template>

<script>
	import {
		mapGetters
	} from 'vuex'
	const NET = require('@/utils/request')
	const API = require('@/config/api')
	export default {
		computed: {
			...mapGetters([
				'mainColor',
				'customStyle',
			])
		},
		data() {
			return {
				statusBarHeight: uni.getSystemInfoSync().statusBarHeight,
				navbarHeight: 44,
				form: {
					areaId: '',
					tenantId: '',
					tenantName: '',
					assetId: '',
					assetName: '',
					content: '',
					fileList: [],
				},
				uploadUrl: API.uploadFile,
				uploadHeader: {
					Authorization: 'Bearer ' + uni.getStorageSync('token')
				},
				rules: {
					content: [{
						required: true,
						message: '请描述您的建议反馈',
						trigger: 'change'
					}],
				},
			}
		},
		onLoad(options) {
			this.form.areaId = options.areaId
			this.form.tenantId = options.tenantId
			this.form.tenantName = options.tenantName
			this.form.assetId = options.assetId
			this.form.assetName = options.assetName
		},
		onReady() {
			this.$refs.form.setRules(this.rules);
		},
		methods: {
			//  文件上传成功回调
			uploadSuccess(res, index, lists, name) {
				this.form.fileList.push(res.data[0])
				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.form.fileList.splice(index, 1)
			},
			//  提交表单
			submitForm() {
				this.$refs.form.validate(valid => {
					if (valid) {
						NET.request(API.submitAdviseForm, {
							...this.form
						}, 'POST').then(res => {
							this.$refs.uTips.show({
								title: '提交成功',
								type: 'success',
							})
							setTimeout(() => {
								uni.navigateBack()
							}, 1000)
						}).catch(error => {
							this.$refs.uTips.show({
								title: error.message,
								type: 'warning',
							})
						})
					}
				});
			},
		},
	}
</script>

<style>
	page {
		width: 100%;
		height: 100%;
		position: relative;
	}
</style>
<style lang="scss" scoped>
	@import "@/static/css/themes.scss";

	.content {
		width: 100%;
		float: left;
		padding: 0 15px 60px 15px;
		box-sizing: border-box;
	}
</style>
