<template>
	<view class="wrap">
		<uni-forms ref="form" :modelValue="formData" :rules="rules" :label-width="300" label-position="top">	
			<uni-forms-item label="问题描述" name="remark">
			  <uni-easyinput v-model="formData.snapshotContent" type="textarea" :maxlength="-1" autoHeight placeholder="备注" />
			</uni-forms-item>
<!-- 			<uni-forms-item label="处理人员" name="accountId" required >
			  <uni-data-select v-model="formData.accountId" :localdata="userList"></uni-data-select>
			</uni-forms-item>	 -->
			<uni-file-picker  v-model="formData.attachList" 
			 fileMediatype="image" 
			 title="附件"
			 limit="1"
			 @select="uploadSuccess"
			 @delete="deleteFile"></uni-file-picker>	
		</uni-forms>
		<button type="primary" @click="onSubmit" class="submit-BT">提交</button>		
	</view>
</template>

<script>
	import {upload} from '@/api/system/upload.js'	
	import {getUserList} from '@/api/system/user.js'
	import { createSnapshot } from '@/api/aqpt/snapshotApi'
	export default {
		data() {
			return {
				formData:{
					snapshotContent:"",
					attachList:[]
				},
				userList:[],
				rules:{},
				snapshot:{}
			}
		},
		onLoad() {
			this.init()
		},
		methods: {
			init(){
				this.getUserList()
			},
			async onSubmit() {
				let attachList=[]
				for(let i=0;i<this.formData.attachList.length;i++){
					let filePath=this.formData.attachList[i].url
					let fileresq=await upload({filePath})
					fileresq=JSON.parse(fileresq)
					attachList.push(fileresq.data)
				}
				let qrcode=uni.getStorageSync('qrcode')
				let qrcode_target={
					targetId:qrcode.targetId,
					targetType:qrcode.targetType,
					targetTitle:qrcode.targetTitle,
					targetGroupId:qrcode.groupId,
					targetGroupName:qrcode.groupName,
				}
				// let flow=this.userList.filter(item=>this.formData.accountId===item.accountId)[0]
				// this.handleSelectUser(flow)
				await createSnapshot({
					...this.formData,
					...qrcode_target,
					attachList:attachList
				}).then(()=>{
					uni.showToast({
						icon:'none',
						title:"提交成功！",
						complete() {
							uni.$emit('type',5)					
							uni.switchTab({
								url:'/pages/history/history?type=snapshot'
							})
						}
					})					
				})
				.catch(()=>{
					uni.showToast({
						icon:'none',
						title:"提交失败！"
					})				
				})
			},
			uploadSuccess(e){
				let attachList=JSON.parse(JSON.stringify(this.formData.attachList))
				attachList.push(e.tempFiles[0])
				this.formData.attachList=attachList
			},
			deleteFile(e){							
				let attachList=JSON.parse(JSON.stringify(this.formData.attachList))
				attachList.filter(item=>item.uuid!==e.tempFile.uuid)
				this.formData.attachList=attachList
			},
			handleSelectUser(item) {
			  this.formData.handleAccountId = item.accountId
			  this.formData.handleAccountName = item.accountName
			  this.formData.handlePositionId = item.positionId
			  this.formData.handlePositionName = item.positionName
			  this.formData.handleGroupId = item.groupId
			  this.formData.handleGroupName = item.groupName
			},
			getUserList(){
				getUserList().then((res)=>{
					var userList=[]
					for (var i = 0; i < res.data.length; i++) {
						userList.push({
								value: -i, 
								text: res.data[i].name,
								disable:true
						})
						for(let j = 0; j < res.data[i].children.length; j++){
							userList.push({
								...res.data[i].children[j],
								value: res.data[i].children[j].accountId, 
								text: res.data[i].children[j].accountName
							})							
						}
					}
					this.userList=userList
				})
			}
		},
		onUnload() {			
			uni.removeStorageSync('hazard')
		}
	}
</script>

<style lang="scss" scoped>
.wrap{
	padding: 32rpx;
	padding-bottom: 200rpx;
	.submit-BT {
		width: 750rpx;
		height:156rpx;
		line-height:88rpx;
		color: #4D73FF;
		text-align: center;
		font-size: 32rpx;
		padding-bottom: 68rpx;
		background-color: #fff;
		position: fixed;
		left: 0;
		bottom: 0;
		z-index: 99;
		box-shadow: 0px 0px 12px 0px #0000000A;
		border-radius: 8px 8px 0px 0px
	}
}
</style>
