<template>
	<uni-drawer ref="drawer" mode="left" :mask-click="false" width="85%" :maskClick='true'>
		<scroll-view class="scroll-view" scroll-y="true">
			<view class="form-wrap" >
				<uni-forms ref="form" label-position="top" :rules="rules" :model="form">
					<uni-forms-item label="队伍名称" name="teamName" required>
						<uni-easyinput v-model="form.teamName" placeholder="请输入队伍名称" :clearable="false" />
					</uni-forms-item>
					<uni-forms-item label="队伍类别" name="teamCatId" required>
						<uni-data-select  v-model="form.teamCatId" :localdata="teamCats" placeholder="请选择值班人员" :clear="false"></uni-data-select>
					</uni-forms-item>
					<uni-forms-item label="出库人员" name="accountId" required>
						<uni-data-select  v-model="form.accountId" :localdata="userList" placeholder="请选择出库人员" :clear="false"></uni-data-select>
					</uni-forms-item>
					<uni-forms-item label="职责说明">
						<uni-easyinput type="textarea" :maxlength="1000" v-model="form.teamDuty" placeholder="请输入职责说明" autoHeight ></uni-easyinput>
						<view class="word-limit">{{form.teamDuty.length||0}}/1000</view>
					</uni-forms-item>
					<uni-forms-item label="说明">
						<uni-easyinput type="textarea" :maxlength="500"
						 v-model="form.teamDesc" autoHeight 
						placeholder="请输入职责说明"></uni-easyinput>
						<view class="word-limit">{{form.teamDesc.length||0}}/500</view>
					</uni-forms-item>
					<uni-forms-item label="排序" v-model="form.sortNo">
						<uni-number-box :min="1"></uni-number-box>
					</uni-forms-item>
				</uni-forms>
				<view class="handle-container">
					<button class="save" type="primary" @click="onSubmit">保存</button>
					<button class="cancel" type="default" @click="close">取消</button>
				</view>
			</view>	
		</scroll-view>
	</uni-drawer>
</template>

<script>
	import teamApi from '@/api/team.js'	
	export default{
		name:"Inbound",
		computed:{
			teamCats(){
				let teamCats=uni.getStorageSync('teamCat')
				return teamCats.map(item=>{
					return{
						value:item.teamCatId,
						text:item.teamCatTitle
					}
				})
			},
			userList(){
				let groupUser=uni.getStorageSync('groupUser')
				let userList=[]
				groupUser.map((item,index)=>{
					userList.push({...item,value:`root-${index+1}`,text:item.name,disable:true})
					if(Array.isArray(item.children)&&item.children.length>0){
						userList=userList.concat(item.children.map(item=>{return{...item,value:item.accountId,text:item.accountName}}))
					}			
				})
				return userList
			}			
		},
		data(){
			return{
				type:undefined,
				rules:{
					teamName:{
						rules:[
							{
								required: true,
								errorMessage: '请输入队伍名称'
							}					
						],
						validateTrigger:'submit'
					},
					teamCatId:{
						rules:[
							{
								required: true,
								errorMessage: '请选择队伍类别',
							}
						],
						validateTrigger:'submit'
					},
				},
				form:{
					teamId: 0,
					teamName: '',
					teamType: 1,
					teamCatId: 1,
					teamDuty: '',
					teamDesc: '',
					sortNo: 1
				}
			}
		},
		methods:{
			resetForm(){
				this.form={
					teamId: 0,
					teamName: '',
					teamType: 1,
					teamCatId: 1,
					teamDuty: '',
					teamDesc: '',
					sortNo: 1
				}
			},
			onSubmit(){
				this.$refs.form.validate().then(res=>{
					let submitFx=this.type==='add'?teamApi.create:teamApi.update
					submitFx(this.form).then(()=>{
						uni.showToast({
							icon:"none",
							title:"成功！！"
						})
						this.$emit('success')
						this.close();
					})

				}).catch(err =>{
					uni.showToast({
						icon:"none",
						title:"请检查填写信息！"
					})
				})
			},
			show(type,item){
				this.resetForm()
				if(type==='edit'){
					this.form={...item}
				}else{
					this.form.teamType=item
				}
				this.type=type
				this.$refs.drawer.open()
			},
			close(){
				this.$refs.drawer.close()
			}
		}
	}
</script>
<style lang="scss" scoped>
	.scroll-view {
		height: 100%;
		padding: 20rpx;
		box-sizing: border-box;
		.form-wrap{
			padding:40rpx;
			.handle-container{
				display: flex;
				justify-content: center;
				align-items: center;
				button{
					width: 160rpx;
					padding: 20rpx 16rpx;
					line-height: 1;
					font-size: 28rpx;
					&.save{
						background-color: #007aff;
					}
				}
			}
			.word-limit{
				text-align: right;
				padding: 10rpx 0;
				color: #999;
				font-size: 26rpx;
			}
		}
	}
</style>