<template>
	<view>
		<u--form labelPosition="left" :model="form" :rules="rules" ref="uForm" labelWidth="80">
			<view class="se-p-20">
				<view class="se-p-20 se-bgc-white se-br-10 se-fs-20">
					<u-form-item label="姓名" prop="name">
						<u--input v-model="form.name" class="se-bgc-f5" placeholder="请输入姓名名称"></u--input>
					</u-form-item>
					<!-- <u-form-item label="密码" prop="password">
						<u--input v-model="form.password" class="se-bgc-f5" placeholder="请输入您的密码"></u--input>
					</u-form-item> -->
					<u-form-item label="联系电话" prop="mobile">
						<u--input v-model="form.mobile" class="se-bgc-f5" placeholder="请输入联系方式"></u--input>
					</u-form-item>
					<u-form-item label="年龄" prop="age">
						<u--input v-model="form.age" class="se-bgc-f5" placeholder="请输入年龄"></u--input>
					</u-form-item>
					<u-form-item label="名族" prop="nation">
						<u--input v-model="form.nation" class="se-bgc-f5" placeholder="请输入名族"></u--input>
					</u-form-item>
					
					
					<u-form-item label="地址" prop="address">
						<u--input v-model="form.address" class="se-bgc-f5" placeholder="请输入地址"></u--input>
					</u-form-item>
					<u-form-item label="性别" prop="sex" @click="handleSexChange">
						<u--input @click="handleSexChange" readonly v-model="form.sex" placeholder="请选择性别"
							border="none"></u--input>
						<u-icon @click="handleSexChange" slot="right" name="arrow-right"></u-icon>
					</u-form-item>
					<u-form-item label="工种" prop="industryId" @click="handleIndChange">
						<u--input v-model="form.industryName" readonly placeholder="请选择工种" border="none"></u--input>
						<u-icon slot="right" name="arrow-right"></u-icon>
					</u-form-item>
					<u-form-item label="薪资待遇" prop="salary">
						<view class="se-flex se-flex-h-c">
							<u--input v-model="form.salaryMin" class="se-w-200" placeholder="最小值"></u--input>
							<text class="se-mx-10">~</text>
							<u--input v-model="form.salaryMax" class="se-w-200" placeholder="最大值"></u--input>
						</view>
					</u-form-item>
					<u-form-item label="个人介绍" prop="introduce">
						<u--textarea v-model="form.introduce" height="100" count maxlength="300" class="se-bgc-f5"
							placeholder="请选择个人介绍"></u--textarea>
					</u-form-item>
				</view>
			</view>
			<view class="se-p-20">
				<view class="se-px-20 se-pb-20 se-bgc-white se-br-10 se-fs-20">
					<u-form-item prop="idCardFile" labelWidth="2">
						<view class="se-flex se-flex-v-sa">
							<view class="se-py-20 se-w-p-100 se-flex">
								<view class="line-orange"></view>
								<view class="se-ml-10">
									个人照片
								</view>
							</view>
							<view class="se-py-20 se-w-p-100">
								<u-upload :fileList="form.idCardFile" @afterRead="afterRead" @delete="deletePic" name="1" multiple :maxCount="10"></u-upload>
							</view>
						</view>
					</u-form-item>
				</view>
			</view>
			<view class="se-px-20 se-pt-20">
				<view class="se-px-20 se-pb-80 se-fs-20 se-flex">
					<view @click="submit" class="se-mx-10 se-flex-1 se-br-40 se-flex-h-c se-h-60 se-lh-60 se-ta-c se-fs-28 se-c-white se-bgc-orange">
						<text>保存</text>
					</view>
					<!-- <view
						class="se-mx-10 se-flex-1 se-b se-br-40 se-flex-h-c se-h-60 se-lh-60 se-ta-c se-fs-28 se-c-66 se-bgc-f5">
						<text>联系我们</text>
					</view> -->
				</view>
			</view>
		</u--form>
		<!-- 性别 -->
		<u-action-sheet :show="showSex" :actions="sexList" title="请选择性别" @close="showSex = false" @select="sexSelect"></u-action-sheet>
		<!-- 种类 -->
		<u-action-sheet :show="showType" :actions="typeList" title="请选择种类" @close="showType = false" @select="typeSelect"></u-action-sheet>
	</view>
