<template>
	<view>	
		 <mescroll-body :sticky="true" ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback">
		 	<view class="sticky-tabs">
		 		<me-tabs v-model="tabIndex" :tabs="tabs" @change="tabChange"></me-tabs>
		 	</view>
		 	
		 	<!-- 搜索  -->
		 	<view class="cu-bar bg-white search">
		 		<view class="search-form round">
		 			<input type="text" @confirm="keywordSearch" v-model="codeOrName" placeholder="请输入提单人/客户名称" confirm-type="search"></input>
		 			<text class="cuIcon-search"></text>
		 		</view>
		 	</view>	
						
			<view class="add-customer mat15">
				<text v-if="operatingStatus === ''">共{{TotalPage}}条</text>
				<text v-if="operatingStatus === 0">共{{initiateNum}}条</text>
				<text v-if="operatingStatus === 1">共{{pendingNum}}条</text>
				<text v-if="operatingStatus === 2">共{{processedNum}}条</text>
				<text v-if="operatingStatus === 3">共{{copyNum}}条</text>
			</view>
			
			<!-- 数据列表-非抄送 -->
			<view v-if="operatingStatus!=3"  class="cu-list menu-avatar">
				<view class="cu-item" :class="modalName=='move-box-'+ index?'move-cur':''"  v-for="(item,index) in tableView" :key="index" @touchstart="ListTouchStart" @touchmove="ListTouchMove" @touchend="ListTouchEnd" :data-target="'move-box-' + index">
					<view class="good-list" @tap="jumpToCopy(item,'查看')">
						<view class="charge" :id="'index-' + item.name" :data-index="item.name">
							<view class="charge-title flex-between">
								<text>{{item.clientName}}</text>
							</view>
							<view class="charge-text">
								<view>
									<text>提单人：</text>
									<text class="mgl30">{{item.applicant}}</text>
								</view>
								<view>
									<text>申请日期：</text>
									<text class="mgl30">{{item.applicationDate}}</text>
								</view>
								<view>
									<text>累计耗时：</text>
									<text class="mgl30">{{item.timeConsuming||''}}</text>
								</view>
								<view>
									<text>任务节点：</text>
									<text class="mgl30">{{item.taskNode}}</text>
								</view>
								<view>
									<text>完成时间：</text>
									<text class="mgl30">{{item.completeTime||''}}</text>
								</view>
								<view>
									<text>审批状态：</text>
									<text class="mgl30">{{item.approveStatusVal}}</text>
								</view>
								<view>
									<text>业务状态：</text>
									<text class="mgl30">{{item.businessStatusVal}}</text>
								</view>
								<view>
									<text>操作状态：</text>
									<text class="mgl30">{{item.operatingStatusVal}}</text>
								</view>
							</view>
						</view>
					</view>
					<!-- 待处理的按钮,此处仅有一个按钮 -->
					<view class="move foot-btn sideBtn" v-if="tabIndex===2||tabIndex===0">
						<button v-if="item.operatingStatus===1&&item.businessStatus !== 3" class="mini-btn round def-btn " type="primary" size="mini" @click="decide(item)">{{item.btn}}</button>
						<!-- <button v-show="" class="mini-btn round def-btn mar-lr" type="primary" size="mini" @click="handleGe()">审核</button> -->
					</view>
				</view>
			</view>
			<!-- 抄送列表 -->
			<view v-if="operatingStatus==3">
				<view v-for="(item,index) in tableView" :key="index">
					<view class="good-list" @tap="jumpToCopy(item)">
						<view class="charge" :id="'index-' + item.name" :data-index="item.name">
							<view class="charge-title flex-between">
								<text>{{item.clientName}}</text>
							</view>
							<view class="charge-text">
								<view>
									<text>提单人：</text>
									<text class="mgl30">{{item.applicant}}</text>
								</view>
								<view>
									<text>客户名称：</text>
									<text class="mgl30">{{item.clientName||''}}</text>
								</view>
								<view>
									<text>申请日期：</text>
									<text class="mgl30">{{item.applicationDate}}</text>
								</view>
								<view>
									<text>任务节点：</text>
									<text class="mgl30">{{item.taskNode}}</text>
								</view>
								<view>
									<text>完成时间：</text>
									<text class="mgl30">{{item.passingTime||''}}</text>
								</view>
								<view>
									<text>业务状态：</text>
									<text class="mgl30">{{item.businessStatus}}</text>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</mescroll-body>
	</view>
</template>

