<template>
	<view class="content">

		<view class="content-middle">
			<form report-submit='true'>
				<view class='merchantsSettled'>
					<view class='list' style="padding-bottom: 30rpx;">

						<view class="item">
							<view class="acea-row row-middle" @click="seleckImage">
								<text class="item-name">用户头像</text>
								<input placeholder="上传头像" type="text" disabled readonly>
								<image :src="changeAvatar" mode="aspectFit" style="width: 72rpx;height: 72rpx;"></image>

							</view>
						</view>
						<view class="item">
							<view class="acea-row row-middle">
								<text class="item-name">员工姓名:</text>
								<input placeholder="请输入员工姓名" type="text" readonly v-model="merchantData.name">

							</view>
						</view>
						<view class="item">
							<view class="acea-row row-middle">
								<text class="item-name">性别: </text>
								<radio-group class="select_group" @change="deliveryWayChangethree">
									<label class="radio_select" v-for="(val, i) in list2" :key="val.value"
										style="margin-right:15rpx;">
										<view style="margin-right:5rpx;">
											<radio :value="val.value" :checked="val.checked" />
										</view>
										<view>{{ val.name }}</view>
									</label>
								</radio-group>

							</view>
						</view>

						<view class="item" v-if="type==0">
							<view class="acea-row row-middle">
								<text class="item-name">初始密码:</text>
								<input placeholder="请输入初始密码" type="text" maxlength="6" readonly
									v-model="merchantData.reg_pwd">

							</view>
						</view>
						<view class="item">
							<view class="acea-row row-middle">
								<text class="item-name">手机号码: </text>
								<input placeholder="请输入手机号码" type="text" readonly v-model="merchantData.mobile">

							</view>
						</view>
						<view class="item">
							<view class="acea-row row-middle">
								<text class="item-name">电子邮箱: </text>
								<input placeholder="请输入电子邮箱" type="text" readonly v-model="merchantData.email">

							</view>
						</view>
						<view class="item">
							<view class="acea-row row-middle">
								<text class="item-name">所在部门: </text>
								<input placeholder="请选择部门" type="text" disabled readonly @click="showckone"
									v-model="companybm">

							</view>
						</view>
						<view class="item">
							<view class="acea-row row-middle">
								<text class="item-name">岗位职称: </text>
								<input placeholder="请选择岗位职称" type="text" disabled readonly @click="showcktwo"
									v-model="companygw">

							</view>
						</view>
						<view class="item">
							<view class="acea-row row-middle">
								<text class="item-name">入职日期: </text>
								<input placeholder="请选择入职日期" type="text" disabled readonly
									v-model="merchantData.entry_time" @click="showckthree">

							</view>
						</view>
						<view class="item">
							<view class="acea-row row-middle">
								<text class="item-name">员工类型: </text>

								<radio-group class="select_group" @change="deliveryWayChangeone">

									<label class="radio_select" v-for="(val, i) in list" :key="val.value"
										style="margin-right:25rpx;">
										<view style="margin-right:5rpx;">
											<radio :value="val.value" :checked="val.checked" />
										</view>
										<view>{{ val.name }}</view>
									</label>
								</radio-group>
							</view>
						</view>
						<view class="item">
							<view class="acea-row row-middle">
								<text class="item-name">账号状态: </text>
								<radio-group class="select_group" @change="deliveryWayChangetwo">
									<label class="radio_select" v-for="(val, i) in list1" :key="val.value"
										style="margin-right:15rpx;">
										<view style="margin-right:5rpx;">
											<radio :value="val.value" :checked="val.checked" />
										</view>
										<view>{{ val.name }}</view>
									</label>
								</radio-group>

							</view>
						</view>
						<view class="item">
							<view class="acea-row row-middle">
								<text class="item-name">员工简介: </text>
								<input placeholder="请选择员工简介" type="text" readonly v-model="merchantData.desc">

							</view>
						</view>
					</view>
				</view>
			</form>

			<u-picker :show="flag1" :columns="columns3" ref="uPicker4" @cancel="flag1 = false" closeOnClickOverlay
				@close="flag1 = false" @confirm="confirm" keyName="title" :defaultIndex=[0,0]>
			</u-picker>
			<u-picker :show="flag2" :columns="columns4" ref="uPicker5" @cancel="flag2 = false" closeOnClickOverlay
				@close="flag2 = false" @confirm="confirm1" keyName="title" :defaultIndex=[0,0]>
			</u-picker>
			<u-datetime-picker :show="endmonth" mode="date" closeOnClickOverlay @confirm="end_monthConfirm"
				@cancel="end_monthClose" @close="end_monthClose"></u-datetime-picker>
			<view class="content-bottom">
				<view class="content-bottom-one">
					重置
				</view>
				<view class="content-bottom-two" @click="submit">
					立即提交
				</view>
			</view>
		</view>

	</view>
