<template>
	<view class="page">
		<view class="top_container item_line" style="padding-right: 30upx;clear: both;overflow: auto; ">
			<view class="report_status">
				<text class="magtext">审批结果</text>
			</view>
			<view> 
				<view class="btn" :class="[result_btn==1?'btn_':'']" @click="result_btn=1">同意</view>
				<view class="btn" :class="[result_btn==0?'btn_':'']" @click="result_btn=0">拒绝</view>
				<view class="btn" :class="[result_btn==2?'btn_':'']" @click="result_btn=2">回退</view>
				<view class="btn" :class="[result_btn==3?'btn_':'']" @click="selectOperater">{{userName}}</view>
			</view>
		</view>
		<view class="item item_line item_textarea" v-if="result_btn==1">
			<view class="itemname redMi">审批意见</view>
			<textarea class="textarea" placeholder="请填写审批意见" v-model="reduceReason"></textarea>
		</view>
		<view class="item item_line item_textarea" v-if="result_btn==0">
			<view class="itemname redMi">拒绝原因</view>
			<textarea class="textarea" placeholder="请详细描述拒绝原因" v-model="reduceReason"></textarea>
		</view>
		<view class="item item_line item_textarea" v-if="result_btn==2">
			<view class="itemname redMi">回退原因</view>
			<textarea class="textarea" placeholder="请详细描述回退原因" v-model="reduceReason"></textarea>
		</view>
		<view class="fixMargin"></view>
		<view class="goHome" @click='goMain("workInfo")'>
			<img src="../../static/img/gohome.png">
		</view>
		<view class="fixBottom">
			<view class="uni-flex uni-row">
				<view style="-webkit-flex:1;flex: 1;">
					<button type="primary"  style="width: 100%;"  @click="submit()">确认提交</button>
				</view>
			</view>
		</view>
		<view class="mysearchlist" style="z-index: 10000;">
			<chooseUserSingle ref="mysearchlist" 
				:showsearch.sync="show_search_user"
				:itemInfo_.sync="search_value_user">
			</chooseUserSingle>
		</view>
	</view>
</template>

