<template>
	<view class="danger-form">
		<div class="form-container">
			<uni-forms ref="form" :modelValue="formData" :rules="rules" :label-width="300" label-position="top">
				<uni-forms-item label="隐患标题" name="dangerTitle" required>
					<uni-easyinput type="text" v-model="formData.dangerTitle" placeholder="请输入姓名" />
				</uni-forms-item>
				<uni-forms-item label="所属部门" name="groupId" required>
				  <uni-data-select v-model="formData.groupId" :localdata="groupList" :clear="false"></uni-data-select>
				</uni-forms-item>	
				<uni-forms-item label="发生位置" name="riskPointId" required>
				  <uni-data-select v-model="formData.riskPointId" :localdata="riskList" :clear="false"></uni-data-select>
				</uni-forms-item>	
				<uni-forms-item label="隐患类别" name="dangerCatId" required>
				  <uni-data-select v-model="formData.dangerCatId" :localdata="dangerCats" :clear="false"></uni-data-select>
				</uni-forms-item>											
				<uni-forms-item label="隐患来源" name="dangerSource" required>
				  <uni-data-select v-model="formData.dangerSource" :localdata="dangerSources" :clear="false"></uni-data-select>
				</uni-forms-item>	
				<uni-forms-item label="隐患等级" name="dangerLevel" required>
				  <uni-data-select v-model="formData.dangerLevel" :localdata="dangerLevels" :clear="false"></uni-data-select>
				</uni-forms-item>	
				<uni-forms-item label="整改截止时间" name="dangerDeadLine" required>		 
					<uni-datetime-picker type="date" :clear-icon="false" v-model="formData.dangerDeadLine" />			
				</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-forms-item label="执行部门" name="groupId" required>
				  <uni-data-select v-model="formData.handgroupId" :localdata="groupList" @change="changeGroup" :clear="false"></uni-data-select>
				</uni-forms-item>	
				<uni-forms-item label="执行人" name="accountIdTo" required>
				  <uni-data-select v-model="formData.accountIdTo" :localdata="userList"></uni-data-select>
				</uni-forms-item>	
			</uni-forms>
			<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>
		</div>
		<view class="button-container" @click="onSubmit">
			<text>提交</text>
		</view>
	</view>
</template>

