<template>
    <div class="row" style="width: 450px; margin: auto;" @keyup.enter="submitForm">
        <div class="col bg-white b-rad-lg">
            <div class="row m-b-15">
                <div class="col">
                    <h6 class="all-caps m-b-5 bold no-margin">{{ existingData ? 'Update' : 'Add' }} PERMITS REMINDER</h6>
                </div>
            </div>
            <div class="row m-b-15 animate__animated animate__fadeInUpBig animate__fast" v-if="error">
                <div class="col">
                    <small class="bold fs-10 text-danger" v-html="error"></small>
                </div>
            </div>
            <div class="row">
                <div class="col">
                    <validation-wrapper-component class="m-b-15" :validator="$v.parameters.model">
                        <label class="text-primary">Model</label>
                        <div class="controls">
                            <input type="text" class="form-control fs-12" v-model.trim="parameters.model">
                        </div>
                    </validation-wrapper-component>
                    <div class="row m-auto mt-10 mb-10">
                        <div class="col p-l-0 p-r-10">
                            <validation-wrapper-component class="m-b-15" :validator="$v.parameters.expiry_date">
                                <label class="text-primary">Expiry Date</label>
                                <date-picker-component v-model="parameters.expiry_date"></date-picker-component>
                            </validation-wrapper-component>
                        </div>
                    </div>
                    <div class="row m-auto mt-10 mb-10">
                        <div class="col p-l-0 p-r-10">
                            <validation-wrapper-component class="m-b-15" :validator="$v.parameters.reminder_date">
                                <label class="text-primary">Reminder Date</label>
                                <date-picker-component v-model="parameters.reminder_date"></date-picker-component>
                            </validation-wrapper-component>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row m-t-15">
                <div class="col p-r-5">
                    <div class="btn btn-sm btn-default b-rad-none w-100" data-dismiss="modal">Cancel</div>
                </div>
                <div class="col p-l-5">
                    <div class="btn btn-sm btn-primary w-100" @click="submitForm()">Confirm</div>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
import FormHandler from '../../../general/mixins/formHandler';
import { required } from "vuelidate/lib/validators";

export default {
    props: {
        existingData: {
            type: Object,
        }
    },
    data() {
        return {
            parameters: {
                expiry_date: '',
                reminder_date: '',
                model: ''
            },
        };
    },
    mounted() {
        if (this.existingData) {
            this.parameters.expiry_date = this.existingData.expiry_date;
            this.parameters.reminder_date = this.existingData.reminder_date;
            this.parameters.model = this.existingData.model;
        }
    },
    validations: {
        parameters: {
            expiry_date: { required },
            reminder_date: { required },
            model: { required },
        }
    },
    methods: {
        submitForm() {
            var apiRoute = this.existingData ? this.route('api.permits_reminder.update', this.existingData.id) : this.route('api.permits_reminder.create');
            this.submit(apiRoute, 'post', this.section, true, true);
        },
        errorHandler(error) {
            // this.formHandler(error.message);
            console.log(error);
        },
        successHandler() {
            this.closeModal();

            // Reset the validation state of the parameters object
            this.$v.parameters.$reset();

            this.parameters = {
                expiry_date: '',
                reminder_date: '',
                model: ''
            };
        },
    },
    mixins: [FormHandler]
}

</script>