<template>
    <div class="row">
        <div class="col">
            <div class="row m-b-5" @keyup.enter="submitSearch">
                <div class="col p-r-0">
                    <validation-wrapper-component :validator="$v.parameters.marking">
                        <label class="all-caps">Marking</label>
                        <!-- <input type="text" class="form-control" v-model="parameters.marking" :class="[{ 'not-allowed': hasMarkingParameter }]" :disabled="hasMarkingParameter"> -->
                        <input type="text" class="form-control" v-model="parameters.marking" :class="[{ 'not-allowed': hasMarking }]" :disabled="hasMarking">
                    </validation-wrapper-component>
                </div>
                <div class="col p-r-0">
                    <validation-wrapper-component :validator="$v.parameters.startDate">
                        <label class="all-caps">Start Date</label>
                        <date-picker-component v-model.lazy="parameters.startDate"></date-picker-component>
                    </validation-wrapper-component>
                </div>
                <div class="col p-r-0">
                    <validation-wrapper-component :validator="$v.parameters.endDate">
                        <label class="all-caps">End Date</label>
                        <date-picker-component v-model.lazy="parameters.endDate"></date-picker-component>
                    </validation-wrapper-component>
                </div>
                <div class="col col-md-auto d-flex justify-content-center align-items-center">
                    <button type="button" class="btn btn-lg btn-primary fs-11 w-100"
                        @click="submitSearch()">Download</button>
                </div>
            </div>
            <loading-component style="height: 100px; top: 0;" key="1" color="success" v-show="isLoading"></loading-component>
            <div class="row align-items-center justify-content-center bg-white p-t-50 p-b-50 p-l-15 p-r-15 margin-15" v-if="!isLoading && returnData">
                <div class="col-10">
                    <div class="row align-items-center justify-content-center  hint-text">
                        <div class="col-4 hint-text"><img src="/images/not-found-illustration.png" class="w-100 hint-text" /></div>
                    </div>
                    <div class="row text-center">
                        <div class="col">
                            <div class="row m-t-20">
                                <div class="col">
                                    <p class="all-caps no-margin fs-11" :class="[{'text-danger': returnData.status == 'Error'}]" style="letter-spacing: 2px;">{{ returnData.message }}</p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
    import { required } from "vuelidate/lib/validators";

    export default {
        data() {
            return {
                key: 1,
                returnData: null,
                isLoading: false,
                hasMarking: false,
                parameters: {
                    marking: null,
                    startDate: null,
                    endDate: null,
                },
            }
        },
        validations: {
            parameters: {
                marking: { required },
                startDate: { required },
                endDate: { required },
            }
        },
        created() {
            const urlSearchParams = new URLSearchParams(window.location.search);
            if (urlSearchParams.has('marking')) {
                this.parameters.marking = urlSearchParams.get('marking');
            }
            this.hasMarkingParameter();
        },
        methods: {
            submitSearch() {
                this.isLoading = true;
                this.submit(this.route('api.customers.invoices'), 'post', this.section, false, false);
            },
            errorHandler(error) {
                this.isLoading = false;
                console.log(error);
            },
            successHandler(response) {
                this.isLoading = false;
                if (response.message) {
                    this.returnData = response;
                } else {
                    this.returnData = null;
                }
            },
            hasMarkingParameter() {
                const urlSearchParams = new URLSearchParams(window.location.search);
                this.hasMarking = urlSearchParams.has('marking');
            },

        },
    }
</script>
