<script>
    $(document).ready(function () {
        <?php if (isset($_SESSION['member_certificate']) && !empty($_SESSION['member_certificate'])) { 
            if($_SESSION['member_certificate']['is_downloaded']){?>
                Swal.fire({
                    title: 'Oops!',
                    width: 600,
                    text: "<?php echo ALREADY_CERTIFICATE_DOWNLOADED_MESSAGE; ?>",
                    icon: 'error',
                    showCancelButton: false,
                    confirmButtonColor: '#FF440A',
                    confirmButtonText: 'OK',
                    allowOutsideClick: false
                });
                <?php unset($_SESSION['member_certificate']); ?>
            <?php 
                            
                } else { ?>
                Swal.fire({
                    width: 600,
                    title: '<?php echo $_SESSION['member_certificate']['name'] ?>',
                    html: `<?= $this->Form->create(null, array('id' => 'certificate-form', 'url' => '/certificate/generatecertificate')) ?>
                            <input type="text" name="fname" id="fname" class="swal2-input" placeholder="First Name">
                            <input type="text" name="lname" id="lname" class="swal2-input" placeholder="Last Name">
                            <input type="email" name="email" id="email" class="swal2-input" placeholder="Email">
                            <?= $this->Form->end() ?>`,
                    confirmButtonText: 'Get My Certificate Now',
                    focusConfirm: false,
                    allowOutsideClick: false,
                    showCancelButton: true,
                    cancelButtonColor: '#FF440A',
                    cancelButtonText: 'Later',
                    preConfirm: () => {
                        const validateEmail = (email) => {
                            return String(email)
                                .toLowerCase()
                                .match(
                                    /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
                                );
                        };

                        const fname = Swal.getPopup().querySelector('#fname').value
                        const lname = Swal.getPopup().querySelector('#lname').value
                        const email = Swal.getPopup().querySelector('#email').value
                        $('.swal2-inputerror').removeClass('swal2-inputerror');
                        if (!fname || !lname || !email) {
                            var message = `Please enter required fields!`
                            if (!fname) {
                                $('#fname').addClass('swal2-inputerror')
                            }

                            if (!lname) {
                                $('#lname').addClass('swal2-inputerror')
                            }

                            if (!email) {
                                $('#email').addClass('swal2-inputerror')
                            }
                            Swal.showValidationMessage(message)
                        }
                        if (!validateEmail(email)) {
                            $('#email').addClass('swal2-inputerror')
                            message = `Please enter valid email!`
                            Swal.showValidationMessage(message)
                        }
                        return {
                            fname: fname,
                            lname: lname,
                            email: email
                        }
                    }
                }).then((result) => {
                    if(result.isConfirmed){
                        $('#certificate-form').submit();
                    }else{
                        $.ajax({
                            url: '<?php echo $this->Url->build('/certificate/generatecertificate'); ?>',
                            type: 'POST',
                            data:{
                                "action":"session_unset",
                                "session_unset": 1
                            },
                            success: function(data){
                                data = JSON.parse(data);
                                console.log(data);
                            }
                        });
                    }
                });
            <?php }  ?>
            
        <?php } ?>
    });
</script>