<template>
	<div class="container">
		<div class="row">
			<div class="col-lg-8 m-auto">
				<card :title="$t('register')">
					<form @submit.prevent="register" @keydown="form.onKeydown($event)">
						<!-- Marking -->
						<div class="form-group row">
							<label class="col-md-3 col-form-label text-md-right">{{ $t('Marking') }}</label>
							<div class="col-md-7">
								<input v-model="form.marking" :class="{ 'is-invalid': form.errors.has('marking') }" class="form-control" type="text" name="marking">
								<has-error :form="form" field="marking"/>
								<ul class="text-danger list-unstyled" :class="[{hide: !errors.marking}]">
									<li v-for="(error, index) in errors.marking" v-bind:key="index">{{error}}</li>
								</ul>
							</div>
						</div>

						<!-- Email -->
						<div class="form-group row">
							<label class="col-md-3 col-form-label text-md-right">{{ $t('email') }}</label>
							<div class="col-md-7">
								<el-input v-model="form.email" type="email" name="email" placeholder="Email">
								</el-input>
								<has-error :form="form" field="email"/>
								<ul class="text-danger list-unstyled" :class="[{hide: !errors.email}]">
									<li v-for="(error, index) in errors.email" v-bind:key="index">{{error}}</li>
								</ul>
							</div>
						</div>

						<!-- Password -->
						<div class="form-group row">
							<label class="col-md-3 col-form-label text-md-right">{{ $t('password') }}</label>
							<div class="col-md-7">
								<el-input v-if="password_visible" v-model="form.password"  type="text" name="password" placeholder="Password">
									<template slot="append">
										<el-button type="primary" @click="password_visible = !password_visible"><i class="fas fa-eye"></i></el-button>
									</template>
								</el-input>
								<el-input v-else v-model="form.password"  type="password" name="password" placeholder="Password">
									<template slot="append">
										<el-button type="primary" @click="password_visible = !password_visible"><i class="fas fa-eye-slash"></i></el-button>
									</template>
								</el-input>
								<has-error :form="form" field="password"/>
								<ul class="text-danger list-unstyled" :class="[{hide: !errors.password}]">
									<li v-for="(error, index) in errors.password" v-bind:key="index">{{error}}</li>
								</ul>
							</div>
						</div>

						<!-- Password Confirmation -->
						<div class="form-group row">
							<label class="col-md-3 col-form-label text-md-right">{{ $t('confirm_password') }}</label>
							<div class="col-md-7">
								<el-input v-if="password_confirmation_visible" v-model="form.password_confirmation" type="text" name="password_confirmation" placeholder="Confirm Password">
									<template slot="append">
										<el-button type="primary" @click="password_confirmation_visible = !password_confirmation_visible"><i class="fas fa-eye"></i></el-button>
									</template>
								</el-input>
								<el-input v-else v-model="form.password_confirmation" type="password" name="password_confirmation" placeholder="Confirm Password">
									<template slot="append">
										<el-button type="primary" @click="password_confirmation_visible = !password_confirmation_visible"><i class="fas fa-eye-slash"></i></el-button>
									</template>
								</el-input>
								 <has-error :form="form" field="password_confirmation"/>
							</div>
						</div>

						<div class="form-group row">
							<div class="col-md-7 offset-md-3 d-flex">
								<!-- Submit Button -->
								<v-button :loading="form.busy" :disabled="form.password=='' || form.password_confirmation=='' || form.email=='' || form.marking=='' ? true : false">
									{{ $t('register') }}
								</v-button>

								<!-- GitHub Register Button -->
								<login-with-github/>
							</div>
						</div>
					</form>
				</card>
			</div>
		</div>
	</div>
</template>

<script>
import Form from 'vform'
import LoginWithGithub from '~/components/LoginWithGithub'
import store from '~/store'
import Vue from 'vue'

export default {
	middleware: 'guest',

	components: {
		LoginWithGithub
	},

	metaInfo () {
		return { title: this.$t('register') }
	},

	data: () => ({
		form: new Form({
			marking:'',
			email: '',
			password: '',
			password_confirmation: ''
		}),
		errors: {
			marking: '',
			email: '',
			password: ''
		},
		email : '',
		password_visible : false,
		password_confirmation_visible : false
	}),


	methods: {
		async register () {
			var token
			var data 
			// Register the user.
			await this.form.post('/api/register')
			.then( (response) => {
				 data = response.data
			    this.$message({
				message: "Success. Logging in..",
				type: 'success'
				})
			})
			.catch( (error) => {
				this.errors['marking'] = error.response.data.errors.marking;
				this.errors['email'] = error.response.data.errors.email;
				this.errors['password'] = error.response.data.errors.password;
			    
			})

			
			await this.form.post('/api/login')
			.then( (response) => {
				console.log(response.data)
				token = response.data.token
			})
			.catch( (error) => {
			
			}
			)

			console.log(token)
			// Save the token.
			this.$store.dispatch('auth/saveToken', { token } )

			// Update the user.
			await this.$store.dispatch('auth/updateUser', { user: data })

			// Redirect home.
			if (store.getters['auth/user'].role === 'admin') {
				this.$router.push({ name: 'admin.home' })
			} else {
				this.$router.push({ name: 'home' })
			}

		}
	}
}
</script>
