<template>
	<div class="body">		
		<uni-forms ref="form" label-position="top" :rules="rules" :model="formData">
			<uni-forms-item label="隐患标题" name="desc" required>
				<uni-easyinput v-model="formData.hdangerTitle" placeholder="请输入描述"></uni-easyinput>
			</uni-forms-item>	
			<uni-forms-item label="采空区" name="goafId" required>
				<uni-data-select
					v-model="formData.goafId"
					:localdata="goafList"
				></uni-data-select>
			</uni-forms-item>
			<uni-forms-item label="检查表" name="checklistId" required>
				<uni-data-select
					v-model="formData.checklistId"
					:localdata="checkList"
				></uni-data-select>
			</uni-forms-item>
			<uni-forms-item label="评审人" name="reviewAccountId" required>
				<uni-data-select
					v-model="formData.reviewAccountId"
					:localdata="userList"
				></uni-data-select>
			</uni-forms-item>
			<uni-forms-item label="隐患等级" name="hdangerLevel" required>
				<uni-data-checkbox v-model="formData.hdangerLevel" :localdata="hdangerLevels"></uni-data-checkbox>
			</uni-forms-item>							  
			<uni-forms-item label="发生时间" name="submitTime" required>
				<uni-datetime-picker
					type="datetime"
					v-model="formData.submitTime"
				/>
			</uni-forms-item>
			<uni-forms-item label="截止时间" name="dangerDeadline" required>
				<uni-datetime-picker
					type="datetime"
					v-model="formData.dangerDeadline"
				/>
			</uni-forms-item>
			<uni-forms-item label="描述" name="hdangerDesc" required>
				<uni-easyinput type="textarea" autoHeight v-model="formData.hdangerDesc" placeholder="请输入描述"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item label="附件">
				<div class="upload-container">
					<image @click="upload" class="upload" src="/static/icon/upload.png" mode="widthFix"></image>
					<p class="tip">注：单个附件上传不超过10M，附件累计不超过20M</p>					
				</div>
				<view class="preview" v-if="formData.attachList.length>0">
					<image class="preview-item" :src="attach.fileUrl" mode="widthFix" v-for="(attach,index) in formData.attachList" :key="index"></image>
				</view>
			</uni-forms-item>
		</uni-forms>	
		<view class="footer" @click="onSubmit">提交</view>
	</div>
</template>

