<template>
	<view class="detail-view">
		<u-form label-width="140" :model="add_form" ref="uForm">
			<view class="form-model-view">
				<u-form-item label-position="top" label="头像" prop="avatar">
					<upload :images="add_form.avatar ? [add_form.avatar] : []" @handleRemove="imgRemove"
						@uploadSuccess="uploadSuccess" />
				</u-form-item>
				<u-form-item required label="名称" prop="staffName">
					<u-input v-model="add_form.staffName" placeholder="请输入姓名" />
				</u-form-item>
				<!-- <u-form-item required label="UID" prop="staffName">
					<u-input v-model="add_form.staffName" placeholder="请输入姓名" />
				</u-form-item> -->
				<u-form-item label-position="top" label="简介" prop="info">
					<u-input v-model="add_form.info" type="textarea" placeholder="请输入简介" />
				</u-form-item>
			</view>
		</u-form>
		<view class="detail-bottom">
			<view class="handel-btn" @click="submit">提交</view>
		</view>
<!-- 		<SelDeparment v-model="show_department" @confirm="departmentConfirm" @cancel="departmentCancel" />
		<RegionSel v-model="region_show" @confirm="regionConfirm" @cancel="regionCancel" />
		<u-picker @confirm="birthdayConfirm" mode="time" v-model="time_show"></u-picker> -->
	</view>
</template>

<script>
	import upload from '@/components/qiniu/QiniuUpload.vue';
	import SelDeparment from '@/components/SelDepartment.vue';
	import RegionSel from '@/components/region/RegionSel.vue';
	export default {
		components: {
			upload,
			SelDeparment,
			RegionSel
		},
		data() {
			return {
				region_show: false,
				time_show: false,
				show_department: false,
				shopData: '',
				staffData: '',
				reservoirData: '',
				customerTypeData: '',
				reservoir_name: '',
				shop_name: '',
				type_name: '',
				department_name: '',
				region_name: '',
				birthday: '',
				rules: {
					staffName: [{
						required: true,
						message: '请输入姓名',
						trigger: 'blur,change'
					}],
					avatar: [{
						required: true,
						message: '请上传头像',
						trigger: 'blur,change'
					}],
					info: [{
						required: true,
						message: '请输入简介',
						trigger: 'blur,change'
					}]
				},
				isEvidence: '', // 是否必填营业执照
				add_form: {
					staffName: '',
					info: '',
					avatar: ''
				},
				customer_id: ''
			};
		},
		watch: {
			// reservoirData(val) {
			// 	if (val) {
			// 		this.add_form.reservoirId = val.id;
			// 		this.reservoir_name = val.reservoirName;
			// 	}
			// },
			// shopData(val) {
			// 	if (val) {
			// 		this.add_form.shopId = val.id;
			// 		this.shop_name = val.name;
			// 	}
			// },
			// customerTypeData(val) {
			// 	if (val) {
			// 		this.add_form.type = val.id;
			// 		this.type_name = val.name;
			// 		this.isEvidence = val.isEvidence;
			// 	}
			// },
			// staffData(val) {
			// 	if (val) {
			// 		this.add_form.salesManId = val.id;
			// 		this.add_form.salesManCode = val.staffCode;
			// 		this.add_form.salesManName = val.staffName;
			// 	}
			// }
		},
		onLoad(options) {
			this.getUserInfo()
		},
		methods: {
			getUserInfo() {
				this.$u.api.getStaffInfo(this.$store.state.userInfo.staffId).then(({
					data
				}) => {
					console.log(data,'员工信息');
					this.add_form.info = data.info
					this.add_form.avatar = data.avatar
					this.add_form.departmentId = data.departmentId
					this.add_form.staffName = data.staffName
					this.add_form.roleId = data.roleId
					this.add_form.mobile = data.mobile
					this.add_form.email = data.email
					this.add_form.staffCode = data.staffCode
					this.add_form.sex = data.sex
					this.add_form.age = data.age / 1000
					this.add_form.education = data.education // 学历
					this.add_form.departmentPidPath = data.departmentPidPath // 添加部门id的数组
					this.add_form.rolePidPath = data.rolePidPath // 添加角色id的数组
					this.add_form.dataField = data.dataField
					// this.add_form.is_technician = data.is_technician
					// this.add_form.is_clock = data.is_clock
				})
			},
			// const params = {
			//   departmentId: this.form.departmentId,
			//   staffName: this.form.staffName,
			//   roleId: this.form.roleId,
			//   mobile: this.form.mobile,
			//   email: this.form.email,
			//   staffCode: this.form.staffCode,
			//   sex: this.form.sex,
			//   age: this.form.age / 1000,
			//   education: this.form.education, // 学历
			//   departmentPidPath: this.form.departmentPidPath, // 添加部门id的数组
			//   rolePidPath: this.form.rolePidPath, // 添加角色id的数组
			//   dataField: this.form.dataField,
			// };
			// if (this.id) {
			//   target = await updateStaff(this.id, params);
			// } else {
			//   target = await addStaff(params);
			// }
			// const data = target;

			submit() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						this.$u.api.setYgInfo(this.$store.state.userInfo.staffId, {
								...this.add_form
							})
							.then(res => {
								let userInfo = this.$store.state.userInfo
								userInfo.name = this.add_form.staffName
								
								this.$store.commit('commit_userInfo', userInfo);
								this.$u.toast('修改成功');
								setTimeout(res => {
									uni.navigateBack();
								}, 1500);
							});
					}
				});
			},
			regionConfirm(val) {
				this.add_form.provinceCode = val[0].value;
				this.add_form.cityCode = val[1].value;
				this.add_form.districtCode = val[2].value;
				// 联系方式
				this.add_form.contact.provinceCode = val[0].value;
				this.add_form.contact.cityCode = val[1].value;
				this.add_form.contact.districtCode = val[2].value;
				let name = '';
				val.forEach(item => {
					name += item.label;
				});
				this.region_name = name;
			},
			regionCancel(val) {
				this.region_show = false;
			},
			departmentConfirm(val) {
				this.add_form.departmentId = val[val.length - 1].value;
				this.department_name = val[val.length - 1].label;
				this.add_form.salesManId = '';
				this.add_form.salesManCode = '';
				this.add_form.salesManName = '';
			},
			departmentCancel(val) {
				this.show_department = false;
			},
			openSel(key) {
				this[key] = true;
			},
			// 图片上传成功
			uploadSuccess(imgUrl) {
				this.add_form.avatar = imgUrl;
			},
			//移除图片
			imgRemove(arr) {
				this.add_form.extend.license = '';
			},
			birthdayConfirm(val) {
				this.add_form.birthday = val.timestamp;
				this.birthday = val.year + '-' + val.month + '-' + val.day;
			}
		},
		// 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
		// onReady() {
		// 	this.$refs.uForm.setRules(this.rules);
		// }
	};
</script>

<style></style>
