<?php session_start();?>
<!DOCTYPE html>
<html lang="en">
<?php
    if(!isset($_SESSION['id'])){
        echo"<script>alert('you have to log back in to continue');</script>";
        header("Location: Logout.php");
      
    }
?>
<html lang="en">
<head>
    
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.13.1/font/bootstrap-icons.min.css">

    <title>Customer Loyalty</title>
    <?php
    include("db.php");
    $sql="SELECT Points FROM User WHERE User_id = ?";
    $stmt=mysqli_stmt_init($conn);
    mysqli_stmt_prepare($stmt,$sql);
    mysqli_stmt_bind_param($stmt,"i",$_SESSION['id']);

    if(mysqli_stmt_execute($stmt)){
        $result=mysqli_stmt_get_result($stmt);
        $row=mysqli_fetch_assoc($result);
        $points=$row['Points'];
    };
    mysqli_stmt_close($stmt);


    ?>
</head>
 <style>
        .pill-navbar{
            border-radius: 50px;  
            padding:10px 20px;
            
        }
        @media(max-width:992px){
            
            .pill-navbar{
            border-radius: 25px;  
            margin-top:10px;

            }
            .pill-navbar .collapse-navbar{
                margin-top:10px;
                padding-top:10px;
                border-top:1px#eee
            }
        }
        .form-group{
            margin-bottom:1rem;
        }
        .card{
            max-width:30vw;
            border-radius: 40px;  
            padding:10px 20px;
      
        }

        .carousel{
            max-height:10vh;
            min-height:10vh;

        }
        .button{
            min-height:5vh;
            max-height:5vh;
        }

        body{

            background-image: linear-gradient(rgba(0,0,0,0.2),rgba(0,0,0,0.7)), url("Dark-theme.jpg");
            background-position: center;
            background-repeat: no-repeat;
            background-size: cover;
            min-height:100vh;
        }

    </style>
