<template>
	<view class="danger-submit-wrap">
		<uni-forms ref="form" :modelValue="formData" :rules="rules" :label-width="300" label-position="top">
			    <uni-forms-item label="隐患标题" name="dangerTitle" required>
			      <uni-easyinput v-model="formData.dangerTitle" />
			    </uni-forms-item>			
<!-- 			    <uni-forms-item label="隐患编码" name="dangerCode" required>
			      <uni-easyinput v-model="formData.dangerCode" />
			    </uni-forms-item>	 -->		
			    <uni-forms-item label="所在部门" name="groupId" required>
<!-- 					<uni-data-picker :localdata="groupList" 
					v-model="formData.groupId" 
					placeholder="请选择组织"
					popup-title="请选择组织" 
					@nodeclick="onNodeclickGroup"
					@change="onChangeGroup"
					:map="{text:'groupName',value:'groupId'}"></uni-data-picker> -->
					<uni-data-select v-model="formData.groupId" :localdata="groupList"></uni-data-select>
			    </uni-forms-item>			
<!-- 			    <uni-forms-item label="所在位置" name="riskPointId" required>
				  <p>{{formData.dangerLocation}}</p>
			    </uni-forms-item>	 -->		
			    <uni-forms-item label="隐患类别" name="dangerCatId" required>
				  <uni-data-select v-model="formData.dangerCatId" :localdata="dangerCatRange"></uni-data-select>
			    </uni-forms-item>			
			    <uni-forms-item label="隐患来源" name="dangerSource" required>
					<uni-data-select v-model="formData.dangerSource" :localdata="dangerSourceRange"></uni-data-select>
			    </uni-forms-item>			
			    <uni-forms-item label="隐患等级" name="dangerLevel" required>
					<uni-data-checkbox v-model="formData.dangerLevel" :localdata="dangerLevelRange" />
			    </uni-forms-item>			
			    <uni-forms-item label="整改截止时间" name="dangerDeadLine" required>
					<uni-datetime-picker type="datetime" return-type="string" v-model="formData.dangerDeadLine"/>
			    </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-forms-item label="描述" name="dangerDesc">
			      <uni-easyinput v-model="formData.dangerDesc" type="textarea" :maxlength="-1" autoHeight placeholder="隐患描述" />
			    </uni-forms-item>
				<uni-section title="上传附件" type="line">
					<view class="example-body">
						<uni-file-picker  v-model="formData.attachList" 
						 fileMediatype="image" 
						 title="请上传附件"
						 limit="1"
						 @select="uploadSuccess"
						 @delete="deleteFile"></uni-file-picker>
					</view>
				</uni-section>
		</uni-forms>
		<view class="button-container" @click="onSubmit">提交</view>
	</view>
</template>

