<template>
	<view class="g-components-list-job">
		<!-- {{classify}} -->
		<g-loading paddingBottom="g_pb_120" v-if="loading && speed == -1" />

		<view v-if="!loading && speed == 0" style="width: calc(100% - 20px); margin: 0 auto" class="g_h_all g_flex_column_center">
			<view>
				<g-panel-fond v-if="showShare && isHaveOrder" :showRemark="false" @clickCard="goPage" />
				<view class="g_h_60" style="width: 100%"></view>
			</view>
			<g-empty :text="emptyText" :subText="emptySubText" />
			<view class="g_mt_32" v-if="isShowLoginBtn">
				<g-button btnText="去登录" size="small" @clickBtn="goLogin" />
			</view>
		</view>
		<!-- {{ from }} -->

		<view v-if="speed > 0" class="link g_pl_10 g_pr_10">
			<view>
				<view v-for="(item, index) in jobArray" :key="index" class="g_bg_f g_radius_8 g_mb_10 sitem" hover-class="g_bg_e" @click="handleCard(item)" style="box-shadow: 0px 2px 9px 0px #eeeeee; position: relative">
					<!-- {{ from }} -->
					<view v-if="from == 'tob'">
						<view class="g_pt_16 g_pb_12 g_pl_16 g_pr_16" @click="handleTop(item, from)">
							<topPanel :item="item" :from="from" :classify="classify" :recordStatus="recordStatus" :isSelf="isSelf" />
						</view>
						<infoPanel :recordStatus="recordStatus" :classify="classify" :item="item" :from="from" @clickBottom="handleBotom" @clickMore="handleOpenStatus(item)" />
					</view>
					<view v-else class="g_pt_16 g_pb_12 g_pl_16 g_pr_16" @click="handleTop(item, from)">
						<view class="" hover-class="none" hover-stop-propagation="false">
							<view class="g_flex_1 g_flex_row_between">
								<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: 420rpx">{{ item.storeJobName || "我是职位名" }}</view>
								</view>
								<!-- {{item.cus_price}} -->
								<view class="g_flex_column_start g_fs_20 g_c_f40 g_fw_600">
									<!-- <rich-text :nodes="item.cus_price" v-if="item.cus_price != '月薪'"></rich-text>
									<view v-else>{{ item.monthlyPay }}</view> -->
									<view class="g_fs_18" v-html="item.cus_price"></view>
								</view>
							</view>
							<view class="g_flex_none g_flex_row_start g_fs_14 g_mt_8" style="color: rgba(0, 0, 0, 0.45)">
								<view class="g_ell_1" style="max-width: 264px" hover-class="none" hover-stop-propagation="false">
									{{ item.addresa + (item.ageStr ? " | " + item.ageStr : "") }}
								</view>
							</view>
							<view class="g_flex_1 g_flex_row_center g_mt_8">
								<view class="g_flex_row_start_none g_flex_1">
									<!-- <view class="label_btn" v-for="(itm, inx) in item.jobSpecialLabelNameArray" :key="inx">{{ itm }}</view> -->
									<view class="label_btn" v-for="(itm, inx) in item.jobSpecialLabelNames" :key="inx">{{ itm }}</view>
								</view>
								<!-- <view class="g_fs_14" style="color: rgba(0, 0, 0, 0.45)">{{ dateUtil.timeShowXXX(item.updateTime) }}</view> -->
							</view>
							<view class="g_flex_1 g_flex_row_between g_mt_8">
								<view class="g_g_fs_14 g_c_6" hover-class="none" hover-stop-propagation="false"> {{ item.createTime }} </view>
								<view class="g_fs_14 g_c_6">{{ item.status_text }}</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view>
				<view>
					<g-panel-hr :str="query.isFinish >= 0 && query.isFinish < query.size ? speed + '条记录' : '加载中'" />
				</view>
			</view>
			<view class="g_h_17" v-if="!emptyText && !emptySubText"></view>
			<!-- #ifdef H5 -->
			<view style="height: 50px"></view>
			<!-- #endif -->
		</view>
		<u-popup v-model="showSharePop" mode="bottom" uZindex="9999" border-radius="12" @close="watchClose" :mask-close-able="true" width="694rpx">
			<view class="g_fs_18 g_text_c g_bg_f_5">
				<view class="g_bg_f">
					<view class="g_h_56 g_flex_column_center g_border_e_b" v-for="(item, index) in statusList" :key="index" @click="handlerJob(item)">
						<view class="g_h_56 g_fs_18 g_c_3 g_flex_c">{{ item }}</view>
					</view>
				</view>
				<view class="g_h_64 g_mt_8 g_text_c g_pt_16 g_bg_f" @click="hideDrawer"> 取消 </view>
			</view>
		</u-popup>
	</view>
