<template>
	<div class="container add_edit">
		<el-form :model="form" :rules="rules" ref="ruleForm" label-width="100px">
			<el-form-item label="账号" prop="username" ref="username">
				<el-input v-model="form.username" placeholder="请输入账号" clearable></el-input>
			</el-form-item>
			<el-form-item label="密码" prop="password" ref="password" v-if="dataId === ''">
				<el-input v-model="form.password" show-password placeholder="请输入密码"></el-input>
			</el-form-item>
			<!-- <el-form-item label="角色" prop="role" ref="role">
				<el-select class="sele" multiple v-model="role" @change="roleAdd">
					<el-option v-for="(item, idx) in roleOptions" :key="idx" :label="item.role_id"
						:value="item.role_id"></el-option>
				</el-select>
				<div class="remark_txt">用户可拥有多种角色</div>
			</el-form-item> -->
			<el-form-item label="性别">
				<!-- <el-radio v-model="form.gender" label="0">未知</el-radio> -->
				<el-radio v-model="form.gender" label="1">男</el-radio>
				<el-radio v-model="form.gender" label="2">女</el-radio>
			</el-form-item>
			<el-form-item label="头像">
				<uni-file-picker v-model="imageValue" fileMediatype="image" returnType="object"
					:image-styles="imageStyle" @select="imgUpload" @delete="imgDelete" />
			</el-form-item>
			<el-form-item label="手机号">
				<el-input v-model="form.phone" type="number" placeholder="请输入手机号" clearable></el-input>
			</el-form-item>
			<!-- <el-form-item label="备注">
				<el-input v-model="form.comment" placeholder="请输入备注" clearable></el-input>
			</el-form-item> -->
			<!-- <el-form-item label="状态">
				<el-switch v-model="form.state" active-color="#ff6a6c" inactive-color="#bbb" :active-value="0"
					:inactive-value="1"></el-switch>
			</el-form-item> -->
			<el-form-item>
				<el-button round icon="el-icon-check" class="confirm_btn" @click="submitForm('ruleForm')">提交</el-button>
				<el-button round icon="el-icon-back" @click="returnPage">返回</el-button>
			</el-form-item>
		</el-form>
	</div>
</template>