<script>
	import {getDangerById} from '@/api/aqpt/dangerApi.js'
	import {startWorkflow,getWorkflowById,handleWorkflow} from '@/api/system/wfApi.js'
	import {getUserList,selectUserByGroupId} from '@/api/system/user.js'	
	import {getGroupByList} from '@/api/system/groupApi.js'
	import { handleDanger } from '@/api/system/dangerApi.js'
	import {upload} from '@/api/system/upload.js'
	import { getRiskPointByList } from '@/api/aqpt/riskPointApi.js'
	import { getDangerCatByList } from '@/api/aqpt/dangerCatApi.js'
	export default {
		data() {
			return {
				dangerId:undefined,
				baseInfo:{
					
				},
				actionDefList:[],
				userList:[],
				groupList:[],
				riskList:[],
				dangerLevels:[
					{text:"重大隐患",value:1},
					{text:"较大隐患",value:2},
					{text:"严重隐患",value:3},
					{text:"一般隐患",value:4},
				],
				dangerSources:[
					{text:"自查",value:0},
					{text:"内部反馈",value:1},
					{text:"上级检查",value:2},
					{text:"政府执法",value:3},
				],
				dangerCats:[],
				flowData:{},
				formData:{
					dangerTitle:"",
					riskPointId:undefined,
					dangerCatId:undefined,
					dangerSource:undefined,
					dangerLevel:undefined,
					dangerDesc:"",
					accountIdTo:"",
					actionDefId:"",
					groupId:"",
					attachList:[]
				},
				rules:{
					
				}
			}
		},
		onLoad() {
			this.init()
		},
		methods: {
			dangerStatus(i) {
			  if (i >= 0) {
			    const strs = ['提交','评审','整改','验收','已完成']
			    return strs[i]
			  } else {
			    return '已撤销'
			  }
			},
			init(){
				this.getUserList()
				this.getGroupList()
				this.start()
				this.getRiskPoints()
				this.getDangerCats()
			},
			start(){
				startWorkflow(1).then((res)=>{
					this.flowData=res.data
					this.dangerId=res.data.wfInsId
				})
			},
			// 获取流程
			get(wfInsId) {
				getWorkflowById(wfInsId).then((resp) => {
				  const { data } = resp
				  this.flowData = data
				  this.formData.wfDefId = data.wfDefId
				  this.formData.wfInsId = data.wfInsId
				  this.formData.activityDefId = data.activityDefId
				  this.formData.activityInsId = data.activityInsId
				  this.formData.activityCode = data.activityCode
				  this.formData.actionInsId = data.actionInsId
				  if(data.wfInsTitle){
					  uni.setNavigationBarTitle({
						  title:`${data.activityInsTitle}流程`
					  })
				  }
				  this.actionDefList=this.flowData.actionDefList.map(item=>{
					  return{
						  ...item,
						  text:item.actionTitle,
						  value:item.actionDefId
					  }
				  })
				  if(this.actionDefList.length>0){
					  this.formData.actionDefId=this.actionDefList[0].actionDefId
				  }				  
				}).catch(()=>{
					uni.showToast({
						icon:"none",
						title:"流程初始化失败"
					})
				})
			},
			changeGroup(groupId){
				if(!groupId){
					this.getUserList()
					return
				}
				selectUserByGroupId(groupId).then((res)=>{
					var userList=[]
					this.formData.accountIdTo=""
					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
				})				
			},
			getDangerCats(){
				getDangerCatByList().then((res)=>{
					this.dangerCats=res.data.map((item)=>{
						return{
							value:item.dangerCatId, 
							text: item.dangerCatTitle,							
						}
					})
				}) 				
			},
			getRiskPoints(){
				getRiskPointByList().then((res)=>{
					this.riskList=res.data.map((item)=>{
						return{
							value:item.riskPointId, 
							text: item.riskPointTitle,							
						}
					})
				})					
			},
			getGroupList(){
				getGroupByList().then((res)=>{
					this.groupList=res.data.map((item)=>{
						return{
							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
				})
			},
			showImage(index){
				uni.previewImage({
					urls:this.baseInfo.attachList.map(item=>item.fileUrl),
					current:index
				})
			},
			onSubmit(){
				let user=this.userList.filter(item=>this.formData.accountId===item.accountId)[0]
				const arr = this.flowData.actionDefList
				const action = arr[0]
				this.handleUserSelect()
				let workflowForm={				
					"wfDefId": this.flowData.wfDefId,
					"wfInsId": this.flowData.wfInsId,
					"activityDefId": this.flowData.activityDefId,
					"activityInsId":this.flowData.activityInsId,
					"activityCode": "submit",
					"actionId": 1,
					"actionCode":  action.actionCode,
					"actionRemark": this.flowData.dangerDesc,
					"attachList": this.formData.attachList,
					"actionInsId": this.flowData.actionInsId,
					"actionDefId": action.actionDefId,
					"groupIdTo": this.formData.groupIdTo,
					"positionIdTo": this.formData.positionIdTo,
					"accountIdTo": this.formData.accountIdTo,
					"accountNameTo": this.formData.accountNameTo,
					"groupNameTo": this.formData.groupNameTo,
					"positionNameTo": this.formData.positionNameTo
				}
				this.handleSubmit(workflowForm)
			},
			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 scenePhoto= attach[0].fileUrl
				let sceneIcon= attach[0].fileIcon
				let {data}=await handleWorkflow(workflowForm).catch(()=>{uni.showToast({icon:'none',title:"提交失败！"})})
				let dangerLocation=this.riskList.find(item=>item.riskPointId===this.formData.riskPointId)[0].riskPointTitle;
				let resq=await handleDanger({
					"formCode": "submit",
					"dangerId": this.flowData.wfInsId,
					"scenePhoto":scenePhoto,
					"sceneIcon": sceneIcon,
					"groupId": this.formData.groupId,
					"riskPointId": this.formData.riskPointId,
					"dangerCode": this.formData.dangerCode,
					"dangerTitle": this.formData.dangerTitle,
					"dangerCatId": this.formData.dangerCatId,
					"dangerLevel": this.formData.dangerLevel,
					"dangerDesc": this.formData.dangerDesc,
					"dangerLocation":dangerLocation,
					"dangerDeadLine": this.formData.dangerDeadLine,
					"dangerSource": this.formData.dangerSource,
					"submitRemark": this.flowData.dangerDesc,
					"curGroupId": this.formData.groupIdTo,
					"curGroupName": this.formData.groupNameTo,
					"curPositionId": this.formData.positionIdTo,
					"curPositionName": this.formData.positionNameTo,
					"curAccountId": this.formData.accountIdTo,
					"curAccountName": this.formData.accountIdTo,
					"status": 1,
					"attachList": []					
				}).catch(()=>{uni.showToast({icon:'none',title:"提交失败！"})})
				uni.showToast({
					icon:'none',
					title:"提交成功！",
					complete() {
						uni.redirectTo({
							url:'/pages/index/detail/detail?type=danger&name=隐患管理'
						})
					}
				})
			},	
			// 选择执行人员
			handleUserSelect() {
				const accountId=this.formData.accountIdTo
				const self=this;
				this.userList.forEach((obj) => {
					if (obj.accountId === accountId) {
						self.form.groupIdTo= obj.groupId
						self.form.positionIdTo= obj.positionId
						self.form.accountIdTo= obj.accountId
	
						self.form.groupNameTo= obj.groupName,
						self.form.positionNameTo=obj.positionName
						self.form.accountNameTo= obj.accountName
					}
				})
			}, 
			getStatusByActivityCode(activityCode, wfStatus) {
			  let iRet = 0
			  if (wfStatus === 0) {
				if (activityCode === 'submit') {
				  iRet = 0
				} else if (activityCode === 'review') {
				  iRet = 1
				} else if (activityCode === 'rectify') {
				  iRet = 2
				} else if (activityCode === 'accept') {
				  iRet = 3
				}
			  } else if (wfStatus === 1) {
				iRet = 4
			  } else {
				iRet = -1
			  }
			  return iRet
			},
			changeAction(item){
				this.formData.actionDefId=item.value
			},
			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-form{
		padding: 20rpx 20rpx 160rpx 20rpx;
		margin-bottom: 120rpx;
		 .attach-wrap{
			display: flex;
			flex-wrap: wrap;
			.attach{
				display: block;
				width: 100rpx;
				height: 100rpx;
				margin: 10rpx;
				border: 1px solid #ccc;
			}
		}
		.form-container{
			padding: 10rpx;
			// box-shadow: 0px 0px 3px 1px rgba(0, 0, 0, 0.08);
			margin-top: 20rpx;
		}
		.submit_bt{
			width: 600rpx;
			height: 72rpx;
			line-height: 72rpx;
			box-sizing: border-box;
			border-radius: 16upx;
			margin-top: 50upx;
			background-color:#3384FF;
			font-size: 32rpx;
			color: #fff;
		}
		.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
		}
		::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;
						}
					}
				}

			}		
		}
	}

</style>
