<template>
	<view class="content vflex jbetween">
		<view class="">
			<u-navbar title="意见反馈" @leftClick="leftClick" :autoBack="true" :placeholder="true" bgColor="#fff">
				<view class="nav_right hflex acenter" slot="right" @click="toHistory">
					<image src="/static/images/mine/history.png" mode="" style="width: 32rpx;height: 32rpx;"></image>
					<view class="nav_text">记录</view>
				</view>
			</u-navbar>
			<view class="box">
				<view class="top">
					<u-input v-model="title" maxlength="18" border="bottom" placeholder="请填写标题(最多18个字)"></u-input>
					<u-textarea v-model="problem" confirmType="done" placeholder="请填写具体描述" ></u-textarea>
				</view>
				
			</view>
			<view class="box">
				<view class="left">请提供问题截图或者照片（选题）</view>
				<view class="upload">
					<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" :maxCount="9" name="1" multiple :previewFullImage="true" width="200rpx" height="200rpx">
						<image src="/static/images/common/image_add.png" style="width: 204rpx; height: 204rpx	;"></image>
					</u-upload>
				</view>
			</view>
			<view class="box">
				<view class="hflex acenter">
					<view class="left">联系方式</view>
					<u-input v-model="phone" border="none" placeholder="请输入您的联系方式"></u-input>
				</view>
			</view>
			<view class="text_style1">您的反馈我们会尽快解决，但无法保证每一条都及时反馈，如果有紧急 咨询，请直接拨打客服电话：400-988-7788</view>
			<!-- <view class="history" @click="toHistory">反馈记录</view> -->
		</view>
		<view class="btn" @click="submit">提交</view>
	</view>
</template>

<script>
	import $api from '@/static/js/api.js'
	var that = ''
	export default {
		data() {
			return {
				title: '',
				problem: '',
				fileList1: [
				],
				phone: '',
				id: '',
			}
		},
		onLoad(options) {
			that = this
			if(options.id) {
				that.id = options.id
				that.getData(options.id)
			}
		},
		methods: {
			leftClick() {
				console.log('leftClick');
			},
			getData(id) {
				var pages = getCurrentPages()
				var prePage = pages[pages.length - 2]
				var list = prePage.$vm.pageList
				
				for(var i=0;i<list.length;i++) {
					if(list[i].id == id) {
						that.title = list[i].title
						that.problem = list[i].content
						if(list[i].image) {
							var imgs = list[i].image.split(',')
							let obj = {}
							for(let key in imgs) {
								obj[key] = imgs[key]
							}
							var fileList1 = Object.keys(obj).map(item => ({
								url: obj[item]
							}))
							that.fileList1 = fileList1
						} else {
							that.fileList1 = []
						}
						
						that.phone = list[i].phone
					}
				}
			},
			// 删除图片
			deletePic(event) {
				// that.userInfo.imgList.splice(event.index, 1)
				console.log(event)
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				uni.showLoading({
					title: '上传中',
					mask: true
				})
				console.log(`fileList${event.name}`);
				console.log(this[`fileList${event.name}`]);
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result.url
					}))
					fileListLen++
					if(lists.length - 1 == i) {
						uni.hideLoading()
					}
				}
			},
			// 上传图片
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: $api.config.baseUrl + '/data/api.auth.Center/upload',
						filePath: url,
						name: 'file',
						header: {
							'token': uni.getStorageSync('token')?uni.getStorageSync('token'):'',
							'api-name': 'iosapp'
						},
						formData: {
							user: 'test'
						},
						success: (res) => {
							console.log(res);
							setTimeout(() => {
								var data = JSON.parse(res.data)
								resolve(data.data)
							}, 1000)
						}
					});
				})
			},
			// 提交
			submit() {
				if($api.formCheck(that.problem,"requires") && $api.formCheck(that.phone,"phone")) {
					var imgs = ""
					if(that.fileList1.length > 0) {
						for(var i=0;i<that.fileList1.length;i++) {
							if(i == that.fileList1.length - 1) {
								imgs += that.fileList1[i].url
							} else {
								imgs += that.fileList1[i].url + ','
							}
						}
					}
					if(that.id == "") {
						$api.req({
							url: '/data/api.business.User/feedback',
							method: 'POST',
							data: {
								title: that.title,
								content	: that.problem,
								image: imgs,
								phone: that.phone,
							}
						}, function(res) {
							if(res.code == 1) {
								console.log(res);
								$api.info(res.info)
								$api.jump('/pages/tabbar/mine/mine',2)
							}
						})
					} else {
						$api.req({
							url: '/data/api.business.User/save_feedback',
							method: 'POST',
							data: {
								feedback_id: that.id,
								title: that.title,
								content	: that.problem,
								image: imgs,
								phone: that.phone,
							}
						}, function(res) {
							if(res.code == 1) {
								console.log(res);
								$api.info(res.info)
								$api.jump(-1)
							}
						})
					}
				}
			},
			toHistory() {
				$api.jump('/pages/mine/service/feed/history')
			}
		},
	}
</script>

<style lang="scss" scoped>
	.content::v-deep {
		background-color: #FFFFFF;
		padding: 20rpx 30rpx;
		.nav_text {
			font-size: 30rpx;
			font-weight: 400;
			color: #506DFF;
			line-height: 42rpx;
			padding-left: 4rpx;
		}
		.box {
			width: 100%;
			background-color: #F5F5F5;
			border-radius: 20rpx;
			box-sizing: border-box;
			padding: 32rpx 20rpx;
			margin-bottom: 20rpx;
			.left {
				font-size: 30rpx;
				color: #B2B2B2;
				margin-right: 20rpx;
			}
			.top {
				.u-input {
					padding: 0 0 28rpx !important;
				}
				.u-textarea {
					// margin: 28rpx auto 0;
					// background-color: #f4f4f4;
					// border-radius: 28rpx;
					box-sizing: border-box;
					padding: 28rpx 0;
					width: 610rpx;
					border: none;
					background-color: #F5F5F5;
				}
			}
			.upload {
				width: 100%;
				margin: 28rpx 0 24rpx;
			}
		}
		.text_style1 {
			// padding: 20rpx 0;
			font-size: 22rpx;
			font-weight: 400;
			color: #BABABA;
			line-height: 32rpx;
		}
		.history {
			width: 184rpx;
			height: 52rpx;
			border-radius: 26rpx;
			border: 1rpx solid #526FFF;
			margin: 34rpx auto 0;
			color: #506DFF;
			font-size: 26rpx;
			text-align: center;
			line-height: 52rpx;
		}
		.btn {
			width: 100%;
			height: 84rpx;
			background-color: #506dff;
			border-radius: 42rpx;
			font-size: 18px;
			color: #FFFFFF;
			text-align: center;
			line-height: 84rpx;
			margin-bottom: 80rpx;
		}
	}
</style>