<template>
	<view class="content">
		<!-- 提交记录 -->
		<view style="min-height: 100vh;">
			<view class="select_submit">
				<view style="width: 100px;margin-left: 9rpx;position: absolute;">
					<uni-data-select
						v-model="value3"
						:localdata="shenfen"
						@change="changeShen"
					  ></uni-data-select>
				</view>
				<view style="width: 80px;margin-left: 196rpx;position: absolute;">
					<uni-data-select
						v-model="value"
						:localdata="zhuangtai"
						@change="changezhuang"
					  ></uni-data-select>
				</view>
				<view style="width: 80px;margin-left: 396rpx;">
					<uni-data-select
						v-model="value2"
						:localdata="TimeRanges"
						@change="changetime"
					  ></uni-data-select>
				</view>
				<!-- 不同状态的记录 -->
				<view :class="item.department=='4'?'submit-list2':'submit-list'" v-for="(item,index) in submit" :key="index" v-if="item.department=='4'">
					<text @click="clickDetail(item.id,item.department)">
						<text class="submit-title" v-if="item.department=='4'">临时人员</text>
						<!-- <text class="submit-title" v-if="item.department=='4'">教师换课</text> -->
						<text class="submit-time">{{item.submissionTime}}</text>
						<view class="submit-line"></view>
						<view class="submit-start" style="color: rgba(0, 0, 0, 1);">附件：</view>
						<view class="submit-start" style="color: rgba(0, 0, 0, 1);">
							<!-- {{item.imageUrl!=null&&item.imageUrl!=''}}3{{(item.imageUrl).split(',')}} -->
							<text v-if="item.imageUrl!=null&&item.imageUrl!=''">
								<image @click.stop="clickImg2(item2)" style="color: rgba(0, 97, 255, 1);width: 200rpx;height: 200rpx;margin-left: 20rpx;" :src="item2" v-for="(item2,index2) in (item.imageUrl).split(',')" :key="index2">
								</image>
							</text>
						</view>
						<view class="submit-start">开始时间：{{item.startTime}}</view>
						<view class="submit-end">结束时间：{{item.endTime}}</view>
						<view class="submit-teacher" v-if="item.department=='4'">换课老师：{{item.changeUsersName}}</view>
						<view class="submit-remark">
							部门：{{item.orgName}}
							<!-- <template v-if="item.orgName=='0'">其他</template>
							<template v-if="item.orgName=='1'">学生</template>
							<template v-if="item.orgName=='4'">教职工</template>
							<template v-if="item.orgName=='5'">校友</template>
							<template v-if="item.orgName=='6'">访客</template>
							<template v-if="item.orgName=='7'">临时人员</template> -->
						</view>
						<view class="submit-remark">提交人：{{item.usersName}}</view>
						<view class="submit-remark">房间数量：{{item.houseCount}}</view>
						<view class="submit-remark">申请原因：{{item.reasonApplication}}</view>
						<view class="submit-type1" v-if="item.status==0">已拒绝</view>
						<view class="submit-type1" v-if="item.status==1">待审批</view>
						<view class="submit-type2" v-if="item.status==2">处理中</view>
						<view class="submit-type" v-if="item.status==3">已同意</view>
					</text>
					<template v-if="approverType=='1'">
						<template v-if="dengji==0">
							<view class="submit-disagree" v-if="item.status==1 || item.status==2" @click="disagree(item.id)">驳回</view>
							<view class="submit-agree" v-if="item.status==1 || item.status==2" @click="agree(item.id)">同意</view>
						</template>
						<template v-else>
						</template>
					</template>
					<template v-if="approverType=='2'">
						<template v-if="dengji==0">
							<view class="submit-disagree" v-if="item.status==2" @click="disagree(item.id)">驳回</view>
							<view class="submit-agree" v-if="item.status==2" @click="agree(item.id)">同意</view>
						</template>
						<template v-else>
							<view class="submit-disagree" v-if="item.status==1" @click="disagree(item.id)">驳回</view>
							<view class="submit-agree" v-if="item.status==1" @click="agree(item.id)">同意</view>
						</template>
					</template>
					
					<view style="height: 20px;"></view>
				</view>

			</view>
		</view>
	</view>
</template>

