<template>
	<view class="content">

		<view class="contentitem flex-between">
			<view class="contentitemtext">
				头像
			</view>
			<view class="onecontent_itemright flex-start">

				<view class="onecontent_itemrightimges flex-colum" v-if="avatar" @click="chooseImagelist()">
					<image :src="avatar"></image>
				</view>
				<view class="onecontent_itemrightimges flex-colum" v-else @click="chooseImagelist()">
					+
				</view>
			</view>
		</view>
		<view class="contentitem flex-between">
			<text class="contentitemtext">姓名</text>
			<input class="contentiteminput" v-model="form.nickname" type="text" placeholder="请输入员工姓名">
		</view>
		<view class="contentitem flex-between" style="border-radius: 10rpx 10rpx 0 0 ;">
			<text class="contentitemtext">账号/手机号</text>
			<input class="contentiteminput" v-model="form.mobile" type="text" placeholder="请设置员工账号/手机号">
		</view>
		<view class="contentitem flex-between" style="border-radius: 10rpx 10rpx 0 0 ; margin-top:32rpx ;">
			<text class="contentitemtext">登录密码</text>
			<input class="contentiteminput" v-model="form.password" password="false" type="text" placeholder="员工登录密码">
		</view>
		<view v-if="staff_id" style="font-size: 24rpx; padding: 10rpx 0;">不修改密码请留空</view>
		<view class="contentitem flex-between" style="border-radius:0 0 10rpx 10rpx;">
			<text class="contentitemtext">确认密码</text>
			<input class="contentiteminput" password="false" v-model="form.passwords" type="text" placeholder="再次输入密码">
		</view>
		<view class="contentitem flex-between" style="border-radius:10rpx; margin-top:32rpx;"
		>
			<text class="contentitemtext">排序</text>
			<u-number-box integer v-model="form.sort"></u-number-box>
		</view>
		<view v-if="staff_id" class="positionfixed" @click="staffsavestaff">
			确认修改
		</view>
		<view v-else class="positionfixed" @click="staffcreatestaff">
			确认创建
		</view>
		<view class="positionfixeds" @click="staffdeletestaff">
			删除员工
		</view>
		<view class="contentitem flex-between" style="border-radius:10rpx; margin-top:32rpx;"
			@click="pickershow = true">
			<text class="contentitemtext">全部订单权限</text>
			<view class="flex-start">
				<view class="contentiteminput">{{nameis_limits}}</view>
				<u-icon name="arrow-right" color="#999999" size="18"></u-icon>
			</view>
		</view>
		<u-picker :show="pickershow" :columns="columns" @confirm='confirm' keyName="label"
			@cancel='pickershow = false'></u-picker>
	</view>