</template>

<script>
	import retuntop from '@/components/return/index.vue'
	import {
		addcomanyer,
		comanyerview,
		userdepartment,
		userpositiongroup
	} from '@/api/oa.js'

	import {
		oaUploads,

		uploads
	} from '@/api/upload.js'
	import {
		HTTP_REQUEST_URL
	} from '@/config/app.js'
	import {
		Toast
	} from '@/libs/uniApi.js'
	export default {

		data() {
			return {
				flag1: false,
				flag2: false,
				endmonth: false,
				validate: false,
				columns3: [],
				columns4: [],

				companybm: '',
				companygw: '',
				merchantData: {

				},
				changeAvatar: '',
				list: [{
						name: '实习',
						checked: false,
						value: '3',
					}, {
						name: '试用',
						value: '2',
						checked: false,
					},
					{
						name: '正式',
						value: '1',
						checked: false,
					}
				],
				list2: [{
					name: '男',
					checked: false,
					value: '1',
				}, {
					name: '女',
					value: '2',
					checked: false,
				}],
				list1: [{
						name: '禁止登录',
						checked: false,
						value: '0',
					}, {
						name: '正常',
						checked: false,
						value: '1',
					},
					{
						name: '离线',
						checked: false,
						value: '2',
					}
				],
				type: 0,
			};
		},
		onLoad(option) {
			this.type = option.type
			if (this.type == 1) {
				uni.setNavigationBarTitle({
					title: '编辑企业员工'
				})
				this.companydetail(option.data)
				// this.buname=
				// //console.log(this.columns3[0])
			}

			this.getDocumentList()
			this.getDocumentList1()
		},
		methods: {
			//获取部门
			async getDocumentList() {
				this.columns3 = []
				const res = await userdepartment({
					tree: 1
				})
				const deArr = res.data
				this.columns3.push(deArr)

				if (this.type == 1) {
					res.data.map((item, index) => {
						if (item.id == this.merchantData.id) {
							this.buname = item.title
						}
					})
				}
			},
			//获取企业员工详情
			async companydetail(id) {

				const res = await comanyerview({
					id: id
				})

				// this.merchantData = res.data
				this.changeAvatar = HTTP_REQUEST_URL + res.data.thumb
				this.merchantData.position_id = res.data.position_id
				this.merchantData.name = res.data.name
				this.merchantData.email = res.data.email
				this.merchantData.mobile = res.data.mobile
				this.merchantData.entry_time = res.data.entry_time
				this.merchantData.desc = res.data.desc
				this.merchantData.sex = res.data.sex
				this.merchantData.status = res.data.status
				this.merchantData.id = res.data.id
				this.merchantData.did = res.data.did
				this.merchantData.thumb = res.data.thumb
				this.merchantData.type= res.data.type
				this.list2.forEach(option => {

					option.checked = option.value == res.data.sex;
				});

				this.list.forEach(option => {
					option.checked = option.value == res.data.type;
				});
				this.companybm = res.data.department
				this.companygw = res.data.position
				this.list1.forEach(option => {
					option.checked = option.value == res.data.status;
				});

				this.$forceUpdate();


			},
			//获取岗位
			async getDocumentList1() {
				const res = await userpositiongroup()
				const deArr = res.data
				this.columns4.push(deArr)
			},
			//结束月份
			end_monthClose() {
				this.endmonth = false

			},
			end_monthConfirm(e) {
				this.endmonth = false
				this.merchantData.entry_time = uni.$u.timeFormat(e.value, 'yyyy-mm')

			},
			showckthree() {
				this.endmonth = true
			},
			showckone() {
				this.flag1 = true
			},
			showcktwo() {
				this.flag2 = true
			},
			//获取部门 岗位
			confirm(e) {
				this.merchantData.did = e.value[0].id
				this.companybm = e.value[0].title
				this.flag1 = false
			},
			confirm1(e) {
				//console.log(e)
				this.merchantData.position_id = e.value[0].id
				this.companygw = e.value[0].title
				this.flag2 = false
			},
			//验证
			validateForm: function() {
				let that = this,
					value = that.merchantData;
				//console.log(that.merchantData)
				if (!value.thumb) return Toast(
					'请选择头像'
				);
				if (value.name.length == 0) return Toast(
					'请输入员工姓名'
				);
				if (value.sex.length == 0) return Toast(
					'请选择性别'
				);
				if(this.type==0){
					if (value.reg_pwd.length == 0) return Toast(
						'请输入初始密码'
					)
				}
				

				if (value.desc.length == 0) return Toast(
					'请输入员工简介'
				);
				if (value.position_id.length == 0) return Toast(
					'请选择岗位'
				);
				if (value.did.length == 0) return Toast(
					'请选择部门'
				);
				if (value.status.length == 0) return Toast(
					'请选择员工状态'
				);
				if (value.type.length == 0) return Toast(
					'请选择员工类型'
				);
				if (value.entry_time.length == 0) return Toast(
					'请输入入职日期'
				);
				if (!value.mobile) return Toast('请输入手机号');
				if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(value.mobile)) return Toast(
					'请输入正确的手机号码'
				);
				if (!value.email) return Toast('请输入手机号');
				if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(value.email)) return Toast(
					'请输入正确的电子邮箱'
				);

				that.validate = true;
				return true;
			},
			async submit() {
				let that = this
				//console.log(that.validate)
				if (this.validateForm() && this.validate) {
					const res = await addcomanyer(this.merchantData)
					Toast(res.msg);
					if (res.code == 0) {
						uni.redirectTo({
							url: '/pages/employees/index'
						})
					}
				}

			},

			seleckImage() {
				let that = this
				uni.chooseImage({
					count: 1,
					sizeType: ['original', 'compressed'],
					sourceType: ['album', 'camera'],
					success: function(res) {
						oaUploads(res.tempFilePaths[0], 'img').then(res => {

							that.merchantData.thumb = res.filepath
							that.changeAvatar = HTTP_REQUEST_URL + res.filepath

							Toast('上传成功')
						}).catch(err => {
							//console.log('err', err);
							Toast('上传失败')
						})
					},
					fail: function(err) {
						//console.log('choose失败');
						Toast('添加失败')
					}
				});
			},
			deliveryWayChangeone(e) {
				this.merchantData.type = e.detail.value
			},
			deliveryWayChangetwo(type) {

				// this.merchantData.status = e.detail.value
			},
			deliveryWayChangethree(e) {
				this.merchantData.sex = e.detail.value
			}
		}
	};
