<template>
	<view>
		<view style="display: flex;justify-content: center;margin: 30rpx;font-size:30rpx ;align-items: center;">
			<u-icon name="arrow-left-double" style="font-size: 40rpx;margin-right: 30rpx;" @click="previous"></u-icon>
			<div @click="show = true">
				{{"第"+weekNum+'周('+weeList[0]+"至"+weeList[weeList.length-1]+')' }}
			</div>
			<u-icon name="arrow-right-double" style="font-size: 40rpx;margin-left: 30rpx;" @click="next"></u-icon>
		</view>
		<u-divider></u-divider>
		<view class="">
			<uni-section v-for="(item, index) in list" :key="index" :title="item.deptName" type="line">
				<uni-card style="height: 210rpx;">
					<text class="uni-body">提交人：{{item.createBy}}</text>
					<br>
					<text class="uni-body">提交时间：{{item.createTime}}</text>
					<view style="display: flex;margin-top: 20rpx;  position: absolute;
	  left:50%;
	  transform: translateX(-50%);
" class="uni-group">
						<!-- v-if="nowLogin==item.createBy||viewRow" -->
						<u-button style="width:20rpx;" @click="handleView(item)" class="uni-button" size="small"
							type="warning">查看</u-button>
						<u-button style="width: 20rpx;margin-left: 20rpx;width:20rpx;" size="small"
							@click="handleUpdate(item)" class="uni-button" type="primary">修改</u-button>
						<u-button style="width: 20rpx;margin-left: 20rpx;" @click="handleDelete(item)"
							class="uni-button" size="small" type="error">删除</u-button>

					</view>
				</uni-card>
				<u-divider></u-divider>
			</uni-section>
			<!-- <uni-table ref="table" :loading="loading" border stripe emptyText="暂无更多数据">
				<uni-tr>
					<uni-th width="170rpx" align="center">提交人</uni-th>
					<uni-th width="240rpx" align="center">部门名称</uni-th>
					<uni-th width="270rpx" align="center">提交时间</uni-th>
					<uni-th align="center">设置</uni-th>
				</uni-tr>
				<uni-tr v-for="(item, index) in list" :key="index">
					<uni-td align="center">{{ item.createBy }}</uni-td>
					<uni-td align="center">
						<view class="name">{{ item.deptName }}</view>
					</uni-td>
					<uni-td align="center">{{ item.createTime }}</uni-td>
					<uni-td align="center">
						<view style="display: flex;" class="uni-group">
							<u-button v-if="nowLogin==item.createBy" size="small" @click="handleUpdate(item)"
								class="uni-button" type="primary">修改</u-button>
							<u-button v-if="nowLogin==item.createBy" style="margin-left: 10rpx;"
								@click="handleDelete(item)" class="uni-button" size="small" type="error">删除</u-button>
							<u-button v-if="viewRow" style="margin-left: 10rpx;width:20rpx;" @click="handleView(item)"
								class="uni-button" size="small" type="warning">查看</u-button>
						</view>
					</uni-td>
				</uni-tr>
			</uni-table> -->
			<!-- 	<view class="uni-pagination-box"><uni-pagination show-icon :page-size="pageSize" :current="pageCurrent"
					:total="total" @change="change" /></view> -->
			<u-modal :show="showModal" title="确认操作" content="您确定要删除该周报吗？" show-cancel-button cancel-text="取消"
				confirm-text="确定" @cancel="handleCancel" @confirm="handleConfirm"></u-modal>
		</view>
		<u-datetime-picker :show="show" v-model="today" mode="date" @confirm="confirm"
			@cancel="show=false"></u-datetime-picker>
	</view>
</template>


