<template>
	<view @tap='btnShow = false'>
		<u-navbar title="教育经历" :custom-back="router"></u-navbar>
		<view class="list">
			<view :class="{'item-selsect': item.isSelected}" class="flex-col list-item" v-for="(item, index) in schoolList" :key="index" @click="currentidx(item,index)">
				<view class="flex-col section_2">
					<view class="justify-between section_3 view_8">
						<view class="flex-row">
							<image :src="$u.http.config.imgUrl + item.logo.slice(1)" class="image_2" />
							<text class="text_1 text_23">{{ item.school }}</text>
						</view>
						<u-button v-if="item.certifyStatus == 1" disabled class="text-wrapper" type="success" plain
							size='mini' shape='circle'>已认证</u-button>
						<u-button v-if="item.certifyStatus == 2" disabled class="text-wrapper" type="error" plain
							size='mini' shape='circle'>审核中</u-button>
						<u-button v-if="item.certifyStatus == 0" class="text-wrapper" type="primary" size='mini'
							shape='circle' ripple @click='toAuthentication(item)'>去认证</u-button>
						<!-- <navigator url='#' class="flex-col items-center text-wrapper view_9"><text class="text_3 text_24">去认证</text></navigator> -->
					</view>
					<view class="group_3">
						<text class="text_5">学历：</text>
						<text class="text_7">{{ item.educationStr }}</text>
					</view>
					<view class="group_3">
						<text class="text_5">学院：</text>
						<text class="text_7">{{ item.college }}</text>
					</view>
					<view class="group_3">
						<text class="text_5">专业：</text>
						<text class="text_7">{{ item.major }}</text>
					</view>
					<view class="group_3">
						<text class="text_5">时间：</text>
						<text class="text_7">{{ item.startYear }}-{{ item.endYear }}</text>
					</view>
					<view class="flex-row justify-end" style="margin-right: 0.2rem; position: relative;"
						v-if="item.certifyStatus == 0 && schoolList.length > 1" @tap.stop='isindex = index; btnShow = !btnShow'>
						<u-icon name="more-dot-fill" color="#ccc" size="28"></u-icon>
						<view class="btns" v-show='isindex == index && btnShow'>
							<view class="item" @tap="onDelete(item)">删除</view>
						</view>
					</view>
				</view>
				<!-- <text class="text_21">已有10000名校友加入</text> -->
			</view>
		</view>
		<view class="btn-box">
			<u-button type="primary" shape="circle" @click="addEducation" style="width: 80%;">添加教育经历</u-button>
		</view>
		<u-toast ref="uToast" />
		<u-top-tips ref="uTips" :navbar-height="0"></u-top-tips>
	</view>
</template>

<script>
export default {
	data() {
		return {
			isindex: -1,
			btnShow: false,
			schoolList: [], //教育经历列表
		};
	},
	onLoad() {
		// this.onSchoolData()
	},
	onShow() {
		this.onSchoolData()
	},
	methods: {
		toAuthentication(item) {
			this.$u.route({
				url: 'pages/my/shoolAuthentication/shoolAuthentication',
				params: {
					educationId: item.id
				},
			})
		},
		onSchoolData() {
			const data = {
				userId: this.vuex_user.id,
			}
			this.$u.apiList.MyEducations(data).then((res) => {
				this.schoolList = res
			})
		},
		//删除
		async onDelete(item) {
			const req = {
				userId: this.vuex_user.id,
			}
			const res = await this.$u.apiList.MyPage(req)
			const edcationList = res.edcationList
			console.log(JSON.parse(JSON.stringify(edcationList)), 'res')
			if (!edcationList.length) {
				// this.$tips( "至少保留一条教育经历","error");
				this.$refs.uToast.show({
					title: "至少保留一条教育经历",
					type: "error",
				});
				return
			}
			const findRow = edcationList.find(x => x.isSelected === true);
			console.log(findRow, 'findRow')
			if (findRow.id === item.id) {
				// this.$tips( "不可删除当前选中院校","error");
				this.$refs.uToast.show({
					title: "不可删除当前选中院校",
					type: "error",
				});
				return
			}
			this.$u.apiList.DeleteEducation({
				id: item.id
			}).then(res => {
				// this.$tips( "删除成功","success");
				this.$refs.uToast.show({
					title: '删除成功',
					type: 'success',
				})
				this.onSchoolData()
			})
		},
		//返回上一级
		router() {
			uni.switchTab({
				url: '../../my/my/my'
			});
		},
		//添加教育经历
		addEducation() {
			this.$u.route({
				url: "pages/my/AddEducation/AddEducation",
			});
		},
		// 选择学校
		async teacherIdx(item) {
			console.log(item, "item--");
			if (item.isSelected) {
				this.$refs.uToast.show({
					title: "已选择该学校",
					type: "warning",
				});
				// this.$tips( "已选择该学校","warning");
				return;
			}
			const req = {
				userId: this.vuex_user.id,
				educationId: item.id,
			};
			const res = await this.$u.apiList.SelectJZGApi(req);
			console.log(res, "res--选择学校");
			this.$u.vuex("vuex_token", res.token);
			this.$u.vuex("vuex_glyType", res.glyType);
			this.glyType = res.glyType;
			this.$refs.uToast.show({
				title: "绑定学校成功",
				type: "success",
			});
			
			// this.$tips( "绑定学校成功","success");
			this.$u.vuex("vuex_user", res.user);
		},
		//选择教育经历
		currentidx(item, index) {
			if (item.isSelected) {
				this.$refs.uToast.show({
				title: '已选择该学校',
				type: 'warning',
				})
				return
			}
			const data = {
				userId: this.vuex_user.id,
				educationId: item.id
			}
			this.getShoolId(data, index)
			},
			getShoolId(data, index, type) {
				this.$u.apiList.SelectEducation(data).then((res) => {
					console.info("🚀 ~ file:my method: line:496 -----",res )

					// this.curIdx = index
					// this.schoolId = res.schoolId
					console.log('%c%s', 'color:red', res.glyType,'res.glyType');
					// 更新 vuex_token
					this.$u.vuex('vuex_token', res.token)
					this.$u.vuex('vuex_glyType', res.glyType)
					this.glyType = res.glyType
					this.$u.vuex('vuex_user', res.user)
					if (!type) {
					this.$refs.uToast.show({
						title: '绑定学校成功',
						type: 'success',
					})
					this.$u.api.getUser()
					this.onSchoolData()
					}
				}).catch((err) => {
					console.log(err,'err-------')
						this.$refs.uToast.show({
						title: err.error,
						type: 'error',
						})
				})
				},
	}
}
</script>

