<template>
	<view class="evan-form-show flex flex_column">
			<view class="flex_1">
				<evanForm ref="formData" :rules="ruleValidate" :model="formData">
					<evanFormItem label="员工姓名" prop="name">
						<input class="form-input" placeholder-class="form-input-placeholder" :maxlength="30" placeholder="请输入姓名(最多30个字符)" v-model="formData.name" />
					</evanFormItem>
					<evanFormItem label="手机号码" prop="mobile">
						<input type="number" :disabled="formData.isManager==1 && formData.id?true:false" class="form-input" placeholder="请输入您的手机号码" placeholder-class="form-input-placeholder" v-model="formData.mobile" :maxlength="11"/>
					</evanFormItem>
					<evanFormItem label="性别" prop="sex">
						<view style="width: 100%;display:flex;justify-content: flex-end;">
							<u-radio-group v-model="formData.sex">
								<u-radio 
									v-for="(item, index) in sexList" :key="index" 
									:name="item.value"
									shape="circle"
								>
								{{item.name}}
								</u-radio>
							</u-radio-group>
						</view>
					</evanFormItem>
				</evanForm>
			</view>
			<view class="form-footer-btn flex align_center justify_between">
				<u-button shape="circle"  @click="cancel()" :custom-style="{width:'300rpx' }">取消</u-button>
				<u-button shape="circle" type="info" :custom-style="{ background: '#066cff', color: '#fff',width:'300rpx' }" @click="save('formData')">保存</u-button>
			</view>
	</view>
</template>

<script>
	import {saveEmployee } from '@/api/employee'
	import evanForm from '@/components/evan-form/evan-form.vue'
	import evanFormItem from '@/components/evan-form-item/evan-form-item.vue'
	export default{
		components:{
			evanForm,
			evanFormItem
		},
		data(){
			return{
				roleList: [],  //  角色  数据列表
				roleArr: [],  //  当前所选角色
				formData:{
					name: '',
					sex: '1',
					mobile: ''
				},
				ruleValidate: {
					name: [{ required: true, message: '请输入员工姓名'}],
					mobile: [{required: true, min:11, message: '请输入正确手机号码'}],
					sex: [{ required: true, message: '请选择性别' }],
				},
				sexList: [
					{
						value: 1,
						name: '男',
						disabled: false,
					},
					{
						value: 0,
						name: '女',
						disabled: false
					}
				],
				isManager: '',
				isEdit: false
			}
		},
		onLoad() {
			this.init()
		},
		methods:{
			//  初始化数据
			init(){
				let data = this.$store.state.vuex_nowStaffData
				// 编辑信息
				if(data){
					this.isEdit = true
					this.formData = Object.assign({},this.formData,data)
					console.log(this.formData)
				}else{
					this.isEdit = false
				}
				uni.setNavigationBarTitle({
					title:data ? "编辑员工" : '新增员工'
				})
			},
			//  保存
			save(name){
				let data=this.formData
				console.log(data)
				this.$refs[name].validate((valid) => {
				  if (valid) {
					saveEmployee(data).then(res=>{
						console.log(res)
						if(res.status == 200){
							uni.showToast({icon: 'none', title:'保存成功'})
							this.cancel()
						}else{
							uni.showToast({icon: 'none', title: res.message})
						}
					}).catch(err=>{
						console.log('保存失败')
					})
				}
				})
			},
			cancel(){
			 setTimeout(function(){
				 uni.navigateBack()
			 },100)
			}
		}
	}
</script>

<style lang="less">
	.evan-form-show {
			padding: 0;
			background-color: #fff;
			height: 100vh;
			> view{
				padding:0.8rem;
			}
			.form-footer-btn{
				padding: 0.8rem 1.5rem;
			}
			.form-input {
				font-size: 28rpx;
				color: #333;
				text-align: right;
				width: 95%;
				box-sizing: border-box;
				height: 60rpx;
				&.textarea{
					height: 240rpx;
					padding: 24rpx 0;
					text-align: left;
				}
			}
			.form-input-placeholder {
				font-size: 28rpx;
				color: #999;
			}
		}
</style>