</template>

<script>
/* 职位列表
 * @params list 数组
 * @params loading
 * @params speed 加载进度
 * @params query 参数
 * @params showShare 是否展示分享面板（寻找发单号）
 * @params from 来源
 * @function clickCard 整个卡片的点击事件
 * @function clickTop 卡片上半部分的点击事件
 * @function clickBottom 卡片底部信息的点击事件
 * @function clickSwitch 开停招
 * @function clickMore 点击三个点
 * @function clickBtn 按钮点击事件
 */
import topPanel from "./jobChild/top.vue";
import infoPanel from "./jobChild/info.vue";
export default {
	components: {
		topPanel,
		infoPanel,
	},
	data() {
		return {
			cdnBaseImg: this.G.store().cdnBaseImg,
			statusList: [],
			editJobList: ["复制内容", "编辑", "删除"],
			showSharePop: false,
			currentJob: {},

			jobArray: [],
			loginId: "",
		};
	},
	props: {
		list: {
			type: Array,
			default: () => {
				return [];
			},
		},

		classify: {
			type: Number,
			default: () => {
				return 0;
			},
		},
		isSelf: {
			type: Boolean,
			default: () => {
				return false;
			},
		},
		loading: {
			type: Boolean,
			default: () => {
				return true;
			},
		},
		speed: {
			type: Number,
			default: () => {
				return -1;
			},
		},
		query: {
			type: Object,
			default: () => {
				return {
					page: 1,
					size: 10,
					list: [],
					isFinish: -1,
				};
			},
		},
		showShare: {
			type: Boolean,
			default: () => {
				return true;
			},
		},
		from: {
			type: String,
			default: () => {
				return "default";
			},
		},
		emptyText: {
			type: String,
			default: () => {
				return "嘿，这里还没有数据呢";
			},
		},
		emptySubText: {
			type: String,
			default: () => {
				return "";
			},
		},
		isHaveOrder: {
			default: () => {
				return false;
			},
		},
		active: {
			default: () => {
				return 0;
			},
		},
		recordStatus: {
			default: () => {
				return 1;
			},
		},
		bg: {
			default: () => {
				return "#f5f5f5";
			},
		},
		isShowLoginBtn: {
			default: () => {
				return false;
			},
		},
	},
	watch: {
		list(val) {
			this.jobArray = val;
			this.$forceUpdate();
		},
		showSharePop(val) {
			if (val == false) {
				this.currentJob = {};
				setTimeout(() => {
					uni.showTabBar();
				}, 200);
			}
		},
	},
	created() {
		let that = this;
		this.jobArray = this.list;
	},
	methods: {
		handleCard($item) {
			// this.$emit("clickCard", $item);
		},
		handleTop($item, $from) {
			let that = this;
			console.log("handleTop");
			console.log("that.from", that.from);
			this.G.isLogin();
			if (this.G.isLogin()) {
				if (that.from == "toc" || that.from == "tob") {
					console.log("$item", $item);
					// that.G.Get(
					// 	that.api.readorder_resume,
					// 	{
					// 		relationId: $item.relationId || "",
					// 		clueId: $item.realResumeId,
					// 	},
					// 	(res) => {
					// 		console.log(res);
					// 	},
					// 	(err) => {}
					// );
					// console.log('$item',$item);
					// return
					let id, resumeFlag;
					if (that.from == "toc") {
						if ($item.id) {
							id = $item.id;
							resumeFlag = 2;
						} else {
							resumeFlag = 1;
							id = $item.resumeId;
						}
            delete $item.cus_price
						uni.navigateTo({
							url: `/root/detail/applyToc?id=${id}&relationId=${$item.relationId || ""}&resumeFlag=${resumeFlag}&from=${$from}&item=${JSON.stringify($item)}`,
							// url: "/root/detail/apply?id=" + $item.id + "&type=" + (that.tabActive + 1),
						});
					} else {
						if ($item.resumeFlag == 1) {
							id = $item.realResumeId;
						} else {
							id = $item.realUserApplyOrderId;
						}
						resumeFlag = $item.resumeFlag;
						uni.navigateTo({
							url: `/root/detail/applyTob?id=${id}&relationId=${$item.relationId || ""}&resumeFlag=${resumeFlag}&from=${$from}`,
							// url: "/root/detail/apply?id=" + $item.id + "&type=" + (that.tabActive + 1),
						});
					}

					return;
				}
				if (that.from == "townsman") {
					uni.$emit("getTownsman", { info: $item });
					uni.navigateBack({
						delta: 1,
					});
					return;
				}
			}
		},
		handleBotom($item) {
			let that = this;
			this.G.isLogin();
			this.loginId = uni.getStorageSync("apply-userinfo").agencyId;
			if (this.loginId == $item.agencyId) {
				return;
			}
			if (this.G.isLogin()) {
				if (that.recordStatus == 1 || that.recordStatus == 2) {
					let params = {
						id: $item.agencyId,
						type: that.active + 1,
						bindid: $item.id,
						isShowMore: true,
						isShowJob: true,
					};
					uni.navigateTo({
						url: "/root/detail/user?" + that.G.objToStr(params),
					});
				} else {
					uni.showToast({
						title: "关注发单号后才可查看详情",
						icon: "none",
					});
				}
			}
		},
		handleOpenStatus(info) {
			let that = this;
			console.log("handleOpenStatus");
			return;
			that.currentJob = info;
			let popList = [];
			if (info.tip == 0) {
				popList = that.setList(info);
			} else if (info.tip == 1) {
				popList = that.setList(info);
			} else if (info.tip == 2) {
				if (info.collect == 0) {
					// 未
					popList = ["复制内容", "收藏该职位"];
				} else if (info.collect == 1) {
					//
					popList = ["复制内容", "取消收藏"];
				}
			} else if (info.tip == 3) {
				popList = that.setList(info);
			} else {
				popList = that.setList(info);
			}
			that.statusList = popList;
			uni.showActionSheet({
				itemList: popList,
				success: function (res) {
					that.handlerJob(popList[res.tapIndex]);
				},
				fail: function (res) {},
			});
		},
		setList(info) {
			let that = this,
				popList = [];
			// popList = ["复制内容",  "转发到我的发单号", "转发到我的招工小程序", "忽略该职位", "收藏该职位"];
			if (info.collect == 0 && info.ignore == 0) {
				// 未
				popList = ["复制内容", "忽略该职位", "收藏该职位"];
			} else if (info.collect == 0 && info.ignore == 1) {
				// 未
				popList = ["复制内容", "取消忽略", "收藏该职位"];
			} else if (info.collect == 1 && info.ignore == 0) {
				// 未
				popList = ["复制内容", "忽略该职位", "取消收藏"];
			} else if (info.collect == 1 && info.ignore == 1) {
				// 未
				popList = ["复制内容", "取消忽略", "取消收藏"];
			}
			return popList;
		},
		watchClose(e) {},
		handlerJob(e) {
			let that = this;
			switch (e) {
				case "复制内容":
					that.getJobDetail("shareInfo");
					break;
				case "分享给老乡":
					console.log("that.currentJob", that.currentJob);
					that.getJobDetail("shareInfo");
					break;
				case "转发到我的发单号":
					break;
				case "转发到我的招工小程序":
					that.G.handleConfirm(
						"招工小程序正在建设中",
						() => {},
						() => {},
						"",
						false,
						"知道了"
					);
					break;
				case "忽略该职位":
					that.G.handleConfirm(
						"忽略该职位，将不在关注里显示",
						() => {
							that.handleToggleJob("已忽略");
						},
						() => {}
					);
					break;
				case "取消忽略":
					that.G.handleConfirm(
						"是否取消忽略",
						() => {
							that.handleToggleJob("已显示");
						},
						() => {}
					);
					break;
				case "收藏该职位":
					that.handleCollectionJob("收藏成功");
					break;
				case "取消收藏":
					that.handleCollectionJob("取消收藏");
					break;
				default:
					break;
			}
		},
		handleToggleJob($tip) {
			let that = this;
			that.G.Get(
				that.api.job_updateStatus + "/" + that.currentJob.leafCateId,
				{
					classify: 1,
				},
				(res) => {
					uni.showToast({
						title: $tip,
						icon: "success",
					});
					that.$emit("uploadList");
				}
			);
		},
		handleCollectionJob($tip) {
			let that = this;
			that.G.Get(
				that.api.job_updateStatus + "/" + that.currentJob.leafCateId,
				{
					classify: 0,
				},
				(res) => {
					uni.showToast({
						title: $tip,
						icon: "success",
					});
					that.$emit("uploadList");
				}
			);
		},
		/**
		 * 获取职位描述并复制
		 */
		getJobDetail(type) {
			let that = this;
			that.G.Get(that.api.job_getDetailInList + "/" + that.currentJob.leafCateId, "", (res) => {
				if (!res) {
					uni.showToast({
						title: "该职位无内容",
						icon: "none",
					});
				} else {
					if (type == "shareInfo") {
						let info = that.currentJob.title + "\n" + that.currentJob.address + "\n" + that.currentJob.price + "\n" + res;

						if (info.indexOf("————————" > -1)) {
							info = info.replace(/\*\*\*\*\*/g, "").split("————————")[0];
						} else {
							info = info.replace(/\*\*\*\*\*/g, "");
						}
						uni.setClipboardData({
							data: info,
							success(res) {
								uni.showToast({
									title: "内容已复制",
									icon: "none",
									duration: 1500,
								});
							},
							fail(err) {},
						});
					} else {
						uni.setClipboardData({
							data: res.replace(/\*\*\*\*\*/g, ""),
							success(res) {
								uni.showToast({
									title: "内容已复制",
									icon: "none",
									duration: 1500,
								});
							},
							fail(err) {},
						});
					}
				}
				that.showSharePop = false;
			});
		},
		hideDrawer() {
			let that = this;
			that.showSharePop = false;
		},
		/**
		 * 删除职位
		 */
		delJob() {
			let that = this;
			that.G.handleConfirm(
				"是否确定删除该职位",
				() => {
					that.G.Get(
						that.api.job_delJob + "/" + that.currentJob.leafCateId,
						"",
						(res) => {
							uni.showToast({
								title: "删除成功",
								icon: "success",
							});
							that.$emit("uploadDetail");
							that.$emit("uploadList");
						},
						(err) => {}
					);
				},
				() => {}
			);
		},
		goPage() {
			this.G.isLogin();
			if (this.G.isLogin()) {
				uni.navigateTo({
					url: "/root/bind/search?active=0",
				});
			}
		},
		// 开停招
		handleSwitch(e, $item, $index) {
			let that = this;
			if (e) {
				that.list.filter((item, index) => {
					return index == $index;
				})[0].recruitmentImage = 1;
			} else {
				that.list.filter((item, index) => {
					return index == $index;
				})[0].recruitmentImage = 2;
			}
			that.G.Get(
				that.api.job_setStatus + "/" + $item.jobId,
				{
					id: $item.jobId,
					recruitment: e ? 1 : 2,
				},

				() => {
					uni.showToast({
						icon: "success",
						title: e ? "在招中" : "已停招",
					});
					// that.$emit("uploadList");
					that.$emit("uploadDetail");
				}
			);
		},
		// 点击按钮
		handleBtn(e) {
			this.$emit("clickBtn", e);
		},
		goLogin() {
			uni.reLaunch({
				url: "/pages/login/index",
			});
		},
	},
};
</script>

<style lang="scss">
.g-components-list-job {
	height: 100%;
}
.u-switch-text .u-switch {
	border: 0;
}
.label_btn {
	display: inline-block;
	height: 20px;
	box-sizing: border-box;
	font-size: 12px;
	font-weight: 400;
	color: #666;
	background: #f1f1f1;
	border-radius: 2px;
	padding: 0 4px 0px;
	line-height: 20px;
	margin-right: 6px;
	margin-bottom: 0px;
	position: relative;
	top: 1px;
}
</style>
