<template>
	<div class="g_bg_page g_pl_10 g_pr_10">
		<div class="g_h_20"></div>
		<div class="g_mb_10 g_fs_18 g_pl_10">「{{ beFllowAgencyInfo.inviteUserName || "-" }}」邀请你关注他的发单号：</div>
		<div class="r_box g_pt_24 g_text_c g_pl_20 g_pr_20">
			<div class="g_pb_16">
				<img class="g_h_80 g_w_80 g_radius_50" :src="beFllowAgencyInfo.logo || 'https://matripe-cms.oss-cn-beijing.aliyuncs.com/dailibaoming/APP/default.svg'" alt="" />
				<div class="g_fs_20 g_mt_12 g_mb_12 g_fw_600">
					{{ beFllowAgencyInfo.fullName || "-" }}
				</div>
				<div class="g_c_9 g_fs_14">
					{{ beFllowAgencyInfo.desp || "-" }}
				</div>
			</div>
			<div class="g_flex_row_between g_pb_12 g_pt_12" style="border-top: 1rpx dotted #eee">
				<div v-for="item in numList" class="g_flex_row_center flex_end">
					<div class="g_fs_20 g_fw_600">{{ item.num }}</div>
					<div class="g_fs_14 g_c_9">{{ item.label }}</div>
				</div>
			</div>
		</div>
		<div class="g_mt_30">
			<div class="g_fs_18 g_mb_8 g_pl_12">填写企业简称:</div>
			<div class="r_box g_pl_10">
				<u-input v-model="agencyInfo.fullName" :customStyle="{ color: disabled ? '#999' : '#333' }" :disabled="disabled" height="88" placeholder="区域+名称，如:上海发财劳务"></u-input>
			</div>
			<div class="g_fs_12 g_c_9 g_mt_6 g_pl_12">设置后系统将自动创建团队并关注该发单号，您作为创建人可在我的页面管理团队。</div>
		</div>

		<div class="g_mt_84">
			<quickLogin @successLogin="successLogin" v-if="!agencyInfo.id">
				<g-button btnText="申请关注" type="primary" class=""></g-button>
			</quickLogin>
			<g-button v-else-if="isSelf" btnText="不能关注自己" type="disabled"></g-button>
			<div v-else>
				<g-button v-if="beFllowAgencyInfo.recordStatus == 1" btnText="已关注" type="disabled" class=""></g-button>
				<g-button v-else-if="beFllowAgencyInfo.recordStatus == 2" btnText="互相关注" type="disabled" class=""></g-button>
				<g-button v-else-if="(beFllowAgencyInfo.recordStatus == 3 || beFllowAgencyInfo.recordStatus == 4) && !isSelf" btnText="申请关注" type="primary" class="" @clickBtn="applyAdd"></g-button>
				<g-button v-else-if="beFllowAgencyInfo.recordStatus == 5" btnText="等待通过" type="disabled" class=""></g-button>
				<g-button v-else-if="beFllowAgencyInfo.recordStatus == 6 && !isSelf" btnText="申请关注" type="primary" class="" @clickBtn="applyAdd"></g-button>
			</div>
		</div>
	</div>
</template>

<script>
import quickLogin from "../../components/quickLogin.vue";
export default {
	components: {
		quickLogin,
	},
	data() {
		return {
			beFllowAgencyInfo: {
				agencyName: "",
				fullName: "",
			},
			info: {},
			agencyInfo: {
				fullName: "",
			}, // 登陆者的代理信息
			userInfo: {
				userName: "",
				userId: "",
			},
			numList: [
				{ label: "总职位", num: "-" },
				{ label: "最近更新", num: "-" },
				{ label: "粉丝", num: "-" },
			],
			inviteUserId: "",
			uid: "",
			ing: false,
			scene: "",
			isSelf: false,
			disabled: false,
		};
	},
	onLoad(options) {
		let that = this;
		console.log(options);
		// options.scene = "id=3087_101125";
		if (options.scene) {
			console.log("XXXXXXXXXXXX");
			//扫小程序码携带参数
			this.scene = options.scene;
			var sceneStr = decodeURIComponent(options.scene);
			var sceneJson = this.G.sceneToJson(sceneStr);
			console.log("sceneJson===", sceneJson);
			this.inviteUserId = sceneJson.id.split("_")[1]; //被关注团队的邀请人id
			this.uid = sceneJson.id.split("_")[0]; // 被关注团队的id
		}
	},
	onShow() {
		let that = this;
		that.G.Get("/checkUserLogin", {}, (res) => {
			console.log("checkUserLogin", res);
			if (res) {
				that.getAgencyInfo().then(() => {
					that.getAgencyById();
				});
			} else {
				that.getAgencyById();
			}
		});
	},
	created() {},

	methods: {
		applyAdd() {
			let that = this;

			if (!this.agencyInfo.fullName) {
				uni.showToast({
					title: "请输入企业简称",
					icon: "none",
				});
				return;
			}
			if (this.ing) {
				return;
			}
			that.ing = true;
			let _code = 1; // 账号搜索
			// if (this.from == "friend") {
			// 	_code = 2; // 名片分享
			// } else {
			if (this.scene) {
				_code = 3; // 扫一扫
			} else {
				_code = 1; // 账号搜索
			}
			// }
			return new Promise((reso) => {
				this.G.Get(this.api.bind_subscribeSubmit + "/" + that.uid, { agencyId: that.agencyInfo.id, agencyName: that.agencyInfo.fullName, source: _code, remark: " " }, (res) => {
					uni.showToast({
						title: "申请成功",
						icon: "none",
					});
					uni.navigateTo({
						url: "/root/person/inviteFollowSuccess",
					});
					reso();
					// that.checkHasApply();
					console.log(res);
				});
			});
		},
		getAgencyById() {
			let that = this;
			return new Promise((reso, reject) => {
				this.G.Get(this.api.order_getAgencyByIdForApply, { agencyId: that.uid, inviteUserId: this.inviteUserId }, (res) => {
					console.log("resresresresresres++++++++++++++", res);
					that.beFllowAgencyInfo = res;
					if (res.id == that.agencyInfo.id) {
						that.isSelf = true;
					}
					that.numList = [
						{ label: "总职位", num: res.jobNum },
						{ label: "最近更新", num: res.recruitmentJobNum },
						{ label: "粉丝", num: res.downNum },
					];
					reso();
				});
			});
		},
		successLogin(e) {
			let that = this;
			console.log("successLogin", e);
			this.getAgencyInfo().then(() => {
				that.getAgencyById().then(() => {
					if (that.beFllowAgencyInfo.recordStatus == 3 || that.beFllowAgencyInfo.recordStatus == 4 || that.beFllowAgencyInfo.recordStatus == 6) {
						that.applyAdd();
					} else {
						if (that.beFllowAgencyInfo.recordStatus == 5) {
							uni.showToast({
								title: "已发起关注申请, 正在等待通过",
								icon: "none",
							});
						}
					}
				});
			});
		},
		getAgencyInfo() {
			let that = this;
			return new Promise((reso, reject) => {
				that.G.Get(that.api.login_agencyInfo, {}, (aRes) => {
					console.log("获取团队信息:", aRes);
					that.disabled = aRes.agency.fullName ? true : false;
					that.agencyInfo = { ...aRes.agency, fullName: aRes.agency.fullName ? aRes.agency.fullName : that.agencyInfo.fullName };

					uni.setStorageSync("agencyInfo", aRes.agency);
					setTimeout(() => {
						reso();
					}, 200);
				});
			});
		},
	},
};
</script>

<style></style>
