(function($){
    $('form#razer-gold-pin-store-config').submit(function (e){
        e.preventDefault();
        $.ajax({
            type: 'POST',
            url:  ajaxurl,
            data: $(this).serialize() + '&action=razer_gold_pin_store_generate',
            beforeSend: function(){
                Swal.fire({
                    title: 'Guardando cambios',
                    onOpen: () => {
                        Swal.showLoading()
                    },
                    allowOutsideClick: false
                });
            },
            success: function(r){
                Swal.fire({
                    icon: 'success',
                    text: 'Cambios guardados',
                    showCloseButton: true,
                    showConfirmButton: true
                });
            }
        });
    });

    $('#razer-gold-pin-store-excel').on('change', function(e) {
        console.log('change');
        let form = $(this).parents('form');
        let razer_gold_pin_store_generate_excel = form.find('input[name=razer_gold_pin_store_generate_excel]').val();
        let excel = e.target.files[0];
        let fd = new FormData();
        fd.append('razer_gold_pin_store_generate_excel', excel);
        fd.append('action', 'razer_gold_pin_store_generate_upload');
        fd.append('razer_gold_pin_store_generate_excel', razer_gold_pin_store_generate_excel);

        uploadFile(fd);
    });

    function uploadFile(file){
        $.ajax({
            data: file,
            type: 'POST',
            contentType: false,
            processData: false,
            url: ajaxurl,
            dataType: "json",
            beforeSend : () => {
                Swal.fire({
                    title: 'Subiendo información',
                    onOpen: () => {
                        Swal.showLoading()
                    },
                    allowOutsideClick: false
                });
            },
            success: (r) => {
                if (r.status){
                    Swal.fire({
                        title: '',
                        text: 'Información guardada exitosamente',
                        type: 'success'
                    });
                }else{
                    Swal.fire(
                        'Error',
                        r.message,
                        'error'
                    );
                }
            }
        });
    }

    $(document).ready(function(){
        $('#razer_gold_pin_store').select2();
    });

    $('form#razer-gold-pin-store-generate').submit(function (e) {
        e.preventDefault();

        Swal.fire({
            title: '¿ Estas seguro ?',
            html: "<p style='color: #ee0000; font-size: 15px'>Es irreversible después que se cree un Pin</p>",
            showCancelButton: true,
            confirmButtonText: "Confirmar",
            cancelButtonText: "Cancelar"
        }).then(result => {
            if(result.value){
                $.ajax({
                    data: $(this).serialize() + '&action=razer_gold_pin_store_generate_pin',
                    type: 'POST',
                    url: ajaxurl,
                    dataType: "json",
                    beforeSend : () => {
                        Swal.fire({
                            title: 'Generando Pin',
                            onOpen: () => {
                                Swal.showLoading()
                            },
                            allowOutsideClick: false
                        });
                    },
                    success: (r) => {
                        if (r.status){

                            let coupons = r.data;
                            let pins = '';
                            coupons = JSON.parse(coupons);

                            for (var coupon in coupons) {
                                let dataPin = coupons[coupon];
                                pins += `
                                    <p style="text-align: center">Pin: <strong>${dataPin.pins[0]}</strong></p>
                                    <p style="text-align: center">Serial: <strong>${dataPin.serials[0]}</strong></p>
                                    <hr/>
                                `;
                            }

                            Swal.fire({
                                html: pins,
                                icon: 'info',
                                showConfirmButton: false,
                                allowOutsideClick: false
                            });
                        }else{
                            Swal.fire(
                                'Error',
                                r.message,
                                'error'
                            );
                        }
                    }
                });
            }
        })
    })
})(jQuery);