<template>
	<view class="g-components-yi-list-job g_bg_page g_bg_f_5" style="">
		<rh-loading paddingBottom="g_pb_120" v-if="loading && speed == -1" />
		<view
			v-if="!loading && speed == 0 && !isshowskit"
			style="width: calc(100% - 20px); margin: 0 auto"
			class="g_h_all g_flex_column_center"
			:style="{
				'padding-top': pt + 'rpx',
			}"
		>
			<rh-empty :text="emptyText" style="margin-top: 30px"></rh-empty>
		</view>
		<view v-if="speed > 0" class="link">
			<view class="">
				<view
					class="item"
					:style="{
						'border-bottom': isshowskit && item.isskit == 1 ? '0px solid #ededed' : '1px solid #f3f3f3',
						'padding-left': from == 'share' ? '36px' : '',

						'z-index': isshowskit && item.isskit == 1 ? '999' : 'none',
						'background-color': isshowskit && item.isskit == 1 ? '#ededed' : '#fff',
						height: isshowskit && item.isskit == 1 ? '60px' : 'nona',
						'margin-top': isshowskit && item.isskit == 1 ? '-10px' : '0',
					}"
					v-for="(item, index) in jobArray"
					:key="index"
					@click="handleCard(item, index)"
					:class="isshowskit && item.isskit == 1 ? 'g_pl_10 g_pr_10 g_flex_column_center' : ' g_position_rela g_mb_10 g_ml_12 g_mr_12 g_bg_f g_radius_8'"
				>
					<view class="g_c_9 g_fs_14 g_text_c" v-if="isshowskit && item.isskit == 1">
						{{ hrtext == 2 ? 
							"———以下是全部职位———" : 
							(tabactive == 1 && list && list.length == 0 ? "———暂无推荐，以下是全部职位———" : "———以下是全部职位———")
						}}
					</view>
					<view class="g_p_10" v-else>
						<i class="iconfont icon-yitingzhao" v-if="item.recruitment == 2 && from != 'mine'" style="position: absolute; left: 50%; top: 58%; transform: translate(-50%, -50%); color: #ff4d4f; font-size: 80px; z-index: 1"></i>
						<view class="m-top g_flex_row_between">
							<view class="g_flex_none g_mr_8 g_w_53 g_h_53 g_flex_c g_position_rela" style="overflow: hidden" v-if="appid != 'wxb0c590fd696b79be' && !isUseNewJob">
								<image :src="item.logo ? item.logo : defaultLogo" class="g_w_53" mode="widthFix"></image>
								<view class="r-obj" hover-class="none" v-if="item.recommend == 1"> 主推 </view>
							</view>
							<view class="g_flex_1 g_flex_column_between">
								<view class="g_flex_1 g_flex_row_between g_lh_1">
									<view class="g_flex_none g_flex_column_start">
										<view class="g_ell_1 g_fs_18 g_fw_600" style="color: rgba(0, 0, 0, 0.85); max-width: 500rpx; line-height: 1.3">{{ item.jobName }}</view>
									</view>
									<view class="g_fs_14 g_flex_row_end" style="color: rgba(0, 0, 0, 0.45)">
										<view>{{ timeShowXXX(item.updateTime) }}</view>
									</view>
								</view>

								<view class="g_flex_none g_flex_row_between g_mt_6">
									<view class="g_flex_row_between flex_center g_fs_14 g_flex_1" style="color: rgba(0, 0, 0, 0.45)">
										<view class="g_ell_1" style="max-width: 250px">
											<view class="" style=""> {{ item.newadd }}{{ item.newadd ? "丨" : "" }}{{ item.gender + "丨" + item.age }} </view>
										</view>
										<view class="">
											{{ item.distance && item.storeDistrict ? item.distance : "" }}
										</view>
									</view>
									<view class="g_flex_none g_flex_column_center b_btn" @click.stop="openApplyPop(item, index)" v-if="false"> 立即报名 </view>
								</view>
							</view>
						</view>
						<view class="m-bottom g_flex_row_between g_mt_8">
							<view class="g_flex_none g_mr_8 g_w_53" v-if="appid != 'wxb0c590fd696b79be' && !isUseNewJob">
								<view class="collect g_flex_row_center flex_center" @click.stop="handleCollect(item, index)" :class="item.collected == 0 ? '' : 'collectred'" style="width: 52px">
									<view class="g_flex_column_center g_h_20">
										<icon class="iconfont icon-shoucang icon 0" style="color: #888" v-if="item.collected == 0"></icon>
										<icon class="iconfont icon-yishoucang g_c_f40 icon 1" style v-else></icon>
									</view>
									<view class="g_flex_column_center g_h_20">
										<text class="f10" :class="item.collected == 0 ? 'g_c_8' : 'g_c_f40'" style="margin-left: 2px; height: 20px; line-height: 20px">
											{{ item.collected == 0 ? "收藏" : "已收藏" }}
										</text>
									</view>
								</view>
							</view>
							<view class="g_flex_1 g_flex_row_between flex_center">
								<view class="g_flex_1 g_flex_row_between">
									<view class="g_flex_row_start_none g_flex_1" style="max-width: 75%; flex-wrap: wrap; height: 22px; overflow: hidden">
										<view class="label_btn" v-for="(itm, inx) in item.jobSpecialLabelNameArray" :key="inx">{{ itm }}</view>
										<view class="label_btn" v-if="item.jobSpecialLabelNameArray.length == 0"> 暂无特色 </view>
									</view>
								</view>
							</view>
						</view>
						<view class="g_mt_6">
							<view class="g_flex_row_between flex_center g_fs_18 g_c_f40 g_fw_600">
								<rich-text class="g_lh_1" :nodes="item.cus_price" v-if="item.cus_price != '月薪'"></rich-text>
								<view v-else>{{ item.monthlyPay }}<span class="g_fs_14"> 元/月 </span> </view>
								
								<view class="g_flex_row_start g_fs_12 g_pr_4 g_h_20 g_fw_400" 
											style="background: linear-gradient(138deg, #fde0ad 22%, #fac474); border-radius: 2px; color: #754300; line-height: 20px"
								>
									<view class>
										<image class="g_mr_4" 
													 style="width: 20px; height: 20px; display: block" 
													 src="https://bocai-cms.oss-cn-beijing.aliyuncs.com/web-resource/1shoudan/fee.svg" 
													 mode="aspecFill" 
													 lazy-load="false"
										></image>
									</view>
									<view class="g_fs_13" v-if="!isLogin" @click.stop="goLogin">登录查看</view>
									<view class="g_fs_13" v-else-if="isfensi == 2">{{ item.fuWuFei || "详询客服" }}</view>
									<view class="biggerSize2 g_fs_13" v-else>{{ "仅代理可见" }}</view>
								</view>
								
							</view>
							<!-- <view class="g_flex_column_start g_fs_22 g_c_f40 g_fw_600">
								<rich-text class="g_lh_1" :nodes="item.cus_price" v-if="item.cus_price != '月薪'"></rich-text>
								<view class="g_lh_1" v-else>{{ item.monthlyPay }}<span class="g_fs_14"> 元/月 </span> </view>
							</view> -->
						</view>

						<view class="checkBox" hover-class="none" v-if="from == 'share'" @click.stop="checkJob(item)">
							<checkbox class="biggerSize" style="transform: scale(0.8, 0.8)" :color="themeColor" :checked="item.checked" :value="item.checked"></checkbox>
						</view>
						<view class="g_border_e_t g_p_8" style="line-height: 23px;margin-top: 12px;padding-left: 0;padding-bottom: 0;"
								  v-if="isJm && item.newXmf"
						>
							<view class="g_flex_row_start flex_center" >
								<view class="g_c_6 g_fs_12 g_pr_4 g_radius_4 g_flex_row_start flex_center" style="background: #f1faff">
									<img class="g_w_36 g_h_20 g_mr_4" data-use="1" src="https://bocai-cms.oss-cn-beijing.aliyuncs.com/web-resource/bocaigongyinglian/xmf.svg" alt="" />
									<view>
										{{ item.newXmf || "-" }}
									</view>
								</view>
							</view>
						</view>
					</view>
					<view class="g_flex_row_end g_p_10 g_border_e_t" v-if="(from == 'tob' && item.agencyId == userInfo.agencyId) || from == 'chat'">
						<view class="g_flex_row_between flex_center g_flex_1" v-if="from == 'tob' && item.agencyId == userInfo.agencyId" @click.stop>
							<u-switch v-model="item.recruitmentValue" :showText="true" openText="开" closeText="停" class="" size="44" @change="recruitmentChange($event, item)" :activeColor="themeColor"></u-switch>
							<view class="g_flex_column_center g_pl_10 g_h_all biggerSize" @click.stop="showConfig(item)">
								<i class="iconfont icon-gengduo2 icon g_c_b1" style="font-size: 32rpx"></i>
							</view>
						</view>
						<view v-if="from == 'chat'" @click.stop>
							<rh-button btnText="发送" fontSize="14" type="primary" size="mini" @clickBtn="sendJob(item)" />
						</view>
						<view class="g_w_72" v-if="from == 'home' && false" @click.stop>
							<rh-button btnText="聊一聊" height="24" fontSize="14" size="small_auto" @clickBtn="showService(item)"> </rh-button>
						</view>
					</view>
				</view>
			</view>
			<view class="g_mt_24 g_pb_32 g_c_9 g_pl_12 g_text_c">
				<view class="" v-if="isShowLoginBtn" hover-class="none">
					<view class=""> 登录后查看全部职位。</view>
					<view class="g_text_c g_c_sub g_mt_24 g_fs_14">
						<rh-quicklogin mobileToLogin="/appLoginByTelAssistant" codeToMobile="/getWechatTel" @exportSetToken="pushSetToken">马上登录</rh-quicklogin>
					</view>
				</view>

				<view class="" v-if="jobArray && jobArray.length > 0 && !isShowLoginBtn && from != 'collect'">
					<u-loadmore :status="status" :loadText="{ loadmore: '加载中', loading: '加载中', nomore: speed + '个职位' }" />
				</view>
			</view>
		</view>

		<u-popup v-model="popApply.isShow" mode="center" z-index="999999" border-radius="12" :closeable="true" :mask-close-able="true" @close="handleClose">
			<view class="">
				<view class="g_text_c g_fs_18 g_fw_600 g_pt_18 g_flex_c" style="position: absolute; top: 0; left: 0; width: 100%">快速报名</view>
				<applyPanel :jid="jid" @exportForm="submitApply" />
			</view>
		</u-popup>
	</view>
