<template>
	<view class="container">
		<form @submit="onSubmit">
			<view class="page-block">
				<view class="content">

					<view class="cell flex flex-y-center" v-if="type==1">
						<view class="title-block required">
							<text>所属门店</text>
						</view>
						<view class="desc">
							<picker mode="selector" :range="tableTypeList" range-key="title"
								@change="onTableTypeChange">

								<view class="box-pack-between">
									<text>{{tableTypeActive}}</text>
									<text class="iconfont icongengduo"></text>
								</view>

							</picker>
						</view>

					</view>

					<view class="cell flex flex-y-center">
						<view class="title-block">
							<text>员工昵称</text>
						</view>
						<view class="desc">
							<input type="text" :value="info.nickname" name="nickname" class="input-box"
								placeholder="请输入员工昵称" placeholder-class="desc-placeholder">
						</view>
					</view>

					<view class="cell flex flex-y-center">
						<view class="title-block ">
							<text>性别</text>
						</view>
						<view class="desc">
							<view class="flex-center">
								<view class="checklist-box " @click="choicepay(item.num)"
									v-for="(item,index) of integral_arr" :key='index' :class="sex==item.num?'actt':''">
									<view class="radio__inner" :class="sex==item.num?'act_border':''">
										<view :class="sex==item.num?'is-default-checked':''"
											class="radio__inner-icon radio__inner-icon ">
										</view>
									</view>
									<text class="checklist-text">{{item.name}}</text>
								</view>
							</view>
						</view>
					</view>

					<view class="cell flex flex-y-center">
						<view class="title-block ">
							<text>真实姓名</text>
						</view>
						<view class="desc">
							<input type="text" :value="info.name" name="name" class="input-box" placeholder="请输入真实姓名"
								placeholder-class="desc-placeholder">
						</view>
					</view>

					<view class="cell flex flex-y-center">
						<view class="title-block ">
							<text>手机号码</text>
						</view>
						<view class="desc">
							<input type="number"  maxlength="11" :value="info.phone" name="phone" class="input-box"
								placeholder="请输入手机号码" placeholder-class="desc-placeholder">
						</view>
					</view>

					<view class="cell flex flex-y-center">
						<view class="title-block">
							<text>员工登录密码</text>
						</view>
						<view class="desc">
							<input type="text" :value="info.pass" name="pass" class="input-box" placeholder="请输入员工密码"
								placeholder-class="desc-placeholder">
						</view>
					</view>

					<view class="cell flex flex-y-center">
						<view class="title-block">
							<text>员工编号</text>
						</view>
						<view class="desc">
							<input type="text" :value="info.number" name="number" class="input-box"
								placeholder="请输入员工编号" placeholder-class="desc-placeholder">
						</view>
					</view>
				</view>
			</view>

			<view class="footer-bar">
				<view class="content flex flex-center" style="background: none;">
					<button class="btn" hover-class="none" form-type="submit">
						立即添加
					</button>
				</view>
			</view>


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

<script>
	export default {
		data() {
			return {
				id: "",
				store_id: "",
				info: {},
				integral_arr: [{
					num: 1,
					name: '男'
				}, {
					num: 2,
					name: '女'
				}, ],
				sex: 1,
				type: 2,
				tableTypeList: [],
				tableTypeActive: "请选择",
				admin_id: '',
			}
		},

		onLoad(e) {
			this.store_id = uni.getStorageSync("store_id");
			this.admin_id = uni.getStorageSync("admin_id");
			this.type = e.type||2;
			if (e.id) {
				this.id = e.id;
			}			
			this.shop_store_index();

		},
		methods: {
			shop_store_index() {
				this.request({
					url: 'Smdcshop/shop_store_index',
					data: {
						admin_id:this.admin_id,
					}
				}).then(res => {
					if (res.code == 200) {
						this.tableTypeList = res.data;
						if(this.id){
							this.getData(this.id)
						}
					}
				})
			},

			onTableTypeChange(e) {
				console.log(e)
				this.tableTypeActive = this.tableTypeList[e.detail.value].title;
				this.store_id = this.tableTypeList[e.detail.value].id
			},

			choicepay(e) {
				this.sex = e;
			},

			getData(id) {
				this.request({
					url: 'Smdcshop/staff_info',
					data: {
						id: id
					}
				}).then(res => {
					if (res.code == "200") {
						this.info = res.data;
						this.sex = res.data.sex;
						this.store_id = res.data.storeid;	
						if(this.tableTypeList.length>0){
							for (let i of this.tableTypeList) {
								if (i.id == Number(res.data.storeid)) {		
									this.tableTypeActive = i.title;
								}
							}
						}
					}
				})
			},

			onSubmit(e) {
				let params = e.detail.value,
					url;
				params.storeid = this.store_id;
				params.sex = this.sex;
				if (this.id) {
					params.id = this.id;
					url = "Smdcshop/staff_update";
				} else {
					url = "Smdcshop/staff_staff";
				}
				
				if (!params.nickname || !params.phone || !params.pass || !params.name) {
					uni.showToast({
						title: "必须不能为空",
						icon: 'none'
					})
					return false
				}
				var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
				if (!myreg.test(params.phone)) {
					uni.showToast({
						title: "联系电话格式不正确",
						icon: 'none'
					});
					return false;
				}
				this.request({
					url: url,
					data: params
				}).then(res => {
					if (res.code == 200) {
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
						setTimeout(() => {
							uni.navigateBack()
						}, 2000)
					}
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
				});
			},
		}
	}
</script>

<style>
	@import url("./static/css/common.css");

	page {
		background-color: #F4F5F7;
	}

	.checklist-box {
		display: -webkit-box;
		display: -webkit-flex;
		display: flex;

		-webkit-box-orient: horizontal;
		-webkit-box-direction: normal;
		-webkit-flex-direction: row;
		flex-direction: row;
		-webkit-box-align: center;
		-webkit-align-items: center;
		align-items: center;
		margin: 5px 0;
		margin-right: 22px;
	}

	.radio__inner {
		-webkit-flex-shrink: 0;
		flex-shrink: 0;

		display: -webkit-box;
		display: -webkit-flex;
		display: flex;

		-webkit-box-pack: center;
		-webkit-justify-content: center;
		justify-content: center;
		-webkit-box-align: center;
		-webkit-align-items: center;
		align-items: center;
		position: relative;
		border: 1px solid #DCDFE6;
		border-radius: 2px;
		box-sizing: border-box;
		width: 16px;
		height: 16px;
		border-radius: 16px;
		background-color: #fff;
		z-index: 1;
		-webkit-transition: border-color .3s;
		transition: border-color .3s;
	}

	.radio__inner-icon {
		width: 8px;
		height: 8px;
		border-radius: 10px;
		opacity: 0;
		-webkit-transition: -webkit-transform .3s;
		transition: -webkit-transform .3s;
		transition: transform .3s;
		transition: transform .3s, -webkit-transform .3s;
	}

	.is-default-checked {
		opacity: 1;
		background-color: #007aff;
		-webkit-transform: rotate(45deg) scaleY(1);
		transform: rotate(45deg) scaleY(1);
	}

	.checklist-text {
		font-size: 28rpx;
		margin-left: 5px;
		-webkit-transition: color 0.2s;
		transition: color 0.2s;
		color: #000;
	}

	.act_border {
		border: 1rpx solid #007aff;
	}
</style>
