<template>
	<div class="app-container">
		<el-row>
			<el-col :span="24">
				<div class="cell">
					<div class="cell-title">
						<div class="title-left">
							<div class="title">招商资讯</div>
						</div>
					</div>
					<div class="cell-body">
						<div class="controls">
							<el-form :inline="true" :model="formInline" class="demo-form-inline">
								<el-form-item label="">
									<el-input v-model="formInline.keyword" style="width: 150px;" prefix-icon="el-icon-search" clearable
										placeholder="请输入关键字"></el-input>
								</el-form-item>
								<el-form-item>
									<el-button type="primary" class="btn-search" @click="btn_search">查询</el-button>
								</el-form-item>
								<el-form-item label="发布时间">
									<el-date-picker v-model="formInline.search_datatime" type="daterange" align="right" start-placeholder="开始日期"
										end-placeholder="结束日期" format="yyyy-MM-dd" value-format="yyyy-MM-dd" @change="btn_search">
									</el-date-picker>
								</el-form-item>
								<el-form-item label="是否置顶">
									<el-select v-model="formInline.is_top" clearable placeholder="请选择" style="width: 120px;" @change="btn_search">
										<el-option v-for="item in formInline.options" :key="item.is_top" :label="item.label" :value="item.is_top">
										</el-option>
									</el-select>
								</el-form-item>
							</el-form>
							<div class="btns">
								<el-button type="primary" class="btn-article-del" @click="article_del">删除</el-button>
								<el-button type="primary" class="btn-update-get-article" @click="article_update">更新</el-button>
								<el-button type="primary" class="btn-update-get-article" @click="article_get">获取文章</el-button>
							</div>
						</div>
						<el-table ref="myTable" :data="tableData" height="564" style="width: 100%" :cell-style="cell_style"
							:header-cell-style="header_cell_style" @selection-change="handle_table_change">
							<el-table-column type="selection" width="50"></el-table-column>
							<el-table-column label="标题" align="center">
								<template slot-scope="scope">
									<span><el-link type="primary" :underline="false"
											@click="show_article_detail(scope.row)">{{ scope.row.title }}</el-link></span>
								</template>
							</el-table-column>
							<el-table-column label="封面图" align="center">
								<template slot-scope="scope">
									<span v-if="scope.row.first_img != ''">
										<viewer>
											<img style="height: 100px; cursor: pointer;" :src="scope.row.first_img" />
										</viewer>
									</span>
									<span v-else>
										<el-image>
											<div slot="error" class="image-slot"><i class="el-icon-picture-outline"></i></div>
										</el-image>
									</span>
								</template>
							</el-table-column>
							<el-table-column label="内容" width="600">
								<template slot-scope="scope">
									<div class="content-style" v-html="removeEmptyTags(scope.row.content)"></div>
								</template>
							</el-table-column>
							<el-table-column label="发布时间" align="center" width="100">
								<template slot-scope="scope">
									<span>{{ scope.row.update_time }}</span>
								</template>
							</el-table-column>
							<el-table-column label="是否置顶" align="center" width="90">
								<template slot-scope="scope">
									<span v-if="scope.row.is_top == 1" style="color: #0f0;">已置顶</span>
									<span v-else style="color: #f00;">未置顶</span>
								</template>
							</el-table-column>
							<el-table-column label="操作" width="130" align="center">
								<template slot-scope="scope">
									<el-button v-if="scope.row.is_top == 0" @click.native.prevent="handle_top(scope.row.id, 1)" type="text"
										size="small">置顶</el-button>
									<el-button v-else @click.native.prevent="handle_top(scope.row.id, 0)" type="text" size="small">取消置顶</el-button>
								</template>
							</el-table-column>
						</el-table>
						<div class="pagination-table">
							<el-pagination @current-change="currentPageChange" :current-page.sync="pagination.currentPage" :page-size="pagination.pageSize"
								layout="prev, pager, next, jumper" :total="pagination.total">
							</el-pagination>
						</div>
					</div>
				</div>
			</el-col>
		</el-row>
		<el-dialog custom-class="dialog-have-no-title" :visible.sync="dialog_del_article" :close-on-click-modal="false" :close-on-press-escape="false"
			:show-close="false">
			<div slot="" class="dialog-have-no-title-body">
				<img src="../../icons/serveAC/del_warning.png" alt="">
				<div class="dialog-have-no-title-body-txt">删除后的文章将不在民宿小程序<br>上进行展示，确定删除吗？</div>
			</div>
			<div slot="footer" class="dialog-footer">
				<el-button class="add-user-cancel-btn" @click="dialog_del_article = false"> 取消 </el-button>
				<el-button class="add-user-confirm-btn" @click="handle_del_article"> 确定 </el-button>
			</div>
		</el-dialog>
		<el-dialog custom-class="dialog-have-no-title" :visible.sync="dialog_update_article" :close-on-click-modal="false" :close-on-press-escape="false"
			:show-close="false">
			<div slot="" class="dialog-have-no-title-body">
				<img src="../../icons/serveAC/del_warning.png" alt="">
				<div class="dialog-have-no-title-body-txt">确定对选中的文章进行更新吗？</div>
			</div>
			<div slot="footer" class="dialog-footer">
				<el-button class="add-user-cancel-btn" @click="dialog_update_article = false"> 取消 </el-button>
				<el-button class="add-user-confirm-btn" @click="handle_update_article"> 确定 </el-button>
			</div>
		</el-dialog>
		<!-- 获取文章 -->
		<el-dialog custom-class="dialog-have-no-title" :visible.sync="dialog_get_article" :close-on-click-modal="false" :close-on-press-escape="false"
			:show-close="false">
			<div slot="" class="dialog-have-no-title-body">
				<img src="../../icons/serveAC/del_warning.png" alt="">
				<div class="dialog-have-no-title-body-txt">确定从公众号获取最新文章数据吗？</div>
			</div>
			<div slot="footer" class="dialog-footer">
				<el-button class="add-user-cancel-btn" @click="dialog_get_article = false"> 取消 </el-button>
				<el-button :class="dialog_get_article_btn_disable?'':'add-user-confirm-btn'" :loading="dialog_get_article_btn_disable"
					@click="handle_get_article" :disabled="dialog_get_article_btn_disable"> 确定 </el-button>
			</div>
		</el-dialog>
		<!-- 资讯详情 -->
		<el-dialog custom-class="dialog-have-title" :visible.sync="dialog_show_article" title="资讯详情" width="50%">
			<div>
				<div style="display: flex; justify-content: space-around;">
					<div style="width: 75%;">
						<h3 style="text-align: center;" v-if="article_detail.is_top == 1">{{ article_detail.title }}<span style="color: #f00;">【已置顶】</span></h3>
						<h3 style="text-align: center;" v-else>{{ article_detail.title }}</h3>
						<p style="text-align: center;">作者：{{ article_detail.author }}</p>
						<p style="text-align: center;">时间：{{ article_detail.time }}</p>
					</div>
					<div style="width: 25%;">
						<viewer>
							<img :src="article_detail.first_img" style="height: 100px;" />
						</viewer>
					</div>
				</div>
				<div v-html="sanitizeHtml(article_detail.content)" class="show-article"></div>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		getTableData,
		getArticle,
		updateArticle,
		delArticle,
		topArticle
	} from '@/api/businessInfo';
	import {
		isEmpty
	} from '@/js/common';

	export default {
		data() {
			return {
				currentUserLevel: this.$store.state.user.level, // 当前用户的权限
				dialogImageUrl: '',
				formLabelWidth: '120px',
				sumAccount: '', // 总金额
				// 表格单元格样式
				cell_style: {
					color: '#1A202B',
					'font-size': '14px',
					'font-family': 'Microsoft YaHei-3970(82674968)'
				},
				// 表格头部样式
				header_cell_style: {
					background: '#E6ECFE',
					color: '#1A202B',
					'font-size': '16px',
					'font-family': 'Microsoft YaHei-3970(82674968)'
				},
				// 查询数据
				formInline: {
					options: [{
						is_top: 0,
						label: '否'
					}, {
						is_top: 1,
						label: '是'
					}],
					is_top: '',
					search_datatime: '',
					keyword: ''
				},
				tableData: [],
				cond_data: {},
				// 分页参数
				pagination: {
					currentPage: 1,
					pageSize: 4,
					total: 0
				},
				// 文章删除、更新、获取的参数
				dialog_del_article: false,
				del_ids: [],
				dialog_update_article: false,
				update_ids: [],
				dialog_get_article: false,
				dialog_get_article_btn_disable: false,
				dialog_show_article: false,
				article_detail: {
					title: '',
					author: '',
					time: '',
					content: '',
					is_top: '',
					first_img: ''
				}
			}
		},
		created() {
			// 检测权限
			if (this.currentUserLevel !== 2) {
				this.$router.replace({
					path: '/data'
				})
				return
			}
			// 获取消费记录,表格数据
			this.get_table_data('list')
		},
		mounted() {
			document.getElementsByClassName("el-pagination__jump")[0].childNodes[0].nodeValue = "跳至";
		},
		methods: {
			sanitizeHtml(html) {
				return html.replace('data-src', 'src');
			},
			// 显示文章详情
			show_article_detail(row) {
				// console.log(row);
				this.article_detail.title = row.title
				this.article_detail.author = row.author
				this.article_detail.content = row.content
				this.article_detail.time = row.update_time
				this.article_detail.is_top = row.is_top
				this.article_detail.first_img = row.first_img

				this.dialog_show_article = true
			},
			// 置顶/取消置顶
			handle_top(id, param) {
				// console.log(id, param);
				var that = this
				var data = [id, param]
				topArticle(data).then((res) => {
					// console.log(res);
					if (res.code === 200) {
						// 更新列表
						that.get_table_data('list')
						that.$message.success(res.message)
					} else {
						that.$message.warning(res.message)
					}
				}).catch((err) => {
					that.$message.error(err.message)
				});
			},
			// 获取选择的文章ids
			handle_table_change(selection) {
				// console.log(selection);
				// 保存要删除和更新的ids
				this.del_ids = []
				this.update_ids = []
				selection.forEach(row => {
					this.del_ids.push(row.id)
					this.update_ids.push(row.id)
				});
			},
			// 删除文章
			handle_del_article() {
				var that = this
				delArticle(this.del_ids).then((res) => {
					// console.log(res);
					if (res.code === 200) {
						// 更新列表
						that.get_table_data('list')
						that.dialog_del_article = false
						that.$message.success(res.message)
					} else {
						that.dialog_del_article = false
						that.$message.warning(res.message)
					}
				}).catch((err) => {
					that.$message.error(err.message)
				});
			},
			// 更新文章
			handle_update_article() {
				var that = this
				updateArticle(this.update_ids).then((res) => {
					// console.log(res);
					if (res.code === 200) {
						// 更新列表
						that.get_table_data('list')
						that.dialog_update_article = false
						that.$message.success(res.message)
					} else {
						that.dialog_update_article = false
						that.$message.warning(res.message)
					}
				}).catch((err) => {
					that.$message.error(err.message)
				});
			},
			// 获取文章
			handle_get_article() {
				var that = this;
				this.dialog_get_article_btn_disable = true;
				getArticle().then((res) => {
					// console.log(res);
					if (res.code === 200) {
						// 更新列表
						that.get_table_data('list')
						that.dialog_get_article = false
						that.$message.success(res.message)
						that.dialog_get_article_btn_disable = false;
					} else {
						that.dialog_get_article = false
						that.$message.warning(res.message)
						that.dialog_get_article_btn_disable = false;
					}
				}).catch((err) => {
					that.$message.error(err.message);
					that.dialog_get_article_btn_disable = false;
				});
			},
			// 移除文章内容中的html标签
			removeEmptyTags(htmlString) {
				// 确保htmlString不是空的
				// if (!htmlString) {
				// 	console.error('HTML string is empty or null');
				// 	return '';
				// }

				// 创建一个新的DOM元素，用于解析HTML字符串
				const parser = new DOMParser();
				const doc = parser.parseFromString(htmlString, 'text/html');

				// 检查body元素是否存在
				// if (!doc.body) {
				// 	console.error('Parsed document does not have a body element');
				// 	return '';
				// }

				// 递归函数，用于深度优先搜索并处理节点
				function cleanNode(node) {
					// 遍历子节点
					Array.from(node.childNodes).forEach(child => {
						if (child.nodeType === Node.ELEMENT_NODE) {
							// 如果是元素节点，递归调用cleanNode
							cleanNode(child);
						} else if (child.nodeType === Node.TEXT_NODE && !child.nodeValue.trim()) {
							// 如果是文本节点且内容为空，则移除
							node.removeChild(child);
						}
					});

					// 检查是否包含特定文本内容，如果包含，则删除该节点
					if (node.textContent.trim() === '更多精彩内容请关注我们' || node.textContent.trim() === 'END') {
						node.parentNode.removeChild(node);
					}

					// 如果当前节点没有子节点，则移除当前节点
					if (node.childNodes.length === 0) {
						node.parentNode.removeChild(node);
					}
				}

				// 从body开始清理
				cleanNode(doc.body);

				// 返回修改后的HTML字符串
				return doc.body.innerHTML;
			},
			// 获取文章
			article_get() {
				this.dialog_get_article = true
			},
			// 更新文章
			article_update() {
				if (isEmpty(this.update_ids)) {
					this.$message.warning('请选择要【更新】的文章！')
					return
				}
				this.dialog_update_article = true
			},
			// 删除文章
			article_del() {
				if (isEmpty(this.del_ids)) {
					this.$message.warning('请选择要【删除】的文章！')
					return
				}
				this.dialog_del_article = true
			},
			/**
			 * 获取消费记录,表格数据
			 */
			get_table_data(param) {
				this.get_condtion();

				let data = this.cond_data;
				if (param == 'search') {
					data.page = 1
					this.pagination.currentPage = 1
				}

				var that = this
				// console.log(data);
				getTableData(data).then((res) => {
					// console.log(res);
					if (res.code === 200) {
						that.pagination.total = res.total
						that.tableData = res.rows
					} else {
						that.tableData = []
						that.$message.warning('没有符合条件的数据！')
					}
				}).catch((err) => {
					// console.log(err);
					that.$message.error(err.message)
				});
			},
			/**
			 * 查询条件
			 */
			get_condtion() {
				let data = {
					page: this.pagination.currentPage,
					rows: this.pagination.pageSize
				}

				if (this.formInline.search_datatime !== '') {
					data.search_datatime = this.formInline.search_datatime
				}

				if (this.formInline.keyword !== '') {
					data.keyword = this.formInline.keyword
				}

				if (this.formInline.is_top !== '') {
					data.is_top = this.formInline.is_top
				}

				this.cond_data = data;
			},
			/**
			 * 搜索
			 */
			btn_search() {
				this.get_table_data('search')
			},
			/**
			 * currentPage 改变时会触发
			 * @param {Object} val
			 */
			currentPageChange(val) {
				this.pagination.currentPage = val
				this.get_table_data('pages')
			}
		}
	}
