<template>
	<div class="page-cont">
		<p class="page-title">请确认会签</p>
		<div class="page-select">
			<div v-if="isLastSign">
				<div class="select-item">
					<span>业务转出到</span>
					<view class="select-unit" @tap="choese(1)">
						<picker mode="selector" @change="pickerChange($event,0)"  :value="pickerIndex[0]" range-key="ActDef_Name" :range="NextActDefList">
							<view class="uni-ellipsis">{{NextActDefList[pickerIndex[0]].ActDef_Name}}</view>
						</picker>
						<i class="uni-icon uni-icon-arrowright"></i>
					</view>
					<!-- <span @tap="choese(1)">{{Actdef_Name}}<i class="uni-icon uni-icon-arrowright"></i></span> -->
				</div>
				<div class="select-item" v-show="!notUserData">
					<span>指定经办人</span>
					<view class="select-unit" @tap="choese(2)">
						<picker mode="selector" @change="pickerChange($event,1)"  :value="pickerIndex[1]" range-key="USER_NAME" :range="TransferUserList">
							<view class="uni-ellipsis">{{TransferUserList[pickerIndex[1]].USER_NAME}}</view>
						</picker>
						<i class="uni-icon uni-icon-arrowright"></i>
						
					</view>
					<!-- <span @tap="choese(2)">{{USER_NAME}}<i class="uni-icon uni-icon-arrowright"></i></span> -->
				</div>
			</div>
			<div class="text-area-box">
				<textarea value="" placeholder="请填写您的会签意见" v-model="postData.APPROVED_INFO" />
				</div>
			<p class="tip">注意：当您为最后一名会签人员时，您选择的业务和经办人才能即时生效。</p>
		</div>
	
		
		<button @click="postFn()" :loading="isSubmit" :disabled="isSubmit">确认</button>
		
	</div>
</template>

