<template>
	<view class="p-person-order g_w_all g_bg_f_5 g_pt_10 g_kuaishou" :class="applyStatus == 2 || applyStatus == 3 ? '' : ''">
		<!-- 加载前 -->
		<view v-if="speed == -1"></view>
		<!-- 加载后 -->
		<view v-if="speed == 1" style="height: 100%">
			<!-- 
			1.未申请：进入创建表单
			2.已申请但处于审核中：进入审核中页面
			3.已申请且审核失败：进入审核失败页面
			4.已申请且审核成功：此时接口会提示账户状态发生变化，会重新登录
			-->
			<view v-if="applyStatus == 1">
				<g-panel-form-slot
					class="g_mb_10"
					:list="[
						{
							icon: '',
							label: '发单账号名称',
							result: '',
							value: info.agencyName,
							path: '',
							tip: 'slot-name',
							placeholder: '请填写企业简称',
							type: 'slot',
							pColumn: 10,
							pRow: 12,
							fontSize: '16px',
							require: true,
						},
						{
							icon: '',
							label: '发单账号头像',
							result: '',
							value: info.logo,
							path: '',
							tip: 'update-avatar',
							type: '',
							fontSize: '16px',
							type: 'slot',
							pColumn: 12,
							pRow: 12,
							require: true,
						},
						{
							icon: '',
							label: '发单账号简介',
							result: '',
							value: info.agencyDesp,
							path: '/root/person/inputAccountInfo',
							tip: 'desp',
							placeholder: '请简单介绍发单号',
							type: '',
							result: info.agencyDesp,
							fontSize: '16px',
							fontColor: info.agencyDesp == '请简单介绍发单号' ? 'g_c_9' : 'g_c_3',
							pColumn: 17,
							pRow: 12,
						},
						{
							icon: '',
							label: '企业全称',
							result: '',
							value: info.companyName,
							path: '',
							tip: 'slot-name',
							placeholder: '请输入企业全称',
							fontSize: '16px',
							type: 'slot',
							pColumn: 10,
							pRow: 12,
							require: false,
						},
						{
							icon: '',
							label: '企业地址',
							result: '',
							value: info.address,
							path: '',
							tip: 'slot-choose-address',
							placeholder: '请选择企业地址',
							type: 'slot',
							fontSize: '16px',
							pColumn: 17,
							pRow: 12,
							require: false,
							suffix: 'icon-dizhi1',
						},
					]"
					@changeName="changeName"
					@changeMoreWord="changeMoreWord"
					@updateImg="updateImg"
					@chooseAddress="chooseAddress"
					@clickResult="toDesp"
				>
				</g-panel-form-slot>
				<view class="g_mt_24 g_pl_10 g_pr_10 g_flex_row_around">
					<view class="g_flex_1 g_mr_20 g_fs_16" @click="uploadImg('yyzz')">
						<view class="g_pl_10 g_fw_600">营业执照</view>
						<view class="g_w_all g_flex_row_center flex_center g_bg_f g_h_100 g_radius_8 g_mt_8" style="overflow: hidden">
							<image :src="info.imgs.yyzz" v-if="info.imgs.yyzz" class="g_w_all g_h_all" mode=""></image>
							<i class="iconfont icon-paizhao3 g_c_9 g_fsi_32" v-else></i>
						</view>
					</view>
					<view class="g_flex_1 g_fs_16" @click="uploadImg('rlzy')">
						<view class="g_pl_10 g_fw_600">人力资源服务许可证</view>
						<view class="g_w_all g_flex_row_center flex_center g_bg_f g_h_100 g_radius_8 g_mt_8" style="overflow: hidden">
							<image :src="info.imgs.rlzy" v-if="info.imgs.rlzy" class="g_w_all g_h_all" mode=""></image>
							<i class="iconfont icon-paizhao3 g_c_9 g_fsi_32" v-else></i>
						</view>
					</view>
				</view>
				<view class="g_mt_24 g_pl_10 g_pr_10 g_flex_row_around">
					<view class="g_flex_1 g_mr_20 g_fs_16" @click="uploadImg('other')">
						<view class="g_pl_10 g_fw_600">其他附件</view>
						<view class="g_w_all g_flex_row_center flex_center g_bg_f g_h_100 g_radius_8 g_mt_8" style="overflow: hidden">
							<image :src="info.newImg[0]" v-if="info.newImg[0]" class="g_w_all g_h_all" mode=""></image>
							<i class="iconfont icon-paizhao3 g_c_9 g_fsi_32" v-else></i>
						</view>
					</view>
					<view class="g_flex_1 g_fs_16" @click="uploadImg('other')">
						<view class="g_h_20"></view>
						<view class="g_w_all g_flex_row_center flex_center g_bg_f g_h_100 g_radius_8 g_mt_8" style="overflow: hidden">
							<image :src="info.newImg[1]" v-if="info.newImg[1]" class="g_w_all g_h_all" mode=""></image>
							<i class="iconfont icon-paizhao3 g_c_9 g_fsi_32" v-else></i>
						</view>
					</view>
				</view>
				<view class="g_flex_c g_pb_42 g_mt_42">
					<!-- #ifdef APP-PLUS -->
					<g-button type="primary" @clickBtn="commitApply" btnText="报名助手"></g-button>
					<!-- #endif -->
					<!-- #ifdef MP-WEIXIN -->
					<g-button type="primary" @clickBtn="commitApply" btnText="提交申请"></g-button>
					<!-- #endif -->
					<!-- <g-button type="primary" @clickBtn="commitApply" btnText="提交申请"></g-button> -->
				</view>
			</view>
			<view v-if="applyStatus == 2" class="g_flex_c" style="height: 100%; margin-top: 180px">
				<view class="">
					<view class="g_flex_row_center">
						<image :src="localBaseImg + 'apply/time.svg'" mode="widthFix" class="g_w_54 g_h_54"></image>
					</view>
					<view class="g_flex_row_center g_mt_13 g_pb_32 g_c_3 g_fs_18 g_fw_600">已提交申请</view>
					<view class="g_w_220 g_flex_row_center g_c_9 g_fs_14">您已提交申请，我们将会在1-3个工作日内将结果反馈给您</view>
				</view>
			</view>
			<view v-if="applyStatus == 3" class="g_flex_c" style="height: 100%; margin-top: 180px">
				<view class="">
					<view class="g_flex_row_center">
						<image :src="localBaseImg + 'apply/error_yellow.svg'" mode="widthFix" class="g_w_54 g_h_54"></image>
					</view>
					<view class="g_flex_row_center g_mt_13 g_pb_32 g_c_3 g_fs_18 g_fw_600">申请失败</view>
					<view class="g_w_220 g_flex_row_center g_c_9 g_fs_14">抱歉，你提交的申请暂未通过</view>
					<view class="g_w_220 g_flex_row_center g_c_9 g_fs_14" style="color: #ff4d4f">未通过原因：{{ reason }}</view>
					<view class="g_c_sub g_mt_16 g_fs_14 g_text_c g_w_all g_fw_700" style="position: fixed; bottom: 56px; width: calc(100vw - 20px); color: #576b95; left: 50%; transform: translateX(-50%)" hover-class="thover" @click="applyStatus = 1">再次申请</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	onReady() {
		this.G.setNavStyle();
	},
	onShareAppMessage() {
		return this.G.shareFun();
	},
	data() {
		return {
			speed: -1,
			applyStatus: -1,
			info: {
				userName: "",
				imgs: {},
				newImg: [],
				addressDetail: {},
				agencyDesp: "请简单介绍发单号",
			},
			loginInfo: {},
			localBaseImg: this.G.store().localBaseImg,
		};
	},
	onLoad(options) {
		let that = this;
	},
	onShow() {
		let that = this;
		// console.log('that.G.ajax',that.G.ajaxUrl);
		that.G.Get(that.api.user_getAccountOrderStatus, {}, (res) => {
			console.log(res);
			that.speed = 1;
			// res.status = -1
			if (res.status == -1) {
				// 未提交过申请
				that.applyStatus = 1;
			} else if (res.status == 0) {
				// 申请中
				that.applyStatus = 2;
			} else if (res.status == 1) {
				// 已通过
				that.applyStatus = 4;
			} else if (res.status == 2) {
				// 已驳回
				that.applyStatus = 3;
			}
			that.reason = res.reason;
			that.loginInfo = uni.getStorageSync("apply-userinfo");
			that.info.userName = that.loginInfo.userName || "";
			that.info.tel = that.loginInfo.tel || "";
			that.info.agencyDesp = uni.getStorageSync("agencyDesp") || "请简单介绍发单号";
			this.$forceUpdate();
		});
	},
	onUnload() {
		uni.removeStorageSync("agencyDesp");
	},
	methods: {
		changeName(e, item) {
			if (item.label == "发单账号名称") {
				this.info.agencyName = e;
			} else if (item.label == "申请人名称") {
				this.info.userName = e;
			} else if (item.label == "企业全称") {
				this.info.companyName = e;
			}
		},
		toDesp(e) {
			uni.navigateTo({
				url: e.path + "?val=" + this.info.agencyDesp,
			});
		},
		changeTel(e) {
			this.info.tel = e;
		},
		changeMoreWord(e) {
			if (e.label == "简介") {
				this.info.agencyDesp = e.value;
			} else if (e.label == "备注") {
				this.info.desp = e.value;
			}
		},
		chooseAddress(e) {
			this.info.address = e.address;
			this.info.addressDetail = e;
			this.$forceUpdate();
		},
		updateImg(e) {
			this.info.logo = e;
			this.$forceUpdate();
		},
		editNickname() {
			let obj = {};
			uni.navigateTo({
				url: `/root/person/change?title=设置用户名&value=${this.loginInfo.userName}`,
			});
		},
		commitApply() {
			let that = this;
			if (!that.info.agencyName) {
				wx.showToast({
					title: "请输入发单号名称",
					icon: "none",
				});
				return;
			}
			if (!that.info.logo) {
				wx.showToast({
					title: "请设置发单号的头像",
					icon: "none",
				});
				return;
			}

			let _desp = "";
			if (that.info.agencyDesp) {
				if (that.info.agencyDesp == "请简单介绍发单号") {
					_desp = "";
				} else {
					_desp = that.info.agencyDesp;
				}
			} else {
				_desp = "";
			}
			let info = {
				recordType: 4,
				agencyId: uni.getStorageSync("apply-agencyId"),
				userName: that.info.userName,
				tel: that.info.tel,
				companyName: that.info.companyName || "",
				tel: that.info.tel || "",
				desp: that.info.desp || "",
				district: that.info.addressDetail.district || "",
				address: that.info.addressDetail.address || "",
				lng: that.info.addressDetail.lng || "",
				lat: that.info.addressDetail.lat || "",
				companyPic: that.info.imgs.yyzz || "",
				businessLicensePic: that.info.imgs.yyzz || "",
				logo: that.info.logo,
				agencyName: that.info.agencyName,
				agencyDesp: _desp,
				attachs: that.info.newImg.join(","),
			};
			/* 这里的域名是单独的，单独处理
			 */
			let url;
			if (that.G.ajaxUrl == "https://test.renminshitang.com.cn/daotian/api_dev") {
				url = 'https://test.renminshitang.com.cn/pc/api_dev' + "/business/coop/add";
			} else {
				url = "https://a.matripe.com.cn/business/coop/add";
			}
			uni.request({
				url: url,
				method: "post",
				data: Object.assign(info, that.G.setPublicParams()),
				header: Object.assign(
					{
						Authorization: "Bearer " + uni.getStorageSync("apply-token"),
						"content-type": "application/x-www-form-urlencoded; charset=UTF-8",
					},
					that.G.setPublicParams()
				),
				success: function (res) {
					if (res.data.status == 200) {
						uni.showToast({
							title: "提交成功,请等待审核",
							icon: "success",
							duration: 2000,
							success() {
								setTimeout(() => {
									// 进入待审核状态
									that.applyStatus = 2;
									// uni.navigateTo({
									// 	url: "/root/person/creatSuccess",
									// });
								}, 1000);
							},
						});
					} else {
						uni.showToast({
							title: "提交失败,请重试",
							icon: "fail",
							duration: 2000,
						});
					}
					uni.hideLoading();
				},
				complete: function () {},
			});
		},
		uploadImg(e) {
			let that = this;
			this.G.uploadImg((res) => {
				if (res.status == "上传成功") {
					if (e != "other") {
						that.info.imgs[e] = res.image;
					} else {
						if (that.info.newImg.length < 2) {
							that.info.newImg[that.info.newImg.length] = res.image;
						} else {
							uni.showToast({
								title: "最多只能上传两张图片",
								icon: "none",
							});
						}
					}
					this.$forceUpdate();
					uni.hideLoading();
				}
			});
		},
	},
};
</script>

<style>
.p-person-order {
	min-height: 100%;
}
</style>
