<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-select v-model="formInline.status" clearable placeholder="请选择">
										<el-option v-for="item in formInline.options" :key="item.id" :label="item.label" :value="item.id">
										</el-option>
									</el-select>
								</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">
									</el-date-picker>
								</el-form-item>
								<el-form-item label="">
									<el-input v-model="formInline.keyword" style="width: 200px;" 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>
							<el-button type="primary" icon="el-icon-download" class="export" @click="exportExcel">导出投诉</el-button>
						</div>
						<el-table ref="multipleTable" :data="tableData" height="558" style="width: 100%" :cell-style="cell_style" v-loading="loading"
							:header-cell-style="header_cell_style">
							<el-table-column fixed="left" label="状态" align="center">
								<template slot-scope="scope">
									<span v-if="scope.row.status == 1" style="color: #F9273F;">待处理</span>
									<span v-else-if="scope.row.status == 2" style="color: #ccc;">处理中</span>
									<span v-else-if="scope.row.status == 3" style="color: #56AA2E;">处理完成</span>
									<span v-else style="color: #ccc;">未知状态</span>
								</template>
							</el-table-column>
							<el-table-column label="投诉单号" align="center" width="180">
								<template slot-scope="scope">
									<span>{{ scope.row.id }}</span>
								</template>
							</el-table-column>
							<el-table-column label="民宿" align="center" width="280">
								<template slot-scope="scope">
									<span>{{ scope.row.hotelName }}</span>
								</template>
							</el-table-column>

							<el-table-column label="民宿电话" align="center" width="120">
								<template slot-scope="scope">
									<span>{{ scope.row.hotelPhone }}</span>
								</template>
							</el-table-column>
							<el-table-column label="投诉人电话" align="center" width="120">
								<template slot-scope="scope">
									<span>{{ scope.row.complainPhone }}</span>
								</template>
							</el-table-column>
							<el-table-column label="订单号" align="center" width="180">
								<template slot-scope="scope">
									<span>{{ scope.row.order_num }}</span>
								</template>
							</el-table-column>
							<el-table-column label="标题" align="center" width="180">
								<template slot-scope="scope">
									<span>{{ scope.row.title }}</span>
								</template>
							</el-table-column>
							<el-table-column label="问题描述" align="center" width="280">
								<template slot-scope="scope">
									<span>{{ scope.row.content }}</span>
								</template>
							</el-table-column>
							<el-table-column label="投诉时间" align="center" width="168">
								<template slot-scope="scope">
									<span>{{ scope.row.createDate | truncateDate }}</span>
								</template>
							</el-table-column>
							<el-table-column label="备注" align="center" width="280">
								<template slot-scope="scope">
									<span>{{ scope.row.remark }}</span>
								</template>
							</el-table-column>
							<el-table-column fixed="right" label="操作" width="120">
								<template slot-scope="scope">
									<el-button @click="handleClickRemarks(scope.row)" type="text" size="small">受理</el-button>
									<el-button @click="handleRemarks(scope.row)" 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 title="投诉详情" custom-class="acceptance-class" :visible.sync="dialogComplaintVisible" :close-on-click-modal="false"
			:close-on-press-escape="false" @close="dialogComplaintVisible = false">
			<p v-if="complaint.status == 1"><label class="lbl-cls">投诉状态:</label><span style="color: #F9273F;">待处理</span></p>
			<p v-else-if="complaint.status == 2"><label class="lbl-cls">投诉状态:</label><span style="color: #56AA2E;">处理中</span></p>
			<p v-else-if="complaint.status == 3"><label class="lbl-cls">投诉状态:</label><span style="color: #56AA2E;">处理完成</span></p>
			<p v-else><label class="lbl-cls">投诉状态:</label><span style="color: #ccc;">未知状态</span></p>
			<p><label class="lbl-cls">投诉单号:</label> {{ complaint.id }}</p>
			<p><label class="lbl-cls">民宿:</label> {{ complaint.hotelName }}</p>
			<p><label class="lbl-cls">民宿电话:</label> {{ complaint.hotelPhone }}</p>
			<p><label class="lbl-cls">投诉人电话:</label> {{ complaint.complainPhone }}</p>
			<p><label class="lbl-cls">订单号:</label> {{ complaint.order_num }}</p>
			<div>
				<h3 style="padding-top: 20px;">投诉信息</h3>
				<p><label class="lbl-cls">标题:</label> {{ complaint.title }}</p>
				<p><label class="lbl-cls">问题描述:</label> {{ complaint.content }}</p>
				<div class="show-imgs">
					<div><label class="lbl-cls">图片:</label></div>
					<div class="imgs">
						<viewer :images="complaint.fileInfos">
							<div style="display: flex;">
								<div v-for="(image, index) in complaint.fileInfos" :key="index">
									<img :src="image.url" v-if="!image.url.endsWith('.mp4')" style="margin: 0 2px; border-radius: 5px; cursor: pointer;" alt=""
										height="80px" width="80px" />
								</div>
							</div>
						</viewer>
					</div>
				</div>
				<p><label class="lbl-cls">投诉时间:</label> {{ complaint.createDate | truncateDate }}</p>
				<p><label class="lbl-cls">备注:</label><el-input type="textarea" style="margin-left: 100px; margin-top: -20px; width: calc(100% - 100px);"
						:rows="2" placeholder="暂无备注信息" v-model="complaint.remark">
					</el-input>
				</p>
				<p style="display: flex; justify-content: flex-end;">
					<el-button class="cancel-btn" @click="dialogComplaintVisible = false">取 消</el-button>
					<el-button v-if="complaint.status != 3" type="primary" class="confirm-btn" @click="handleClickAcceptance">确定受理</el-button>
					<el-button v-else type="primary" class="confirm-btn-disabled" @click="handleClickAcceptance" disabled>确定受理</el-button>
				</p>
			</div>
		</el-dialog>
		<el-dialog title="添加备注" custom-class="add-remarks-class" :visible.sync="dialogRemarksFormVisible" :close-on-click-modal="false">
			<el-form :model="formRemarks">
				<el-form-item>
					<el-input type="textarea" placeholder="请输入备注" v-model="formRemarks.remark" :autosize="{minRows: 4, maxRows: 6}" maxlength="100"
						show-word-limit></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button class="cancel-btn" @click="dialogRemarksFormVisible = false">取 消</el-button>
				<el-button type="primary" class="confirm-btn" @click="handleRemarksClick">确 定</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		getTableData,
		getXiang,
		downloadExcel,
		Complainacceptance,
		ComplainupdateRemarkById
	} from '@/api/complaintMgr'
	import {
		isEmpty
	} from '@/js/common';

	export default {
		data() {
			return {
				currentUserLevel: this.$store.state.user.level, // 当前用户的权限
				dialogImageUrl: '',
				formLabelWidth: '120px',
				// 表格单元格样式
				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: [{
						id: 1,
						label: '待处理'
					}, {
						id: 2,
						label: '处理中'
					}, {
						id: 3,
						label: '处理完成'
					}],
					status: '',
					search_datatime: '',
					keyword: ''
				},
				tableData: [],
				cond_data: {},
				// 分页参数
				pagination: {
					currentPage: 1,
					pageSize: 9,
					total: 0
				},
				loading: true,
				complaint: {
					id: '',
					order_num: '',
					status: '',
					bookingId: '',
					hotelName: '',
					hotelPhone: '',
					complainPhone: '',
					title: '',
					content: '',
					fileInfos: [],
					createDate: '',
					remark: ''
				},
				dialogComplaintVisible: false,
				dialogRemarksFormVisible: false, // 添加备注
				formRemarks: {
					id: '',
					remark: ''
				}
			}
		},
		filters: {
			truncateDate(value) {
				return value.substring(0, value.length - 2);
			}
		},
		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: {
			// 添加备注
			handleRemarksClick() {
				if (isEmpty(this.formRemarks.remark)) {
					this.$message.error('请输入【备注】内容')
					return
				}

				var query = {
					id: this.formRemarks.id,
					remark: this.formRemarks.remark
				}

				ComplainupdateRemarkById(query).then((res) => {
					// console.log(res.data);
					if (res.code === 200) {
						this.get_table_data('search')
						this.dialogRemarksFormVisible = false;
						this.$message.warning('备注完成！')
					} else {
						this.$message.warning('备注失败！')
					}
				}).catch((err) => {
					this.$message.error(err.message)
				});
			},
			/**
			 * 显示添加备注对话框
			 */
			handleRemarks(row) {
				// console.log(row);
				this.formRemarks.id = row.id;
				this.formRemarks.remark = row.remark;

				this.dialogRemarksFormVisible = true;
			},
			/**
			 * 受理
			 * @param {Object} row
			 */
			handleClickAcceptance() {
				// console.log(this.complaint);
				var query = {
					id: this.complaint.id,
					remark: this.complaint.remark
				}
				
				Complainacceptance(query).then((res) => {
					// console.log(res.data);
					if (res.code === 200) {
						this.get_table_data('search')
						this.dialogComplaintVisible = false
						this.$message.warning('受理完成！')
					} else {
						this.$message.warning('受理失败！')
					}
				}).catch((err) => {
					this.$message.error(err.message)
				});
			},
			// 点击受理，弹出投诉详情对话框
			handleClickRemarks(row) {
				// console.log(row);
				this.$set(this.complaint, 'id', row.id);
				this.$set(this.complaint, 'order_num', row.order_num);
				this.$set(this.complaint, 'status', row.status);
				this.$set(this.complaint, 'bookingId', row.bookingId);
				this.$set(this.complaint, 'hotelName', row.hotelName);
				this.$set(this.complaint, 'hotelPhone', row.hotelPhone);
				this.$set(this.complaint, 'complainPhone', row.complainPhone);
				this.$set(this.complaint, 'title', row.title);
				this.$set(this.complaint, 'content', row.content);
				this.$set(this.complaint, 'createDate', row.createDate);
				this.$set(this.complaint, 'remark', row.remark);

				getXiang(row).then((res) => {
					// console.log(res.data);
					if (res.code === 200) {
						this.$set(this.complaint, 'fileInfos', res.data.fileInfos);
					} else {
						this.complaint = []
						this.$message.warning('没有符合条件的数据！')
					}
				}).catch((err) => {
					this.$message.error(err.message)
				});

				this.dialogComplaintVisible = true
			},
			/**
			 * 获取消费记录,表格数据
			 */
			get_table_data(param) {
				this.loading = true
				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.data);
					if (res.code === 200) {
						var d = res.data
						that.pagination.total = d.total
						that.tableData = []
						for (var i = 0; i < d.pageList.length; i++) {
							that.tableData.push(d.pageList[i])
						}
					} else {
						that.tableData = []
						that.$message.warning('没有符合条件的数据！')
					}
				}).catch((err) => {
					that.$message.error(err.message)
				});
				that.loading = false;
			},
			/**
			 * 查询条件
			 */
			get_condtion() {
				let data = {
					page: this.pagination.currentPage,
					rows: this.pagination.pageSize
				}

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

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

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

				this.cond_data = data;
			},
			/**
			 * 导出为Excel
			 */
			exportExcel() {
				var that = this
				downloadExcel(this.cond_data).then((res) => {
					// console.log(res);
					if (res.code === 200) {
						that.$message.success(res.message)
						let xls = res.downurl
						window.open(xls)
					} else {
						that.tableData = []
						that.$message.warning(res.message)
					}
				}).catch((err) => {
					that.$message.error(err.message)
				});
			},
			/**
			 * 搜索
			 */
			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;

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

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

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

							.txt-notice {

								span {
									font-size: 28px;
									font-weight: 500;
									font-family: '微软雅黑';
								}
							}

							::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;
							}
						}
					}
				}
			}
		}

		::v-deep .el-dialog {
			margin: 0 !important;
			width: 650px;
			height: 560px;
			background: #FFFFFF;
			box-shadow: 0px 0px 13px 0px rgba(0, 0, 0, 0.29);
			border-radius: 6px;
			position: absolute;
			top: 50%;
			left: 50%;
			transform: translate(-50%, -50%);

			.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);
			}

			.el-dialog__body {
				padding-bottom: 0;

				.el-form-item__label,
				.el-form-item__content {
					font-size: 16px;
					font-family: Microsoft YaHei-3970(82674968);
					color: #53575A;
				}

				.el-input__inner {
					width: 180px;
				}
			}

			// 删除对话框的样式
			.del-account-body {
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				height: 180px;

				img {
					width: 72px;
				}

				.del-account-body-txt {
					height: 58px;
					line-height: 58px;
					font-size: 18px;
					font-family: Microsoft YaHei-3970(82674968);
					color: #333333;
				}
			}

			.el-dialog__footer {
				padding-bottom: 0 !important;
				text-align: center;

				.el-button.el-button--default {
					width: 75px;
					height: 40px;
					border: 1px solid #2B4CFE;
					border-radius: 6px;
					font-size: 16px;
					font-family: Microsoft YaHei-3970(82674968);
					color: #2B4CFE;
				}

				.el-button.el-button--primary {
					width: 75px;
					height: 40px;
					background: #2B4CFE;
					border-radius: 6px;
					font-size: 16px;
					font-family: Microsoft YaHei-3970(82674968);
					color: #FFFFFF;
					margin-left: 60px;
				}
			}
		}

		>>>.el-table__fixed-body-wrapper .el-table__body {
			padding-bottom: 17px !important; // 滚动条高度
		}

		>>>.el-table__fixed,
		>>>.el-table__fixed-right {
			height: calc(100% - 14px) !important;
		}
	}
</style>

<style>
	.acceptance-class {
		height: 680px !important;
	}

	.add-remarks-class {
		height: 300px !important;
	}

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

	.show-imgs {
		display: flex;
		height: 90px;
	}

	.imgs {
		display: flex;
		margin-left: 20px;
	}

	.lbl-cls {
		display: inline-block;
		width: 100px;
		text-align: right;
		padding-right: 10px;
		color: #bbb;
	}

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

	.confirm-btn {
		border: none !important;
		background: rgba(9, 101, 98, 1) !important;
		color: #fff !important;
		border-radius: 5px !important;
		margin-left: 28px !important;
	}
	
	.confirm-btn-disabled {
		border: none !important;
		background: rgba(9, 101, 98, 1) !important;
		color: #9c9c9c !important;
		border-radius: 5px !important;
		margin-left: 28px !important;
	}
</style>