<template>
	<view class="check-message-detail">
		<div class="main">
			<view class="title-area" v-if="!isManager">
				<view class="text" >
					<text>您的访客申请{{status}}</text>
					<text v-if="status === '已提交'" class="text_text">请耐心等待管理员审核</text>
				</view>
				<view :class="status === '未通过' ? 'button-area' : status === '已通过' ? 'button-area pass' : 'button-area submit'">申请{{status}}</view>
			</view>
			
			<!-- 申请者未通过 -->
			<view class="message-text" v-if="!isManager && status === '未通过'">
				请联系客服电话020-61228107、台内分级可直接拨打8107进行确认
			</view>
			
			<!-- 申请者已通过 -->
			<view  class="message-text message-text-css" v-if="!isManager && status === '已通过'">
				访客{{name}}可凭借身份证在{{time}}时间内进入{{place}}
			</view>
			
			<!-- 访客、管理员已提交 -->
			<view 
				class="content" 
				v-for="item in list" 
				:key="item.id" 
				v-if="isManager"
				>
				<!-- v-if="isManager ? true : item.id === 'staffPhone' || item.id === 'staffName' ? false : true"> -->
					<view class="title">{{item.name}}</view>
					<view :class="item.value === '申请已通过' ? 'green' : 
					item.value === '申请未通过' ? 'red' :
					item.value === '申请已提交' ? 'blue' :
					'value'">{{item.value}}</view>
			</view>
		</div>
		<u-popup
		  :show="show"
		  mode="bottom"
		  :round="10"
		  :closeOnClickOverlay="false"
		  @close="cancel">
		  <view class="popup">
				<view class="title">提示</view>
		    <view class="content">
		      {{content}}
		    </view>
		    <view class="footer">
					<button class="allow refuse" @click="cancel">取消</button>
					<button class="allow" @click="confirm"> 确认</button>
		    </view>
		  </view>
		</u-popup>
		<!-- 管理员button -->
		<view v-if="isManager && status === '已提交'" style="display: flex;justify-content: space-around;margin-top: 32px;">
			<u-button customStyle="background-color:#2ebf00;border: none;color: #fff;font-weight: 600;width: 220rpx;font-size:16px;" type="primary" @click="handleClick(item, '通过')">申请通过</u-button>
			<u-button customStyle="border: none;color: #fff;font-weight: 600;width: 220rpx;font-size:16px;" type="error" @click="handleClick(item, '拒绝')">申请不通过</u-button>
		</view>
	</view>
</template>

<script>
	import { getMessageInfo, solveStatus } from '@/api/message.js';
	import { getLocationParams } from '@/common/utils.js';
	export default {
		data() {
			return {
				isManager: true,
				userId: '',  //用户id
				show: false, //控制模态框显隐
				content: '', //模态框显示内容
				list: [
					{
						id: 'applyNo',
						name: '申请单号',
						value: ''
					},
					{
						id: 'visitorApplyStatus',
						name: '申请状态',
						value: ''
					},
					{
						id: 'staffCode',
						name: '申请者工号',
						value: ''
					},
					{
						id: 'staffName',
						name: '申请者姓名',
						value: ''
					},
					{
						id: 'staffPhone',
						name: '申请者手机号',
						value: ''
					},
					{
						id: 'visitorName',
						name: '访客姓名',
						value: ''
					},
					{
						id: 'visitorIdCard',
						name: '访客身份证',
						value: ''
					},
					{
						id: 'visitorPhone',
						name: '访客联系方式',
						value: ''
					},
					{
						id: 'visitReason',
						name: '访问目的',
						value: ''
					},
					{
						id: 'visitPosition',
						name: '访问区域',
						value: ''
					},
					{
						id: 'time',
						name: '访问时间',
						value: ''
					},
					{
						id: 'remarks',
						name: '备注',
						value: ''
					},
				],
				status: '',
				name: '',
				time: '',
				place: '',
				willchange: '',
				// messageId: '', //消息id
				solveId: '', //管理员处理id
			}
		},
		mounted() {
			const id = getLocationParams('id');
			const userInfo = JSON.parse(uni.getStorageSync('userInfo'));
			this.userId = userInfo.id;
			if(userInfo.salt === '物业人员' || userInfo.salt === '运维人员') {
				this.isManager = true;
			} else {
				this.isManager = false;
			}
			this.fetchMessageInfo(id);
		},
		methods: {
			handleClick(value, text) {
				if(text === '通过') {
					this.willchange = '2'; //1提交2通过3不通过
				} else {
					this.willchange = '3';
				}
				let index = this.list.findIndex(item => item.id === 'applyNo');
				if(index !== -1) {
					let number = this.list[index].value;
					this.content = `确认${text}单号${number}的访客申请？`;
					this.show = true;
				} else {
					wx.showToast({
						title: '找不到单号',
						icon: 'none',
						duration: 1000,
					})
				}
			},
			//点击模态框取消按钮
			cancel() {
				this.show = false;
			},
			//点击模态框确认按钮
			confirm() {
				this.solveStatusData();
				wx.reLaunch({
					url: '/pages/message/message'
				})
			},
			//获取消息详情
			async fetchMessageInfo(id) {
				const res = await getMessageInfo(id);
				this.status = res.visitorApplyStatus === "1" ? '已提交' :  res.visitorApplyStatus === "2" ? 
				'已通过' : '未通过';
				this.name = res.visitorName;
				this.time = res.inTime + ' ~ ' + res.outTime;
				this.place = res.visitPosition;
				this.solveId = res.id;
				this.list.forEach(item => {
					for(let key in res) {
						if(item.id === key) {
							if(key === 'visitorApplyStatus') {
								item.value = res[key] === '1' ? '申请已提交' : res[key] === '2' ? '申请已通过' : '申请未通过';
							} else {
								item.value = res[key];
							}
						} else if(item.id === 'time') {
							item.value = res.inTime + ' ~ ' + res.outTime;
						} 
					}
				})
			},
			//管理员处理是否通过申请
			async solveStatusData() {
				await solveStatus(this.willchange, this.solveId);
			},
		}
	}
