<?php

session_start();
include "inc/eagro.php";
include "inc/eagro2.php";
include "inc/eagro3.php";
$eagro = new Eagro();

$eagro2 = new Eagro2();

$client_ip = $eagro->get_client_ip();
$result_client_ip = json_decode($client_ip,true);
$ip_details = file_get_contents("http://ipinfo.io/$result_client_ip/geo");
$json = json_decode($ip_details,true);
$city = $json["region"];
$category = $_GET["category"];

// echo $city;

$result_subtotal = $eagro->getCartSubTotal($_SESSION["member_id"]);
$array_subtotal = json_decode($result_subtotal,true);
$result_product_list_row1 = $eagro2->getProductRow1($json["region"]);
$array_product_list_row1 = json_decode($result_product_list_row1, true);

$result_product_list_row2 = $eagro2->getProductRow2($json["region"]);
$array_product_list_row2 = json_decode($result_product_list_row2,true);

$result_product = $eagro2->getCart($_SESSION["member_id"]);
$array_product = json_decode($result_product, true);

$result_grand_total = $eagro->cartGrandTotal($_SESSION["member_id"]);
$array_grand_total = json_decode($result_grand_total,true);

$result_produts = $eagro->getProductListingHome(12, $city, $_GET["search"], $category);
$array_produts = json_decode($result_produts, true);

// echo '<pre>';
// print_r($array_produts);
// echo '</pre>';

?>

<!DOCTYPE html>
<html>
<head>
    <title>Welcome to E-AGRO. Malaysia First Agriculture E-commerce Portal</title>
    <?php include "inc/head.php"; ?>

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.carousel.min.css" integrity="sha512-tS3S5qG0BlhnQROyJXvNjeEM4UpMXHrQfTGmbQ1gKmelCxlSEBUaxhRBj/EFTzpbP4RVSrpEikbmdJobCvhE3g==" crossorigin="anonymous" referrerpolicy="no-referrer" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.theme.default.min.css" integrity="sha512-sMXtMNL1zRzolHYKEujM2AqCLUR9F2C4/05cdbxjjLSRvMQIciEPCQZo++nk7go3BtSuK9kfa/s+a4f4i5pLkw==" crossorigin="anonymous" referrerpolicy="no-referrer" />

    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      #alert-message {
        display: none;
      }

      #verification_container .col-md-12 {
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 3rem 0 5rem 0;
      }

      #verification_container .col-md-12:first-child {
        border-bottom: 1px solid #02352529;
      }

      #verification_container #logo {
        width: 100%;
        max-width: 180px;
        margin-right: 50px;
      }

      #verification_container .title {
        font-weight: 400;
      }

      #check-btn {
        width: 120px;
        max-width: 100%;
        border: 2px solid var(--primary-green);
        color: var(--primary-green);
        border-radius: 20px;
      }

      #check-btn:hover{
        background-color: #008459;
            color: #fff;
      }

      .warrenty-item {
        margin-bottom: 20px;
      }

      .check-wrapper {
        padding: 50px 0px;
        display: flex;
        align-items: center;
        justify-content: center;
      }

      .warrenty-inp {
        width: 400px;
      }

      #verify-img{
        padding-bottom: 10px;
        max-width: 100% !important;
        width: 500px !important;
        height: auto;

      }

      #response-image {
        position: fixed;
        top: 50%;
        left: 50%;
        -ms-transform: translate(-50%, -50%);
        transform: translate(-50%, -50%);
        width: 350px;
        max-width: 100%;
        z-index: 1; 
      }

      @media (max-width: 768px) {
        html,
        body {
          font-size: 10px;
        }

        #verification_container .col-md-12 {
          padding: 3rem 3rem 5rem 3rem;
        }

        #verification_container .col-md-12:first-child {
          flex-direction: column;
          text-align: center;
        }

        #verification_container #logo {
          margin: 0 0 40px 0;
          max-width: 140px;
        }

        #check-btn {
          max-width: 120px;
          width: 100%;
          font-size: 14px;
        }
        .warrenty-inp {
          max-width: 300px;
          width: 100%;
          text-align: center;
          margin: 0 auto 20px auto;
          font-size: 14px;
        }
      }
    </style>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script>
      $(document).ready(function () {
    $("#check-btn").click(function () {
        var code = $("#warrenty-inp").val().toUpperCase().replace(/\s/g, '');
        if (code === "") {
            // If input is empty, show a default fail image and hide the image container
            $("#response-image").attr("src", "../assets/images/popup_fail.png");
            $("#image-container").show();
        } else {
            $.ajax({
                url: "inc/verify_code.php",
                type: "POST",
                data: { code: code },
                success: function (response) {
                    response = response.trim();
                    if (response === "Code is valid.") {
                        $("#response-image").attr("src", "../assets/images/popup_success.png");
                        $("#image-container").show();
                    } else if (response === "Code has been used more than 20 times. Please contact support.") {
                        $("#response-image").attr("src", "../assets/images/popup_warning.png");
                        $("#image-container").show();
                    } else if (response === "Product might not be authentic. Invalid Code.") {
                        $("#response-image").attr("src", "../assets/images/popup_fail.png");
                        $("#image-container").show();
                    } else {
                        // Handle unexpected responses
                        $("#response-image").attr("src", "../assets/images/popup_fail.png");
                        $("#image-container").show();
                        console.warn("Unexpected server response:", response);
                    }
                },
                error: function (xhr, status, error) {
                    // Handle AJAX errors
                    $("#response-image").attr("src", "../assets/images/popup_fail.png");
                    $("#image-container").show();
                    console.error("AJAX error:", status, error, xhr.responseText);
                }
            });
        }
    });

    // Hide image when clicking outside of it
    $(document).click(function (event) {
        var target = $(event.target);
        if (!target.closest("#image-container, #check-btn").length) {
            $("#image-container").hide();
        }
    });
});


    </script>
  </head>
  <header>
    <?php include "inc/header2.php"; ?>
</header>
  <body id="home">

    <div id="image-container" style="display: none;">
      <img id="response-image" src="" alt=""/>
    </div>
    
    <div class="container-fluid">
     <!-- Code Verification -->
    <div id="verification_container" class="row content-div">
      <div class="col-md-12 bg-green">
        <img id="logo" src="../assets/images/logo_eagro_tech.PNG" alt="eAgro Tech">
          <div>
            <h2 class="warrenty-item warrenty-first-item title">SISTEM DIGITAL eBAJATULEN</h2>
            <!-- <div id="alert-message" class="warrenty-item"></div> -->
            <input
              type="text"
              name=""
              id="warrenty-inp"
              class="warrenty-item form-control warrenty-inp"
              placeholder="Please enter the verification code here."
            />
            <button class="btn" id="check-btn">Check</button>
          </div>
      </div>

      <div class="col-md-12 bg-green">
        <img id="verify-img" src="assets/images/code_verification_img_1.png">
      </div>
    </div>

     <!-- Strategic Partners -->
    <div id="section5" class="row content-div bg-green">
        <div class="col-md-12 text-center">
            <h2 class="title"><?php echo change("home_10"); ?></h2>
        </div>
        <div class="col-md-12 text-center">
            <img src="image/logo-kementerian-pertanian.png">
            <img src="image/logo-mpob.png">
            <img src="image/logo-agrobank.png">
            <img src="image/logo-fiam.png">
            <img src="image/logo-info-pertanian.png">
        </div>
    </div>
  </div>
  </body>

<footer>
    <?php include "inc/footer2.php"; ?>
</footer>
</html>