<template>
	<view class="p-root-detail-fellow g_w_all g_bg_page g_kuaishou">
		<view v-if="speed == 0">
			<rh-loading />
		</view>
		<view v-if="speed == 1">
			<!-- <view class="g_h_10"></view> -->
			<AiApply class="" @outputInfo="getTownsManInfo" v-if="true" />
			<!-- <view class="m-photo">
				<g-panel-card-info
					:info="{
						avatar: cdnBaseImg + 'mock_photo.svg',
						title: '拍照或相册上传',
						num: '系统智能识别并自动填写',
						titleFS: 16,
						imgSize: 72,
					}"
					radius="0"
					rightType="image"
					:rightImage="info.idCardImageUrl"
					@clickCard="handleClickInfo"
				/>
			</view>
			<view class="g_h_10"></view> -->
			<view class="g_h_10"></view>
			<rh-form-slot :list="telList" @changeWeChat="handleChangeWeChat" @changeMobile="handleClickMobile"></rh-form-slot>
			<view v-if="pageType == 'add'">
				<view class="g_h_10"></view>
				<view class="card-tip g_pb_8 g_pl_20 g_c_6">职位信息</view>
				<view class="fs_top text-wrap">
					<rh-form-slot :list="jobData" @clickResult="changePage"> </rh-form-slot>
				</view>
			</view>
			<view v-if="roleType == 'resumeSpecialist'">
				<view class="g_h_10"></view>
				<view class="card-tip g_pb_8 g_pl_20 g_c_6">简历分配</view>
				<view class="fs_top text-wrap">
					<rh-form-slot :list="jlData" @clickResult="openChoosePop"> </rh-form-slot>
				</view>
			</view>
			<view class="g_h_10"></view>
			<rh-form-slot
				:list="[
					{
						icon: '',
						label: '姓名',
						result: '',
						value: info.name,
						path: '',
						tip: 'slot-name',
						img: info.idCardImageUrl,
						placeholder: '请输入姓名',
						type: 'slot',
						pColumn: 12,
						require: true,
					},
					{
						icon: '',
						label: '身份证号',
						result: '',
						value: info.idCard,
						path: '',
						tip: 'slot-code-idcard',
						placeholder: '请输入身份证号',
						type: 'slot',
						pColumn: 12,
					},
					{
						icon: '',
						label: '性别',
						result: '',
						value: info.sex,
						path: '',
						tip: 'slot-sex',
						type: 'slot',
						pColumn: 12,
						require: false,
						customRequire: false,
					},
					{
						icon: '',
						label: '年龄',
						result: '',
						value: info.age,
						path: '',
						tip: 'slot-age',
						placeholder: '请输入年龄',
						type: 'slot',
						pColumn: 12,
						require: false,
					},
					{
						icon: '',
						label: '民族',
						result: '',
						value: info.nation,
						nationIndex: nationIndex,
						path: '',
						tip: 'slot-nation',
						pColumn: 12,
						type: 'slot',
					},

					{
						icon: '',
						label: '身份证住址',
						result: '',
						value: info.address,
						placeholder: '请输入身份证住址',
						path: '',
						tip: 'slot-address',
						type: 'slot',
						pColumn: 12,
					},
				]"
				@changeName="handleClickName"
				@changeSex="handleClickSex"
				@changeAge="handleClickAge"
				@changeNation="handleClickNation"
				@changeIDcard="handleClickIDcard"
				@changeAddress="handleClickAddress"
				@updateIDInfo="handlerIDInfo"
				@updateFile="updateFile"
			>
				<!-- {
						icon: '',
						label: '上传附件',
						result: '',
						path: '',
						type: 'slot',
						tip: 'slot-update-file',
						pColumn: 16,
					}, -->
				<template v-slot:after>
					<view class="g_flex_row_start g_p_10" v-if="imgList.length > 0">
						<view v-for="(item, index) in imgList" class="g_position_rela" :class="index <= 2 ? 'g_mr_18' : ''" :key="index">
							<i class="iconfont icon-guanbi img-close" @click="delImg(index)"></i>
							<image :src="item" class="g_w_56 g_h_56 g_radius_8" />
						</view>
					</view>
				</template>
			</rh-form-slot>
			<view style="height: 200px"></view>
			<rh-fixed>
				<slot>
					<view class="g_pl_12 g_pr_12">
						<!-- <rh-button :primaryColor='themeColor' btnText="取消" class="g_mr_16" size="small" type="default" @clickBtn="goReturn"></rh-button> -->
						<rh-button :primaryColor="themeColor" btnText="确定" class=" " @clickBtn="handleSubmit" size="auto" type="primary"></rh-button>
					</view>
				</slot>
			</rh-fixed>
		</view>

		<u-popup data-remark="选择跟进人" v-model="peopleModal.isShow" mode="bottom" border-radius="16" height="1000" :mask-close-able="true" :closeable="true">
			<view class="g_fs_18 g_c_3 g_flex_row_center g_pt_16"> 跟进人选择 </view>
			<scroll-view scroll-y="true" style="height: 384px">
				<view class="g_pl_12 g_pr_12">
					<view class="g_flex_row_between g_pt_16 g_pb_16 g_border_d_b" v-for="(item, index) in peopleModal.recordList" :key="index" @click="handleChoosePeople(item, index)">
						<view class="g_fs_17 g_c_3">{{ item.aliasName || item.userName || item.tel }}</view>
						<view class="g_flex_column_center">
							<i
								v-if="yId == item.id"
								class="iconfont icon-a-duigoubeifen2"
								style="font-size: 24px"
								:style="{
									color: yId == item.id ? themeColor : '#fff',
								}"
							></i>
						</view>
					</view>
				</view>
			</scroll-view>
			<view class="g_flex_row_center">
				<rh-button :primaryColor="themeColor" btnText="确定" class="bocai_btn_size_default g_mt_24" @clickBtn="handleSubmitChoosePeople" type="primary"></rh-button>
			</view>
		</u-popup>
	</view>
