<template>
	<view class="danger-form">
		<uni-collapse>
			<uni-collapse-item :title="baseInfo.dangerTitle">
				<view class="content">
					<uni-list-item title="隐患编码" :rightText="baseInfo.dangerCode" ></uni-list-item>					
					<uni-list-item title="隐患状态" :rightText="dangerStatus(baseInfo.status)" ></uni-list-item>
					<uni-list-item title="隐患类别" :rightText="baseInfo.dangerCatTitle" ></uni-list-item>
					<uni-list-item title="隐患等级" :rightText="baseInfo.dangerLevel===1?'一般':'重大'" ></uni-list-item>
					<uni-list-item title="所在位置" :rightText="baseInfo.dangerLocation" v-if="baseInfo.dangerLocation"></uni-list-item>
					<uni-list-item title="描述" :rightText="baseInfo.dangerDesc" ></uni-list-item>
					<uni-list-item title="所在部门" :rightText="baseInfo.curGroupName" v-if="baseInfo.curGroupName"></uni-list-item>
					<uni-list-item title="整改人员" :rightText="baseInfo.curAccountName"  v-if="baseInfo.curAccountName"></uni-list-item>
<!-- 					<uni-list-item title="所在部门" :rightText="baseInfo.submitGroupName" v-if="baseInfo.submitGroupName"></uni-list-item>
					<uni-list-item title="整改人员" :rightText="baseInfo.submitAccountName"  v-if="baseInfo.submitAccountName"></uni-list-item>
					<uni-list-item title="所在部门" :rightText="baseInfo.rectifyGroupName" v-if="baseInfo.rectifyGroupName"></uni-list-item>
					<uni-list-item title="整改人员" :rightText="baseInfo.rectifyAccountName"  v-if="baseInfo.rectifyAccountName"></uni-list-item> -->
					<uni-list-item title="整改措施" :rightText="baseInfo.rectifyMeasure" v-if="baseInfo.rectifyMeasure"></uni-list-item>
					<uni-list-item title="整改说明" :rightText="baseInfo.rectifyRemark" v-if="baseInfo.rectifyRemark"></uni-list-item>
				</view>
				<uni-section title="附件" type="line">
					<div class="attach-wrap">
						<image @click="showImage(attachindex)" class="attach" :src="attach.fileUrl" v-for="(attach,attachindex) in baseInfo.attachList" :key="attachindex"></image>						
					</div>
				</uni-section>
			</uni-collapse-item>
		</uni-collapse>
		<div class="form-container">
			<uni-forms ref="form" :modelValue="formData" :rules="rules" :label-width="300" label-position="top">
				<uni-forms-item label="处理动作" name="actionDefId" required v-if="actionDefList&&actionDefList.length>0">
					<div class="uni-data-checkbox-wrap">
						 <view class="action" v-for="(action,index) in actionDefList" :key="index" 
							  :class="formData.actionDefId===action.value?'active':''"
							  @click="changeAction(action)">
						 	<text>{{action.text}}</text>
						 </view>
					</div>
				</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.groupId" :localdata="groupList" @change="changeGroup" :clear="false"></uni-data-select>
				</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>
			<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" v-if="flowData.activityInsTitle">
			<text>{{flowData.activityInsTitle}}</text>
		</view> -->
		<view class="button-container" @click="onSubmit">
			<text>提交</text>
		</view>
	</view>
</template>

<script>
	import {getDangerById} from '@/api/aqpt/dangerApi.js'
	import {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'
	export default {
		data() {
			return {
				dangerId:undefined,
				baseInfo:{
					
				},
				actionDefList:[],
				userList:[],
				groupList:[],
				flowData:{},
				formData:{
					dangerDesc:"",
					accountId:"",
					actionDefId:"",
					groupId:"",
					attachList:[]
				},
				rules:{
					
				}
			}
		},
		onLoad(options) {
			this.init(options.id)
		},
		methods: {
			dangerStatus(i) {
			  if (i >= 0) {
			    const strs = ['提交','评审','整改','验收','已完成']
			    return strs[i]
			  } else {
			    return '已撤销'
			  }
			},
			init(dangerId){
				this.dangerId=dangerId
				getDangerById(dangerId).then((res)=>{
					this.baseInfo=res.data
				})
				this.getUserList()
				this.getGroupList()
				this.get(dangerId)
			},
			// 获取流程
			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.accountId=""
					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
				})				
			},
			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 dangerId=this.dangerId
				let action=this.actionDefList.filter(item=>item.actionDefId===this.formData.actionDefId)[0]
				let user=this.userList.filter(item=>this.formData.accountId===item.accountId)[0]
				let activityCode=this.formData.activityCode
				let res={
					...this.flowData,
					"formCode": activityCode,
					"dangerId": dangerId,
					[activityCode+"Remark"]: this.formData.dangerDesc
				}
				let workflowForm={				
					"wfDefId": 1,
					"wfInsId": dangerId,
					"activityDefId": this.formData.activityDefId,
					"activityInsId": this.formData.activityInsId,
					"activityCode": activityCode,
					"actionId": 1,
					"actionCode": action.actionCode,
					"actionInsId": this.formData.actionInsId,
					"actionDefId": action.actionDefId,					
					"actionRemark": this.formData.dangerDesc,
					"groupIdTo": user.groupId,
					"positionIdTo": user.positionId,
					"accountIdTo": user.accountId,
					"accountNameTo": user.accountName,
					"groupNameTo": user.groupName,
					"positionNameTo": user.positionName
				}
				this.handleSubmit({res,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 {data}=await handleWorkflow({...workflowForm,attachList}).catch(()=>{uni.showToast({icon:'none',title:"提交失败！"})})
				let status=this.getStatusByActivityCode(data.activityCode,data.status)
				let resq=await handleDanger({...res,attachList,status}).catch(()=>{uni.showToast({icon:'none',title:"提交失败！"})})
				uni.showToast({
					icon:'none',
					title:"提交成功！",
					complete() {
						uni.navigateBack()
					}
				})
			},	
			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>