</script>

<style lang="scss">
	page {
		padding-bottom: 88rpx;
	}

	.content-top {
		width: 100%;
		background: #3274F9;
		position: fixed;
		z-index: 100;

	}

	.content-middle {


		.merchantsSettled .list {
			background-color: #fff;
		}

		/deep/ uni-checkbox .uni-checkbox-input {
			border-radius: 50%;
		}

		/deep/ uni-checkbox .uni-checkbox-input.uni-checkbox-input-checked {
			background-color: #3274F9 !important;
			color: #fff !important;
		}

		.select_group {
			display: flex;

			.radio_select {
				display: flex;
				font-size: 35rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #666666;

				.checkbox {}
			}
		}

		.application-record {
			position: absolute;
			display: flex;
			align-items: center;
			top: 240rpx;
			right: 0;
			color: #fff;
			font-size: 22rpx;
			background-color: rgba(0, 0, 0, 0.3);
			padding: 8rpx 18rpx;

		}

		.merchantsSettled .list .item {
			padding: 50rpx 0 20rpx;

			position: relative;
			margin: 0 20px;

			&.no-border {
				border-bottom: none;
				padding-left: 0;
				padding-right: 0;
			}

			.item-title {
				color: #666666;
				font-size: 28rpx;
				display: block;
			}

			.item-desc {
				color: #B2B2B2;
				font-size: 22rpx;
				display: block;
				margin-top: 9rpx;
				line-height: 36rpx;
			}
		}

		.item-name {
			margin-right: 30rpx;
		}

		.acea-row,
		.upload {
			display: -webkit-box;
			display: -moz-box;
			display: -webkit-flex;
			display: -ms-flexbox;
			display: flex;
			-webkit-box-lines: multiple;
			-moz-box-lines: multiple;
			-o-box-lines: multiple;
			-webkit-flex-wrap: wrap;
			-ms-flex-wrap: wrap;
			flex-wrap: wrap;
		}

		.upload {
			margin-top: 20rpx;
		}

		.acea-row.row-middle {
			-webkit-box-align: center;
			-moz-box-align: center;
			-o-box-align: center;
			-ms-flex-align: center;
			-webkit-align-items: center;
			align-items: center;
			padding-left: 2px;
		}

		.acea-row.row-column {
			-webkit-box-orient: vertical;
			-moz-box-orient: vertical;
			-o-box-orient: vertical;
			-webkit-flex-direction: column;
			-ms-flex-direction: column;
			flex-direction: column;
		}

		.acea-row.row-center-wrapper {
			-webkit-box-align: center;
			-moz-box-align: center;
			-o-box-align: center;
			-ms-flex-align: center;
			-webkit-align-items: center;
			align-items: center;
			-webkit-box-pack: center;
			-moz-box-pack: center;
			-o-box-pack: center;
			-ms-flex-pack: center;
			-webkit-justify-content: center;
			justify-content: center;
		}


		.uni-list-cell-db {
			position: relative;
		}




		.merchantsSettled .list .item input {
			width: 400rpx;
			font-size: 30rpx;
		}

		.merchantsSettled .list .item .placeholder {
			color: #b2b2b2;
		}

		.merchantsSettled .default {
			padding: 0 30rpx;
			height: 90rpx;
			background-color: #fff;
			margin-top: 23rpx;
		}

		.merchantsSettled .default checkbox {
			margin-right: 15rpx;
		}

		.merchantsSettled .acea-row uni-image {
			width: 20px;
			height: 20px;
			display: block;
		}

		.merchantsSettled .list .item .codeIput {
			width: 125px;
		}

		.uni-input-input {
			display: block;
			height: 100%;
			background: none;
			color: inherit;
			opacity: 1;
			-webkit-text-fill-color: currentcolor;
			font: inherit;
			line-height: inherit;
			letter-spacing: inherit;
			text-align: inherit;
			text-indent: inherit;
			text-transform: inherit;
			text-shadow: inherit;
		}

		.merchantsSettled .list .item .code {
			position: absolute;
			width: 93px;
			line-height: 27px;
			border: 1px solid var(--view-theme);
			border-radius: 15px;
			color: var(--view-theme);
			text-align: center;
			bottom: 8px;
			right: 0;
			font-size: 12px;
		}

		.merchantsSettled .list .item .code.on {
			background-color: #bbb;
			color: #fff;
			border-color: #bbb;
		}

		.merchantsSettled .submitBtn {
			width: 588rpx;
			margin: 0 auto;
			height: 86rpx;
			border-radius: 25px;
			text-align: center;
			line-height: 86rpx;
			font-size: 15px;
			color: #fff;
			background: #E3E3E3;
			margin-top: 25px;
			pointer-events: none;
		}

		.merchantsSettled .submitBtn.on {
			background: var(--view-theme);
			pointer-events: all;
		}

		uni-checkbox-group,
		.settleAgree {
			display: inline-block;
			font-size: 24rpx;
		}

		uni-checkbox-group {
			color: #b2b2b2;
		}

		.settleAgree {
			color: var(--view-theme);
			position: relative;
			top: 2px;
			left: 8px;
		}

		.merchantsSettled uni-checkbox .uni-checkbox-wrapper {
			width: 30rpx;
			height: 30rpx;
			border: 2rpx solid #C3C3C3;
			border-radius: 15px;
		}
	}

	.content-bottom {
		width: 100%;
		position: absolute;
		bottom: 0;
		left: 0;
		position: fixed;
		display: flex;

		.content-bottom-one {
			width: 50%;
			height: 88rpx;
			line-height: 88rpx;
			text-align: center;
			font-size: 32rpx;
			font-family: PingFang SC-Regular, PingFang SC;
			font-weight: 400;
			color: #999999;
			background: #fff;
		}

		.content-bottom-two {
			width: 50%;
			height: 88rpx;
			line-height: 88rpx;
			text-align: center;
			font-size: 32rpx;
			font-family: PingFang SC-Regular, PingFang SC;
			font-weight: 400;
			color: #FFFFFF;
			background: #3274F9;

		}
	}
</style>