<template>
	<view>
		<view class="popup-height">
			<uni-forms :modelValue="addForm" ref="addForm" validateTrigger="bind" :rules="rules" label-width="160rpx"
				label-align="right">
				<uni-forms-item label="员工姓名" required name="nickname">
					<uni-easyinput type="text" v-model="addForm.nickname" placeholder="请输入员工姓名" />
				</uni-forms-item>
				<uni-forms-item label="手机号" required name="mobile">
					<uni-easyinput type="text" v-model="addForm.mobile" placeholder="请输入员工手机号" />
				</uni-forms-item>
				<uni-forms-item label="身份权限" required name="type">
					<!-- <uni-data-checkbox multiple v-model="addForm.type" :localdata="typeList"></uni-data-checkbox> -->
					<uni-data-select style="width: 100%" v-model="addForm.type" :localdata="typeList"></uni-data-select>
				</uni-forms-item>
				<uni-forms-item label="入库确认" required name="warehouseKeeperFlag">
					<!-- <uni-data-checkbox multiple v-model="addForm.type" :localdata="typeList"></uni-data-checkbox> -->
					<uni-data-select style="width: 100%" v-model="addForm.warehouseKeeperFlag" :localdata="wareList"></uni-data-select>
				</uni-forms-item>
			</uni-forms>
			<u-button type="primary" @click="onFormSubmit">保存</u-button>
			<button type="primary" style="margin-top: 13px !important;background-color:#5fd9ee;font-size:14px;height: 40px;;"  @click="deleteSubmit" v-if="type==='edit'">删除</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				addForm: {
					nickname: '',
					mobile: '',
					type: [],
					password:'123456',
					warehouseKeeperFlag:0
				},
				username:'',
				id: '',
				type:'',
				typeList: [{
						value: 0,
						text: "管理员"
					},
					{
						value: 1,
						text: "员工"
					},
					// {
					// 	value: 3,
					// 	text: "库房配货"
					// },
					// {
					// 	value: 4,
					// 	text: "入库员"
					// },
				],
				wareList: [{
						value: 0,
						text: "否"
					},
					{
						value: 1,
						text: "是"
					},
					// {
					// 	value: 3,
					// 	text: "库房配货"
					// },
					// {
					// 	value: 4,
					// 	text: "入库员"
					// },
				],
				rules: {
					nickname: {
						rules: [{
							required: true,
							errorMessage: '不能为空'
						}]
					},
					mobile: {
						rules: [{
							required: true,
							errorMessage: '不能为空'
						}]
					},
					type: {
						rules: [{
							required: true,
							errorMessage: '不能为空'
						}]
					},
				}
			}
		},
		onLoad(options) {
			const type = options.type
			const item = JSON.parse(options.item)
			this.type = options.type
			if (type === 'edit') {
				this.addForm.nickname = item.nickname
				this.addForm.mobile = item.mobile
				this.addForm.warehouseKeeperFlag = Number(item.warehouseKeeperFlag)
				this.addForm.type = Number(item.type)
				this.id = item.id
			}
			uni.setNavigationBarTitle({
				title: type === 'add' ? '新增员工' : '编辑信息'
			});
		},
		methods: {
			onFormSubmit() {
				if(this.addForm.type.length < 1)return
				this.addForm.type = JSON.stringify(this.addForm.type)
				this.$refs.addForm.validate().then(async () => {
					let url;
					if(this.type=='edit'){
						url = '/app/common/user/edit'
						this.addForm.username = this.addForm.mobile
						this.addForm.id = this.id
					}else{
						url = '/app/common/user/add'
					}
					const res = await this.tui.request(url, 'post', this.addForm, false,
						true, false)
					if (res.code == 200) {
						uni.navigateBack()
						if(this.addForm.mobile == uni.getStorageSync('username')){
							uni.setStorageSync('warehouseKeeperFlag',this.addForm.warehouseKeeperFlag)
						}
					} else {
						this.tui.toast(res.message)
					}
				}, (reason) => {

				})
			},
			deleteSubmit() {
				let ids = []
				ids.push(this.id)
				this.tui.request("/app/common/user/delByIds", "POST", {
					ids: ids
				}, false, true).then((res1) => {
					uni.navigateBack()
				}).catch((res) => {})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.popup-height {
		padding: 0 25upx;
		background-color: #fff;
	}
</style>