</template>

<script>
import applyPanel from "./applyRecord.vue";
export default {
	components: {
		applyPanel,
	},
	data() {
		return {
			isUseNewJob: getApp().globalData.isUseNewJob,
			localBaseImg: this.G.store().localBaseImg,
			themeColor: getApp().globalData.themeColor,
			userInfo: uni.getStorageSync("apply-userinfo"),
			appid: getApp().globalData.appid,
			defaultLogo: "",
			cdnBaseImg: this.G.store().cdnBaseImg,
			jobArray: [],
			isLogin: false,
			popApply: {
				isShow: false,
			},
			jid: 0,
			appInfo: {},
			goImLoading: false,
			isAgency:uni.getStorageSync("apply-userinfo") && uni.getStorageSync("apply-userinfo").agencyId,
			isJm:uni.getStorageSync("apply-userinfo") && uni.getStorageSync("apply-userinfo").agencyId && uni.getStorageSync("apply-userinfo").agencyId == '114827',
			isfensi:uni.getStorageSync("apply-userinfo") && uni.getStorageSync("apply-userinfo").isfensi,
		};
	},
	// 计算属性
	computed: {
		status() {
			// console.log("this.query", this.query);
			if (this.query.isFinish >= 0 && this.query.isFinish < this.query.size) {
				return "nomore";
			} else {
				return "loading";
			}
		},
	},
	props: {
		tabactive:{
			default: () => {
				return -1;
			},
		},
		hrtext: {
			default: () => {
				return 2;
			},
		},
		list: {
			type: Array,
			default: () => {
				return [];
			},
		},
		loading: {
			type: Boolean,
			default: () => {
				return true;
			},
		},
		isshowskit: {
			type: Boolean,
			default: () => {
				return true;
			},
		},
		speed: {
			type: Number,
			default: () => {
				return -1;
			},
		},
		query: {
			type: Object,
			default: () => {
				return {
					page: 1,
					size: 10,
					list: [],
					isFinish: -1,
				};
			},
		},
		from: {
			type: String,
			default: () => {
				return "default";
			},
		},
		isuse: {
			type: String,
			default: () => {
				return "default";
			},
		},
		emptyText: {
			type: String,
			default: () => {
				return "嘿，这里还没有数据呢";
			},
		},
		isShowLoginBtn: {
			default: () => {
				return false;
			},
		},
		pt: {
			default: 0,
		},
	},
	watch: {
		list(val) {
			this.jobArray = val;
			this.appInfo = uni.getStorageSync("miniApp-info");
			this.$forceUpdate();
			if (uni.getStorageSync("apply-token")) {
				this.isLogin = true;
			} else {
				this.isLogin = false;
			}
		},
	},
	beforeCreate() {
		setTimeout(() => {}, 1000);
	},
	created() {
		let that = this;
		uni.$on('themeChanged', (color) => {
			that.themeColor = color;
		});
		that.defaultLogo = uni.getStorageSync("miniApp-info").logo;
		this.jobArray = this.list;
		if (uni.getStorageSync("apply-token")) {
			that.isLogin = true;
		} else {
			that.isLogin = false;
		}
	},
	methods: {
		getEmptyDom(text = "暂无数据", subText = "") {
			this.G.getEmptyDom(text, subText);
		},
		getHrDom($str) {
			this.G.getHrDom($str);
		},
		timeShowXXX(val) {
			if (!val) {
				return "";
			}
			let date = "";
			let differenceTime = new Date().getTime() - new Date(val).getTime();
			var D = Math.floor(differenceTime / (24 * 60 * 60 * 1000));
			var level1 = differenceTime % (24 * 3600 * 1000);
			var H = Math.floor(level1 / (3600 * 1000));
			var level2 = level1 % (3600 * 1000);
			var M = Math.floor(level2 / (60 * 1000));
			var level3 = level2 % (60 * 1000);
			var S = Math.floor(level3 / 1000);
			date = null;
			if (D > 99) {
				date = "99+天前";
			} else if (D > 0 && D < 100) {
				date = D + "天前";
			} else if (D > 0) {
				let date1 = new Date(val);
				var y = date1.getFullYear();
				var m = date1.getMonth() + 1;
				m = m < 10 ? "0" + m : m;
				var d = date1.getDate();
				d = d < 10 ? "0" + d : d;
				var h = date1.getHours();
				h = h < 10 ? "0" + h : h;
				var minute = date1.getMinutes();
				minute = minute < 10 ? "0" + minute : minute;
				var second = date1.getSeconds();
				second = second < 10 ? "0" + second : second;
				date = y + "-" + m + "-" + d + " " + h + ":" + minute;
			} else if (H > 0) {
				date = H + "小时前";
			} else if (M > 0) {
				date = M + "分钟前";
			} else {
				date = "刚刚";
			}
			return date;
		},
		handleCard($item) {
			// console.log("$item", $item);
			let that = this;
			if (that.isshowskit && $item.isskit == 1) {
			} else {
				if (that.from == "record") {
					uni.$emit("changeJobInfo", { info: $item });
					uni.navigateBack({
						delta: 1,
					});
					return;
				} else {
					this.$emit("clickCard", $item);
				}
			}
		},
		goLogin() {
			uni.reLaunch({
				url: "/root/login/index",
			});
		},
		openApplyPop($item) {
			let that = this;
			this.getCode((res) => {
				uni.setStorageSync("apply-code", res);
				that.jid = $item.id;
				that.popApply.isShow = true;
			});
		},
		getCode(callback = () => {}) {
			uni.login({
				success(wxRes) {
					callback(wxRes.code);
				},
				fail(res) {
					// console.log(res);
				},
			});
		},
		handleClose() {
			// uni.showTabBar();
		},
		checkJob(_item) {
			this.$emit("checkJob", _item);
		},
		sendJob(_item) {
			this.$emit("sendJob", _item);
		},
		submitApply(e) {
			let that = this;
			let params = {
				userName: e.name,
				tel: e.tel,
				jobId: that.jid,
			};
			// console.log('uni.getStorageSync("AGENCY_ID")', uni.getStorageSync("AGENCY_ID"));
			// console.log('uni.getStorageSync("userinfo")', uni.getStorageSync("apply-userinfo"));
			return;
			if (uni.getStorageSync("AGENCY_ID") == uni.getStorageSync("apply-userinfo").user.agencyId) {
				params.agencyUserId = uni.getStorageSync("apply-userinfo").user.id;
			}

			that.G.Post(that.api.yi_job_recordJob, params, (res) => {
				that.popApply.isShow = false;
				// uni.showTabBar();
				uni.showToast({
					title: "报名成功",
					icon: "success",
				});
			});
		},
		handleCollect($item) {
			let that = this;
			if (this.isLogin) {
				// uni.navigateTo({
				// 	url: "/root/detail/work?id=" + $item.id,
				// });
				$item.collected = !$item.collected;
				that.G.Get(that.api.yi_job_collect + "/" + $item.id, {}, (res) => {
					if ($item.collected) {
						uni.showToast({
							icon: "none",
							title: "收藏成功",
						});
					} else {
						uni.showToast({
							icon: "none",
							title: "取消收藏",
						});
					}
				});
			} else {
				uni.showModal({
					title: "登录提示",
					content: "收藏功能需要登录才能使用,是否登录",
					success(res) {
						if (res.confirm) {
							uni.navigateTo({
								url: "/root/login/index",
							});
						} else {
						}
					},
				});
			}
		},
		pushSetToken(e) {
			let that = this,
				$data = e;
			// console.log("获取登录用户信息：", $data);
			that.G.checkToken().then(() => {
				that.isLogin = true;
				that.loginInfo = uni.getStorageSync("apply-userinfo");
				//联系客服
				this.$emit("successLogin");
				uni.$emit("isGlogin", {
					uid: $data.user.id,
				});
			});
		},
		recruitmentChange($e, $item) {
			let that = this;
			// console.log($e);
			// console.log($item);
			this.G.Get(this.api.update_job_recruitment + "/" + $item.id, { recruitment: $e ? 1 : 2 }, (res) => {
				// console.log("res", res);
				$item.recruitment = $e ? 1 : 2;
				// that.$emit("updateList");
			});
		},
		showConfig(_item) {
			let that = this;

			// console.log(_item);
			this.jid = _item.id;
			uni.showActionSheet({
				title: "",
				itemList: ["编辑", "删除"],
				success: (res) => {
					if (res.tapIndex === 0) {
						// 保存图片到相册
						that.updateJob();
					} else if (res.tapIndex === 1) {
						that.delJob();
					}
				},
				fail: (err) => {
					// console.log("用户取消操作");
				},
			});
			// this.popApply.isShow = true;
		},
		updateJob() {
			let that = this;
			uni.navigateTo({
				url: `/root/merchantManagement/jobForm?id=${that.jid}`,
			});
			this.popApply.isShow = false;
		},
		delJob() {
			let that = this;
			this.G.handleConfirm({
				content: "是否删除该职位",
				success(res) {
					if (res.confirm) {
						that.G.Post(that.api.job_delJob + "/" + that.jid, {}, (res) => {
							// console.log(res);
							that.$emit("updateList");
							that.popApply.isShow = false;
						});
					}
				},
			});
		},
		showService(_item) {
			let that = this;
			// console.log("_item", _item);
			if (!that.isLogin) {
				uni.showToast({
					title: "请先登录",
					icon: "none",
				});
				return;
			}
			if (that.goImLoading) {
				return false;
			}

			that.goImLoading = true;
			that.F.wyyxGet(that.api.job_get_cus + "?jobId=" + _item.id, {}, (cusData) => {
				that.F.wyyxPost(
					that.api.wyyx_create,
					{
						// senderUserId: uni.getStorageSync("apply-uid"),
						// receiverUserId: cusData,
						senderAccid: uni.getStorageSync("im-accid"),
						receiverAccid: cusData,
					},
					(res) => {
						uni.$UIKitStore.uiStore.selectConversation(res.conversationId);
						let list = ["district", "gender", "age"];
						let newList = [];
						list.forEach((item) => {
							if (_item[item]) {
								newList.push(_item[item]);
							}
						});
						_item.info = newList.join("丨");
						// console.log("_item", _item);
						that.F.wyyxPost(
							that.api.wyyx_sendCard,
							{
								conversationType: 1,
								// senderUserId: uni.getStorageSync("apply-uid"), // 发送者id 即当前登录用户id
								// receiverUserId: cusData, // 接收方用户id
								senderAccid: uni.getStorageSync("im-accid"),
								receiverAccid: cusData,
								type: 100000,
								jobDetail: {
									title: _item.jobName || "",
									info: _item.info,
									label: _item.jobSpecialLabelNameArray.length > 0 ? _item.jobSpecialLabelNameArray : [],
									salaryClassifyValue: _item.priceStr || "",
									serviceFee: that.showPrice ? _item.serverPrice : "",
									monthPay: _item.minMonthlyPay / 100 + "-" + _item.maxMonthlyPay / 100 || "",
									jobId: _item.id || "",
								},
							},
							() => {
								uni.navigateTo({
									url: "/root/NEUIKit/pages/Chat/index",
								});
								that.goImLoading = false;
							},
							() => {},
						);
					},
					() => {},
				);
			});
		},
	},
};
</script>

