<template>
	<div class="list_container">
		<div class="title_examine">
			<div class="pub_title">后缀管理</div>
			<div style="margin-left: auto">
				<el-input
					v-model="checkOid"
					placeholder="请输入学校"
					size="normal"
					clearable
					@input="changeOid"
					style="margin-right: 10px; width: 200px"
				></el-input>

				<el-button type="primary" size="small" @click="addsch">新增学校</el-button>
			</div>
		</div>
		<div class="tableBox">
			<el-table
				:data="schoolData"
				stripe
				border
				style="width: 100%"
				:header-cell-style="headerCellStyle"
			>
				<el-table-column
					show-overflow-tooltip
					prop="name"
					label="学校名称"
					min-width="45"
				></el-table-column>
			
				<el-table-column
					show-overflow-tooltip
					prop="englishname"
					label="englishname"
					min-width="45"
				>
					<template slot-scope="scope">
						{{ scope.row.englishname || "-" }}
					</template>
				</el-table-column>

				<el-table-column
					show-overflow-tooltip
					prop="mail"
					label="mail"
					min-width="45"
				>
					<template slot-scope="scope">
						{{ scope.row.mail || "-" }}
					</template>
				</el-table-column>

				<el-table-column
					show-overflow-tooltip
					prop="number"
					label="number"
					min-width="45"
				>
					<template slot-scope="scope">
						{{ scope.row.number || "-" }}
					</template>
				</el-table-column>
				<el-table-column
					show-overflow-tooltip
					prop="Ctime"
					label="创建时间"
					min-width="45"
				></el-table-column>
				<el-table-column label="操作" width="250px">
					<template slot-scope="scope">
						<div class="operate">
							<button @click="update(scope.row)">编辑</button>
							<button style="color: red;" @click="del(scope.row)">删除</button>
						</div>
					</template>
				</el-table-column>
			</el-table>
		</div>
		<!-- 分页组件 -->
		<el-pagination
			:current-page="currentPage"
			:page-size="pageSize"
			:total="total"
			@current-change="handlePageChange"
			layout="total,prev, pager, next, jumper"
		>
		</el-pagination>
		<!-- 修改弹窗 -->
		<el-dialog
			:title="iseit == 0 ? '添加' : '编辑'"
			class="table"
			:visible.sync="dialogShow"
			:before-close="closedialog"
			width="700px"
		>
			<div class="el_dialog_org_content" style="height: none">
				<form class="el-form">
					<div class="el-form-item">
						<label class="el-form-item__label" style="width: 100px">学校</label>
						<div class="el-form-item__content" style="margin-left: 5px">
							<el-input
								v-model="sufsch.name"
								placeholder="请输入内容"
							></el-input>
						</div>
						<div v-if="judgmentSuf({id:1,name:sufsch.name}).length">
							与{{ judgmentSuf({id:1,name:sufsch.name})[0].name }}学校名称重复
						</div>
					</div>
					<div class="el-form-item">
						<label class="el-form-item__label" style="width: 100px"
							>englishname</label
						>
						<div class="el-form-item__content" style="margin-left: 5px">
							<el-input
								v-model.trim="sufsch.englishname"
								placeholder="请输入内容"
							></el-input>
						</div>
						<div v-if="judgmentSuf({id:2,name:sufsch.englishname}).length">
							与{{ judgmentSuf({id:2,name:sufsch.englishname})[0].name }}学校englishname重复
						</div>
					</div>
					<div class="el-form-item">
						<label class="el-form-item__label" style="width: 100px">mail</label>
						<div class="el-form-item__content" style="margin-left: 5px">
							<el-input v-model.trim="sufsch.mail" placeholder="请输入内容"></el-input>
						</div>
						<div v-if="judgmentSuf({id:3,name:sufsch.mail}).length">
							与{{ judgmentSuf({id:3,name:sufsch.mail})[0].name }}学校mail重复
						</div>
					</div>
					<div class="el-form-item">
						<label class="el-form-item__label" style="width: 100px"
							>number</label
						>
						<div class="el-form-item__content" style="margin-left: 5px">
							<el-input
								v-model.trim="sufsch.number"
								placeholder="请输入内容"
							></el-input>
						</div>
						<div v-if="judgmentSuf({id:4,name:sufsch.number}).length">
							与{{ judgmentSuf({id:4,name:sufsch.number})[0].name }}学校number重复
						</div>
					</div>
				</form>
			</div>
			<!-- 按钮区域 -->
			<div slot="footer" class="el-dialog__footer">
				<el-button @click="closedialog">取 消</el-button>
				<el-button v-if="iseit == 0" type="primary" @click="addConfirm">确认</el-button>
				<el-button v-else type="primary" @click="sufConfirm">确认</el-button>
			</div>
		</el-dialog>
		
	</div>