</script>

<style lang="scss" scoped>
	.check-message-detail {
		padding: 20rpx;
		.popup {
			.title {
				font-size: 36rpx;
				color: #000;
				font-weight: 600;
				margin-bottom: 32rpx;
				text-align: center;
			}
			.content {
				font-size: 32rpx;
				color: #000;
				margin-bottom: 42rpx;
				text-align: center;
			}
			.footer {
				display: flex;
				flex-direction: row;
				justify-content: space-between;
				align-items: flex-end;
				.allow {
					color: rgba(197, 0, 10, 1);
					height: 68rpx;
					padding: 16px 19px;
					line-height: 0;
					border: 1rpx solid rgba(197, 0, 10, 1);
					margin: 0 40rpx;
				}
				.refuse {
					color: rgba(142, 142, 142, 1.0);
					border: 1rpx solid rgba(142, 142, 142, 1.0);
					background-color: transparent;
				}
			}
		}
		.main {
			padding: 32rpx 20rpx;
			border-radius: 10rpx;
			background-color: #fff;
			.message-text {
				font-weight: 600;
			}
			.message-text-css {
				margin-top: 100rpx;
				line-height: 72rpx;
			}
			.title-area {
				display: flex;
				flex-direction: row;
				justify-content: space-between;
				align-items: center;
				font-size: 32rpx;
				margin-bottom: 100rpx;
				.text {
					display: flex;
					flex-direction: column;
					font-weight: 600;
				}
				.button-area {
					padding: 6rpx 16rpx;
					background-color: #f02a0a;
					border-radius: 16rpx;
					color: #ecf9e8;
					font-size: 28rpx;
					white-space: nowrap;
				}
				.pass {
					background-color: #2ebf00;
				}
				.submit {
					background-color: #1076e3;
				}
			}
			.content {
				display: flex;
				flex-direction: row;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 30rpx;
				.title {
					white-space: nowrap;
					margin-right: 60rpx;
					font-weight: 600;
				}
				.green {
					color: #2ebf00;
				}
				.blue {
					color: #1076e3;
				}
				.red {
					color: #f02a0a;
				}
			}
			.button-area-manager {
				display: flex;
				flex-direction: row;
				justify-content: center;
				align-items: center;
				margin-top: 80rpx;
				.pass {
					width: fit-content;
					padding: 10rpx 20rpx;
					background-color: #2ebf00;
					color: #fff;
					border-radius: 16rpx;
					margin-right: 60rpx;
				}
				.no-pass {
					background-color: #f02a0a;
					color: #fff;
				}
			}
		}
		
	}

</style>

<style lang="scss">
.check-message-detail {
	.u-popup__content {
	  padding: 30rpx 40rpx;
		// padding-bottom: 0;
	}
}
</style>