<template>
    <div class="row">
        <div class="col">
            <div class="row">
                <div class="col ml-md-3 ml-0 mt-md-0 mt-3 bg-white padding-15">
                    <div class="row">
                        <div class="col">
                            <div class="row m-b-10">
                                <div class="col">
                                    <div class="font-heading fs-16 all-caps bold m-b-15">Add Voucher</div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col">
                                    <validation-wrapper-component :validator="$v.voucherCode">
                                        <label>Voucher Code</label>
                                        <input type="text" class="form-control" v-model="voucherCode">
                                    </validation-wrapper-component>
                                </div>
                            </div>
                            <div class="row m-l-0 m-r-0" style="height: 20px">
                                <i class="fa fa-spinner fa-spin m-b-5" v-show="$store.getters.isLoading(section) && voucherIsChecking"></i>
                                <span class="text-danger" v-if="voucherCodeFailedReason">{{ voucherCodeFailedReason }}</span>
                                <span class="text-success" v-if="voucherCodeFailedReason === '' && voucherFetched">Voucher added</span>
                            </div>
                            <div class="row m-t-20">
                                <div class="col">
                                    <button type="button" class="btn btn-block p-t-10 p-b-10 p-r-35 p-l-35 btn-success b-rad-none" @click="submitForm">Add Voucher</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    import { required } from "vuelidate/lib/validators";
    export default {
        props: {
            id: {
                type: String,
                required: true
            },
            section:{
                type: String,
                required: true
            }
        },
        data(){
            return {
                parameters: {
                    voucherCode: '',
                },
                voucherCode: '',
                voucherCodeFailedReason: '',
                voucherFetched: false,
                voucherIsChecking: false
            }
        },
        validations: {
            voucherCode: {
                required
            }
        },
        methods: {
            successHandler(response){
                if(response.payload.data && response.payload.data.voucher_id !== undefined){
                    this.voucherFetched = true;
                    this.$store.dispatch('reloadList', {'name': "customerVouchersListSection"});
                    this.voucherCode = '';
                    this.$v.$reset();
                }
                else{
                    this.voucherFetched = false;
                    if(response.payload.data && response.payload.data.reason){
                        this.voucherCodeFailedReason = response.payload.data.reason;
                    }
                    else{
                        this.voucherCodeFailedReason = 'Voucher Invalid';
                    }
                }
                this.voucherIsChecking = false;
            },
            submitForm(){
                this.reset();
                this.voucherIsChecking = true;
                this.parameters = {
                    voucherCode: this.voucherCode,
                    userId: this.id
                };
                this.submit(this.route('api.voucher.create'), 'post', this.section, false, false);
            },
            errorHandler(error){
                this.voucherFetched = false;
                this.voucherIsChecking = false;
            },
            reset(){
                this.voucherFetched = false;
                this.voucherCodeFailedReason = "";
            }
        }
    }
</script>
