<template>
    <div class="row" v-if="$store.getters.isAdmin">
        <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 Select</div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col">
                                    <validation-wrapper-component selectable :validator="$v.voucherCode">
                                        <label>Voucher Options</label>
                                        <selectable-hard-coded-component section="rewardisActiveOptions" :selectable-options="options" v-model="voucherCode"></selectable-hard-coded-component >
                                    </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
            },
            options: {
                type: Array,
                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;
                    this.voucherCodeFailedReason = 'Voucher Invalid';
                    if(response.payload.data && response.payload.data.reason){
                        this.voucherCodeFailedReason = response.payload.data.reason;
                    }
                }
                this.$store.dispatch('reloadList', {'name': "voucherCampaignsSection"});
                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.voucherCodeFailedReason = 'Some went wrong, please report this to tech';
                this.voucherFetched = false;
                this.voucherIsChecking = false;
            },
            reset(){
                this.voucherFetched = false;
                this.voucherCodeFailedReason = "";
            }
        }
    }
</script>