<style lang="scss">
.g-components-yi-list-job {
	height: 100%;
	.label_btn {
		display: inline-block;
		height: 20px;
		box-sizing: border-box;
		font-size: 12px;
		font-weight: 400;
		color: #666;
		background: #f1faff;
		border-radius: 2px;
		padding: 0 4px 0px;
		line-height: 20px;
		margin-right: 6px;
		margin-bottom: 0px;
		position: relative;
		top: 1px;
	}
	.b_btn {
		width: 72px;
		height: 24px;
		background-color: #ff4400;
		font-size: 14px;
		border-radius: 99px;
		line-height: 24px;
		color: #fff;
		text-align: center;
	}
	.collect {
		border: 1px solid rgb(236, 235, 235);
		border-radius: 2px;
		height: 20px;
		box-sizing: border-box;
		text-align: center;
		margin: 0;
		padding: 0px;
		background-color: #fff;
		line-height: 20px;
		width: 52px;
		// margin-top: 2px;
		.icon {
			font-size: 10px;
			// height: 22px;
			line-height: 38px;
		}
		.f10 {
			font-size: 10px !important;
		}
		.lh12 {
			line-height: 12px;
		}
	}
	.collectred {
		background: #ffeee9;
		color: #ff4400;
		border: 1rpx solid #ff4400;
	}
	.r-obj {
		width: 28px;
		background-color: #ff4400;
		position: absolute;
		right: 0;
		top: 0px;
		border-radius: 0;
		color: #fff;
		font-size: 12px;
		zoom: 0.85;
		padding: 0px;
		text-align: center;
	}
}
.u-switch-text .u-switch {
	border: 0;
}
.checkBox {
	position: absolute;
	left: 4px;
	top: 50%;
	transform: translateY(-50%);
	height: 100px;
	display: flex;
	flex-direction: column;
	justify-content: center;
}
.biggerSize::after {
	content: "";
	/* display: inline-block; */
	width: 40px;
	height: 100px;
	position: absolute;
	left: 50%;
	top: 50%;
	z-index: 99;
	transform: translate(-50%, -50%);
}
</style>
