<template>
	<view class="p-apply-inedx g_w_all g_bg_page g_kuaishou g-apply-tab">
		<!-- #ifdef MP-TOUTIAO  -->
		<view class="g_flex_row_center apply-header" v-if="isHaveOrder == 1">
			<view class="g_flex_row_between box">
				<view v-for="(item, index) in tabInfo" :key="index" class="item g_flex_column_center" :class="tabActive == index ? 'item-active' : ''" @click="handleUpdateTab(index)">
					<view class="btn g_flex_c g_fw_600">{{ item.name }}</view>
				</view>
			</view>
		</view>
		<!-- #endif -->

		<view class="">
			<!-- #ifdef APP-PLUS || H5 || MP-KUAISHOU || MP-WEIXIN -->
			<!-- <view class="" style="height: calc(58px + 48px)" v-if="from != 'chat'"></view> -->
			<!-- #endif -->
			<scroll-view class="m-list" :style="{ height: from != 'chat' ? `calc(100vh - 50px - env(safe-area-inset-bottom))` : `calc(95vh )` }" :scroll-y="true" @scrolltolower="reachBottom">
				<!-- <view class=""> -->
				<view>
					<view :class="[from ? 'sticky' : '']" v-if="isLogin">
						<div class="g_text_c g_h_24 g_pt_16 g_pb_4 g_bg_f" style="z-index: 100" v-if="from == 'chat'">发送工单</div>
						<view style="background-color: #fff" class="">
							<view class="m-select">
								<view style="height: 52px" class="g_flex_column_center g_pt_8">
									<view class="m-search g_pb_0 g_position_rela" style="padding-bottom: 0; padding-left: 12px; padding-right: 12px">
										<u-search @btnSearch="getSearch" height="80" v-model="keyword" placeholder="搜索姓名/职位/手机号" bg-color="#ededed" :show-action="false" @clear="getSearch('search')" @change="getSearch('clear')" @search="getSearch('search')" search-icon-color="#999999" :maxlength="20"></u-search>
										<!-- <view class="g_w_all g_h_40 g_position_abso" style="left: 0; top: 0px; z-index: 9999" @click.stop="goSearch"></view> -->
									</view>
								</view>
							</view>
							<!-- <view class="g_h_10 g_bg_f_5"></view> -->
						</view>
						<view style="width: 100vw" class="g_border_e_b" v-if="isLogin">
							<g-tabs :list="menuList" item-width="126" :current="menuActive" :noScroll="false" :active-color="activeColor" bar-width="60" bar-height="6" font-size="32" @change="handleUpdateMenu" :gutter="22" duration="0.1" itemWidth="auto" height="84"></g-tabs>
						</view>
					</view>
					<!-- 面板 -->
					<g-list-apply :from="from" @uploadList="getList('concat')" bg="" class="" :tabActive="tabActive" :loading="loading" :speed="speed" :isLogin="isLogin" :query.sync="query" :navInfo="navInfo" />
				</view>
			</scroll-view>
		</view>
		<!-- <g-tabbar class="tabbar" v-if="from != 'chat'"></g-tabbar> -->
	</view>
</template>