<body>
    <?php
    if($_SERVER["REQUEST_METHOD"]=="POST"){
        include('db.php');
        $id=$_POST['id'];

        // adds the discount to the user's info
        $sql="UPDATE User u JOIN  discount d on d.id=? SET u.DiscountValue = d.DiscountValue WHERE u.id =? ";
        $stmt=mysqli_stmt_init($conn);
        mysqli_stmt_prepare($stmt,$sql);
        mysqli_stmt_bind_param($stmt,"ii",$id,$_SESSION['id']);
        mysqli_stmt_execute($stmt);
        mysqli_stmt_close($stmt);

        // gets the discount cost
        $sql="SELECT * FROM discount WHERE Discount_id = ? ";
        $stmt=mysqli_stmt_init($conn);
        mysqli_stmt_prepare($stmt,$sql);
        mysqli_stmt_bind_param($stmt,"i",$id);
        mysqli_stmt_execute($stmt);
        $result=mysqli_stmt_get_result($stmt);
        $row=mysqli_fetch_assoc($result);
        $cost=$row['DiscountPrice'];
        mysqli_stmt_close($stmt);

        // subtracts the cost with points
        $points=$points-$cost;
        
        // Updates the user's points
        $sql="UPDATE User SET `Points`='?' WHERE `User_id` = ?";
        $stmt=mysqli_stmt_init($conn);
        mysqli_stmt_prepare($stmt,$sql);
        mysqli_stmt_bind_param($stmt,"ii",$points,$_SESSION['id']);
        mysqli_stmt_execute($stmt);
    }  
  ?>
  
    
    
    <!-- navbar -->
    <nav class="navbar navbar-expand-lg navbar-light bg-light shadow pill-navbar w-50 mx-auto my-4 ">
    
        <a class="navbar-brand" href="#">Logo</a>

        <!-- expand/collapse button -->
        <button
            class="navbar-toggler"
            type="button"
            data-bs-toggle="collapse"
            data-bs-target="#Navbar">
            <span class="navbar-toggler-icon"></span>
        </button>

        <!-- nav links -->
        <div class="collapse navbar-collapse ms-4" id="Navbar">
            <ul class="navbar-nav mt-auto">
                <li class="nav-item"><a class="nav-link" href="ProducerProducts.php">Products</a></li>
                <li class="nav-item"><a class="nav-link" href="ProducerSuppliers.php">Suppliers</a></li>
                <li class="nav-item"><a class="nav-link" href="ProducerOrders.php">Orders</a></li>
            </ul>
            <!-- right side nav links -->
            <ul class="navbar-nav ms-auto ">
                <li class="nav-item"><a class="nav-link" href="Logout.php">Log out<span class="bi bi-person-fill"></span></a></li>
                <li class="nav-item"><a class="nav-link" href="">Settings<span class="bi bi-gear-fill"></span></a></li>
            </ul>
        </div>

    </nav>

    
    
    <!-- main content -->
   <!-- main content -->
   <div class="container">
        <!-- Slider/Carousel -->
        <div class="carousel slide mb-5 border" id="carouselId" data-bs-ride="carousel">

            <ol class="carousel-indicators">
                <li
                    data-bs-target="#carouselId"
                    data-bs-slide-to="0"
                    class="active"
                    aria-current="true"
                    aria-label="First slide">
                </li>

                <li
                    data-bs-target="#carouselId"
                    data-bs-slide-to="1"
                    aria-label="Second slide">
                </li>

                <li
                    data-bs-target="#carouselId"
                    data-bs-slide-to="2"
                    aria-label="Third slide">
                </li>
            </ol>


            <div class="carousel-inner" role="listbox">

                <div class="carousel-item active">
                    <img
                        src="holder.js/900x500/auto/#777:#555/text:First slide"
                        class="w-100 d-block"
                        alt="First slide"
                    />
                </div>

                <div class="carousel-item">
                    <img
                        src="holder.js/900x500/auto/#666:#444/text:Second slide"
                        class="w-100 d-block"
                        alt="Second slide"
                    />
                </div>

                <div class="carousel-item">
                    <img
                        src="holder.js/900x500/auto/#666:#444/text:Third slide"
                        class="w-100 d-block"
                        alt="Third slide"
                    />
                </div>

            </div>
        </div>

        <!-- welcome message -->
        <div class="row">
            <div class="col-lg-12 text-center p-auto mt-5 " style="color:white;">
                <strong>
                    <h1>Welcome</h1>
                    <h3>To the Greenfield Local Hub website</h3>
                </strong>
                <h5>On this website you can order products, view suppliers and even read<br>about the benfits of buying locally down below</h5>
            </div>
        </div>

        <!-- top artical -->
        <div class="row">
            <div class="col-12 mt-5" style="height:20vh; align-content:center;">

                <div class="card mx-auto">
                    <img class="card-img-top" src="holder.js/100x180/" alt="Title" />
                    <div class="card-body">
                        <h4 class="card-title">Title</h4>
                        <p class="card-text">Text</p>
                    </div>
                    <a class="stretched-link" href=""></a>
                </div>
    
            </div>
        </div>

        <!-- bottom two articals -->
        <div class="row">

            <div class="col-lg-6 col-sm-12 mt-2" style="height:20vh; align-content:center;">

                <div class="card mx-auto">
                    
                    <img class="card-img-top" src="holder.js/100x180/" alt="Title" />
                    <div class="card-body">
                        <h4 class="card-title">Title</h4>
                        <p class="card-text">text</p>
                    </div>
                    <a class="stretched-link" href=""></a>
                </div>
                
            </div>
            

            <div class="col-lg-6 col-sm-12 mt-2" style="height:20vh; align-content:center;">

                <div class="card mx-auto">
                    <img class="card-img-top" src="holder.js/100x180/" alt="Title" />
                    <div class="card-body">
                        <h4 class="card-title">Title</h4>
                        <p class="card-text">Text</p>
                    </div>
                    <a class="stretched-link" href=""></a>
                </div>
            </div>

        </div>

        

    </div>
    <div clas="footer">

    </div>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
</body>
</html>