<template>
	<view class="total-page page-box task-bg">
		<Nav :title="title" :genre="1" :opacity="scrollTop" is_fixed :bgHeight="bgHeight"></Nav>
		<view class="operate_body page-env-160" :style="{top:`${$tools.topHeight()}px`}">
			<view class="sys-background-fff m-lr30 m-t30 m-b20 p-lr30 r-20">
				<view class="" v-if="type!=='3'">
					<view class="row-justify-sb center flex p-tb30 bor-bottom-1 size-28">
						<view class="wh-text"><text></text>下一阶段处理</view>
						<view class="center line-34" @click="openPopupObj(1)">
							<text class="size-28">{{stageData.stageName?stageData.stageName:'请选择任务阶段'}}</text>
							<uni-icons type="forward" size="18" color="#D8D8D8"></uni-icons>
						</view>
					</view>
					<view class="row-justify-sb center p-tb30 bor-bottom-1 size-28" v-if="selectType<2">
						<view class="wh-text"><text></text>下一执行人员</view>
						<view class="center line-30" @click="openPopupObj(2)">
							<text class="size-28">{{stageData.userName?stageData.userName:'请选择执行人员'}}</text>
							<uni-icons type="forward" size="18" color="#D8D8D8"></uni-icons>
						</view>
					</view>
					<en-input :label="'回款金额'" v-if="stageData.operate_ids.indexOf('2')>-1"
						v-model="businessData.returned_total" type="digit" placeholder="请输入回款金额"></en-input>
					<en-input :label="'出款金额'" v-if="stageData.operate_ids.indexOf('1')>-1"
						v-model="businessData.out_total" type="digit" placeholder="请输入出款金额"></en-input>
					<en-input :label="'银行出款'" v-if="stageData.operate_ids.indexOf('4')>-1"
						v-model="businessData.bank_total" type="digit" placeholder="请输入银行出款"></en-input>
					<view v-if="stageData.operate_ids.indexOf('3')>-1">
						<en-input :label="'回款姓名'" v-model="businessData.returned_name" type="text"
							placeholder="请输入回款人姓名"></en-input>
						<!--            此处应该是select-->
						<en-select :label="'回款银行'" v-model="businessData.returned_bank_id" :localData="bankData"
							placeholder="请选择回款银行"></en-select>
					</view>

				</view>
				<!--				<view class="" v-else>-->
				<!--					<WhInput :name="'冲销类型'" :is_border="false" is_select disabled></WhInput>-->
				<!--				</view>-->
			</view>
			<view class="sys-background-fff m-lr30 r-20">
				<SendChat v-model="msgData" :placeholder="type==='2'?'请输入审核意见~~':'请输入描述~~'"></SendChat>
			</view>
		</view>
		<view v-show="showExecute">
			<uni-data-picker class="" :popup-title="popupTitle" :localdata="popupData" ref="popupObj"
				@change="setNewExecute" :border="false" :clear-icon="false" @popupclosed="setPopupClosed">
			</uni-data-picker>
		</view>
		<EnButton text="提交" @onSubmit="submit"></EnButton>
	</view>
</template>