<script>
	import {getUserByPage} from '@/api/user.js'
	import {upload} from '@/api/index.js'
	import { getChecklist } from '@/api/check'
	import { getGoafBaseInfo } from '@/api/goaf'
    import { startWorkflow, getWorkflowById, handleWorkflow } from '@/api/system/wfApi'
	import { handleDanger, getDangerById } from '@/api/dangerApi'
	import {parseTime} from '@/libs'
	export default {
		data() {
			return {
				formData:{
					'hdangerId': undefined,
					'formCode': 'submit',
					'hdangerTitle': '', // 隐患标题
					'dangerCatId': undefined, // 隐患类别ID
					'hdangerLevel': 1, // 隐患等级
					'hdangerDesc': '', // 描述
					'submitTime': parseTime(new Date()),
					'dangerDeadline': '', // 截止时间
					'goafId': undefined,
					'checklistId': '',
					"attachList":[]
				},
				flowData:{},
				userList:[],
				checkList:[],
				goafList:[],
				hdangerLevels:[
					{"value": 0,"text": "较低"	},
					{"value": 1,"text": "一般"},
					{"value": 2,"text": "较大"},
					{"value": 3,"text": "重大"},
				],
				rules:{
					reviewAccountId:{
						rules:[
							{
								required: true,
								errorMessage: '请选择评审人',
							},						
						],
					},
					desc:{
						rules:[
							{
								required: true,
								errorMessage: '请填写描述',
							},						
						]						
					}
				},
			}
		},
		created() {
			this.init()
		},
		onLoad() {
			let scanCode_info=uni.getStorageSync('scanCode-info')
			if(scanCode_info){
				console.log('扫码登记')
			}
		},
		methods: {
			// 启动流程
			async start(wfDefId) {
			  const { code, data, msg } = await startWorkflow(wfDefId)
			  if (code === 0) {
				this.flowData = data
				this.flowData.wfDefId = data.wfDefId
				this.flowData.wfInsId = data.wfInsId
				this.flowData.activityDefId = data.curActivityIns.activityDefId
				this.flowData.activityInsId = data.curActivityIns.activityInsId
				this.flowData.activityCode = data.curActivityIns.activityCode
				this.formData.hdangerId=data.wfInsId
				return data.wfInsId
			  }
			},
			// 获取流程
			get(wfInsId) {
			  this.resetFormData()
			  getWorkflowById(wfInsId).then((resp) => {
				const { code, data, msg } = resp
				if (code === 0) {
				  this.flowData = data
				  this.flowData.wfDefId = data.wfDefId
				  this.flowData.wfInsId = data.wfInsId
				  this.flowData.activityDefId = data.curActivityIns.activityDefId
				  this.flowData.activityInsId = data.curActivityIns.activityInsId
				  this.flowData.activityCode = data.curActivityIns.activityCode
				  this.renderAction()
				} else {
				  this.$message.error(msg)
				}
			  }).catch((error) => {
				console.log(error)
			  })
			},
			init(){
				let data=uni.getStorageSync('submit-data')
				if(data){
					this.formData.checklistId = data.checklistId
					this.formData.goafId = data.goafId
					this.formData.hdangerDesc = data.checkItemNopass
					this.formData.hdangerTitle = data.checkItemNopass		
				}
				this.getUser()
				this.getChecklist()
				this.getGoafBaseInfo()
				//初始化流程
				this.start(1)
			},
			getUser(){
				getUserByPage({
					page: 1,
					limit: 999999
				}).then((res)=>{
					this.userList=res.data.map(item=>{
						return{
							...item,
							value:item.accountId,
							text:item.accountName
						}
					})
				})
			},
			getChecklist(){
				getChecklist().then((res)=>{
					this.checkList=res.data.map(item=>{
						return{
							...item,
							value:item.checklistId,
							text:item.checklistTitle
						}
					})
				})
			},
			getGoafBaseInfo(){
				getGoafBaseInfo().then((res)=>{
					this.goafList=res.data.map(item=>{
						return{
							...item,
							value:item.goafId,
							text:item.goafName
						}
					})
				})				
			},
			async onSubmit(){
				let user=this.userList.filter(item=>item.accountId===this.formData.reviewAccountId)[0]
				if(user){
					this.formData.reviewAccountName = user.accountName
					this.formData.reviewGroupId = user.groupId
					this.formData.reviewGroupName = user.groupName
					this.formData.reviewPositionId = user.positionId
					this.formData.reviewPositionName = user.positionName	
					this.formData.ocId=user.ocId
				}
				if(!this.verify()){
					return
				}
				handleWorkflow({
				    "wfDefId": this.flowData.wfDefId,
				    "wfInsId": this.flowData.wfInsId,
				    "activityDefId": this.flowData.activityDefId,
				    "activityInsId": this.flowData.activityInsId,
				    "activityCode": this.flowData.activityCode,
				    "actionId": 1,
				    "actionCode": "submit",
				    "actionRemark": this.flowData.hdangerDesc,
					"attachList":this.formData.attachList,
					"groupIdTo": user.groupId,
					"positionIdTo": user.positionId,
					"accountIdTo": user.groupId,
					"groupNameTo": user.groupName,
					"positionNameTo": user.positionName,
					"accountNameTo": user.accountName,
				})
				//renshengkuduan !
				this.formData.status=1
				this.formData.submitTime=parseTime(this.formData.submitTime)
				this.formData.dangerDeadline=parseTime(this.formData.dangerDeadline)
				handleDanger(this.formData).then((resp) => {
					uni.switchTab({
						url:'/pages/risk/risk'
					})
				})
			},
			upload(){
				let self=this;
				uni.chooseImage({
					success:(files)=>{
						let filePath = files.tempFilePaths[0]
						console.log(self.formData.attachList)
						upload({
							filePath
						}).then((res)=>{
							let result=JSON.parse(res);
							let attachList=[result.data]
							self.$set(self.formData,'attachList',attachList)
						})
					}
				})
			},
			verify(){
				let items={
					'hdangerTitle': '隐患标题', // 
					"reviewAccountId":'评审人',
					'hdangerLevel': "隐患等级", // 隐患等级
					// 'hdangerDesc': '描述', // 
					'submitTime': "发生时间",
					'dangerDeadline': '截止时间', // 
					'goafId': "采空区",
					'checklistId': '检查表',
					// 'attachList':"附件"			
				}
				for(let key in items){
					if(this.formData[key]===""||this.formData[key]===undefined||this.formData[key]==="undefined"){
						uni.showToast({
							icon:'none',
							title:`请填写${items[key]}`
						})	
						return false;
					}
				}
				return true
			}
		}
	}
</script>
<style lang="scss" scoped>
.body{
	min-height: 100vh;
	box-sizing: border-box;
	padding:21rpx 16rpx;
	background-color: #F3F5FB;
	padding-bottom: 160rpx;
	.status-item{
		padding: 22rpx 32rpx;
		background-color: #fff;
		font-size: 28rpx;
		line-height: 1;
		display: inline-block;
		color: #434343;
		margin-right: 20rpx;
		border-radius: 2px;
		&.active{
			background: rgba(22, 141, 236, 0.16);
			color:#168DEC ;
		}
	}
	::v-deep .uni-forms-item__content{
		.uni-easyinput,.uni-select{
			background-color: #fff;
		}	
	}
	.upload-container{
		.upload{
			width: 216rpx;
			display: block;
		}
		.tip{
			font-size: 24rpx;
			line-height: 28rpx;
			color: #999999;
			padding-top: 20rpx;
		}
	}
	.footer{
		width: 100%;
		height: 136upx;
		background: #FFFFFF;
		border-radius: 16upx 16upx 0px 0px;
		position: fixed;
		left: 0;
		bottom: 0;
		text-align: center;
		color: #168DEC;
		font-size: 32upx;
		padding-top: 20upx;
		letter-spacing: 2px;
	}
	.preview{
		padding-top: 50rpx;
		.preview-item{
			width: 200rpx;
			height: 200rpx;
		}
	}
}
</style>
