<template>
	<view>
		<u-navbar title="订餐上报"></u-navbar>
		<view class="head">
			<view class="department">
				订餐部门：{{orgName}}
			</view>
			<view class="date" @click="show = true">
				{{orderFoodDate}}<u-icon name="arrow-down" size="24"></u-icon>
			</view>
		</view>

		<u-picker mode="time" v-model="show" :params="params" :defaultTime="defaultTime" @confirm="confirm">
		</u-picker>

		<!-- 上报记录 -->
		<view class="records">
			<!-- <view class="record-item">
				<view class="date">
					{{today}}
				</view>
				<view class="number report" @click="maskShow">
					{{report}} <u-icon name="arrow-right" size="20"></u-icon>
				</view>
			</view> -->
			<view class="record-item" v-for="(item,index) in orderFoodList" :key="index">
				<view class="date">
					{{item.date}}
				</view>
				<view class="number report" @click="maskShow(item)" v-if="item.date == today">
					{{report}} <u-icon name="arrow-right" size="20"></u-icon>
				</view>
				<view class="number" v-else>
					订餐{{item.peopleNum}}人
				</view>
			</view>
		</view>
		<!-- 遮罩层 -->
		<u-mask :show="show1" @click="show1 = false" :zoom='false'>
			<view class="warp">
				<view class="rect" @tap.stop>
					<view class="title">
						<u-icon name="close" color="#777777" @click="close"></u-icon>
						订餐人数
					</view>
					<u-input v-model="name1" :clearable="false" type="number" placeholder="请填写订餐人数"> </u-input>
					<view class="unit">
						人
					</view>
					<u-button class="btn" @click="submit">保存</u-button>
				</view>
			</view>
		</u-mask>
	</view>
</template>

<script>
	import * as staffHomeAPI from '@/apis/pagejs/staffHome.js'

	export default {
		data() {
			return {
				dateShow: false,
				show1: false,
				report: '点击上报',
				name1: '',
				defaultTime: '',
				params: {
					year: true,
					month: true,
					day: false,
					hour: false,
					minute: false,
					second: false,
					timestamp: true
				},
				show: false,
				orderFoodDate: '',
				orderFoodList: [],
				yearMonth: '',
				today: '',
				orgName: '',
				orderFoodId: ''
			}
		},
		onReady() {
			var date = new Date();
			var year = date.getFullYear();
			var month = date.getMonth() + 1;
			var day = date.getDate();
			var week = "星期" + "日一二三四五六".charAt(new Date().getDay())
			if (month >= 1 && month <= 9) {
				month = '0' + month;
			}
			this.defaultTime = year + '-' + month;
			this.yearMonth = year + '-' + month;
			this.orderFoodDate = year + '年' + parseInt(month) + '月';
			this.today = month + '月' + day + '日' + ' ' + week;

			this.getOrderFoodList();
		},
		methods: {
			getOrderFoodList() {
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				staffHomeAPI.monthList({
					yearMonth: this.yearMonth
				}).then((response) => {
					uni.hideLoading();
					var list = response.data.mapList;
					if(this.yearMonth == this.defaultTime) {
						if(list.length == 0) {
							list.unshift({
								id: '',
								date: this.today,
								peopleNum: 0
							});
						} else if(list[0].date != this.today) {
							list.unshift({
								id: '',
								date: this.today,
								peopleNum: 0
							});
						} else {
							this.report = "订餐" + list[0].peopleNum + "人";
							this.name1 = list[0].peopleNum;
						}
					}
					this.orderFoodList = list;
					this.orgName = response.data.orgName;
				}).catch(error => {
					uni.showToast({
						title: error,
						icon: "none"
					})
				})
			},
			confirm(params) {
				this.orderFoodDate = params.year + '年' + parseInt(params.month) + '月';
				this.yearMonth = params.year + '-' + params.month;
				this.getOrderFoodList();
			},
			maskShow(item) {
				this.show1 = true;
				this.orderFoodId = item.id;
			},
			close() {
				this.show1 = false;
			},
			submit() {
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				if(this.orderFoodId.length == 0) {
					staffHomeAPI.add({
						peopleNum: this.name1
					}).then((response) => {
						uni.hideLoading();
						this.show1 = false;
						this.getOrderFoodList();
					}).catch(error => {
						uni.showToast({
							title: error,
							icon: "none"
						})
					})
				} else {
					staffHomeAPI.update({
						id: this.orderFoodId,
						peopleNum: this.name1
					}).then((response) => {
						uni.hideLoading();
						this.show1 = false;
						this.getOrderFoodList();
					}).catch(error => {
						uni.showToast({
							title: error,
							icon: "none"
						})
					})
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.head {
		background: linear-gradient(89.02deg, rgba(18, 114, 238, 1) 1.34%, rgba(102, 169, 255, 1) 99.5%);
		padding: 40rpx 32rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		color: rgba(255, 255, 255, 1);

		.department {

			font-size: 36rpx;
			font-family: 'Medium';
		}

		.date {
			width: 218rpx;
			height: 72rpx;
			line-height: 72rpx;
			text-align: center;
			border-radius: 8px;
			border: 1px solid rgba(255, 255, 255, 1);
			position: relative;

			/deep/.uicon-arrow-down {
				margin-left: 8rpx;
			}

			.date-box {
				background-color: #fff;
				z-index: 999;
				width: 180rpx;
				box-shadow: 0px 0px 2px #999999;
				position: absolute;
				top: 70rpx;
				left: 0px;

				.options-item {
					line-height: 60rpx;
					padding-left: 16rpx;
					background-color: #fff;
					color: #101010;
					text-align: left;
				}

			}
		}
	}

	// 上报记录
	.records {
		background-color: #fff;

		.record-item {
			font-family: 'PingFang Regular';
			padding: 0 24rpx;
			line-height: 88rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			color: rgba(16, 16, 16, 1);
			border-bottom: 1px solid rgba(232, 232, 232, 1);

		}

		.report {
			color: rgba(18, 114, 238, 1);

			/deep/.uicon-arrow-right {
				margin-left: 8rpx;
			}
		}
	}

	.warp {

		position: fixed;
		left: 0;
		bottom: 0;
		width: 100%;

		/deep/.uicon-close {
			position: absolute;
			left: 32rpx;
			top: 6rpx !important;

		}

		;

		.title {
			color: rgba(16, 16, 16, 100);
			line-height: 48rpx;
			color: rgba(16, 16, 16, 1);
			font-size: 36rpx;
			text-align: center;
			position: relative;
			font-weight: bold
		}
	}

	.rect {
		width: 100%;
		height: 100%;
		background-color: #fff;
		padding: 32rpx 0;



		/deep/.u-input {
			width: 87.2%;
			border-radius: 8px;
			background-color: rgba(229, 231, 234, 100);
			margin: 40rpx auto 0;
			height: 96rpx;
			line-height: 96rpx !important;
			padding: 0 16rpx;
			position: relative;

		}

		/deep/.u-input__input {
			height: 96rpx !important;
			line-height: 96rpx !important;
			text-indent: 24rpx;
		}

		.unit {
			width: 20px;
			height: 20px;
			position: absolute;
			top: 140rpx;
			right: 60rpx;
			color: rgba(16, 16, 16, 1);
			font-size: 36rpx;

		}



		.u-btn {
			width: 89.2%;
			color: rgba(255, 255, 255, 100);
			font-size: 36rpx;
			border-radius: 8px;
			margin-top: 48rpx;
			z-index: 99999;
			background-color: rgba(31, 74, 153, 1);
		}
	}
</style>