</template>

<script>
import AiApply from "@/components/AiApply.vue";
import { nationlist } from "../../utils/nation.js";
export default {
	components: {
		AiApply,
	},
	onReady() {
		this.G.setNavStyle();
	},
	onShareAppMessage() {
		return this.G.shareFun();
	},
	data() {
		return {
			themeColor: getApp().globalData.themeColor,
			peopleModal: {
				isShow: false,
				recordList: [],
			},
			cdnBaseImg: this.G.store().cdnBaseImg,
			speed: 0,
			info: {},
			nation: nationlist.map((item, index) => {
				return item.name;
			}),
			nationIndex: 0,
			imgList: [],
			jobData: {},
			jlData: {},
			telList: [],
			pageType: "",
			resumeFlag: 0,
			chooseUserId: "",
			yId: 0,
			roleType: "",
			isOrder:false,
			euid:0
		};
	},
	onLoad(options) {
		let that = this;
		console.log("info", this.info);
		console.log("options", options);
		that.isOrder = options.isOrder == 1 ? true : false;
		that.euid = options.euid;
		if (options.sex) {
			options.sex = Number(options.sex);
		} else {
			options.sex = 1;
		}
		if (!options.nation) {
			options.nation = "汉族";
		}
		if (options.type == "add") {
			this.pageType = "add";
			this.resumeFlag = 1;
		} else {
			this.info = options;
		}

		console.log("获取老乡信息：", options);
		if (this.info.imgs) {
			this.imgList = this.info.imgs.split(",");
		}
		this.telList = [
			{
				icon: "",
				label: "手机号",
				result: "",
				path: "",
				value: this.info.tel,
				tip: "slot-mobile",
				placeholder: "请输入手机号",
				type: "slot",
				pColumn: 8,
				require: true,
			},
		];

		if (this.info.resumeFlag == 1 || this.resumeFlag == 1) {
			this.telList.push({
				icon: "",
				label: "微信",
				result: "",
				path: "",
				value: this.info.weChat,
				tip: "slot-weChat",
				placeholder: "请输入联系微信",
				type: "slot",
				pColumn: 8,
				require: false,
			});
		}
		that.nation.forEach((item, index) => {
			if (item == that.info.nation) {
				console.log(that.info.nation);
				console.log(index);
				that.nationIndex = index;
			}
		});
		that.speed = 0;
		setTimeout(() => {
			that.speed = 1;
		}, 300);
		uni.$on("changeJobInfo", function (data) {
			console.log("changeJobInfo", data);
			that.info.jobId = data.info.leafCateId;
			that.info.jobName = data.info.title;
			console.log("data.info", data.info);
			that.$forceUpdate();
		});
	},
	onShow() {
		let that = this;

		uni.getStorageSync("currentRoleInfo") && (this.roleType = uni.getStorageSync("currentRoleInfo").roleCode);

		this.jobData = [
			{
				radius: "no_bottom",
				icon: "",
				label: "意向职位",
				result: that.info.jobId ? that.info.jobName : "请选择意向职位",
				path: "/root/other/search?from=record",
				placeholder: "请选择意向职位",
				fontColor: that.info.jobName == "请选择意向职位" || that.info.jobName == "" || that.info.jobName == null ? "g_c_9" : "g_c_3",
				tip: "applia-job",
				require: false,
				customRequire: true,
				pColumn: 18,
			},
		];
		this.jlData = [
			{
				radius: "no_bottom",
				icon: "",
				label: "跟进人",
				result: that.info.fellowId ? that.info.fellowName : "请选择跟进人",
				path: "",
				placeholder: "请选择跟进人",
				fontColor: that.info.fellowName == "请选择跟进人" || that.info.fellowName == "" || that.info.fellowName == null ? "g_c_9" : "g_c_3",
				tip: "choose-people-record",
				require: false,
				pColumn: 18,
			},
		];

		console.log("info", this.info);
		console.log("telList", this.telList);
		that.getPeopleList();
	},
	created() {
		let that = this;
	},
	methods: {
		getPeopleList() {
			let that = this;
			that.G.Get(that.api.job_getUserListOfSameTeam, {}, (res) => {
				that.peopleModal.recordList = res;
			});
		},
		openChoosePop() {
			let that = this;
			that.yId = "";
			that.chooseUserId = "";
			that.peopleModal.isShow = true;
		},
		handleChoosePeople($item, $index) {
			let that = this;
			that.yId = $item.id;
		},

		handleSubmitChoosePeople() {
			let that = this;
			that.chooseUserId = that.yId;
			that.info.fellowId = that.chooseUserId;
			that.info.fellowName = that.peopleModal.recordList.find((item) => item.id == that.chooseUserId).aliasName || that.peopleModal.recordList.find((item) => item.id == that.chooseUserId).userName || that.peopleModal.recordList.find((item) => item.id == that.chooseUserId).tel;

			this.jlData[0].result = that.info.fellowName;
			this.jlData[0].fontColor = that.info.fellowName == "请选择跟进人" || that.info.fellowName == "" || that.info.fellowName == null ? "g_c_9" : "g_c_3";
			that.peopleModal.isShow = false;
		},
		changePage(e) {
			uni.navigateTo({
				url: e.path,
			});
		},
		getTownsManInfo(_val) {
			console.log("info", _val);
			if (_val.name) {
				this.info = { ..._val, resumeFlag: this.info.resumeFlag };
				// 没有手机号但微信号是手机号，则赋值给手机号
				if (!this.info.tel && this.info.weChat && this.G.setReg(this.info.weChat, "tel")) {
					this.info.tel = this.info.weChat;
				}
				if (this.info.resumeFlag == 1 || this.resumeFlag == 1) {
					this.telList = [
						{
							icon: "",
							label: "手机号",
							result: "",
							path: "",
							value: this.info.tel,
							tip: "slot-mobile",
							placeholder: "请输入手机号",
							type: "slot",
							pColumn: 8,
							require: true,
						},
						{
							icon: "",
							label: "微信",
							result: "",
							path: "",
							value: this.info.weChat,
							tip: "slot-weChat",
							placeholder: "请输入联系微信",
							type: "slot",
							pColumn: 8,
							require: false,
						},
					];
				} else {
					this.telList = [
						{
							icon: "",
							label: "手机号",
							result: "",
							path: "",
							value: this.info.tel,
							tip: "slot-mobile",
							placeholder: "请输入手机号",
							type: "slot",
							pColumn: 8,
							require: true,
						},
					];
				}
			} else {
				uni.showToast({
					icon: "none",
					title: "识别失败,请重试",
				});
			}
			// uni.$emit("getTownsman", {
			// 	info,
			// });
		},
		handleClickName(e) {
			this.info.name = this.info.userName = e;
		},
		handleClickSex(e) {
			this.info.sex = e;
		},
		handleClickAge(e) {
			this.info.age = e;
		},
		handleClickNation(e) {
			let that = this;
			console.log(e.name);
			this.info.nation = e.name;
			that.nation.forEach((item, index) => {
				if (item == e.name) {
					// console.log(that.info.nation)
					// console.log(index)
					that.nationIndex = index;
				}
			});
		},
		handleClickNation(e) {
			let that = this;
			console.log(e.name);
			this.info.nation = e.name;
			that.nation.forEach((item, index) => {
				if (item == e.name) {
					// console.log(that.info.nation)
					// console.log(index)
					that.nationIndex = index;
				}
			});
		},
		handleClickIDcard(e) {
			this.info.idCard = e;
		},
		handleClickAddress(e) {
			this.info.address = e;
		},
		goReturn() {
			uni.navigateBack();
		},
		handleSubmit() {
			let that = this;
			if (this.info.resumeFlag == 1 || this.resumeFlag == 1) {
				if (!that.info.tel && !that.info.weChat) {
					uni.showToast({
						icon: "none",
						title: "请输入手机号或微信",
					});
					return false;
				}
			} else {
				if (!that.info.tel) {
					uni.showToast({
						icon: "none",
						title: "请输入手机号",
					});
					return false;
				}
			}

			if (that.info.tel && !that.G.setReg(that.info.tel, "tel")) {
				uni.showToast({
					icon: "none",
					title: "请输入正确的手机号",
				});
				return false;
			}

			if (that.info.idCard && !that.G.isValidIdCard(that.info.idCard)) {
				uni.showToast({
					icon: "none",
					title: "请输入正确的身份证号",
				});
				return false;
			}
			// if (that.imgList.length > 0) {
			// 	that.info.imgs = that.imgList;
			// }
			if (!that.info.nation) {
				that.info.nation = "汉族";
			}
			let url = "";
			if (this.pageType == "add") {
				url = "add_resume";
				if (that.roleType == "resumeSpecialist") {
					// 简历专员
					// 不传或者选择的id
					that.info.followUserId = that.chooseUserId;
				} else {
					// 招聘专员 当前登录用户id
					that.info.followUserId = uni.getStorageSync("apply-userinfo").id;
				}
				that.info.clueType = 1;
			} else {
				url = "order_submitFellow";
			}
			console.log(204,that.euid,'==',that.info)
			if(that.euid && that.euid != 'undefined'){
				if(that.isOrder){
					that.info.orderId = that.euid;
				}else{
					that.info.clueId = that.euid;
				}
			}
			that.G.Post(that.api[url], that.info, (res) => {
				uni.showToast({
					icon: "success",
					title: this.pageType == "add" ? "新建成功" : "保存成功",
				});
				if (this.pageType == "add") {
					that.goReturn();
					setTimeout(() => {
						uni.$emit("goNew", 999);
					}, 100);
				} else {
					setTimeout(() => {
						that.goReturn();
					}, 300);
				}
			});
		},
		handleClickInfo() {
			let that = this;
			that.G.uploadImg((res) => {
				that.info.name = res.info.name;
				that.info.sex = res.info.sex;
				that.info.age = res.info.age;
				that.info.nation = res.info.nationality;
				that.info.idCard = res.info.num;
				that.info.idCardImageUrl = res.image;
				that.info.userName = res.info.name;
				that.info.address = res.info.address;
				that.nation.forEach((item, index) => {
					if (item == that.info.nation) {
						that.nationIndex = index;
					}
				});
			}, "idcard");
		},
		handleClickMobile(e) {
			console.log(e);
			this.info.tel = e;
		},

		handleChangeWeChat(e) {
			console.log(e);
			this.info.weChat = e;
		},

		// 删除图片
		delImg(ind) {
			this.imgList.splice(
				this.imgList.findIndex((item, index) => index == ind),
				1
			);
		},
		// 上传图片
		updateFile(e) {
			let that = this;
			if (that.imgList.length < 4) {
				that.imgList.push(e);
				uni.hideLoading();
			} else {
				uni.showToast({
					title: "最多只能上传4张",
					icon: "none",
				});
			}
		},
		/**
		 * 获取上传的身份信息
		 */
		handlerIDInfo(res) {
			console.log("res", res);
			let that = this;
			that.info.name = res.info.name;
			that.info.sex = res.info.sex ? (res.info.sex == "男" ? 1 : 2) : "";
			that.info.age = res.info.age;
			that.info.nation = res.info.nationality + "族";
			that.info.idCard = res.info.num;
			that.info.idCardImageUrl = res.image;
			that.info.userName = res.info.name;
			that.info.address = res.info.address;
			that.nation.forEach((item, index) => {
				// console.log("item1", item);
				// console.log('that.info.nation + "族"', that.info.nation + "族");
				if (item == that.info.nation) {
					console.log("item2", item);

					that.nationIndex = index;
				}
			});
			console.log("that.info", that.info);
			console.log("that.nationIndex", that.nationIndex);
			that.$forceUpdate();
		},
	},
};
</script>

<style></style>
