<template>
	<view class="warning-handle-page">
	  <uni-forms ref="form" :modelValue="formData" :rules="rules" :label-width="300" label-position="top">
		<uni-forms-item label="处理动作"  required >
			<div class="uni-data-checkbox-wrap">
				 <view class="action":class="action===1?'active':''" @click="changeAction(1)">  
					<text>完成处理</text>
				 </view>
				 <view class="action":class="action===2?'active':''" @click="changeAction(2)">  
					<text>转交</text>
				 </view>
			</div>
		</uni-forms-item>
		<uni-forms-item v-if="handleRemarkVisible" label="原因分析" prop="actionRemark">
		  <uni-easyinput v-model="formData.warnReason" type="textarea" :maxlength="-1" autoHeight placeholder="原因分析" />
		</uni-forms-item>

		<uni-forms-item v-if="handleRemarkVisible" label="处理措施" prop="actionRemark">
		  <uni-easyinput v-model="formData.handleMeasure" type="textarea" :maxlength="-1" autoHeight placeholder="处理说明" />
		</uni-forms-item>

		<uni-forms-item v-if="handlerVisible" label="执行人" prop="accountIdTo">
		  <uni-data-select v-model="formData.accountId" :localdata="userList"></uni-data-select>
		</uni-forms-item>

		<uni-forms-item v-if="handlerVisible" label="备注" prop="remark">
			<uni-easyinput v-model="transferData.remark" type="textarea" :maxlength="-1" autoHeight placeholder="请输入备注" />
		</uni-forms-item>
		<uni-file-picker  v-model="formData.attachList"
			fileMediatype="image" 
			title="请上传附件"
			limit="1"
			@select="uploadSuccess"
			@delete="deleteFile">
		 </uni-file-picker>	
		<view class="btn-group">
		  <uni-button type="primary" @click="handleSubmit">提交</uni-button>
		</view>
	  </uni-forms>		
	</view>
</template>

<script>
	import {upload} from '@/api/system/upload.js'		
	import {getUserList} from '@/api/system/user.js'
	import { getWarningById, completeWarning, transferWarning } from '@/api/aqpt/warningApi'
	export default {
		data() {
			return {
				formData:{
					warnId: undefined,
					attachList: []					
				},
				transferData:{
					
				},
				viewData:{},
				rules:{},
				userList:[],
				action:1,
				warnId:undefined,
				handleRemarkVisible:false,
				handlerVisible:false
			}
		},
		created() {
			this.changeAction(1)
			this.getUserList()
		},
		onLoad({id}) {
			this.warnId=id	
			this.formData.warnId = id			
			this.getData()
		},
		methods: {
			// Fetch Data
			getData() {
			  const warnId = this.warnId
			  getWarningById(warnId).then((resp) => {
				const { data } = resp
				this.viewData = data
			  })
			},			
			changeAction(iMode){
			  this.action=iMode;
			  if (iMode === 1) {
				this.handleRemarkVisible = true
				this.handlerVisible = false
			  } else if (iMode === 2) {
				this.handleRemarkVisible = false
				this.handlerVisible = true
			  }				
			},
			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
				})
			},	
			// 提交
			async handleSubmit() {
				  const action = this.action
				  if (action === 1) {
					  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)
					  }
					this.formData.attachList=attachList
					await completeWarning(this.formData).then((resp) => {
					  const { msg } = resp
					  uni.showToast({
						icon:"none",
						title:msg||'处理成功！'
					  })
					  setTimeout(()=>{
					  	uni.navigateBack()  
					  },1000)
					})
				  } else {
					let user=this.userList.filter(item=>this.formData.accountId===item.accountId)[0]
					this.handleSelectUser(user)
					transferWarning(this.warnId, this.transferData).then((resp) => {
					  const { msg } = resp
					  uni.showToast({
						icon:"none",
						title:msg||'处理成功！'
					  })
					  setTimeout(()=>{
						uni.navigateBack()  
					  },1000)
					})
				}
			},
			handleSelectUser(obj) {
			  this.transferData.accountIdTo = obj.accountId
			  this.transferData.groupIdTo = obj.groupId
			  this.transferData.positionIdTo = obj.positionId
			  this.transferData.accountNameTo = obj.accountName
			  this.transferData.groupNameTo = obj.groupName
			  this.transferData.positionNameTo = obj.positionName
			},	
			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
			},	
		}
	}
</script>

<style lang="scss" scoped>
.warning-handle-page{
	padding: 32rpx;
	::v-deep .uni-forms-item{
		.uni-forms-item__content{
			.uni-data-checkbox-wrap{
				height: 100%;
				display: flex;
				align-items: center;
				.action{
					padding: 22rpx 32rpx;
					background-color: #fff;
					border-radius: 2px;
					margin-right: 20rpx;
					font-size: 28rpx;
					line-height: 1;
					&.active{
						background: rgba(77, 115, 255, 0.16);
						color: #4D73FF;
					}
				}
			}
	
		}		
	}
	.btn-group{
		margin-top: 100rpx;
	}
}
</style>
