<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{company}}</title>

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css">
    {% load static %}
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    <style>
        
        body > div {
            {% comment %} margin-top: 8vh !important; {% endcomment %}
        }

        body > div.swal2-container.swal2-center.swal2-backdrop-show {
            margin-top: 0px !important;
        }
    </style>
</head>
<body>

    

    {% include "partials/css.html" %}
    <style>
        .loading-card{
        max-width: 28% !important;
        margin-top: 25vh !important;
        z-index: 99999999999;
        border-radius: 10px;
        
        min-height: min-content;
        margin: auto;
        
    }


    @media (max-width: 768px) {
            body > div.container.d-lg-flex.p-2 > div.box-1.bg-light.user > div > div.radiobtn{
                max-height: fit-content !important;
            }

            .loading-card{
                max-width: 91% !important;
                min-height: min-content;
            }
        }
        
    </style>

    <div id="mycontent" class="container d-lg-flex p-2">
        <div id="loading" style="background-color: rgba(6, 29, 29, 0.678);backdrop-filter: blur(3px);width: 100vw; height: 100vh;position: fixed;top: 0;left: 0; z-index: 10;margin: 0;display: none;">

            <div onclick="" class="container is-flex is-flex-direction-column is-justify-content-center  loading-card p-3 " >

                <h3 style="text-align: center;" class="title is-5 has-text-centered primary-dark-color mb-3 mt-1" > PAIEMENT </h3>
                <iframe id="loading-anime" style="display: block;margin: auto; pointer-events: none;pointer-events: none;" src="https://embed.lottiefiles.com/animation/10558"></iframe>
                 <iframe id="loading-success" style="display: block;margin: auto; margin-top: 15%;pointer-events: none;display: none;pointer-events: none;" src="https://embed.lottiefiles.com/animation/96673"></iframe>
                <iframe id="loading-error" style="display: block;margin: auto; margin-top: 15%; pointer-events: none;display: none;pointer-events: none;" src="https://embed.lottiefiles.com/animation/76699"></iframe>  
                <!-- <img id="loading-wave" style="margin-left: auto;margin-right: auto;width: 50%;display: block;border-radius: 7px;" src="https://res.cloudinary.com/dqawf7ttx/image/upload/v1731026629/t%C3%A9l%C3%A9chargement_2_d3ibrz.png" alt="" srcset="">

                <a id="wave_btn"  style="background-color: #1DC8FF;color: white;text-transform: uppercase;font-weight: bold;margin-left: auto;margin-right: auto;text-align: center;border: none;display: block;width: 50%;" class="btn btn-primary mt-3" >Payer</a> -->
                <h5 id="r-message" style="text-align: center;color: black; width: 80% !important;margin-left: auto;margin-right: auto;"></h5>
                
            </div>
            
        </div>
        <div class="box-1 bg-light user">
            
            <div class="box-inner-1 pb-3 mb-3 ">
                <div class="d-flex justify-content-between mb-3 userdetails">
                    <p class="fw-bold">{{company}}</p>
                    <p class="fw-lighter"> </p>
                </div>
                <div id="my" class="carousel slide carousel-fade img-details" data-bs-ride="carousel"
                    data-bs-interval="5000">
                    <div class="carousel-indicators">
                        <button type="button" data-bs-target="#my" data-bs-slide-to="0" class="active"
                            aria-current="true" aria-label="Slide 1"></button>
                        <button type="button" data-bs-target="#my" data-bs-slide-to="1" aria-label="Slide 2"></button>
                        <button type="button" data-bs-target="#my" data-bs-slide-to="2" aria-label="Slide 3"></button>
                    </div>
                    <div class="carousel-inner">

                        {% if image != "" %}
                            <div class="carousel-item active">
                                <img src="{{image}}"
                                    class="d-block w-100" width="300">
                            </div>
                        {% endif %}

                        <div class="carousel-item {% if image == '' %} active {% endif %}">
                            <img src="https://res.cloudinary.com/dqawf7ttx/image/upload/v1731023698/Sans-titre---1_pranxu.png"
                                class="d-block w-100" width="300">
                            <!-- <img src="https://res.cloudinary.com/dzd2e3xv0/image/upload/v1671205346/5917_opiie4.jpg"
                                class="d-block w-100" width="300"> -->
                        </div>

                        
                    </div>
                    <button class="carousel-control-prev" type="button" data-bs-target="#my" data-bs-slide="prev">
                        <div class="icon">
                            <span class="fas fa-arrow-left"></span>
                        </div>
                        <span class="visually-hidden">Previous</span>
                    </button>

                    <button class="carousel-control-next" type="button" data-bs-target="#my" data-bs-slide="next">
                        <div class="icon">
                            <span class="fas fa-arrow-right"></span>
                        </div>
                        <span class="visually-hidden">Next</span>
                    </button>
                </div>
                <div class="radiobtn" style="max-height: 230px !important;">

                    {% for p in products  %}
                        <label for="one" class="box py-2 first">
                            <div class="d-flex align-items-start">
                                <span class="circle"></span>
                                <div class="course">
                                    <div class="d-flex align-items-center justify-content-between mb-2">
                                        <span class="fw-bold">
                                            {{p.name}}
                                        </span>
                                        <span class="">{{p.price}} {{devise}}</span>
                                    </div>
                                    <span>{{p.qte}} x {{p.name}}</span>
                                </div>
                            </div>
                        </label>
                    {% endfor %}
                    


                </div>
            </div>
        </div>
        <div class="box-2">
            
            <div class="box-inner-2">
                <div>
                    <p class="fw-bold">Details du payment</p>
                    <p class="dis mb-3">
                        Complétez votre achat en fournissant vos informations de paiement</p>
                </div>
                <form action="">
                {% csrf_token %}
                <input type="hidden" id="slug" value="{{slug}}">
                    <div class="mb-3">
                        <p class="dis fw-bold mb-2">Moyen de paiement</p>
						<style>
						#type option{
							padding:5px 15px;
						}
						</style>
                        <select onchange="checkTypePayment()" class="form-select" aria-label="Default select example" id="type" style="border-radius: 6px;">
                            
                            {% for m in methods  %}
                            <option value="{{m.0}}">{{m.1}}</option>
                            {% endfor %}

                            
                        </select>
                    </div>

                    
                    <div>
                        <div id="wallet-number" style="display: block;">

                            <p class="dis fw-bold mb-2">Téléphone</p>
                            <div class="d-flex align-items-center justify-content-between card-atm border rounded">
                                <div class="fas fa-credit-card ps-3"></div>
                                <input id="mytel" class="form-control"  maxlength="12" required  type="text" value="" placeholder="78 000 00 00">
                            </div>

                        </div>

                        <div id="om-otp" style="display: block;margin-top: 15px;" class="mt-3">

                            <p class="dis fw-bold mb-2">CODE ( <i class="fas fa-phone"></i> Composez <b style="color: rgb(158, 88, 2);letter-spacing: 1.5px;font-weight: bold;" > #144#391*CODE_PIN#</b> )</p>
                            <div class="d-flex align-items-center justify-content-between card-atm border rounded">
                                
                                <input id="myotp" class="form-control"   maxlength="6"   required  type="text" value="" placeholder="XXXXXX">
                            </div>

                        </div>
                        
                        

                        <div id="credit-card" style="display: none;">
                            <p class="dis fw-bold mb-2">Carte crédit</p>
                            <div class="d-flex align-items-center justify-content-between card-atm border rounded">
                                <div id="ic-card-number" class="fas fa-credit-card ps-3"></div>
                                <input id="card-number" maxlength="19"  onkeyup="checkTypeOfCard(event)" type="text" value="" class="form-control" placeholder="XXXX XXXX XXXX XXXX">
                                <div class="d-flex w-50">
                                    <input id="card-expire" type="text" onchange="validateDateInput(this)" class="form-control px-0" maxlength="5" placeholder="MM/YY" value="" autocomplete="off" readonly 
                                    onfocus="this.removeAttribute('readonly');">
                                    <input id="card-cvv" type="password" maxlength=3 class="form-control px-0" placeholder="CVV" value="" autocomplete="off" readonly 
                                    onfocus="this.removeAttribute('readonly');">
                                </div>
                            </div>
                            <div class="my-3 cardname">
                                <p class="dis fw-bold mb-2">Nom complet de la carte</p>
                                <input id="fname"  class="form-control" placeholder="Prénom" autocomplete="off" value="" readonly   onfocus="this.removeAttribute('readonly');" type="text">
                                <input id="lname" class="form-control mt-2" placeholder="Nom" autocomplete="off" value="" readonly  onfocus="this.removeAttribute('readonly');" type="text">
                            </div>
                        </div>

                        <script>

                            function validateDateInput(input) {
                                // Vérifier si le format est correct (MM/YY)
                                var regex = /^(0[1-9]|1[0-2])\/\d{2}$/;
                                if (!regex.test(input.value)) {
                                  alert("Le format de date doit être MM/YY (ex: 07/24)");
                                  input.value = "";
                                  input.focus();
                                  return false;
                                }
                                // Vérifier si la date est valide
                                var parts = input.value.split('/');
                                var month = parseInt(parts[0], 10);
                                var year = parseInt(parts[1], 10);
                                var currentDate = new Date();
                                var currentYear = currentDate.getFullYear() % 100;
                                if (year < currentYear || (year == currentYear && month < (currentDate.getMonth() + 1))) {
                                  alert("La date doit être supérieure à la date actuelle");
                                  input.value = "";
                                  input.focus();
                                  return false;
                                }
                                return true;
                              }

                            function formatcc(value){
                                var v = value.replace(/\s+/g, '').replace(/[^0-9]/gi, '')
                                var matches = v.match(/\d{4,16}/g);
                                var match = matches && matches[0] || ''
                                var parts = []

                                for (i=0, len=match.length; i<len; i+=4) {
                                    parts.push(match.substring(i, i+4))
                                }

                                if (parts.length) {
                                    return parts.join(' ')
                                } else {
                                    return value
                                }
                            }
                            function checkTypeOfCard(evt){

                                if(evt.srcElement.value[0] == "4"){
                                    document.getElementById("ic-card-number").className = "fab fa-cc-visa ps-3"
                                }else if(evt.srcElement.value[0] == "5"){
                                    document.getElementById("ic-card-number").className = "fab fa-cc-mastercard ps-3"
                                }else{
                                    document.getElementById("ic-card-number").className = "fas fa-credit-card ps-3"
                                }

                                evt.srcElement.value = formatcc(evt.srcElement.value)
                                
                            }
                            function checkTypePayment(){

                                    var type = document.getElementById('type').value 
                                    if(type != "OM"){
                                        document.getElementById('om-otp').style.display="none"
                                        document.getElementById('wallet-number').style.display="none"
                                    }else{
                                        document.getElementById('om-otp').style.display="block"
                                        document.getElementById('wallet-number').style.display="block"
                                    }
                                    if(type != "WAVE"){
                                        
                                        document.getElementById('wallet-number').style.display="block"
                                    }else{
                                        
                                        document.getElementById('wallet-number').style.display="none"
                                    }

                                    // if(type == "CARD"){
                                    //     document.getElementById('credit-card').style.display="block"
                                    //     document.getElementById('wallet-number').style.display="none"
                                    // }else{
                                    //     document.getElementById('credit-card').style.display="none"
                                    //     document.getElementById('wallet-number').style.display="block"
                                    // }
                            }
                            checkTypePayment()
                        </script>
                        

                        <div class="address mt-4">
                            
                            <div class="d-flex flex-column dis">
                                <div class="d-flex align-items-center justify-content-between mb-2">
                                    <p>PRIX</p>
                                    <p> {{price}} {{devise}}</p>
                                </div>
                                <div class="d-flex align-items-center justify-content-between mb-2">
                                    <p>TAXES<span></span></p>
                                    <p>0</p>
                                </div>
                                <div class="d-flex align-items-center justify-content-between mb-2">
                                    <p class="fw-bold">Total</p>
                                    <p class="fw-bold">{{price}} {{devise}}</p>
                                </div>
                                <div id="#paybtn" onclick="upToPayWallet()" class="btn btn-primary mt-2" >Payer  {{price}} {{devise}} </div>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <script> 
        const socketUrl = `{{url}}`;
    
        let socket = new WebSocket(socketUrl);
    
        socket.onopen = function(event) {
            {% comment %} console.log("WebSocket   opened."); {% endcomment %}
        };
    
        socket.onmessage = function(event) {
            const message = JSON.parse(event.data);
    
            if (message.type === "notify") {
                {% comment %} console.log("Received notify message: ", message); {% endcomment %}

                if (message.status == "SUCCESS") {
                    Swal.fire({
                        title: 'Notification!',
                        text: message.msg,
                        icon: 'success',
                        confirmButtonText: 'Ok'
                    });
                } else {
                    Swal.fire({
                        title: 'Notification!',
                        text: message.msg,
                        icon: 'error',
                        confirmButtonText: 'Ok'
                    });
                }
                
            }
        };
    
        socket.onclose = function(event) {
            if (event.wasClean) {
                console.log(`Connection closed cleanly, code=${event.code} reason=${event.reason}`);
            } else {
                console.error('Connection died');
            }
        };
    
        socket.onerror = function(error) {
            console.error("WebSocket error:", error);
        };
    </script>

    <!-- <script src="//code.tidio.co/r7ycmtkxu4tz02brt9xttvuzsaegm4jf.js" async></script> -->
    <!-- <script src="{% static 'script_no_min.js' %}"></script> -->
    <script src="{% static 'script.js' %}"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</body>
</html>