</template>
<script>
	import uploadImage from "@/js_sdk/yushijie-ossutil/ossutil/uploadFile.js";
	import {
		nextTick
	} from "vue";
	export default {
		data() {
			return {
				columns: [
					[{
						label: '允许',
						id: 1
					}, {
						label: '不允许',
						id: 2
					}],
				],
				pickershow: false,
				staff_id: '',
				avatar: '',
				nameis_limits: '允许',
				form: {
					nickname: '',
					mobile: '',
					store_id: '',
					password: '',
					passwords: "",
					sort: 1,
					is_limits: 1,
					avatar: ""
				}
			}
		},
		onLoad(e) {
			if (e.id) {
				uni.setNavigationBarTitle({
					title: '修改员工'
				})
				this.staff_id = e.id
				this.staffstaffreturn()
			} else {
				uni.setNavigationBarTitle({
					title: '添加员工'
				})
			}
		},
		onShow() {},
		methods: {
			async staffdeletestaff(){
				let res = await this.api.staffdeletestaff({
					staff_id:this.staff_id
				})
				if (res.code == 1) {
					uni.showToast({
						title: '删除成功',
						icon: 'none'
					});
					setTimeout(() => {
						uni.navigateBack();
					}, 2000);
				}
			},
			confirm(e) {
				this.pickershow = false
				this.form.is_limits = e.value[0].id
				this.nameis_limits = e.value[0].label
				console.log(e)
			},
			// 上传
			chooseImagelist() {
				uni.showLoading({
					title: '上传中',
					mask: true
				})
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						let file = res.tempFilePaths[0];
						uploadImage(file, 'wechat/staffadd/',
							result => {
								this.avatar = ''
								this.$nextTick(() => {
									this.form.avatar = result
									this.avatar = result
									uni.hideLoading()
								})
							}, result => {
								uni.hideLoading()
							})
					},
					fail: (res) => {
						uni.hideLoading()
					}
				});
			},
			async staffstaffreturn() { //员工信息回显
				let res = await this.api.staffstaffreturn({
					staff_id: this.staff_id,
				});
				if (res.code == 1) {
					this.form = res.data
					this.avatar = res.data.avatar
					if (this.form.is_limits == 1) {
						this.nameis_limits = '允许'
					} else {
						this.nameis_limits = '不允许'
					}
				}
			},
			async staffsavestaff() { //修改员工
				if (this.form.avatar == null || this.form.avatar == '') {
					uni.showToast({
						title: '请上传头像',
						icon: 'none'
					});
					return false;
				}
				if (this.form.nickname == null || this.form.nickname == '') {
					uni.showToast({
						title: '请输入姓名',
						icon: 'none'
					});
					return false;
				}
				if (this.form.mobile == null || this.form.mobile == '') {
					uni.showToast({
						title: '请输入电话/员工账号',
						icon: 'none'
					});
					return false;
				}
				if (this.form.password != null || this.form.passwords != '' || this.form.password != '' || this.form
					.passwords != null) {
					if (this.form.password != this.form.passwords) {
						uni.showToast({
							title: '两次输入密码不相同',
							icon: 'none'
						});
						return false;
					}
				}
				if (this.form.password != this.form.passwords) {
					uni.showToast({
						title: '两次输入密码不相同',
						icon: 'none'
					});
					return false;
				}
				if (this.form.sort == null || this.form.sort == '') {
					uni.showToast({
						title: '请输入排序',
						icon: 'none'
					});
					return false;
				}
				let res = await this.api.staffsavestaff({
					nickname: this.form.nickname,
					mobile: this.form.mobile,
					staff_id: this.staff_id,
					sort: this.form.sort,
					is_limits:this.form.is_limits,
					avatar:this.form.avatar
				});
				if (res.code == 1) {
					uni.showToast({
						title: '修改成功',
						icon: 'none'
					});
					setTimeout(() => {
						uni.navigateBack();
					}, 2000);
				}
			},
			async staffcreatestaff() { //创建员工
				if (this.form.avatar == null || this.form.avatar == '') {
					uni.showToast({
						title: '请上传头像',
						icon: 'none'
					});
					return false;
				}
				if (this.form.nickname == null || this.form.nickname == '') {
					uni.showToast({
						title: '请输入姓名',
						icon: 'none'
					});
					return false;
				}
				if (this.form.mobile == null || this.form.mobile == '') {
					uni.showToast({
						title: '请输入电话/员工账号',
						icon: 'none'
					});
					return false;
				}
				if (this.form.password == null || this.form.password == '') {
					uni.showToast({
						title: '请输入密码',
						icon: 'none'
					});
					return false;
				}
				if (this.form.passwords == null || this.form.passwords == '') {
					uni.showToast({
						title: '请输入确认密码',
						icon: 'none'
					});
					return false;
				}
				if (this.form.password != this.form.passwords) {
					uni.showToast({
						title: '两次输入密码不相同',
						icon: 'none'
					});
					return false;
				}
				if (this.form.sort == null || this.form.sort == '') {
					uni.showToast({
						title: '请输入排序',
						icon: 'none'
					});
					return false;
				}
				let res = await this.api.staffcreatestaff(this.form);
				if (res.code == 1) {
					uni.showToast({
						title: '创建成功',
						icon: 'none'
					});
					setTimeout(() => {
						uni.navigateBack();
					}, 2000);
				}
			},

		}
	}
</script>

<style lang="scss">
	page {
		background: #FBFBFA;
	}

	.content {
		margin: 32rpx 28rpx;

		.positionfixeds {
			position: fixed;
			width: 70%;
			bottom: 120rpx;
			left: 15%;
			padding: 10rpx 0;
			text-align: center;
			background: #d4d4d4;
			font-size: 36rpx;
			border-radius: 18rpx;
			font-family: SourceHanSansCN-Regular, SourceHanSansCN;
			font-weight: 400;
			color: #707070;
		}

		.positionfixed {
			position: fixed;
			width: 70%;
			bottom: 40rpx;
			left: 15%;
			padding: 10rpx 0;
			text-align: center;
			background: #2F87FD;
			font-size: 36rpx;
			border-radius: 18rpx;
			font-family: SourceHanSansCN-Regular, SourceHanSansCN;
			font-weight: 400;
			color: #FFFFFF;
		}

		.contentitem {
			width: 100%;
			padding: 12rpx 16rpx;
			border-bottom: 2rpx solid #E5E5E5;
			background: #FFFFFF;

			.contentitemtext {
				font-size: 32rpx;
				font-family: SourceHanSansCN-Regular, SourceHanSansCN;
				font-weight: 400;
				color: #333333;
			}

			.onecontent_itemright {
				.onecontent_itemrighttext {
					font-size: 24rpx;
					font-family: SourceHanSansCN-Regular, SourceHanSansCN;
					font-weight: 400;
					color: #999999;
				}

				.onecontent_itemrightimges {
					width: 112rpx;
					height: 112rpx;
					background: #F5F5F5;
					border-radius: 10rpx;
					font-size: 40rpx;
					color: #878787;

					image {
						width: 112rpx;
						height: 112rpx;
					}
				}

				.onecontent_itemright_text {
					width: 100%;
					font-size: 24rpx;
					font-family: SourceHanSansCN-Regular, SourceHanSansCN;
					font-weight: 400;
					color: #999999;
				}
			}

			.contentiteminput {
				padding-left: 20rpx;
				text-align: right;
				flex: auto;
				font-size: 28rpx;
				font-family: SourceHanSansCN-Regular, SourceHanSansCN;
				font-weight: 400;
				color: #999999;
			}
		}
	}
</style>