<script>
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
	import linkMan from '../../components/linkMan/linkMan.vue'
	export default {
		mixins: [MescrollMixin], // 使用mixin
		components:{
			linkMan
		},
		data() {
			return {
				tabs:[
					{name:'全部', value: ''},
					{name:'我发起', value: 0},
					{name:'待处理', value: 1},
					{name:'已处理', value: 2},
					{name:'抄送我', value: 3}
				],
				tabIndex: 0, // 当前菜单下标
				page: {
					num: 1,
					size: 10,
				},
				
				tableData: [],
				tableView: [],
				copyTable: [],//深拷贝的数组
				TotalPage: 0,
				totals: 0, //全部数目
				initiateNum:0,// 发起数目
				pendingNum:0,// 待处理
				processedNum:0,// 已处理数目
				copyNum:0,//抄送我的数目
				createFixedTime:'',// 查询时间
				operatingStatus :'',//选择的值
				codeOrName: '',//搜索关键词
				TotalPageNum: 1,
				status:true,// 
				scrolltop: false,
				listTouchStart: 0,
				listTouchDirection: null,
				modalName: null,
			};
		},
		onShow() {
			// 未登录判断
			if(!uni.getStorageSync('token')){
				this.status = false
				let that = this
				uni.showModal({
					title:"未登录",
					content:'是否跳转登录页？',
					success: function (res) {
						if (res.confirm) {
							// console.log('用户点击确定');
							uni.switchTab({
							    url: '/pages/user/user'
							});
						} else if (res.cancel) {
							// console.log('用户点击取消');
						}
					}
				})
			}else{
				this.status = true
				this.upCallback(this.page)
			}
		},
		created() {
		},
		methods: {
			// 待处理
			decide(item){
				// if(item.operatingStatus===1){
					// 传值
					uni.setStorageSync('decideIndex',JSON.stringify(item))
					switch (item.taskNode){
						case '业务申请':
							this.goto('/guaranteePages/pages/addApplication/addApplication')
							break;
						case '担保部调查':
							if(item.btn==='指派'){
								this.goto('/guaranteePages/pages/assignAB/assignAB')
							}else{
								this.goto('/guaranteePages/pages/investigationSee/investigationSee')
							}
							break;
						case '资产部调查':
							if(item.btn==='指派'){
								this.goto('/guaranteePages/pages/assignCommissioner/assignCommissioner')
							}else{
								this.goto('/guaranteePages/pages/assetSee/assetSee')
							}
							break;
						case '信息部调查':
							if(item.btn==='指派'){
								this.goto('/guaranteePages/pages/assignInformation/assignInformation')
							}else{
								this.goto('/guaranteePages/pages/InformationSee/InformationSee')
							}
							break;
						case '合规调查':
							if(item.btn==='指派'){
								this.goto('/guaranteePages/pages/assignRegulation/assignRegulation')
							}else{
								this.goto('/guaranteePages/pages/regulationSee/regulationSee')
							}
							break;
						case '工作会':
							if(item.btn==='审议'){
								this.goto('/guaranteePages/pages/workMeetingSee/workMeetingSee')
							}else if(item.btn==='抽取评委'){
								this.goto('/guaranteePages/pages/reviewJudges/reviewJudges')
							}
							break;
						case '贷审会':
							if(item.btn==='审议'){
								this.goto('/guaranteePages/pages/reviewSee/reviewSee')
							}
							break;
						case '担保函':
								this.goto('/guaranteePages/pages/letterSee/letterSee')
							break;
						case '财务确认':
							if(item.btn==='确认'){
								this.goto('/guaranteePages/pages/paymentSee/paymentSee')
							}
							break;
						case '放款通知':
							if(item.btn==='确认'){
								this.goto('/guaranteePages/pages/noticeSee/noticeSee')
							}
							break;
					}
				// }
			},
			keywordSearch(e){
				this.codeOrName = e.detail.value
				this.tableView = this.copyTable
				this.tableView = this.tableView.filter(data => !this.codeOrName|| data.clientName.toLowerCase().includes(this.codeOrName.toLowerCase()) || data.applicant.toLowerCase().includes(this.codeOrName.toLowerCase()))
				if(this.operatingStatus==3){// 抄送的查询
					this.upCallback({
						num:1,size:10
					})
				}
				
			},
			/*下拉刷新的回调 */
			downCallback() {
				this.mescroll.resetUpScroll()
			},
			/*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
			upCallback(page) {
				// 非抄送，切换接口
				if(this.operatingStatus!==3){
					this.$http.post('/api-guarantee/guarantee/efficiency/personalEfficiencyList',{
						createFixedTime: this.createFixedTime,
						operatingStatus: this.operatingStatus
					}).then(res => {
						
						if(page.num == 1) this.tableView = []; //如果是第一页需手动制空列表
						// console.log(res.data,'当前的数据')
						res.data.responseList.map(e =>{
							e.approveStatusVal = this.core.statusVal(e.approveStatus) //审批状态
							e.businessStatusVal = this.core.businessType(e.businessStatus) //业务状态
							e.operatingStatusVal = this.core.operationType(e.operatingStatus) //操作状态
							if(e.operatingStatus===1){
								switch (e.taskNode) {
									// 拥有十套不同的按钮判断逻辑，按钮根据取得的状态进行判断操作
									case '业务申请':
										e.btn = this.btn1(e)
										break;
									case '担保部调查':
										e.btn = this.btn2(e)
										break;
									case '资产部调查':
										e.btn = this.btn3(e)
										break;
									case '信息部调查':
										e.btn = this.btn4(e)
										break;
									case '合规调查':
										e.btn = this.btn5(e)
										break;
									case '工作会':
										e.btn = this.btn6(e)
										break;
									case '贷审会':
										e.btn = this.btn7(e)
										break;
									case '担保函':
										e.btn = this.btn8(e)
										break;
									case '财务确认':
										e.btn = this.btn9(e)
										break;
									case '放款通知':
										e.btn = this.btn10(e)
										break;
									default:
										break;
								}
							}
						})
						// console.log('处理完毕',res.data.responseList)
						this.tableData = res.data.responseList
						this.TotalPage = res.data.responseList.length
						this.TotalPageNum = Math.ceil(this.TotalPage / 10)
						
						this.initiateNum = res.data.statistics.initiateNum
						this.pendingNum = res.data.statistics.pendingNum
						this.processedNum = res.data.statistics.processedNum
						this.copySendNum = res.data.statistics.copySendNum
						
						let pageNum =(page.num-1) * page.size  // 懒得做size了，直接固定10，需要size改这里
						let number = this.tableData.slice(pageNum, pageNum + page.size)// 修改size，10也改
						this.tableView = this.tableView.concat(number)  
						this.copyTable = Object.assign([],this.tableView)
						this.mescroll.endSuccess(res.data.responseList); // 隐藏加载状态栏
					}).catch(()=>{
						//联网失败, 结束加载
						this.mescroll.endErr();
					})
				}else{
					this.$http.post('/api-guarantee/dg-copy-user/queryCopyForMe',{
						clientName: this.codeOrName,
						createFixedTime: "",
						endTime: "",
						startTime: "",
						userId: "",
						page:page.num||1,
						size:page.size||10
					}).then(res => {
						if(page.num == 1) this.tableView = []; //如果是第一页需手动制空列表
						// console.log(res,'抄送接口')
						res.data.copyForMeList.map(e =>{
						  e.businessStatus = this.core.businessType(e.businessStatus) //业务状态
						})
						this.copyNum = res.data.total
						// this.tableData = res.data.copyForMeList
						this.tableView = this.tableView.concat(res.data.copyForMeList); //追加新数据
						// console.log(this.tableView,'抄送')
						let pageNum =(page.num-1) * page.size  // 懒得做size了，直接固定10，需要size改这里
						// let number = this.tableData.slice(pageNum, pageNum + page.size)// 修改size，10也改
						this.copyTable = Object.assign([],this.tableView)
						this.mescroll.endSuccess(res.data.copyForMeList); // 隐藏加载状态栏
					}).catch(()=>{
						//联网失败, 结束加载
						this.mescroll.endErr();
					})
				}
			},
			
			// 切换菜单
			tabChange (index) {
				// console.log(this.tabIndex,'当前index')
				this.operatingStatus = this.tabs[index].value;//当前下标的值
				this.codeOrName = '';
				this.tableView = []; // 置空列表,显示加载进度条
				this.mescroll.resetUpScroll()
				
				// this.$http.get('/api-guarantee/dg-copy-user/queryCopyForMe',).then(res=>{
				// 	console.log(res,'当前抄送的数据');
				// })
			},
			jumpToFrom (node) {
				let path
				switch (node) {
					case '业务申请':
						path = 'application/application'
						break;
					case '担保部调查':
						path = 'investigation/investigation'
						break;
					case '资产部调查':
						path = 'asset/asset'
						break;
					case '信息部调查':
						path = 'Information/Information'
						break;
					case '合规调查':
						path = 'regulation/regulation'
						break;
					case '工作会':
						path = 'workMeeting/workMeeting'
						break;
					case '贷审会':
						path = 'review/review'
						break;
					case '担保函':
						path = 'letter/letter'
						break;
					case '财务确认':
						path = 'payment/payment'
						break;
					case '放款通知':
						path = 'notice/notice'
						break;
					default:
						break;
				}
				this.goto('/guaranteePages/pages/'+path)
			},
			jumpToCopy(data,val){
				// console.log('执行抄送的方法')
				
				uni.setStorageSync('copyData',JSON.stringify(data))
				uni.setStorageSync('businessId',data.businessId)
				let path,index
				switch (data.taskNode) {
					case '业务申请':
						path = 'addApplication/addApplication'
						index = 1
						break;
					case '担保部调查':
						path = 'investigationSee/investigationSee'
						index = 2
						break;
					case '资产部调查':
						path = 'assetSee/assetSee'
						index = 3
						break;
					case '信息部调查':
						path = 'InformationSee/InformationSee'
						index = 4
						break;
					case '合规调查':
						path = 'regulationSee/regulationSee'
						index = 5
						break;
					case '工作会':
						path = 'workMeetingSee/workMeetingSee'
						index = 6
						break;
					case '贷审会':
						path = 'reviewSee/reviewSee'
						index = 7
						break;
					case '担保函':
						path = 'letterSee/letterSee'
						index = 8
						break;
					case '财务确认':
						path = 'paymentSee/paymentSee'
						index = 9
						break;
					case '放款通知':
						path = 'noticeSee/noticeSee'
						index = 10
						break;
					default:
						break;
				}
				uni.setStorageSync('processId',index);
				// 非抄送的状态下-记录一个值用于区分抄送，执行不同操作
				// 原因：后端嫌这块拼数据麻烦加上了 担保函后三个模块的详情接口
				if(val==='查看'){ // 区分抄送用
					uni.setStorageSync('notCopy','查看')
				}
				if(data.operatingStatus==1){
					this.jumpToFrom(data.taskNode)  // 待处理进列表
				}else{
					this.goto('/guaranteePages/pages/'+path)// 非待处理进详情
				}
			},
			// ListTouch触摸开始
			ListTouchStart(e) {
				this.listTouchStart = e.touches[0].pageX
			},
			// ListTouch计算方向
			ListTouchMove(e) {
				this.listTouchDirection = e.touches[0].pageX - this.listTouchStart > 0 ? 'right' : 'left'
			},
			// ListTouch计算滚动
			ListTouchEnd(e) {
				if (this.listTouchDirection == 'left') {
					this.modalName = e.currentTarget.dataset.target
				} else {
					this.modalName = null
				}
				this.listTouchDirection = null
			},
			// 待处理的按钮的赋值,分别展示所有的模块待处理的值
			// 传入对应 的业务即可
			btn1(data){
				// 两个操作，需要进行两个操作的判定，多个操作，则需要多个判断
				let survey = 0,
					audit = 0
				// 判断调查员
				if(data.approveStatus===4||data.approveStatus===5||data.approveStatus == 6&&data.operatingStatus == 3&&data.businessStatus !== 2){
					survey+1
				}
				if([57].includes(data.roleId)&&data.businessStatus !== 3){
					survey+1  // 判定为调查员，就return出去，无其他操作
					if(survey==2) return '修改'
				}
				// 判断审核
				if(data.approveStatus===1){
					audit++
				}
				if([58].includes(data.roleId)&&data.businessStatus !== 3){
					audit++
					if(audit==2)return '审核'
				}
				// if(survey!==2&&audit!==2){
				//     return '查看'
				// }
			},
			// 担保部调查
			btn2(data){
				let survey = 0,
					audit = 0
				// 先判断指派
				if(data.approveStatus == 0) return '指派'
				// 判断调查
				if(data.approveStatus===1&&data.operatingStatus == 1||data.approveStatus===4&&data.operatingStatus == 1){
					survey++
				}
				if([59,60].includes(data.roleId)&&data.businessStatus !== 3){
					survey++
					if(survey==2)return '调查'
				}
				// 判断审核
				if([58,61].includes(data.roleId)&&data.businessStatus !== 3){
					audit++
				}
				if(data.approveStatus===1&&data.operatingStatus == 1||data.approveStatus===4&&data.operatingStatus == 1){
					audit++
					if(audit==2)return '审核'
				}
				
			},
			// 资产部调查
			btn3(data){
				let survey = 0,
					audit = 0
				// 先判断指派
				if(data.approveStatus == 0) return '指派'
				// 判断调查
				if([63].includes(data.roleId)&&data.businessStatus !== 3){
					survey++
				}
				if(data.approveStatus===1&&data.operatingStatus == 1||data.approveStatus===4&&data.operatingStatus == 1){
					survey++
					if(survey==2)return '调查'
				}
				// 判断审核
				if([62,64].includes(data.roleId)&&data.businessStatus !== 3){
					audit++
				}
				if(data.approveStatus===1&&data.operatingStatus == 1||data.status===4&&data.operatingStatus == 1){
					audit++
					if(audit==2)return '审核'
				}
			},
			// 信息部调查
			btn4(data){
				let survey = 0,
					audit = 0
				// 先判断指派
				if(data.approveStatus == 0) return '指派'
				// 判断调查
				if(data.approveStatus===1&&data.operatingStatus == 1||data.approveStatus===4&&data.operatingStatus == 1){
					survey++
				}
				if([66].includes(data.roleId)&&data.businessStatus !== 3){
					survey++
					if(survey==2)return '调查'
				}
				// 判断审核
				if(data.approveStatus===1&&data.operatingStatus == 1||data.approveStatus===4&&data.operatingStatus == 1){
					audit++
				}
				if([65,67].includes(data.roleId)&&data.businessStatus !== 3){
					audit++
					if(audit==2)return '审核'
				}
			},
			// 合规部调查
			btn5(data){
				let survey = 0,
					audit = 0
				// 先判断指派
				if(data.approveStatus == 0) return '指派'
				// 判断调查
				if([69].includes(data.roleId)&&data.businessStatus !== 3){
					survey++
				}
				if(data.approveStatus===1&&data.operatingStatus == 1||data.approveStatus===4&&data.operatingStatus == 1){
					survey++
					if(survey==2)return '调查'
				}
				// 判断审核
				if([68,70].includes(data.roleId)&&data.businessStatus !== 3){
					audit++
				}
				if(data.approveStatus===1&&data.operatingStatus == 1||data.approveStatus===4&&data.operatingStatus == 1){
					audit++
					if(audit==2)return '审核'
				}
			},
			// 工作调查
			btn6(data){
				let deliberation = 0,// 审议
					extract = 0// 抽取评委
				// 判断审议
				if([71].includes(data.roleId)&&data.businessStatus !== 3){
					deliberation++
				}
				if(data.approveStatus===1&&data.operatingStatus == 1||data.approveStatus===4&&data.operatingStatus == 1){
					deliberation++
					if(deliberation==2)return '审议'
				}
				// 判断抽取评委
				if([71].includes(data.roleId)&&data.businessStatus !== 3){
					extract++
				}
				if(data.approveStatus===2&&data.operatingStatus == 1){
					extract++
					if(extract==2)return '抽取评委'
				}
			},
			// 贷审会
			btn7(data){
				let deliberation = 0// 审议
				// 判断审议
				if(data.approveStatus===1&&data.operatingStatus == 1||data.approveStatus===4&&data.operatingStatus == 1){
					deliberation++
				}
				if([72,59,60].includes(data.roleId)&&data.businessStatus !== 3){
					deliberation++
					if(deliberation==2)return '审议'
				}
			},
			// 担保函调查
			btn8(data){
				// 判断审议
				if(data.isFgJl == 1 && data.operatingStatus !== 2&&data.businessStatus !== 3){
					return '担保函'
				}
				if(data.isFgJl != 1 && data.operatingStatus !== 2&&data.businessStatus !== 3){
					return '确认'
				}
			},
			// 财务确认
			btn9(data){
				return '确认'
			},
			// 放款通知
			btn10(data){
				return '确认'
			},
		}
	}
</script>

<style lang="scss" scoped>
.cu-bar .search-form{
	border: 2rpx solid #00B9FF;
	border-radius: 20rpx;
	background-color: #fff;
	margin: 0 50rpx;
	height: 72rpx;
	line-height: 72rpx;
	margin-top: 20rpx;
	input{
		height: 72rpx;
		line-height: 72rpx;
		padding: 0 40rpx;
	}
	.cuIcon-search{
		margin: 0 20rpx;
	}
}
.add-customer{
	padding: 0 50rpx;
	button{
		background-color: #00B9FF;
		border-radius: 30rpx;
		.mar-icon{
			margin-right: 10rpx;
		}
	}
}
/deep/ .cu-list>.cu-item .move view{
	flex: none;
	margin-top: 10rpx;
}
</style>