<script>
	import {mapState,mapMutations,mapActions} from 'vuex'
	export default {
		data() {
			
			return {
				postData: {
					PROINST_ID:'',
					NOWACTINST_ID:'',
					PROINST_NEXTID: 2000,
					NextACTDEF_IDS: '',
					APPROVED_INFO: '', // 审核意见
					ACTDEF_ID: '',
					businessType: '' // 1000,合同审批; 2000:商品审批; 3000:财务审批
				},
	
	
				pickerIndex: [0,0],
				isLastSign: false,
				isSubmit:false,
				notUserData: false ,//  下一转出环节为 办结时，不需要选择转出人员
				TransferUserList: [] ,// 待移交人员列表 
				NextActDefList: [] ,// 可选环节列表
				
				// nowType: 1 ,// 1: 业务 2：转出人员 
			};
		},
		computed:{
			...mapState({
				'userId': state => state.userData.UserId
				
			})
			
		},
		methods: {
			...mapMutations(['shouldReLoadingList']),
			...mapActions(['getTodoList']),
			getIsSign(){
				let _this = this
				_this.$request.$get('GetSignInfo',{
				
				  HIGHWAYPROINST_ID: _this.postData.PROINST_ID,
				  ACTDEF_ID: _this.postData.ACTDEF_ID,
				  USER_ID: _this.userId
				}).then(res =>{
					if(res.Data.JointlySignList[0].SelectNextActDef === 1){
					  _this.isLastSign = true
					}
					if(res.Data.ApprovedList){
					  if(res.Data.ApprovedList[0].JointlySignType === 1){
						_this.postData.APPROVED_INFO = res.Data.ApprovedList[0].APPROVED_INFO
					  }
					}
				})
			},
		
			pickerChange(e,index){
			
				this.pickerIndex[index] = e.detail.value
				this.$forceUpdate()
			},
			choese (type) { // 1: 业务 2：转出人员 3.会签
				let noList = false
				// this.nowType = type
				if (type!==1 && !this.NextActDefList[this.pickerIndex[0]].ActDef_ID) {
					uni.showToast({
						title: '请先选择转出业务后再指定经办人。',
						icon: 'none'
					})
					return false
				}
				switch (type) {
					case 1:
						if(this.NextActDefList.length===0){
							noList = true
						}
						
						break
					default:
						if(this.TransferUserList.length===0){
							noList = true
						}
						break
				}
				
				
				if (noList) {
					uni.showToast({
						title: '该选项没有待选项，请稍后重试或待管理人员添加待选项后重试。',
						icon: 'none'
					})
					return false
				}
		
			},
			getHighNextActDef() { // 获取下一环节
				let _this = this
				this.$request.$get('GetNextStep',{
					
					businessType: _this.postData.businessType,
					PROINST_ID: _this.postData.PROINST_ID,
					PROINST_NEXTID: _this.postData.PROINST_NEXTID
				}).then((res)=> {

					let _data = res.Data.List
					
				
					if (_data[0].ActDef_Type && _data[0].ActDef_Type===5010) { // 是否是办结
						_this.notUserData = true
					}
					_this.NextActDefList= _data
					_this.getTransferUser()
				})
			},
			getTransferUser() { // 是否是请求移交人员
				let _this = this
				let _data = {
				
					businessType: _this.postData.businessType,
					PROINST_ID: _this.postData.PROINST_ID,
					NextStepId: _this.NextActDefList[_this.pickerIndex[0]].ActDef_ID,
					USER_ID: _this.userId
				}
				uni.showLoading()
				this.$request.$get("GetNextStepUser",_data).then((res)=> {
					_this.TransferUserList= res.Data.List
					uni.hideLoading()
				})
			},
	
			postFn () {
				let _this = this
				if(_this.isLastSign){
					if(!_this.NextActDefList[_this.pickerIndex[0]]) {
						uni.showToast({
							title: '请选择转出业务后提交。',
							icon: 'none'
						})
						return false
					}
					if(!_this.TransferUserList[_this.pickerIndex[1]]) {
						uni.showToast({
							title: '请选择人员后提交。',
							icon: 'none'
						})
						return false
					}
				}
				if( !_this.postData.APPROVED_INFO) {
					uni.showToast({
						title: '请填写会签意见后再提交。',
						icon: 'none'
					})
					return false
				}
				let arr = {
					
					
					businessType: _this.postData.businessType,
					PROINST_ID: _this.postData.PROINST_ID,
					PROINST_NEXTID: _this.postData.PROINST_NEXTID,
					NOWACTINST_IDS: _this.postData.NOWACTINST_ID,
					NextACTDEF_IDS: _this.NextActDefList[_this.pickerIndex[0]].ActDef_ID,
					NextSTAFF_ID: _this.TransferUserList[_this.pickerIndex[1]].USER_ID || "",
					APPROVED_INFO: _this.postData.APPROVED_INFO,
					APPROVED_NAME: _this.postData.ApproName,
				}
				
				if (_this.notUserData) {
					delete arr.NextSTAFF_ID
					
				}
				_this.isSubmit = true
				this.$request.$post('ProcessApproval',arr).then((_data)=> {
					_this.isSubmit = false
					if (_data.ResultCode==='100') {
						uni.$emit(_this.postData.businessType,true);
						_this.getTodoList(); // 更新全局待办事项
						_this.shouldReLoadingList(true);
						uni.showToast({
							title: _data.ResultDesc,
						
							success () {
								setTimeout(()=> {
									uni.navigateBack({ delta: 1})
								},200)
								
							}
						})
					}else {
						uni.showToast({
							title: _data.ResultDesc || '操作错误',
							icon: 'none'
						})
						
					}
				})
			},
		},
		onUnload() {
			this.$util.addUserBehavior()
		},
		onLoad(option) {
			this.postData = option
			this.postData.PROINST_NEXTID= 2000
			this.postData.APPROVED_INFO= '' // 审核意见
			this.getIsSign()
			// this.getTransferUser()
			this.getHighNextActDef()
		}
	}
</script>

<style scoped>
	@import url("../../common/css/auditingPop.css");
	@import url("../../common/css/picker.css");
	.text-area-box {
		background-color: #fff;
	}
	textarea {
		height: 200upx;
	}
	.tip {
		font-size: 20upx;
		color: #F17E5F;
	}
</style>