</template>
<script>
// import { addOp } from "@/api/user";
// import { API_CONFIG } from "@/common/apiConfig";
import { mapGetters } from "vuex";
import { getAllSchoolSuffix,addSuffxSchool,exitSuffxSchool,delSuffxSchool } from "@/api/user.js";
import { addOp } from "@/api/user";
// 
export default {
	name: "schoolList",
	data() {
		return {
			dialogShow: false,
			sufsch: {
				name: "",
				englishname: "",
				mail: "",
				number: "",
			},
			sufschCopy:{},
			add_school_dialog:false,

			AddDialogShow:false,

			iseit:0,

			schoolData: [],
			schoolList: [],

			checkOid: "",
			checkOrg: "",
			checkArea: "",
			currentPage: 1,
			pageSize: 10,
			total: 0,

			allSchool:[]
		};
	},
	computed: {
		...mapGetters(["userid"]),
		judgmentSuf(){
			return function(val){	
						
				if (val.id == 1) {
					// if (!val.name) return []
					let kk = []
					if (this.iseit == 0) {
						kk = this.allSchool.filter(e=>{
							return e.name == val.name
						})
					}else{
						kk = this.allSchool.filter(e=>{
							return e.name == val.name
						})
						if (kk.length) {
							if (kk[0].name == this.sufschCopy.name) return []
						}
					}
					
					return kk
				}else if (val.id == 2) {					
					if (!val.name) return []
					let kk = []
					if (this.iseit == 0) {
						kk = this.allSchool.filter(e=>{
							return e.englishname == val.name
						})
					}else{
						kk = this.allSchool.filter(e=>{
							return e.englishname == val.name
						})
						if (kk.length) {
							if (kk[0].englishname == this.sufschCopy.englishname) return []
						}
					}
					
					return kk
				}else if (val.id == 3) {
					if (!val.name) return []
					let kk = []
					if (this.iseit == 0) {
						kk = this.allSchool.filter(e=>{
							return e.mail == val.name
						})
					}else{
						kk = this.allSchool.filter(e=>{
							return e.mail == val.name
						})
						if (kk.length) {
							if (kk[0].mail == this.sufschCopy.mail) return []
						}
					}
					
					return kk
				}else{
					if (!val.name) return []
					let kk = []
					if (this.iseit == 0) {
						kk = this.allSchool.filter(e=>{
							return e.number == val.name
						})
					}else{
						kk = this.allSchool.filter(e=>{
							return e.number == val.name
						})						
						if (kk.length) {
							if (kk[0].number == this.sufschCopy.number) return []
						}
					}
					
					return kk
				}
			}
		}
	},
	methods: {
		// 表头的背景色
		headerCellStyle() {
			return { backgroundColor: "#f1f1f1" };
		},
		// 打开修改弹框 
		update(val) {
			this.iseit = 1
			console.log(val);
			this.sufsch = JSON.parse(JSON.stringify(val));
			this.sufschCopy = JSON.parse(JSON.stringify(val));

			this.dialogShow = true;
		},
		// 确定修改
		sufConfirm() {

			if (!this.sufsch.name) return this.$message.error("学校名称不能为空")

			let q =this.jud({id:1,name:this.sufsch.name})
			let w =this.jud({id:2,name:this.sufsch.englishname})
			let e =this.jud({id:3,name:this.sufsch.mail})
			let r =this.jud({id:4,name:this.sufsch.number})
			if (q.length || w.length || e.length || r.length) return this.$message.error('请修改重复数据')

			this.$confirm(`确认修改?`, "提示", {
				confirmButtonText: "确定",
				cancelButtonText: "取消",
				type: "warning",
			})
			.then(() => {
				let params = {
					id: this.sufsch.id,
					sn:this.sufsch.name,
					en:this.sufsch.englishname,
					ma:this.sufsch.mail,
					nu:this.sufsch.number,
				};
				console.log(params);
				
				exitSuffxSchool(params)
					.then(async (res) => {
						console.log(res);
						this.$message.success("修改成功")
						this.getSchool()
						this.getAllSchool()
						await addOp({
							uid: this.userid,
							cid: "",
							type: "user_op",
							content: `${this.userid}修改了后缀管理${this.sufsch.id}学校${params}`,
						});
						this.closedialog()
					})
					.catch((error) => {
						this.$message.error("修改失败")
						console.error("删除失败：", error);
					});
			})
			.catch(() => {
				// 取消操作后提示
				this.$message({
					type: "info",
					message: '已取消',
				});
			});
		},
		// 删除 
		del(val){
			this.$confirm(`确认删除?`, "提示", {
				confirmButtonText: "确定",
				cancelButtonText: "取消",
				type: "warning",
			})
			.then(() => {
				let params = {
					id:val.id
				};
				console.log('params',params);
				
				delSuffxSchool(params)
					.then(async (res) => {
						console.log(res);
						this.getSchool()
						this.getAllSchool()
						await addOp({
							uid: this.userid,
							cid: "",
							type: "user_op",
							content: `${this.userid}删除了后缀管理${val.id}学校`,
						});
					})
					.catch((error) => {
						this.$message.error("删除失败")
						console.error("删除失败：", error);
					});
			})
			.catch(() => {
				// 取消操作后提示
				this.$message({
					type: "info",
					message: '已取消',
				});
			});
		},
		// 添加学校
		addsch(){
			this.iseit = 0;
			this.sufsch={
				name: "",
				englishname: "",
				mail: "",
				number: "",
			},
			this.dialogShow = true;
		},
		// 确认添加学校
		addConfirm(){
			if (!this.sufsch.name) return this.$message.error("学校名称不能为空")
			let q =this.jud({id:1,name:this.sufsch.name})
			let w =this.jud({id:2,name:this.sufsch.englishname})
			let e =this.jud({id:3,name:this.sufsch.mail})
			let r =this.jud({id:4,name:this.sufsch.number})
			if (q.length || w.length || e.length || r.length) return this.$message.error('请修改重复数据')

			let params = {
				uid:this.userid,
				sn:this.sufsch.name,
				en:this.sufsch.englishname,
				ma:this.sufsch.mail,
				nu:this.sufsch.number,
			};
			console.log('params',params);
			
			addSuffxSchool(params)
				.then(async (res) => {
					console.log(res);
					this.$message.success("添加成功")
					
					this.getSchool()
					this.getAllSchool()

					this.closedialog()
					await addOp({
						uid: this.userid,
						cid: "",
						type: "user_op",
						content: `${this.userid}修改了后缀管理${this.sufsch.name}学校`,
					});
				})
				.catch((error) => {
					this.$message.error("添加失败")
					console.error("添加失败", error);
				});
			
		},
		jud(val){	
			if (val.id == 1) {
				// if (!val.name) return []
				let kk = []
				if (this.iseit == 0) {
					kk = this.allSchool.filter(e=>{
						return e.name == val.name
					})
				}else{
					kk = this.allSchool.filter(e=>{
						return e.name == val.name
					})
					if (kk.length) {
						if (kk[0].name == this.sufschCopy.name) return []
					}
				}
				
				return kk
			}else if (val.id == 2) {					
				if (!val.name) return []
				let kk = []
				if (this.iseit == 0) {
					kk = this.allSchool.filter(e=>{
						return e.englishname == val.name
					})
				}else{
					kk = this.allSchool.filter(e=>{
						return e.englishname == val.name
					})
					if (kk.length) {
						if (kk[0].englishname == this.sufschCopy.englishname) return []
					}
				}
				
				return kk
			}else if (val.id == 3) {
				if (!val.name) return []
				let kk = []
				if (this.iseit == 0) {
					kk = this.allSchool.filter(e=>{
						return e.mail == val.name
					})
				}else{
					kk = this.allSchool.filter(e=>{
						return e.mail == val.name
					})
					if (kk.length) {
						if (kk[0].mail == this.sufschCopy.mail) return []
					}
				}
				
				return kk
			}else{
				if (!val.name) return []
				let kk = []
				if (this.iseit == 0) {
					kk = this.allSchool.filter(e=>{
						return e.number == val.name
					})
				}else{
					kk = this.allSchool.filter(e=>{
						return e.number == val.name
					})						
					if (kk.length) {
						if (kk[0].number == this.sufschCopy.number) return []
					}
				}
				
				return kk
			}
		},
		// 关闭弹框
		closedialog() {
			this.dialogShow = false;
			this.sufsch = {};
			this.sufschCopy = {}
		},

		// 学校输入框查询
		changeOid() {
			this.currentPage = 1; // 重置当前页
			this.getSchool(); // 调用获取数据的方法
		},

		// 获取学校
		getSchool() {
			let params = {
				sname: this.checkOid,
				oname: this.checkOrg,
				aname: this.checkArea,
				page: this.currentPage,
				num: this.pageSize,
			};
			getAllSchoolSuffix(params)
				.then(async (res) => {
					console.log(res);
					let data = res.data[0];
					this.total = data.length ? data[0].num : 0;
					this.schoolData = data;
				})
				.catch((error) => {
					console.error("请求失败，错误信息：", error);
				});
		},

		// 获取所有学校
		getAllSchool() {
			let params = {
				sname: this.checkOid,
				oname: this.checkOrg,
				aname: this.checkArea,
				page: 1,
				num: 10000,
			};
			getAllSchoolSuffix(params)
				.then(async (res) => {
					console.log(res);
					let data = res.data[0];
					if (data[0].num > 10000){
						this.$message.error('请修改后缀管理getAllSchool函数num参数到100000')
					}
					this.allSchool = data;
				})
				.catch((error) => {
					console.error("请求失败，错误信息：", error);
				});
		},
		// 页码扩展按钮
		handlePageChange(page) {
			this.currentPage = page;
			this.getSchool();
		},
	},
	mounted() {
		this.getSchool();
		this.getAllSchool();
	},
	
};
</script>
<style scoped>
.list_container {
	width: 100%;
	height: 100%;
	padding: 10px;
	box-sizing: border-box;
	overflow: auto;
}

/* 新增组织的内容容器 */
.el_dialog_org_content {
	color: #606266;
	font-size: 14px;
	/* height: 120px; */
}

.el-form-item {
	margin-bottom: 22px;
	display: flex;
	align-items: center;
	/* border: 1px solid black; */
}

.el-form-item__label {
	font-size: 14px;
	color: #606266;
	line-height: 40px;
	width: 70px;
}

.el-form-item__content {
	line-height: 40px;
	position: relative;
	font-size: 14px;
}
.el-form-item__content >>> .el-input__inner{
	width: 300px;
}
.table >>> .el-dialog__header {
	padding: 15px 20px;
	background: #454545;
}

.table >>> .el-dialog__title {
	color: #fff;
}

.tableBox {
	margin: 10px 0;
}

.operate {
	display: flex;
	gap: 20px;
}

.operate button {
	background: none;
	border: none;
	cursor: pointer;
	color: #308fff;
}
</style>