</script>

<style lang="scss" scoped>
	.app-container {
		background-color: #EFF2F7;
		padding: 10px;

		.el-row {

			.el-col {
				padding: 10px;

				.cell {
					padding: 30px;
					border-radius: 10px;
					background-color: #FFFFFF;
					box-shadow: 0px 3px 21px 0px rgba(60, 108, 254, 0.16);

					.cell-title {
						display: flex;
						justify-content: space-between;
						align-items: center;
						margin-bottom: 30px;
						padding-bottom: 30px;
						border-bottom: 1px solid #CCCCCC;

						.title-left {
							display: flex;
							align-items: center;

							.title {
								font-size: 22px;
								font-family: Microsoft YaHei-3970(82674968);
								font-weight: bold;
								color: #1A202B;
							}

							.title-detail {
								margin-left: 50px;
								font-size: 18px;
								font-family: Microsoft YaHei-3970(82674968);
								color: #1A202B;
							}
						}

						.title-right {
							display: flex;
							justify-content: space-between;
							align-items: center;

							.el-button {
								width: 110px;
								height: 46px;
								background: #2B4CFE;
								font-size: 18px;
								color: #FFFFFF;
								font-family: Microsoft YaHei-3970(82674968);
								border-radius: 5px;
							}

							.el-button--warning {
								background: #F88A64;
							}
						}
					}

					.cell-body {

						.controls {
							display: flex;
							justify-content: space-between;
							align-items: center;

							.demo-form-inline {
								display: inline-flex;
								flex-wrap: wrap;
								align-items: center;
								justify-content: flex-start;

								.el-form-item {
									margin-bottom: 16px;
								}

								.btn-search {
									background: rgba(9, 101, 98, 1);
									font-size: 14px;
									color: #FFFFFF;
									font-family: Microsoft YaHei-3970(82674968);
									border-radius: 5px;
									border: none;
								}
							}

							.btns {
								display: flex;
								align-items: center;
								margin-bottom: 16px;

								.btn-article-del {
									border-radius: 5px;
									background: rgba(212, 48, 48, 1);
									border: none;
								}

								.btn-update-get-article {
									background: rgba(9, 101, 98, 1);
									font-size: 14px;
									color: #FFFFFF;
									font-family: Microsoft YaHei-3970(82674968);
									border-radius: 5px;
									border: none;
								}
							}
						}

						.pagination-table {
							display: flex;
							justify-content: flex-end;
							align-items: center;
							height: 30px;
							margin-top: 20px;

							::v-deep .el-pagination {
								display: flex;
								justify-content: flex-end;
								align-items: center;
							}

							::v-deep .el-pagination ul {
								display: flex;

							}

							::v-deep .el-pagination button,
							::v-deep .el-pagination li {
								display: flex;
								justify-content: center;
								align-items: center;
								width: 50px;
								height: 36px;
								border: 1px solid #626262;
								border-radius: 3px;
								font-size: 14px;
								margin: 0 5px;
							}

							::v-deep .el-pagination span {
								margin-left: 10px;
							}

							::v-deep .el-pagination .el-pagination__jump {
								font-size: 16px;
							}

							::v-deep .el-pagination .el-pagination__editor {
								width: 50px;
								height: 36px;
								margin: 0 5px;
							}

							::v-deep .el-pagination .el-input__inner {
								width: 50px;
								height: 36px;
								border: 1px solid #626262;
								margin: -4px 0 0 0;
							}
						}
					}
				}
			}
		}

		.dialog-have-no-title-body {
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			height: 180px;

			img {
				width: 72px;
			}

			.dialog-have-no-title-body-txt {
				height: 38px;
				line-height: 38px;
				text-align: center;
				font-size: 18px;
				font-family: Microsoft YaHei-3970(82674968);
				color: #333333;
				margin-top: 20px;
			}
		}

		.show-article {
			height: 500px;
			overflow: auto;
			border: 1px solid #e3e3e3;
			padding: 10px 10px 10px 30px;
		}
	}