<style lang="scss" scoped>
.btns {
	background-color: #fff;
	box-shadow: 0 0 0.05rem #ccc;
	border-radius: 0.05rem;
	position: absolute;
	bottom: 150%;

	.item {
		line-height: 2;
		padding: 0 0.1rem;
	}
}
.item-selsect{
		border-color: #2e9bff!important;
	}

.list {
	padding: 0.18rem 0.15rem 1rem;

	.list-item {
		margin-bottom: 0.18rem;
		border: 2rpx solid #fff;
		border-radius: 0.1rem;
	}
	

	.section_2 {
		padding-bottom: 0.2rem;
		background-color: #ffffff;
		border-radius: 0.1rem;
	}

	.text_21 {
		margin-left: 0.03rem;
		margin-top: 0.085rem;
		align-self: flex-start;
		color: #a0a2ac;
		font-size: 0.14rem;
		font-family: PingFang;
		line-height: 0.14rem;
	}

	.section_3 {
		padding: 0.05rem 0.15rem 0.05rem 0.23rem;
		background-color: #ffffff;
		box-shadow: 0.0002rem 0.005rem #1d262e14;
		border-radius: 0.1rem 0.1rem 0px 0px;
		z-index: 1;
	}

	.group_3 {
		margin-left: 0.26rem;
		margin-top: 0.15rem;
		align-self: flex-start;
		line-height: 0.14rem;
	}

	.text-wrapper {
		margin: 0.025rem 0;
		width: 0.75rem;
		height: 0.25rem;
	}

	.text_5 {
		color: #373b48;
		font-size: 0.15rem;
		font-family: PingFang;
		line-height: 0.14rem;
	}

	.text_7 {
		color: #373b48;
		font-size: 0.15rem;
		font-family: PingFang;
		line-height: 0.14rem;
	}

	.image_2 {
		flex-shrink: 0;
		width: 0.3rem;
		height: 0.3rem;
	}

	.text_1 {
		margin-left: 0.07rem;
		align-self: center;
		color: #373b48;
		font-size: 0.15rem;
		font-family: PingFang;
		line-height: 0.14rem;
		width: 360rpx;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	.text_3 {
		color: #15c349;
		font-size: 0.13rem;
		font-family: PingFang;
		line-height: 0.13rem;
	}

	.view_8 {
		padding: 0.05rem 0.14rem 0.05rem 0.23rem;
	}

	.view_9 {
		padding: 0.06rem 0 0.065rem;
		background-color: #2e9bff;
		box-sizing: border-box;
	}

	.text_23 {
		margin-left: 0.075rem;
		line-height: 0.16rem;
	}

	.text_24 {
		color: #ffffff;
	}
}

// 按钮
.btn-box {
	position: fixed;
	width: 100%;
	bottom: 0;
	height: 150rpx;
	border-top: 1px solid #f1f2f6;
	display: flex;
	align-items: center;
	justify-content: center;
	background-color: white;
	z-index: 666;
}
</style>