<script>
	import {getUserList} from '@/api/system/user.js'
	import {getGroupView,getGroupByList} from '@/api/system/groupApi.js'
	import {getDangerCatByList} from '@/api/danger.js'
	import {getRiskPointByList} from '@/api/riskPiont.js'
	import { startWorkflow, handleWorkflow } from '@/api/system/wfApi'
	import { handleDanger } from '@/api/system/dangerApi.js'
	import {upload} from '@/api/system/upload.js'
	export default {
		data() {
			return {
				dangerSourceRange:[
				  { value: 0, text: "自查" },
				  { value: 1, text: "内部反馈" },
				  { value: 2, text: "上级检查" },	
				  { value: 3, text: "政府执法" },
				],
				dangerLevelRange:[
				  { value: 1, text: "一般隐患" },
				  { value: 2, text: "重大隐患" }
				],
				groupList:[],
				riskPointRange:[],
				dangerCatRange:[],
				userList:[],
				formData:{
					formCode: 'submit',
					dangerId: undefined,
					dangerCode: undefined,
					dangerTitle: '',
					dangerSource: 0,
					dangerDesc: '',
					dangerLevel: 1,
					dangerCatId: undefined,
					dangerCatTitle: '',
					riskPointId: undefined,
					dangerLocation: '',		
					attachList:[]
				},
				rules: {
					dangerTitle: {
						rules:[
							{required: true,errorMessage: '请填写姓名',},
						]
					},
					// dangerCode: {rules:[{required: true,errorMessage: '请填写隐患编码'}]},
					dangerDeadLine: {rules:[{required: true,errorMessage: '请填写整改截止时间'}]}										
				},
				flowData:{}
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		created() {
			this.init()
		},
		methods: {
			init(){
				this.getDangerCat()
				this.getRiskPoint()
				this.getUserList()
				this.getGroupRange()
				this.start(1)
			},
			// 启动流程
			async start(wfDefId) {
			  const { data } = await startWorkflow(wfDefId)
			  this.flowData = data
			},
			getDangerCat(){
				getDangerCatByList().then((res)=>{
					this.dangerCatRange=res.data.map(item=>{
						return{
							value: item.dangerCatId, 
							text: item.dangerCatTitle
						}
					})
				})
			},
			getRiskPoint(){
				let app=uni.getStorageSync('qrcode');
				this.formData.dangerLocation=app.riskPointTitle
				this.formData.riskPointId=app.riskPointId
			},
			getGroupRange(){
				getGroupByList().then((res)=>{
					this.groupList=res.data.map(item=>{
						return{
							...item,
							value: item.groupId, 
							text: 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
				})
			},
			onNodeclickGroup(node){
				// console.log({node})	
			},
			onChangeGroup({detail}){
				// console.log(detail)
			},
			onSubmit(){
				this.$refs.form.validate().then(res=>{
					let flow=this.userList.filter(item=>this.formData.accountId===item.accountId)[0]
					let group=this.groupList.filter(item=>this.formData.groupId===item.groupId)[0]
					res.curGroupId = res.groupId
					res.curGroupName = group.groupName
					res.curPositionId = flow.positionId
					res.curPositionName = flow.positionName
					res.curAccountId = flow.accountId
					res.curAccountName = flow.accountName
					res.formCode= 'submit'
					res.dangerId=this.flowData.wfInsId
					res.dangerLocation=this.formData.dangerLocation
					res.riskPointId=this.formData.riskPointId
					res.submitRemark=this.formData.dangerDesc	
					/*start*/
					let workflowForm={
					    "actionId": 1,						
						"wfDefId":this.flowData.wfDefId,
						"wfInsId":this.flowData.wfInsId,
						"activityDefId":this.flowData.activityDefId,
						"activityInsId":this.flowData.activityInsId,
						"activityCode":this.flowData.activityCode,
						"actionInsId":this.flowData.actionInsId,                    
						"actionDefId":this.flowData.actionDefList[0].actionDefId,
						"actionCode":this.flowData.actionDefList[0].actionCode,  	
						"groupIdTo": res.groupId||flow.groupId,
						"accountIdTo": flow.accountId,						
						"accountNameTo": flow.accountName,
						"groupNameTo": group.groupName||flow.groupName,
						"positionIdTo": flow.positionId,						
						"positionNameTo": flow.positionName,
						"actionRemark":this.formData.dangerDesc									
					}
					this.handleSubmit({res,workflowForm})					
				}).catch(err =>{
					console.log('表单错误信息：', err);
				})				
			},
			async handleSubmit({res,workflowForm}) {
				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 data=await handleWorkflow({...workflowForm,attachList})
				let resq=await handleDanger({...res,...qrcode_target,attachList,status:1,riskPointId:qrcode.targetId})
				uni.showToast({
					icon:'none',
					title:"提交成功！",
					complete() {
						uni.$emit('type',2)						
						uni.switchTab({
							url:'/pages/history/history?type=dangersubmit'
						})
					}
				})
			},
			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>
.danger-submit-wrap{
	padding: 20rpx 20rpx 200rpx 20rpx;
	.button-container{
		width: 750rpx;
		height:88rpx;
		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: 2;
		box-shadow: 0px 0px 12px 0px #0000000A;
		border-radius: 8px 8px 0px 0px
	}
}
</style>
