<template>
	<view class="container">
		<u-form :model="form" :label-width="140" ref="uForm" :label-align="'left'" error-type="toast">
			<u-form-item label="姓名" prop="username" required><u-input v-model="form.username" /></u-form-item>
			<u-form-item label="性别" prop="sex" required><u-input @click="showSex = true" v-model="form.sex"
					type="select" /></u-form-item>
			<u-form-item label="身份证号" prop="idcard" required><u-input type="idcard"
					v-model="form.idcard" /></u-form-item>
			<u-form-item label="手机号码" prop="mobile" required><u-input type="number"
					v-model="form.mobile" /></u-form-item>
			<u-form-item label="公司名称" prop="company_name"><u-input v-model="form.company_name" /></u-form-item>
			<u-form-item label="职务" prop="company_position"><u-input v-model="form.company_position" /></u-form-item>
		</u-form>
		<u-button @click="saveUser">提交</u-button>
		<u-picker @confirm="selectSex" v-model="showSex" :range="sexList" range-key="value" mode="selector"></u-picker>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				showSex: false,
				form: {},
				sexList: [{
					label: '男',
					value: '男'
				}, {
					label: '女',
					value: '女'
				}],
				rules: {
					username: [{
						required: true,
						message: '请输入姓名',
						trigger: ['change', 'blur'],
					}],
					sex: [{
						required: true,
						message: '请选择性别',
						trigger: ['change', 'blur'],
					}],
				}
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		onLoad() {
			
		},
		methods:{
			selectSex(e){
				console.log("e",e)
				this.form.sex = this.sexList[e[0]]['value']
			},
			saveUser(){
				this.$u.api.saveUser(this.form).then(res=>{
					this.base.toast("注册成功")
					this.$u.api.user().then(res => {
						this.$u.vuex('vuex_user', res.user)
						uni.redirectTo({
							url:'/pages/me/index'
						})
					})
				})
			},
		}
	}
</script>

<style>
</style>