<script>
	import {
		getWeekInYear,
		getNowWeekList
	} from "@/utils/wjw.js"
	export default {
		data() {
			return {
				model: {
					pageNum: 1,
					pageSize: 5,
					periods: null
				},
				//加载状态
				status: 'loadmore',
				viewRow: false,
				hasMore: true, // 是否有更多数据
				showModal: false,
				nowLogin: '',
				searchVal: '',
				tableData: [],
				// 每页数据量
				pageSize: 10,
				// 当前页
				pageCurrent: 1,
				// 数据总量
				total: 0,
				loading: false,
				list: [], //人员列表
				nowWeekNum: null, //当前周数
				//当前周数
				weekNum: null,
				weeList: [], //周日期列表
				nowWeeList: [], //当前周日期列表
				xiugaiDay: null, //可修改日期
				today: null, //当前日期
				show: false, //日历展示
				xgType: true, //是否能修改
				day: null, //当前日期
				mounth: null, //当前月
				year: null, //当前年
				rowNow: null
			}
		},
		onLoad() {
			this.selectedIndexs = []
			this.list = []
			this.today = new Date().getTime()
			this.nowWeekNum = getWeekInYear(uni.$u.timeFormat(new Date().toLocaleDateString(), 'yyyy-mm-dd'))
			this.weekNum = getWeekInYear(uni.$u.timeFormat(new Date().toLocaleDateString(), 'yyyy-mm-dd'))
			this.weeList = getNowWeekList(uni.$u.timeFormat(new Date().toLocaleDateString(), 'yyyy-mm-dd'))
			this.nowWeeList = getNowWeekList(uni.$u.timeFormat(new Date().toLocaleDateString(), 'yyyy-mm-dd'))
			this.model.periods = this.weekNum
			this.getPeople()
			let roles = uni.getStorageSync('userInfo').roles
			this.nowLogin = uni.getStorageSync('userInfo').nickName
			console.log('roles', roles)
			for (let i in roles) {
				// this.nowLogin.push(roles[i].roleName)
				if (roles[i].roleKey == 'admin' || roles[i].roleKey == 'zhoubaoGly') {
					this.viewRow = true
				}
			}

			// this.getModifyDay()
		},
		onNavigationBarButtonTap() {
			uni.navigateTo({
				url: `/pages/weeklySubmit/weeklySubmit?title=周报新增`
			})
		},
		onShow() {
			uni.$once('query', (query) => {
				if (query.a === 1) {
					this.list = []
					this.model.pageNum = 1
					this.model.pageSize = 5
					this.getPeople()
				} else if (query.a == 2) {
					this.list = []
					this.model.pageNum = 1
					this.model.pageSize = 5
					this.getPeople()
				}
			});
		},

		onPullDownRefresh() {
			this.model.pageNum = 1
			this.list = []
			this.getPeople()
		},
		onReachBottom() {
			console.log('total', this.total)
			if (this.model.pageNum * this.model.pageSize >= this.total) {
				this.status = 'nomore'
			} else {
				this.model.pageNum++
				this.getPeople()
			}
		},
		methods: {
			handleUpdate(row) {
				uni.navigateTo({
					url: `/pages/weeklySubmit/weeklySubmit?title=周报修改&id=` + row.id
				})
			},
			handleView(row) {
				uni.navigateTo({
					url: `/pages/weeklySubmit/weeklySubmit?title=周报查看&showNo=no&id=` + row.id
				})
			},
			handleConfirm() {
				uni.$u.http.delete('/offcial/weekly/' + this.rowNow.id).then((res) => {
					uni.showToast({
						title: "删除成功",
						icon: "none"
					})
					this.showModal = false
					setTimeout(() => {
						this.list = []
						this.model.pageNum = 1
						this.model.pageSize = 5
						this.getPeople()
					}, 200)

				})
			},
			handleCancel() {
				this.showModal = false
			},
			handleDelete(row) {
				console.log('row', row);
				this.rowNow = row;
				this.showModal = true;
				// const _ids = row.id || ids.value;
				// proxy.$modal
				//   .confirm("是否确认删除该周报？")
				//   .then(function () {
				//     return delWeekly(_ids);
				//   })
				//   .then(() => {
				//     getList();
				//     proxy.$modal.msgSuccess("删除成功");
				//   })
				//   .catch(() => {});
			},

			// 分页触发
			change(e) {
				this.$refs.table.clearSelection()
				this.selectedIndexs.length = 0
				this.getPeople(e.current)
			},


			//点击上一周
			previous() {
				uni.showLoading({
					title: "加载中...",
					mask: true
				})
				this.today = this.today - 7 * 86400000
				this.weekNum = getWeekInYear(uni.$u.timeFormat(this.today, 'yyyy-mm-dd'))
				this.weeList = getNowWeekList(uni.$u.timeFormat(this.today, 'yyyy-mm-dd'))
				this.model.periods = this.weekNum
				this.getPeople()
			},
			//点击下一周
			next() {
				this.today = this.today + 7 * 86400000
				this.weekNum = getWeekInYear(uni.$u.timeFormat(this.today, 'yyyy-mm-dd'))
				this.weeList = getNowWeekList(uni.$u.timeFormat(this.today, 'yyyy-mm-dd'))
				this.model.periods = this.weekNum
				this.getPeople()
			},
			//日历确认
			confirm(e) {
				this.today = e.value
				this.weekNum = getWeekInYear(uni.$u.timeFormat(this.today, 'yyyy-mm-dd'))
				this.weeList = getNowWeekList(uni.$u.timeFormat(this.today, 'yyyy-mm-dd'))
				this.show = false
				this.getPeople()
			},
			//获取列表人员
			getPeople() {
				let that = this
				uni.showLoading({
					title: "加载中...",
					mask: true
				})
				// {
				// 	periods: this.weekNum,
				// 	pageSize: this.pageSize,
				// 	pageNum: pageCurrent,
				// }
				uni.$u.http.get(`/offcial/weekly/list`, {
					params: that.model
				}).then(res => {
					uni.hideLoading()
					that.total = res.total
					that.list = [...that.list, ...res.rows]
					if (res.total < this.model.pageSize) {
						that.status = "nomore"
					} else {
						that.status = "loadmore"
					}
					if (that.model.pageNum == 1) {
						// that.total = Math.ceil(res.total / that.model.pageSize)
						that.total = res.total
						uni.stopPullDownRefresh()
					}
				}).catch((res) => {
					uni.hideLoading()
					uni.showToast({
						title: res.msg,
						icon: "none"
					})
				})
			},

		}
	}
</script>

<style>
	.uni-section .uni-section-header[data-v-f7ca1098] {
		position: relative;
		display: flex;
		flex-direction: row;
		align-items: center;
		padding: 2px 10px;
		font-weight: normal;
	}

	.uni-card {
		/* margin: 0px 15px !important */
	}

	.uni-section__content-title {
		font-size: 16px !important;
	}
</style>