<template>
	<!-- 禁止滚动穿透 -->
	<page-meta :page-style="'overflow:'+(show?'hidden':'visible')"></page-meta>
	<view class="main">
		<view class="one">
			<view class="one_1">
				<input type="text" v-model="searchInfo.title" @input="toInput" placeholder="搜索文件标题">
			</view>
			<view class="one_2">
				<button size="mini" class="button" type="primary" @click="toAdd">添加</button>
			</view>
		</view>
		<view class="two">
			<scroll-view scroll-y="true" class="scroll-view" @scrolltolower="toPage" @scroll="toScroll">
				<view class="list-scroll-view">
					<view class="list" v-for="(item, index) in list" :key="index" @tap="toView(item)">
						<view class="title">{{item.title||'暂无标题'}}</view>
						<view class="button">
							<button class="warning" v-if="user.role_type == '0' ||  user.role_type == '1'" size="mini"
								type="warn" @tap.stop="toEdit(item)">修改</button>
							<button class="danger" v-if="user.role_type == '0' ||  user.role_type == '1'" size="mini"
								type="warn" @tap.stop="toDel(item)">删除</button>
						</view>
					</view>
				</view>
				<view class="is_bottom" v-if="is_bottom">
					<text>没数据到底了！</text>
				</view>
			</scroll-view>
		</view>
		<uni-popup ref="popup" background-color="#fff" type="center" :is-mask-click="false" @change="change">
			<view class="popup">
				<view class="close">
					<text>信息管理</text>
					<uni-icons @tap="toClose" type="close" size="20"></uni-icons>
				</view>
				<view class="info_1">
					<view class="info">
						<view class="title">{{info.title||'暂无标题'}}</view>
						<view @tap.stop="toFile(info.file)" class="file" v-if="info.file&&info.file.length>0">
							文件：{{info.file[0].name||'暂无'}}</view>
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				user: {},
				searchInfo: {},
				list: [],
				total: 0,
				skip: 0,
				limit: 10,
				page: 0,
				info: {},
				// 数据是否触底
				is_bottom: false,
				scrollTop: 0,
				// 禁止滚动穿透
				show: false
			}
		},
		onShow: async function() {
			const that = this;
			that.clearPage();
			await that.searchToken();
			await that.search();
		},
		onPullDownRefresh: async function() {
			const that = this;
			that.clearPage();
			await that.search();
			uni.stopPullDownRefresh();
		},
		methods: {
			// 禁止滚动穿透
			change(e) {
				const that = this;
				that.show = e.show
			},
			searchToken() {
				const that = this;
				try {
					const res = uni.getStorageSync('token');
					if (res) {
						const user = that.$jwt(res);
						that.$set(that, `user`, user);
					} else {
						uni.navigateTo({
							url: `/pages/login/index`
						})
					}
				} catch (e) {
					uni.showToast({
						title: err.errmsg,
						icon: 'error',
						duration: 2000
					});
				}
			},
			async search() {
				const that = this;
				let user = that.user;
				let info = {
					skip: that.skip,
					limit: that.limit,
				}
				const res = await that.$api(`/policyfile`, 'GET', {
					...info,
					...that.searchInfo
				}, 'freeLabel')
				if (res.errcode == '0') {
					let list = [...that.list, ...res.data];
					that.$set(that, `list`, list)
					that.$set(that, `total`, res.total)
				} else {
					uni.showToast({
						title: res.errmsg,
					});
				}
			},
			// 输入框
			toInput(e) {
				const that = this;
				if (that.searchInfo.title) that.$set(that.searchInfo, `title`, e.detail.value)
				else that.$set(that, `searchInfo`, {})
				that.clearPage();
				that.search();
			},
			// 添加
			toAdd() {
				uni.navigateTo({
					url: `/pagesBasic/policyfile/detail`
				})
			},
			// 查看
			toView(item) {
				const that = this;
				that.$set(that, `info`, item)
				that.$refs.popup.open()
			},
			// 查看文件
			toFile(item) {
				const that = this;
				let serverUrl = that.$config.serverFile;
				let url = item[0].url
				// 判断前面是否有http
				const http = url.lastIndexOf("http");
				if (http == -1) url = serverUrl + url
				//获取最后一个.的位置
				const arr = url.lastIndexOf(".");
				//获取后缀
				const typeName = url.substr(arr + 1);
				//支持预览的文件类型
				//微信小程序
				let fileType = ['doc', 'xls', 'ppt', 'pdf', 'docx', 'xlsx', 'pptx'];
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				//下载文件资源到本地
				uni.downloadFile({
					url: url,
					success: function(res) {
						uni.hideLoading();
						var filePath = res.tempFilePath;
						if (!fileType.includes(typeName)) {
							return false;
						}
						uni.showLoading({
							title: '正在打开',
							mask: true
						})
						// 新开页面打开文档，支持格式：doc, xls, ppt, pdf, docx, xlsx, pptx。
						uni.openDocument({
							filePath: filePath,
							fileType: typeName, // 文件类型，指定文件类型打开文件，有效值 doc, xls, ppt, pdf, docx, xlsx, pptx 
							success: res => {
								uni.hideLoading();
								console.log('打开文档成功', res);
							},
							fail: openError => {
								uni.hideLoading();
								console.log('fail:' + JSON.stringify(openError));
							}
						});
					},
					fail: function(err) {
						uni.hideLoading();
						console.log('fail:' + JSON.stringify(err));
					}
				});
			},
			// 修改
			toEdit(item) {
				uni.navigateTo({
					url: `/pagesBasic/policyfile/detail?id=${item._id||item.id}`
				})
			},
			// 删除
			async toDel(item) {
				const that = this;
				uni.showModal({
					title: '提示',
					content: '您确定删除该数据吗?',
					success: async function(res) {
						if (res.confirm) {
							const res = await that.$api(`/policyfile/${item._id||item.id}`, 'DELETE', {},
								'freeLabel')
							if (res.errcode == 0) {
								that.clearPage();
								that.search();
							} else {
								uni.showToast({
									title: res.errmsg,
									icon: 'none'
								})
							}
						}
					}
				});
			},
			// 关闭弹框
			toClose() {
				const that = this;
				that.$refs.popup.close();
			},
			// 分页
			toPage(e) {
				const that = this;
				let list = that.list;
				let limit = that.limit;
				if (that.total > list.length) {
					uni.showLoading({
						title: '加载中',
						mask: true
					})
					let page = that.page + 1;
					that.$set(that, `page`, page)
					let skip = page * limit;
					that.$set(that, `skip`, skip)
					that.search();
					uni.hideLoading();
				} else that.$set(that, `is_bottom`, true)
			},
			toScroll(e) {
				const that = this;
				let up = that.scrollTop;
				that.$set(that, `scrollTop`, e.detail.scrollTop);
				let num = Math.sign(up - e.detail.scrollTop);
				if (num == 1) that.$set(that, `is_bottom`, false);
			},
			// 清空列表
			clearPage() {
				const that = this;
				that.$set(that, `list`, [])
				that.$set(that, `skip`, 0)
				that.$set(that, `limit`, 10)
				that.$set(that, `page`, 0)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.main {
		display: flex;
		flex-direction: column;
		width: 100vw;
		height: 100vh;

		.one {
			display: flex;
			justify-content: center;
			align-items: center;
			padding: 2vw;

			.one_1 {
				padding: 0 2vw;
				width: 75vw;

				input {
					padding: 2vw;
					background-color: var(--f1Color);
					font-size: var(--font14Size);
					border-radius: 5px;
				}
			}

			.one_2 {
				padding: 2vw 0;

				.button {
					background-color: var(--f3CColor);
					color: var(--mainColor);
				}
			}
		}

		.two {
			position: relative;
			flex-grow: 1;
			background-color: var(--f9Color);

			.list:first-child {
				margin: 2vw;
			}

			.list {
				background-color: var(--mainColor);
				border: 1px solid var(--f5Color);
				padding: 2vw;
				margin: 0 2vw 2vw 2vw;
				border-radius: 5px;

				.title {
					font-size: var(--font14Size);
					font-weight: bold;
					padding: 2vw 0;
				}


				.button {
					margin: 2vw 0 0 0;
					text-align: center;

					.warning {
						background: var(--f3CColor);
					}

					.danger {
						background: var(--fF0Color);
					}

					button {
						margin: 0 1vw 0 0;
					}
				}
			}
		}

		.uni-popup {
			z-index: 9999 !important;
		}

		.popup {
			display: flex;
			flex-direction: column;
			width: 90vw;
			height: 20vh;
			background-color: var(--f9Color);

			.close {
				display: flex;
				justify-content: space-between;
				padding: 2vw;

				text:first-child {
					font-size: var(--font16Size);
					font-weight: bold;
				}
			}

			.info_1 {
				position: relative;
				display: flex;
				flex-direction: column;
				max-height: 15vh;
				padding: 2vw;

				.info {
					text-align: center;

					.title {
						font-size: var(--font14Size);
						font-weight: bold;
						padding: 0 0 2vw 0;
					}

					.file {
						font-size: var(--font12Size);
						color: var(--f85Color);
					}
				}
			}
		}
	}

	.scroll-view {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;

		.list-scroll-view {
			display: flex;
			flex-direction: column;
		}
	}

	.is_bottom {
		width: 100%;
		text-align: center;

		text {
			padding: 2vw 0;
			display: inline-block;
			color: var(--f85Color);
			font-size: var(--font14Size);
		}
	}
</style>