<script>
	import {
			shenpiguiju,
			getManaLinlist,
			getManaTealist,
			shenpiLinshi,
			shenpiTeacher
		} from '../../utils/api_hotel.js'
	export default {
		data() {
			return {
				// 提交记录
				value3: 0,
				shenfen: [
				  { value: 0, text: "临时人员" },
				  // { value: 1, text: "教师换课" },
				],
				value: '',
				zhuangtai: [
				  { value: 0, text: "已拒绝" },
				  { value: 1, text: "待审批" },
				  { value: 2, text: "处理中" },
				  { value: 3, text: "已同意" },
				  { value: '', text: "全部" },
				],
				value2: 2,
				TimeRanges: [
				  { value: 0, text: "近一周" },
				  { value: 1, text: "本月" },
				  { value: 2, text: "全部" },
				],
				submit:[],
				page:1,
				size:10,
				startTime:'',
				endTime:'',
				nowTime: new Date().toISOString().slice(0, 10),//当前日期2024-07-23
				totalCount:0,
				agreestatu:'',//1：同意 0：拒绝
				
				dengji:localStorage.getItem('parentId'),//当前人等级，0最上级
				approverType:'',//审批规则，1最上级审，2批次审
			}
		},
		onLoad() {
			this.shenpiGui()
		},
		mounted() {
			
		},
		methods: {
			//点击放大图片
			clickImg2(item2) {
				// console.log(item.srcUrl)
				uni.previewImage({
					urls: [item2], //需要预览的图片http链接列表，多张的时候，url直接写在后面就行了
					current: '', // 当前显示图片的http链接，默认是第一个
					success: function(res) {},
					fail: function(res) {},
					complete: function(res) {},
				})
			},
			//审批规则
			shenpiGui(){
				let _self = this
				shenpiguiju().then((res) => {
					if (res.success) {
						_self.approverType=res.data.approverType
						_self.getManageLin()
					} else {
						uni.showToast({
							title: res.message
						})
					}
				}).catch((err) => {
					uni.showToast({
						title: err.message
					})
				});
			},
			//管理员查看临时人员记录
			getManageLin(){
				let _self = this
				// adminMenuId身份类型，localStorage.getItem('manager'),//1管理员，0不是管理员
				// 状态，0:已拒绝，1:待审批，2:处理中，3:已同意
				var data="?adminMenuId="+localStorage.getItem('adminMenuId')+"&startTime="+this.startTime+"&endTime="+this.endTime+"&status="+this.value
				+"&page="+this.page+"&size="+this.size+'&OrgIds='+localStorage.getItem('OrgIds')
				getManaLinlist(data).then((res) => {
					if (res.success) {
						_self.totalCount=res.data.totalCount
						if (_self.page == 1) {
							_self.submit = res.data.list
						} else {
							_self.submit = [..._self.submit, ...res.data.list]
						}
						uni.stopPullDownRefresh();
						uni.hideLoading()
					} else {
						uni.showToast({
							title: res.message
						})
					}
				}).catch((err) => {
					uni.showToast({
						title: err.message
					})
				});
			},
			//管理员查看换课教师记录
			getManageTeacher(){
				let _self = this
				// adminMenuId身份类型，localStorage.getItem('manager'),//1管理员，0不是管理员
				// 状态，0:已拒绝，1:待审批，2:处理中，3:已同意
				var data="?adminMenuId="+localStorage.getItem('adminMenuId')+"&startTime="+this.startTime+"&endTime="+this.endTime+"&status="+this.value+"&page="+this.page+"&size="+this.size
				getManaTealist(data).then((res) => {
					if (res.success) {
						_self.totalCount=res.data.totalCount
						if (_self.page == 1) {
							_self.submit = res.data.list
						} else {
							_self.submit = [..._self.submit, ...res.data.list]
						}
						uni.stopPullDownRefresh();
						uni.hideLoading()
					} else {
						uni.showToast({
							title: res.message
						})
					}
				}).catch((err) => {
					uni.showToast({
						title: err.message
					})
				});
			},
			
			//驳回
			disagree(id){
				this.agreestatu='0'
				var id=id
				if(this.value3==0){
					this.shenpiLinshi(id)
				}else if(this.value3==1){
					this.shenpiTeacher(id)
				}
			},
			//同意
			agree(id){
				this.agreestatu='1'
				var id=id
				if(this.value3==0){
					this.shenpiLinshi(id)
				}else if(this.value3==1){
					this.shenpiTeacher(id)
				}
			},
			// 审批临时人员
			shenpiLinshi(id){
				let _self = this
				this.$axios.defaults.headers.common['token'] = localStorage.getItem('token');
				this.$axios.defaults.headers.common['request_token'] = this.submitToken;
				this.$axios.defaults.headers.common['Content-Type'] ='application/json'
				var data={
					"id": id, //审批表id
					"adminMenuId": localStorage.getItem('adminMenuId'),//1管理员，0不是管理员
					"status": _self.agreestatu, //审批状态   1：同意 0：拒绝
					"adminId": localStorage.getItem('adminId'),//管理端id
					"remark": "" ,//审批备注
				}
				shenpiLinshi(data).then((res) => {
					if (res.success) {
						uni.showToast({
							title: '审批成功',
							icon: 'success',
							duration: 1500
						})
						setTimeout(function(){
							if(_self.value3==1){
								_self.page=1
								_self.getManageTeacher()
							}else if(_self.value3==0){
								_self.page=1
								_self.getManageLin()
							}
						},1500);
					} else {
						uni.showToast({
							title: res.message,
							duration: 1500
						})
					}
					
				}).catch((err) => {
					uni.showToast({
						title: err.message
					})
				});
			},
			// 审批教师换课
			shenpiTeacher(id){
				let _self = this
				this.$axios.defaults.headers.common['token'] = localStorage.getItem('token');
				this.$axios.defaults.headers.common['request_token'] = this.submitToken;
				this.$axios.defaults.headers.common['Content-Type'] ='application/json'
				var data={
					"id": id, //审批表id
					"adminMenuId": localStorage.getItem('adminMenuId'),//1管理员，0不是管理员
					"status": _self.agreestatu, //审批状态   1：同意 0：拒绝
					"adminId": localStorage.getItem('adminId'),//管理端id
					"remark": "" ,//审批备注
				}
				shenpiTeacher(data).then((res) => {
					if (res.success) {
						uni.showToast({
							title: '审批成功',
							icon: 'success',
							duration: 1500
						})
						setTimeout(function(){
							if(_self.value3==1){
								_self.page=1
								_self.getManageTeacher()
							}else if(_self.value3==0){
								_self.page=1
								_self.getManageLin()
							}
						},1500);
					} else {
						uni.showToast({
							title: res.message,
							duration: 1500
						})
					}
				}).catch((err) => {
					uni.showToast({
						title: err.message
					})
				});
			},
			// 提交记录
			//选择审批类型身份
			changeShen(e){
				this.value3=e
				if(this.value3==1){
					this.getManageTeacher()
				}else if(this.value3==0){
					this.getManageLin()
				}
			},
			// 选择审批状态
			changezhuang(e) {
			  this.value=e
			  if(this.value3==1){
			  	this.getManageTeacher()
			  }else if(this.value3==0){
			  	this.getManageLin()
			  }
			},
			//获得本周的开始时间：
			getStartDayOfWeek(time) {
				let now = new Date(time); // 当前日期
				let nowDayOfWeek = now.getDay(); // 今天本周的第几天
				let day = nowDayOfWeek || 7;
				let nowDay = now.getDate(); // 当前日
				let nowMonth = now.getMonth(); // 当前月
				return this.formatDate(new Date(now.getFullYear(), nowMonth, nowDay + 1 - day));
			},
			//获得本周的结束时间：
			getEndDayOfWeek(time) {
				let now = new Date(time); // 当前日期
				let nowDayOfWeek = now.getDay(); // 今天本周的第几天
				let day = nowDayOfWeek || 7;
				let nowDay = now.getDate(); // 当前日
				let nowMonth = now.getMonth(); // 当前月
				return this.formatDate(new Date(now.getFullYear(), nowMonth, nowDay + 7 - day));
			},
			//获得本月的开始时间：
			getStartDayOfMonth(time) {
				let now = new Date(time); // 当前日期
				let nowDay = now.getDate(); // 当前日
				let nowMonth = now.getMonth(); // 当前月
				return this.formatDate(new Date(now.getFullYear(), nowMonth, 1));
			},
			// 日期格式化
			formatDate(date) {
				let myyear = date.getFullYear();
				let mymonth = date.getMonth() + 1;
				let myweekday = date.getDate();
				if (mymonth < 10) {
					mymonth = '0' + mymonth;
				}
				if (myweekday < 10) {
					myweekday = '0' + myweekday;
				}
				return (myyear + '-' + mymonth + '-' + myweekday);
			},
			// 选择时间
			changetime(e) {
			    this.value2=e
				if(this.value2==0){
					this.startTime=this.getStartDayOfWeek(this.nowTime).slice(0, 10)+' 00:00:00'
					this.endTime=this.getEndDayOfWeek(this.nowTime).slice(0, 10)+' 23:59:59'
				}else if(this.value2==1){
					this.startTime=this.getStartDayOfMonth(this.nowTime).slice(0, 10)+' 00:00:00'
					this.endTime=this.nowTime+' 23:59:59'
				}else if(this.value2==2){
					this.startTime=''
					this.endTime=''
				}
				if(this.value3==1){
					this.getManageTeacher()
				}else if(this.value3==0){
					this.getManageLin()
				}
			},
			// 跳转到详情页面
			clickDetail(id,department){
				uni.navigateTo({
					url: "../liuchengManger/detail/teach_detail?id="+id+"&department="+department
				})
			},
			onReachBottom: function() {
				
				if (this.submit.length < this.totalCount) {
					wx.showLoading({
					   title: '加载中...',
					 })
					this.page = this.page + 1;
					if(this.value3==1){
						this.getManageTeacher()
					}else if(this.value3==0){
						this.getManageLin()
					}
				} else {
					uni.showToast({
						title: '已经到底了',
						icon: 'none'
					})
				}
			},
			onPullDownRefresh: function() {
				this.page = 1;
				if(this.value3==1){
					this.getManageTeacher()
				}else if(this.value3==0){
					this.getManageLin()
				}
			},
		}
	}
</script>

<style>
	@import url("./css/liu_manage.css");
</style>