<script>
	import WhInput from "@/components/en-from/en-input/en-input.vue"
	import SendChat from "@/page_task/task_details/module/send_chat.vue"
	import tools from "@/service/tools";
	import {
		auditTask,
		getReturnedBankList,
		getTaskStage,
		setExecuteTask,
		setMsg,
		setStageOperate
	} from "@/api/task";
	import {
		getStageUser
	} from "@/api/task";
	import EnInput from "@/components/en-from/en-input/en-input.vue";
  import EnSelect from "@/components/en-from/en-select/en-select.vue";
	export default {
		components: {
      EnSelect,
			EnInput,
			WhInput,
			SendChat
		},
		data() {
			return {
				bgHeight: 110,
				scrollTop: 0,
				is_handle: true,
				title: '任务反馈',
				businessId: '',
				type: '',
				stageData: {
					operate_ids: []
				},
				businessData: {
					'id': '',
					'manage_id': '',
					'stage_id': '',
					'audit_type': '',
					'msg': '',
					'msg_img': [],
					'file_list': [],
					'returned_total': '',
					'out_total': '',
					'bank_total': '',
					'returned_name': '',
					'returned_bank_id': '',
				},
				msgData: {
					'msg': '',
					'msg_img': [],
					'file_list': [],
				},
				users: [],
				stageList: [],
				isAudit: false,
				showExecute: false,
				popupData: [],
				popupType: 1,
				auditType: 0,
				popupTitle: '',
				previewList: [],
				current: 0,
				selectType: 1,
				//新增阶段操作数据
				operateType: 1,
				stageId: 0,
				operateArr: [],
				operateData: {
					'returned_total': '',
					'out_total': '',
					'returned_bank': '',
					'returned_bank_id': '',
				},
				bankData: []
			}
		},
		watch: {
			'msgData': {
				handler() {
					this.businessData.msg = this.msgData.msg
					this.businessData.msg_img = this.msgData.msg_img
					this.businessData.file_list = this.msgData.file_list
				},
				deep: true
			}
		},
		onLoad(ret) {
			console.log(ret)
			if (ret.businessId === undefined) {
				tools.leftClick()
			} else {
				this.businessId = ret.businessId
				this.type = ret.type
				console.log(this.type === '2')
				if (ret.auditType) this.businessData.audit_type = ret.auditType * 1
				if (this.type === '1') {
					this.title = '任务反馈'
				} else if (this.type === '2') {
					this.title = '任务审核'
				} else if (this.type === '3') {
					this.title = '任务执行'
				} else {
					this.title = '任务冲销'
				}
				this.getTaskStage();
			}
		},
		onPageScroll(res) {
			this.scrollTop = res.scrollTop / 120
		},
		methods: {
			openPopupObj(type) {
				this.popupType = type;
				if (type === 1) {
					if (this.selectType > 2) {
						tools.error('此阶段不能修')
						return false;
					}
					this.popupTitle = '请选择执行阶段'
					this.popupData = this.stageList
				} else {
					if (this.selectType > 1) {
						tools.error('此阶段不能修改')
						return false;
					}
					this.popupTitle = '请选择执行人'
					this.popupData = this.users
				}
				this.showExecute = true;
				this.$refs.popupObj.show();
			},
			setPopupClosed() {
				//关闭选择器
				this.showExecute = false;
			},
			submitOperate() {
				this.operateData.businessId = this.businessData.id
				this.operateData.stageId = this.stageId
				setStageOperate(this.operateData).then((res) => {
					if (res.code === 1) {
						this.operateData = {
							'returned_total': '',
							'out_total': '',
						}
						this.operateType = 1
						tools.success(res.msg)
						this.$emit('endTaskSet')
					} else {
						tools.error(res.msg)
					}
				})
			},
			setNewExecute(e) {
				//设置新执行人
				let newData = e.detail.value[0]
				this.showExecute = false;
				if (this.popupType === 1) {
					this.businessData.stage_id = newData.value
					this.stageData.stageName = newData.text
					this.getStageUser(this.businessData.stage_id)
					this.stageList.forEach((stage) => {
						if (stage.value === newData.value) {
							if (stage.type !== 2) {
								this.selectType = 2
							} else {
								this.selectType = 1
							}
						}
					})
				} else {
					this.businessData.manage_id = newData.value
					this.stageData.userName = newData.text
				}
			},
			getStageUser(stage_id) {
				getStageUser({
					'stage_id': stage_id
				}).then((res) => {
					if (res.code === 1) {
						this.users = res.data
						this.businessData.manage_id = this.users[0].value
						this.stageData.userName = this.users[0].text
					}
				})
			},
			submit() {
        console.log('aaaaaaaaaaaaaaaaaaaaaaaaaa')
        console.log('this.type:'+this.type)
				if (this.type === '2') {
					this.auditTask()
				} else if (this.type === '1') {
					this.setExecuteTask()
				} else if (this.type === '3') {
					this.setMsg()
				}
			},
			setEnd() {
				uni.$emit('newTaskList')
				setTimeout(() => {
					tools.leftClick()
				}, 1000)
			},
			setMsg() {
				if (this.isAjax) {
					return;
				}
				// this.isAjax=true;
				this.msgData.business_id = this.businessId
				if (this.replyId) {
					this.msgData.reply_id = this.replyId;
				}
				setMsg(this.msgData).then((res) => {
					if (res.code === 1) {
						tools.success(res.msg)
						this.msgData.reply_id = ''
						this.msgData.msg = ''
						this.msgData.msg_img = []
						this.setEnd()
					} else {
						tools.error(res.msg)
						this.isAjax = false;
					}

				})
			},
			getTaskStage() {
				getTaskStage({
					'id': this.businessId
				}).then((res) => {
					if (res.code === 1) {
						this.stageData = res.data.stageData;
						console.log(this.stageData.operate_ids)
						if (this.stageData.operate_ids.indexOf('3') > -1) {
							//获取回款银行信息
							getReturnedBankList().then((bankRes) => {
								if (bankRes.code === 1) {
									this.bankData = bankRes.data
								}
							})
						}
						this.businessData.id = res.data.business.id;
						this.businessData.manage_id = res.data.business.manage_id;
						this.businessData.stage_id = res.data.business.stage_id;
						this.stageId = res.data.business.stage_id;
						this.users = res.data.users;
						this.stageList = res.data.stageList;
						let isNext = false
						this.stageList.forEach((item) => {
							if (res.data.business.stage_id === item.value) {
								isNext = true
							} else if (isNext) {
								this.businessData.stage_id = item.value
								this.stageData.stageName = item.text
								this.getStageUser(this.businessData.stage_id)
								if (item.type > 2) {
									this.selectType = 2
								} else {
									this.selectType = 1
								}
								isNext = false
							}
						})

						if (this.stageData.operateNum > 1) {
							this.selectType = 3;
						}
					} else {
						tools.error('阶段信息异常')
					}
				})
			},
			auditTask() {
				uni.showModal({
					title: '提示',
					content: this.businessData.audit_type === 1 ? '是否通过当前任务？' : '是否驳回当前任务？',
					success: (res) => {
						if (res.confirm) {
							auditTask(this.businessData).then((auditRes) => {
								if (auditRes.code === 1) {
									tools.success(auditRes.msg)
									this.setEnd()
								} else {
									tools.error(auditRes.msg)
								}
							})
						}
					}
				});
			},
			setExecuteTask() {
				setExecuteTask(this.businessData).then((auditRes) => {
					if (auditRes.code === 1) {
						tools.success(auditRes.msg)
						// this.$emit('endTaskSet')
						this.setEnd()
					} else {
						tools.error(auditRes.msg)
					}
				})
			},


		}
	}
</script>

<style lang="scss" scoped>
	.operate_body {
		width: 100%;
		position: absolute;
	}

	.wh-input {
		width: 100%;
		text-align: right;
		direction: rtl;
	}
</style>