<script>
	import {
		config
	} from '../../request/js/config.js'
	import chooseUserSingle from '@/components/xinsoft-search-list/chooseUserSingle.vue';
	export default {
		components:{
			chooseUserSingle
		},
		data() {
			return {
				config:config,
				id:'',
				userId:'',
				taskId:'',
				instanceId:'',
				type:'',
				reduceReason:'同意',
				result_btn:1,
				show_search_user:false,
				userName:'转派',
				search_value_user:"",
			}
		},
		onLoad(option) {
			var _this = this;
			this.id = option.id||'';
			this.taskId = option.taskId||'';
			this.type = option.type||'';
			this.instanceId = option.instanceId||'';
			if (this.id == undefined ) {
				uni.reLaunch({
					url: 'advertiseInfo'
				});
				return false;
			}
		},
		watch:{
			search_value_user(newv){
				console.log(newv)
				// this.$set(this.dataList[this.currentIndex],'operator',newv.userId);
				this.userId = newv.userId
				this.userName = newv.NAME
				// this.filter.userName = newv.NAME;
				// this.listCards=[];
				// this.page=1;
				// this.loading_status="more";
				// this.dataList();
			},
		},
		methods: {
			// 选择转派人
			selectOperater(){
				this.result_btn=3
				this.show_search_user=true;
			},
			submit(){
				let params = {}
				if(this.result_btn == 0){
					if(!this.reduceReason){
						uni.showToast({
							    title: '请填写驳回原因',
							    duration: 2000,
								icon:"none"
							});
						return false;
					}
					params.taskId = this.taskId
					params.type = this.type
					params.remark = this.reduceReason
					this.$http.request({
					  url: 'apis/approveFlow/turnDownTask',
					  methods:'POST',
					  data:params,
					}).then(res => {
						if(res.data.code == 0){
							uni.showToast({
								title: '操作成功',
								duration: 	1000,
								icon:"none"
							});
							setTimeout( function(){
								uni.navigateTo({
									url: 'advertiseInfo'
								});
							},1000)
						}else{
							uni.showToast({
								title: res.data.msg,
								duration: 	2000,
								icon:"none"
							});
						}
					}).catch(err=>{
					uni.showToast({
						title: '操作失败',
						duration: 	2000,
						icon:"none"
					});
				});
				}
				if(this.result_btn == 1){
					if(!this.reduceReason){
						uni.showToast({
							    title: '请填写审批意见',
							    duration: 2000,
								icon:"none"
							});
						return false;
					}
					params.taskId = this.taskId
					params.type = this.type
					params.remark = this.reduceReason
					this.$http.request({
					  url: '/apis/approveFlow/completeTask',
					  methods:'POST',
					  data:params,
					}).then(res => {
						if(res.data.code == 0){
							uni.showToast({
								title: '操作成功',
								duration: 	1000,
								icon:"none"
							});
							setTimeout( function(){
								uni.navigateTo({
									url: 'advertiseInfo'
								});
							},1000)
						}else{
							uni.showToast({
								title: res.data.msg,
								duration: 	2000,
								icon:"none"
							});
						}
					}).catch(err=>{
					uni.showToast({
						title: '操作失败',
						duration: 	2000,
						icon:"none"
					});
				});
				}
				if(this.result_btn == 2){
					if(!this.reduceReason){
						uni.showToast({
							    title: '请填写回退原因',
							    duration: 2000,
								icon:"none"
							});
						return false;
					}
					params.instanceId = this.instanceId
					this.$http.request({
					  url: '/apis/approveFlow/findRollBackUser',
					  methods:'POST',
					  params:params,
					  header: {
					      'Content-Type': 'application/x-www-form-urlencoded'
					  }
					}).then(res => {
						if(res.data.code == 0){
							uni.showToast({
								title: '操作成功',
								duration: 	1000,
								icon:"none"
							});
							setTimeout( function(){
								uni.navigateTo({
									url: 'advertiseInfo'
								});
							},1000)
						}else{
							uni.showToast({
								title: res.data.msg,
								duration: 	2000,
								icon:"none"
							});
						}
					}).catch(err=>{
					uni.showToast({
						title: '操作失败',
						duration: 	2000,
						icon:"none"
					});
				});
				}
				if(this.result_btn == 3){
					if(!this.userId){
						uni.showToast({
							    title: '请选择转派人',
							    duration: 2000,
								icon:"none"
							});
						return false;
					}
					params.taskId = this.taskId
					params.type = this.type
					params.userId = this.userId
					console.log(params)
					this.$http.request({
					  url: '/apis/approveFlow/redeployTask',
					  methods:'POST',
					  data:params,
					}).then(res => {
						if(res.data.code == 0){
							uni.showToast({
								title: '操作成功',
								duration: 	1000,
								icon:"none"
							});
							setTimeout( function(){
								uni.navigateTo({
									url: 'advertiseInfo'
								});
							},1000)
						}else{
							uni.showToast({
								title: res.data.msg,
								duration: 	2000,
								icon:"none"
							});
						}
					}).catch(err=>{
					uni.showToast({
						title: '操作失败',
						duration: 	2000,
						icon:"none"
					});
				});
				}
				// if(this.result_btn == 1){
				// 	this.reduceReason = ''
				// }else{
				// 	if(!(this.reduceReason)){
				// 		uni.showToast({
				// 			    title: '请填写拒绝原因',
				// 			    duration: 2000,
				// 				icon:"none"
				// 			});
				// 		return false;
				// 	}
				// }
				//提交审批接口。
				// this.$http.request({
				//   url: '/apis/overTimeApply/approve',
				//   params:{
				// 	  overTimeApplyId:this.id,
				// 	  isApproved:this.result_btn,
				// 	  remark:this.reduceReason,
				//   }
				// }).then(res=>{
				// 	if(res.data.code == 0){
				// 		uni.showToast({
				// 			title: '操作成功',
				// 			duration: 	1000,
				// 			icon:"none"
				// 		});
				// 		setTimeout( function(){
				// 			uni.navigateTo({
				// 			    url: 'advertiseInfo'
				// 			});
				// 		},1000)
				// 	}else{
				// 		uni.showToast({
				// 			title: res.data.msg,
				// 			duration: 	2000,
				// 			icon:"none"
				// 		});
				// 	}
				// }).catch(err=>{
				// 	uni.showToast({
				// 		title: '操作失败',
				// 		duration: 	2000,
				// 		icon:"none"
				// 	});
				// });
			}
		}
	}
</script>


<style scoped>
	.btn {
		width: 138upx;
		height: 58upx;
		border-radius: 8upx;
		float:left;
		margin-right: 20upx;
		line-height: 58upx;
		text-align: center;
		background-color: #D7E6FF;
		border: 1px solid #3382FF;
		box-sizing: border-box;
		color: #3382FF;
		font-size: 26upx;
		margin-top: 20upx;
	}
	
	.btn_ {
		background-color: #3382FF;
		color: #fff;
		border: none;
	}
</style>