<script>
	import bus from '@/common/bus';
	const __name = 'uni-id-users';
	const __role = 'uni-id-roles';
	export default {
		data() {
			// const validateUsername = async (rule, value, callback) => {
			// 	if (!value) {
			// 		return callback(new Error('请输入账号'));
			// 	}
			// 	let res = await this.$db[__name]
			// 		.field('username')
			// 		.where({
			// 			_id: this.$db.$cmd.neq(this.dataId),
			// 			username: value
			// 		})
			// 		.tofirst({
			// 			loadding: false
			// 		});
			// 	if (res.datas) {
			// 		callback(new Error(value + ' 账号已存在'));
			// 		return;
			// 	}

			// 	callback();
			// };
			return {
				dataId: '',
				roleOptions: [],
				role: [],
				form: {
					username: '',
					password: '',
					role: [],
					gender: '0',
					avatar: '',
					phone: '',
					comment: '',
					state: 0
				},
				imageValue: null,
				imageStyle: {
					height: '150px',
					width: '150px'
				},
				rules: {
					username: [{
						required: true,
						// validator: validateUsername,
						trigger: 'change'
					}],
					password: [{
						required: true,
						message: '请输入密码',
						trigger: 'change'
					}],
					// role: [{
					// 	required: true,
					// 	message: '请选择角色',
					// 	trigger: 'change'
					// }]
				}
			};
		},
		methods: {
			loadData() {
				// await this.$db[__role].tolist({
				// 	page: 1,
				// 	rows: 500
				// }).then(res => {
				// 	if (res.code == 200) {
				// 		this.roleOptions = res.datas;
				// 	}
				// });

				if (!this.dataId) {
					return;
				}

				// await this.$db[__name]
				// 	.field('username, role, gender, avatar, mobile, comment, status')
				// 	.tofirst(this.dataId)
				// 	.then(res => {
				// 		if (res.code == 200) {
				// 			for (let item in this.form) {
				// 				this.form[item] = res.datas[item];
				// 			}

				// 			this.roleOptions.forEach(x => {
				// 				if (res.datas.role.find(g => g == x.role_id)) {
				// 					this.role.push(x.role_id);
				// 				}
				// 			});
				// 			if (this.form.avatar) {
				// 				this.imageValue = {};
				// 				this.imageValue.url = this.form.avatar;
				// 			}
				// 		}
				// 	});
			},
			submitForm(formName) {
				// this.$refs[formName].validate((valid, obj) => {
				// 	this.$api.set_unvalidated_form_focus(this, obj);
				// 	if (valid) {
						if (!this.dataId) {//新增
							this.$axios.post("/admin/save",
							{
								'createBy':uni.getStorageSync("nickName"),//创建人
								'createTime':this.now_date,//创建时间
								"username": this.form.username,
								"phone": this.form.phone,
								"avatar": this.form.avatar,
								"password": this.form.password,
								"state": 1,
								"gender": this.form.gender
							},
							{
								headers:{
									'Mall-Token': uni.getStorageSync("token")
								}
							}).then(response => {
								let res = response
								console.log(res)
								if (res.success) {
									this.getOpenerEventChannel().emit('loadData');
									this.returnPage();
								} else {
								}
							}).catch(res =>{
							});
						} else {
							// delete this.form.password;
							// let userData = uni.getStorageSync('useadmin_user');
							// this.form.dataId = this.dataId;
							// this.$func.useadmin.call('user/edit', this.form).then(res => {
							// 	if (res.code == 200) {
							// 		if (userData._id == this.dataId) {
							// 			userData.avatar = this.form.avatar;
							// 			uni.setStorageSync('useadmin_user', userData);
							// 			bus.$emit('setUserMsg');
							// 		}
							// 		this.getOpenerEventChannel().emit('loadData');
							// 		this.returnPage();
							// 	}
							// });
						}
				// 	}
				// });
			},
			// roleAdd() {
			// 	this.form.role = [];
			// 	this.roleOptions.forEach(x => {
			// 		if (this.role.find(g => g == x.role_id)) {
			// 			this.form.role.push(x.role_id);
			// 		}
			// 	});
			// },
			//获取当前时间
			getNowDate() {
			  var _this = this;
			  // this.timer = setInterval(function() {
				var aData = new Date();
				var month = aData.getMonth() < 9 ? "0" + (aData.getMonth() + 1) : aData.getMonth() + 1;
				var date = aData.getDate() <= 9 ? "0" + aData.getDate() : aData.getDate();
				var date2 = aData.getDate() <= 9 ? "0" + (aData.getDate()-1) : (aData.getDate()-1);
				var Hour = aData.getHours() <= 9 ? "0" + (aData.getHours()) : aData.getHours();
				var Miunte = aData.getMinutes() <= 9 ? "0" + (aData.getMinutes()) : aData.getMinutes();
				var Seconds = aData.getSeconds() <= 9 ? "0" + (aData.getSeconds()) : aData.getSeconds();
				// console.log(aData.getTime())
				_this.now_date = aData.getFullYear() + "-" + month + "-" + date + ' '+ Hour +":"+ Miunte +":"+ Seconds;
				// console.log(aData.getFullYear() + "-" + month + "-" + date2)昨天
			  // }, 86400000);
			},
			imgUpload(e) {
				this.form.avatar = e.tempFiles[0].file;
				const file = this.form.avatar;
				
				const formData = new FormData();
				formData.append('file', file);
				// 文件上传
				this.$axios.post("/file/open/",formData,
				{
					headers:{
						'Content-type' : 'multipart/form-data'
					}
				}).then(response => {
					let res = response
					if (res.success) {
						this.form.avatar =res.data
					} else {
						uni.showToast({
							icon:'error',
							title: '请重新上传！'
						});
						this.form.avatar =''
						this.imageValue = null;
					}
				}).catch(res =>{
					uni.showToast({
						icon:'error',
						title: '请重新上传！'
					});
					this.form.avatar =''
					this.imageValue = null;
				});
			},
			imgDelete() {
				this.imageValue = null;
				this.form.avatar = '';
			},
			returnPage() {
				bus.$emit('closeTab');
				this.$router.push('/pages/system_set/account/account');
			}
		},
		created() {
			this.dataId = this.$route.query.id || '';
			// this.loadData();
		}
	};
</script>

<style></style>