<script>
export default {
	onReady() {
		this.G.setNavStyle();
	},
	onShareAppMessage() {
		return {
			title: " ",
			imageUrl: "https://bocai-cms.oss-cn-beijing.aliyuncs.com/web-resource/dailibaoming/APP/shareCard0609.png",
		};
		// return this.G.shareFun();
	},
	props: {
		from: {
			type: String,
			default: "home",
		},
		upAgencyId: {
			type: Number,
			default: '',
		},
		status: {
			type: Number,
			default: -1,
		},
		hasRequested: {
			type: Boolean,
			default: false,
		},
		isLogin: {
			type: Boolean,
			default: false,
		},
	},

	data() {
		return {
			activeColor:getApp().globalData.themeColor,
			isHaveOrder: -1,
			loading: true,
			speed: -1,
			query: {
				page: 1,
				size: 50,
				list: [],
				isFinish: -1,
			},
			GZHInfo: 0,
			cdnBaseImg: this.G.store().cdnBaseImg,
			keyword: "",
			tabbarHeight: uni.getStorageSync("TABBAR_HEIGHT"),

			tabActive: 0,
			tabTip: "",
			tabInfo: [
				{
					name: "我报的",
					childList: [
						{
							name: "全部",
							tip: "",
							classify: 0,
						},
						{
							name: "审核中",
							tip: "10",
							classify: 40,
						},
						{
							name: "待接待",
							tip: "20",
							classify: 0,
						},
						{
							name: "待面试",
							tip: "25",
							classify: 70,
						},
						{
							name: "待入职",
							tip: "30",
							classify: 80,
						},
						{
							name: "在职中",
							tip: "40,48",
							classify: 90,
						},
						{
							name: "已完成",
							tip: "21,26,35,45,50",
							classify: 110,
						},
					],
				},
				{
					name: "报给我的",
					childList: [
						{
							name: "全部",
							tip: "",
						},
						{
							name: "审核中",
							tip: "10",
						},
						{
							name: "待接待",
							tip: "20",
						},
						{
							name: "待面试",
							tip: "25",
						},
						{
							name: "待入职",
							tip: "30",
						},
						{
							name: "在职中",
							tip: "40,48",
						},
						{
							name: "已完成",
							tip: "21,26,35,45,50",
						},
					],
				},
			],
			menuList: [],
			menuActive: 0,
			// isLogin: false,
			loginInfo: {},
			navInfo: {},
		};
	},
	onHide() {
		let that = this;
		if (uni.getStorageSync("apply-supplierAccount") == 1) {
			// 有
			that.tabActive = 1;
		} else {
			// 无
			that.tabActive = 0;
		}
		uni.removeStorageSync("apply-tab-active");
	},
	onLoad(options) {
		let that = this;
		if (options.status) {
			that.menuActive = Number(options.status);
			// setTimeout(() => {
			// 	that.menuActive = Number(options.status);
			// }, 200);
		}

		// console.log(123123123);
	},
	watch: {
		status: {
			handler(val) {
				// console.log("valvalvalval", val);
				if (!this.hasRequested && val !== -1) {
					this.query.page = 1;
					this.menuActive = val;
					this.getList(); // 仅执行一次请求
					this.$emit("update:hasRequested", true);
					// this.hasRequested = true; // 标记为已请求，后续不再触发
				}
			},
			immediate: true,
		},
	},
	created() {
		let that = this;
		uni.$on('themeChanged', (color) => {
			that.activeColor = color;
		});
		// 导航栏高度信息
		that.navInfo = that.G.getNavInfo();
		if (uni.getStorageSync("apply-supplierAccount") == 1) {
			// 有
			that.isHaveOrder = 1;
			if (uni.getStorageSync("apply-tab-active") == 1) {
				that.tabActive = 0;
			} else {
				that.tabActive = 1;
			}
		} else {
			// 无
			that.isHaveOrder = 0;
			that.tabActive = 0;
		}
		// that.isLogin = uni.getStorageSync("apply-token") ? true : false;
		// console.log("that.isLogin", that.isLogin);
		that.loginInfo = uni.getStorageSync("apply-userinfo");

		// this.getList();
		if (!that.isLogin) {
			that.loading = false;
			that.speed = 0;
			this.menuList = this.tabInfo[this.tabActive].childList;
		} else {
			this.query.page = 1;
			that.loading = true;
			this.menuList = this.tabInfo[this.tabActive].childList;
			this.getList();
		}
	},
	onShow() {
		let that = this;
		// if (that.isLogin) {
		// 	if (uni.getStorageSync("applyType") || uni.getStorageSync("applyType") === 0) {
		// 		that.menuActive = uni.getStorageSync("applyType");
		// 		uni.removeStorageSync("applyType");
		// 	}
		// 	this.getList();
		// }
		// this.getGZHInfo();
		// that.G.watchUserPage("pages/workBench/index");
	},
	onReachBottom() {
		let that = this;
		this.G.isLogin();
		if (this.G.isLogin()) {
			if (that.query.isFinish == -1 || that.query.isFinish == that.query.size) {
				that.query.page++;
				that.getList("concat");
			}
		}
	},
	methods: {
		formatRelativeTime(timeStr) {
			if (!timeStr) {
				return "--";
			}
			let now = new Date().getTime();
			let time = new Date(timeStr).getTime();
			
			if (isNaN(time)) {
				return "--";
			}
			
			let diff = now - time;
			
			let minute = 60 * 1000;
			let hour = 60 * minute;
			let day = 24 * hour;
			
			if (diff < minute) {
				return "刚刚";
			} else if (diff < hour) {
				return Math.floor(diff / minute) + "分钟前";
			} else if (diff < day) {
				return Math.floor(diff / hour) + "小时前";
			} else {
				let date = new Date(time);
				let year = date.getFullYear();
				let month = String(date.getMonth() + 1).padStart(2, '0');
				let day = String(date.getDate()).padStart(2, '0');
				return year + "年" + month + "月" + day + "日";
			}
		},
		reachBottom() {
			let that = this;
			this.G.isLogin();
			if (this.G.isLogin()) {
				if (that.query.isFinish == -1 || that.query.isFinish == that.query.size) {
					that.query.page++;
					that.getList("concat");
				}
			}
		},
		getList($type = "init") {
			let that = this;
			// console.log("是否登录", that.isLogin);
			if (!that.isLogin) {
				return false;
			}
			that.G.Post(
				that.api.order_list_new,
				{
					pageNum: that.query.page,
					pageSize: that.query.size,
					keys: that.keyword,
					classify: 1,
					upAgencyId: that.upAgencyId,
					// classify: that.tabActive + 1,
					statusStr: that.tabInfo[that.tabActive].childList[that.menuActive].tip,
					jobAgencyId:uni.getStorageSync("miniApp-info").id
				},
				(res) => { 
					console.log("res", res);
					// return
					if (res.recordCount == 0) {
						that.loading = false;
					} else {
						that.loading = true;
					}
					that.$forceUpdate();
					that.speed = res.recordCount;
					that.query.isFinish = res.recordList.length;
					if (that.speed > 0) {
						// uni.setBackgroundColor({
						// 	backgroundColorBottom: "#ffffff",
						// });
						res.recordList.forEach((item, index) => {
							console.log('=====================', item)
							if (item.status == 10) {
								// 待审核 报名时间
								item.ordered_time = that.G.getPointTime(item.createTime, "MM--DD HH:MM");
								item.newtime = that.formatRelativeTime(item.createTime || item.updateTime);
							} else if (item.status == 20) {
								// 待接待 预约接待时间
								item.ordered_time = that.G.getPointTime(item.receptionTime, "MM--DD HH:MM");
								item.newtime = that.formatRelativeTime(item.receptionTime || item.updateTime);
							} else if (item.status == 21 || item.status == 26 || item.status == 35 || item.status == 48 || item.status == 45) {
								// 已完成 完成时间
								item.ordered_time = that.G.getPointTime(item.finishTime, "MM--DD HH:MM");
								item.newtime = that.formatRelativeTime(item.finishTime || item.updateTime);
							} else if (item.status == 25) {
								// 待面试 面试时间
								item.ordered_time = that.G.getPointTime(item.interviewTime, "MM--DD HH:MM");
								item.newtime = that.formatRelativeTime(item.interviewTime || item.updateTime);
							} else if (item.status == 30) {
								// 待入职 预约入职时间
								item.ordered_time = that.G.getPointTime(item.willEntryTime, "MM--DD HH:MM");
								item.newtime = that.formatRelativeTime(item.willEntryTime || item.updateTime);
							} else if (item.status == 40) {
								// 已入职 入职时间
								item.ordered_time = that.G.getPointTime(item.entryTime, "MM--DD HH:MM");
								item.newtime = that.formatRelativeTime(item.entryTime || item.updateTime);
							} else if (item.status == 50) {
								// 已离职 离职时间
								item.ordered_time = that.G.getPointTime(item.leaveTime, "MM--DD HH:MM");
								item.newtime = that.formatRelativeTime(item.leaveTime || item.updateTime);
							} else {
								item.ordered_time = that.G.getPointTime(item.updateTime, "MM--DD HH:MM");
								item.newtime = that.formatRelativeTime(item.updateTime || item.updateTime);
							}
							if (item.status != -1) {
								item.status_text = that.G.getOrderStatus().filter((itm, inx) => {
									return itm.id == item.status;
								})[0].name;
							}

							if (that.tabInfo[that.tabActive].classify == 2030) {
								// 当前
								item.right_time_str = that.G.setDeadLine(item.notifyTime || new Date().getTime(), "task");
							} else if (that.tabInfo[that.tabActive].classify == 30) {
								// 稍后
								item.right_time_str = that.G.setDeadLine(item.notifyTime || new Date().getTime(), "task");
							} else if (that.tabInfo[that.tabActive].classify == 40) {
								// 待审核
								item.right_time_str = that.G.setLWTRightTime(item.status10OperateTime || item.createTime || new Date().getTime(), that.tabInfo[that.tabActive].classify);
							} else if (that.tabInfo[that.tabActive].classify == 50) {
								// 新简历
								item.right_time_str = that.G.setLWTRightTime(item.createTime || new Date().getTime(), that.tabInfo[that.tabActive].classify);
							} else if (that.tabInfo[that.tabActive].classify == 60) {
								// 推工作
								item.right_time_str = that.G.setLWTRightTime(item.updateTime || new Date().getTime(), that.tabInfo[that.tabActive].classify);
							} else if (that.tabInfo[that.tabActive].classify == 70) {
								// 待面试
								item.right_time_str = that.G.setLWTRightTime(item.interviewTime || new Date().getTime(), that.tabInfo[that.tabActive].classify);
							} else if (that.tabInfo[that.tabActive].classify == 80) {
								// 待入职
								item.right_time_str = that.G.setLWTRightTime(item.willEntryTime || new Date().getTime(), that.tabInfo[that.tabActive].classify);
							} else if (that.tabInfo[that.tabActive].classify == 90) {
								// 在职中
								item.right_time_str = that.G.setLWTRightTime(item.updateTime || new Date().getTime(), that.tabInfo[that.tabActive].classify);
							} else if (that.tabInfo[that.tabActive].classify == 110) {
								// 已完成
								if (item.statusFailOperateTime) {
									item.right_time_str = that.G.setLWTRightTime(item.updateTime || new Date().getTime(), that.tabInfo[that.tabActive].classify);
								} else if (item.leaveTime) {
									item.right_time_str = that.G.setLWTRightTime(item.updateTime || new Date().getTime(), that.tabInfo[that.tabActive].classify);
								} else {
									item.right_time_str = that.G.setLWTRightTime(item.updateTime || new Date().getTime(), that.tabInfo[that.tabActive].classify);
								}
							}

							item.setTitle = that.G.titleToStr(item);
						});
					} else {
						// uni.setBackgroundColor({
						// 	backgroundColorBottom: "#ededed",
						// });
					}
					if ($type == "init") {
						that.query.list = res.recordList;
						// that.query.list = [...res.recordList, ...res.recordList, ...res.recordList, ...res.recordList, ...res.recordList];
					} else {
						that.query.list = that.query.list.concat(res.recordList);
					}
				},
			);
		},
		goTel($item) {
			// console.log($item);
			if ($item) {
				uni.makePhoneCall({
					phoneNumber: $item,
				});
			}
		},
		getGZHInfo() {
			let that = this;
			if (that.isLogin) {
				that.G.Get(that.api.user_getGZHstatus, { type: "BMZS" }, (res) => {
					// console.log(res);
					that.GZHInfo = res;
				});
			}
		},
		/**
		 * 跳转搜索页
		 */
		goSearch() {
			this.G.isLogin();
			if (this.G.isLogin()) {
				uni.navigateTo({
					url: `/root/other/search?from=apply&active=${this.tabActive}`,
				});
			}
		},
		// 一级tab切换
		handleUpdateTab(e) {
			let that = this;
			// console.log("e", e);
			this.tabActive = e;
			this.menuActive = 0;
			this.menuList = this.tabInfo[this.tabActive].childList;
			this.speed = -1;
			this.query.page = 1;
			that.G.isLogin();
			if (that.G.isLogin()) {
				this.getList();
			}
		},
		// 二级tab切换
		handleUpdateMenu($item, $index) {
			let that = this;
			// console.log("$item", $item);
			this.menuActive = $item;
			this.loading = true;
			this.speed = -1;
			this.query.page = 1;
			that.G.isLogin();
			if (that.G.isLogin()) {
				this.getList();
			}
		},
		getSearch(type) {
			let that = this;
			// console.log("typetypetype", type);
			// console.log("getSearch", that.keyword);
			if (type == "clear" && !that.keyword) {
				that.getList();
			} else if (type == "search") {
				that.getList();
			}
		},
		goDetail($item, $index) {
			let that = this;
			// console.log("$item", $item);
			// return
			uni.navigateTo({
				// url: "/root/detail/apply?id=" + $item.id + "&type=" + (that.tabActive + 1),
				url: `/root/detail/apply?id=${$item.id}&type=${that.tabActive + 1}&relationId=${$item.relationId}`,
			});
		},
		goLogin() {
			uni.reLaunch({
				url: "/root/person/loginIndex",
			});
		},
		toMessage() {
			uni.navigateTo({
				url: "/root/person/message",
			});
		},
	},
};
</script>

