<template>
    <div class="row">
        <div class="col">
            <loading-component style="height: 50px; top: 0;" key="1" color="success" v-show="$store.getters.isLoading(section)"></loading-component>
            <div class="row" v-show="!$store.getters.isLoading(section)">
                <div class="col">
                    <div class="row m-b-20">
                        <div class="col">
                            <h6 class="all-caps m-b-5 bold no-margin">Add Admin Account</h6>
                        </div>
                    </div>
                    <div class="row m-b-5 animate__animated animate__fadeInUpBig animate__fast" v-if="error">
                        <div class="col">
                            <small class="bold fs-10 text-danger">{{error}}</small>
                        </div>
                    </div>
                    <div class="row m-b-10">
                        <div class="col">
                            <validation-wrapper-component :validator="$v.parameters.name">
                                <label>Name</label>
                                <input type="text" class="form-control" v-model="parameters.name">
                            </validation-wrapper-component>
                        </div>
                    </div>
                    <div class="row m-b-10">
                        <div class="col">
                            <validation-wrapper-component :validator="$v.parameters.email">
                                <label>Email</label>
                                <input type="text" class="form-control" v-model="parameters.email">
                            </validation-wrapper-component>
                        </div>
                    </div>
                    <div class="row m-b-10">
                        <div class="col">
                            <validation-wrapper-component :validator="$v.parameters.password">
                                <label>Password</label>
                                <input type="password" class="form-control" v-model="parameters.password">
                            </validation-wrapper-component>
                        </div>
                    </div>
                    <div class="row m-b-10">
                        <div class="col">
                            <validation-wrapper-component :validator="$v.parameters.password_confirmation">
                                <label>Password Confirmation</label>
                                <input type="password" class="form-control" v-model="parameters.password_confirmation">
                            </validation-wrapper-component>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col p-r-5">
                            <div class="btn btn-sm btn-default bg-master-lightest btn-block b-rad-none" :data-dismiss="closable ? 'modal' : ''" @click="$emit('close')">Cancel</div>
                        </div>
                        <div class="col p-l-5">
                            <div class="btn btn-sm btn-success btn-block b-rad-none" @click="submitForm()">Add Admin</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
    import FormHandler from '../../../general/mixins/formHandler';
    import { required, sameAs, minLength , email} from "vuelidate/lib/validators";
    export default {
        props : {
            closable: {
                type: Boolean,
                default: true
            },
        },
        data(){
            return {
                parameters: {
                    name: '',
                    email: '',
                    password: '',
                    password_confirmation: '',
                }
            }
        },
        validations: {
            parameters: {
                name: {
                    required
                },
                email: {
                    required,
                    email
                },
                password: {
                    required,
                    minLength: minLength(6)
                },
                password_confirmation: {
                    required,
                    sameAsPassword: sameAs('password')
                }
            }
        },
        methods: {
            submitForm(){
                this.submit(route('api.account.user.admin.create'), 'post', this.section, true, false);
            },
            successHandler(response){
                this.closeModal();
                this.$emit('createdAdmin', response.payload.data);
                this.formHandler();
                this.resetForm();
            },
            errorHandler(error){
                this.formHandler(error.message);
            }
        },
        mixins: [FormHandler]
    }
</script>