</style>

<style>
	.el-date-editor--daterange.el-input__inner {
		width: 280px !important;
	}

	.demo-table-expand {
		font-size: 0 !important;
	}

	.demo-table-expand label {
		width: 180px !important;
		color: #99a9bf !important;
	}

	.demo-table-expand .el-form-item {
		margin-right: 0 !important;
		margin-bottom: 0 !important;
		width: 33% !important;
	}

	.dialog-footer {
		display: flex !important;
		justify-content: flex-end !important;
		margin-top: -10px !important;
	}

	.dialog-have-no-title {
		height: 300px !important;
		width: 500px !important;
		margin-top: 330px !important;
		border-radius: 8px !important;
	}

	.dialog-have-no-title .el-dialog__header {
		display: none !important;
	}

	.dialog-have-title .el-dialog__header {
		display: flex;
		align-items: center;
		width: 100%;
		height: 58px;
		padding: 30px;
		border-radius: 6px 6px 0px 0px;
		font-weight: bold;
		border-bottom: 1px solid rgba(230, 230, 230, 1);
	}

	.add-user-cancel-btn {
		color: rgba(9, 101, 98, 1) !important;
		border: 1px solid rgba(9, 101, 98, 1) !important;
	}

	.add-user-confirm-btn {
		border: none !important;
		background: rgba(9, 101, 98, 1) !important;
		color: #fff !important;
		border-radius: 5px !important;
		margin-left: 28px !important;
	}

	.content-style {
		height: 200px;
		overflow: auto;
	}

	.content-style::-webkit-scrollbar {
		width: 2px;
		height: 2px;
	}

	.content-style::-webkit-scrollbar-thumb {
		background-color: rgba(9, 101, 98, 0.5);
	}
</style>