<style lang="scss">
.apply-header {
	position: fixed;
	left: 50%;
	transform: translateX(-50%);
	.box {
		padding: 0 2px;
		width: 148px;
		height: 32px;
		background-color: #eeeeee;
		border-radius: 4px;
		.item {
			width: 72px;
			height: 28px;
			margin-top: 2px;
			border-radius: 4px;
			.btn {
				font-size: 14px;
				color: #333;
			}
		}
		.item-active {
			background-color: #fff;
		}
	}
}
.p-apply-inedx {
	width: 100vw;
	background-color: #ededed;
	overflow-x: hidden;
	min-height: 100%;

	.bg_shawdoc {
		background: linear-gradient(180deg, #caf1e0 1%, rgba(249, 249, 249, 0));
	}

	.suffix {
		position: absolute;
		right: 20rpx;
		top: 10px;
		z-index: 1;
		border-top-right-radius: 110rpx;
		border-bottom-right-radius: 110rpx;
	}

	.tab-fixed {
		position: fixed;
		left: 0;
		top: 0;
		width: 100%;
		height: calc(112rpx + 36px);
		background-color: #fff;
		z-index: 1;
	}

	.m-search {
		.u-icon-wrap {
		}
	}

	.m-search-active {
		.u-icon-wrap {
			position: inherit;
			left: 0rpx;
		}
	}

	.m-select {
		.u-tab-bar {
			bottom: 5px !important;
		}

		.link {
			.item {
				.doc {
					bottom: 0;
					left: 50%;
					transform: translateX(-50%);
				}
			}
		}

		.showdoc {
			// box-shadow: 0 2px 4px rgba(0, 0, 0, .04), 0 0 6px rgba(0, 0, 0, .02);
			border-bottom: 1rpx solid #eee;
			overflow-x: "auto";
			width: 100vw;

			.menu-obj {
				min-width: 422px;

				.menu-btn {
					margin-right: 8px;

					.btn {
						background: rgba(204, 204, 204, 0.2);
						color: #666666;
					}

					&:first-child {
						margin-left: 12px;
					}
				}

				.menu-active {
					.btn {
						background: rgba(0, 182, 102, 0.1);
						color: #00b666;
					}
				}
			}
		}
	}
	.m-login-false {
		height: calc(100% - 92px);
		.m-panel,
		.link {
			height: 100%;
			.m-log {
				height: 100%;
			}
		}
	}
	.m-panel {
		min-height: 100%;
	}
	.m-box-fixed {
		position: fixed;
		left: 0;
		// top: 92px;
		width: 100vw;
		background-color: #fff;
		z-index: 999;
	}
}
.sticky {
	position: sticky;
	top: 0px;
	left: 0;
	z-index: 100;
}
</style>