</template>

<script>
	import {
		role,
		industryList,
		updateRole,
		queryResumeById,
		updateResume,
		addResume
	} from "@/common/api.js"
	export default {
		data() {
			return {
				showSex: false,
				sexList: [
					{
						name: '男',
					},
					{
						name: '女',
					}
				],
				showType: false,
				typeList: [],
				fileList: [],
				form: {
					id:"",
					name: '',
					password: '',
					mobile: '',
					address:"",
					age: '',
					sex: '',
					industryId: '',
					industryName: "",
					salary: '',
					salaryMin: '',
					salaryMax: '',
					introduce: '',
					idCardFile: []
				},
				rules: {
					name: [{
						type: 'string',
						required: true,
						message: '请输入姓名名称',
						trigger: ['blur', 'change']
					}],
					nation: [{
						type: 'string',
						required: true,
						message: '请输入名族',
						trigger: ['blur', 'change']
					}],
					industryId: [{
						type: 'string',
						required: true,
						message: '请选择行业',
						trigger: ['blur', 'change']
					}],
					address:[
						{
							type: 'string',
							required: true,
							message: '请选择地址',
							trigger: ['blur', 'change']
						}
					],
					// password: [{
					// 	type: 'string',
					// 	required: true,
					// 	message: '请输入您的密码',
					// 	trigger: ['blur', 'change']
					// }],
					mobile: [{
							required: true,
							message: '请输入手机号',
							trigger: ['change', 'blur'],
						},
						{
							validator: (rule, value, callback) => {
								return uni.$u.test.mobile(value);
							},
							message: '手机号码不正确',
							trigger: ['change', 'blur'],
						},
					],
					age: [{
						validator: (rule, value, callback) => {
							if (!value) {
								callback(new Error('年龄不能为空'));
							} else if (isNaN(value)) {
								callback(new Error('请输入数字'));
							} else if (value < 18 || value > 65) {
								callback(new Error('年龄必须在18至65岁之间'));
							} else {
								callback(); // 验证通过
							}
						},
						trigger: 'blur',
					}],
					sex: [{
						type: 'string',
						max: 1,
						required: true,
						message: '请选择性别',
						trigger: ['blur', 'change']
					}],
					salary: [{
						type: 'string',
						required: true,
						message: '请输入薪资待遇',
						trigger: ['blur', 'change']
					}],
					introduce: [{
						type: 'string',
						required: true,
						message: '请输入个人简介',
						trigger: ['blur', 'change']
					}],
					idCardFile: [{
						validator: (rule, value, callback) => {
							if (value === null || value === undefined || value === '' || (Array.isArray(
									value) && value.length === 0)) {
								callback(new Error('请选择身份证'));
							} else {
								callback();
							}
						},
						trigger: 'blur'
					}]
				},
			}
		},
		watch: {
			'form.salaryMin': {
				handler(newVal, oldVal) {
					if (!uni.$u.test.isEmpty(newVal) && !uni.$u.test.isEmpty(this.form.salaryMax)) {
						this.form.salary = '有'
					} else {
						this.form.salary = ''
					}
				},
				immediate: true
			},
			'form.salaryMax': {
				handler(newVal, oldVal) {
					if (!uni.$u.test.isEmpty(newVal) && !uni.$u.test.isEmpty(this.form.salaryMin)) {
						this.form.salary = '有'
					} else {
						this.form.salary = ''
					}
				},
				immediate: true
			}
		},
		mounted() {
			// let worker = uni.getStorageSync("worker")
			let params = {}
			queryResumeById(params).then(response=>{
				let worker = response.result;
				if(worker){
					this.form.id = worker.id
					this.form.name = worker.name
					this.form.mobile = worker.phone
					this.form.age = worker.age
					this.form.sex = worker.sex==1?'男':'女'
					this.form.nation = worker.nation
					this.form.introduce = worker.brief
					this.form.address = worker.address
					this.form.industryId = worker.categoryOne
					this.form.industryName = worker.categoryOneName
					this.form.salaryMin = worker.salaryMin
					this.form.salaryMax = worker.salaryMax
					this.form.idCardFile = this.stringToKeyValueArray(worker.headImage)
				}
			})
		},
		methods: {
			stringToKeyValueArray(str, delimiter1 = ',') {
				if(str){
					let arro = str.split(delimiter1);
					let arr = [];
					arro.forEach(items=>{
						let obj = {};
						obj["url"] = items
						arr.push(obj)
					})
					console.info(arr)
					return arr
				}else{
					return []
				}
			},
			handleIndChange() {
				this.showType = true
			},
			typeSelect(event) {
				console.info("selectIndClick", event)
				this.form.industryId = event.id
				this.form.industryName = event.name
				this.showType = false
			},
			onIndustryList() {
				industryList({}).then(response => {
					console.info(response)
					this.typeList = response.result
				}).catch(error => {

				})
			},

			handleSexChange() {
				this.showSex = true
			},
			sexSelect(e) {
				this.form.sex = e.name
				this.$refs.uForm.validateField('sex')
			},
			onRole() {
				let that = this
				
				if(this.form.id){
					let params = {
						"id": that.form.id,
						"name": that.form.name,
						"categoryOne": that.form.industryId,
						"categoryOneName": that.form.industryName,
						"address":that.form.address,
						"nation": that.form.nation,
						"age": that.form.age,
						"sex": that.form.sex=="男"?1:2,
						"salaryMin": that.form.salaryMin,
						"salaryMax": that.form.salaryMax,
						"phone": that.form.mobile,
						"brief": that.form.introduce,
						"headImage": that.form.idCardFile.map(item => item.url).join(','),
					}
					updateResume(params).then(response=>{
						uni.$u.toast("保存成功！")
						setTimeout(()=>{
							uni.navigateBack({
								delta:1
							})
						},1500)
					}).catch(error=>{
						
					})
				}else{
					let params = {
						"name": that.form.name,
						"categoryOne": that.form.industryId,
						"categoryOneName": that.form.industryName,
						"address":that.form.address,
						"age": that.form.age,
						"nation": that.form.nation,
						"sex": that.form.sex=="男"?1:2,
						"salaryMin": that.form.salaryMin,
						"salaryMax": that.form.salaryMax,
						"phone": that.form.mobile,
						"brief": that.form.introduce,
						"headImage": that.form.idCardFile.map(item => item.url).join(','),
					}
					addResume(params).then(response => {
						uni.$u.toast("提交成功，等待申请")
						setTimeout(()=>{
							uni.switchTab({
								url:"/pages/home/index"
							})
						},1500)
					}).catch(error => {
						setTimeout(()=>{
							uni.switchTab({
								url:"/pages/home/index"
							})
						},1500)
					})
				}
			},
			submit() {
				this.$refs.uForm.validate().then(res => {
					this.onRole()
				}).catch(errors => {
					// uni.$u.toast('校验失败')
				})
			},
			deletePic(event) {
				this.form.idCardFile.splice(e.index, 1)
			},
			async afterRead(e) {
				let self = this
				e.file.forEach(file => {
					self.$Oss.ossUpload(file.url).then(url => {
						self.form.idCardFile.push({
							url
						})
					})
				})
			}
		},
		onReady() {
			this.onIndustryList()
			this.$refs.uForm.setRules(this.rules)
		},
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #f5f5f5 !important;
	}

	.line-orange {
		width: 8rpx;
		height: 32rpx;
		background: #ff7a31;
		border-radius: 4rpx;
	}
</style>