<?php    
    // attempt to connect to the DB
    $dbconn = mysqli_connect("localhost", "root", "", "mediarentals2021");
    if ($dbconn->connect_error > 0) {
        die('Could not connect: ' . $dbconn->connect_error);
        exit(); // you should redirect to some form of error page !!!!
    }
 
    // lets see if we have the media id if not return to the list
    $mediaid = 0;
    if (isset($_GET['mediaid']))
        $mediaid = $_GET['mediaid'];
    
    if ($mediaid == 0)
    {
        $redirect = "location: http://localhost/rentals/media";
        header($redirect);
    }

    // get the details of the media 
    $sql = $dbconn->prepare("SELECT m.MediaID as mediaid,
                                    m.MediaName as medianame, 
                                    m.Category as category, 
                                    m.Director as director,
                                    IF(g.GenreID is null, 'Genre Not Allocated', g.genredesc) as genredesc, 
                                    IF(s.SupplierID is null, 'Supplier Not Allocated', s.SupplierName) as suppliername,
                                    IF(p.PriceCodeID is null, 0.00, p.RentalPrice) as rentalprice,
                                    IF(m.ImageName = 'No Image', 0, 1) as imagename
                                    FROM media m
                                    left join genre g on g.GenreID = m.GenreID
                                    left join suppliers s on s.SupplierID = m.SupplierID
                                    left join pricecode p on p.PriceCodeID = m.PriceCodeID
                                    where m.MediaID = ?");
    $sql->bind_param('i', $mediaid);	
    // execute the query 
    $sql->execute(); 
    
    // we have the data, bind the fields
    $sql->bind_result($mediaid, $medianame, $category, $director, $genredesc, $suppliername, $rentalprice, $imagename);
    $sql->store_result();
    $sql->fetch();

    // now who rented the film and on what date
    $sql = $dbconn->prepare("SELECT IF(c.MemberID is null, 0, c.MemberID) as memberid,
                                    IF(c.MemberID is null, 
                                            'Customer Deleted/Removed', 
                                            CONCAT(c.Forename, ' ', c.Surname)) as customername,
                                    r.Hired as hired,
                                    r.Expected as expected,
                                    r.Returned as returned,
                                    r.Overdue as overdue
                                    from rentals r
                                    left join customers c on c.MemberID = r.MemberID
                                    where r.DvdID = ?");
    $sql->bind_param('i', $mediaid);
    // execute the query 
    $sql->execute();
    // we have the data, bind the fields
    $sql->bind_result($memberid, $customername, $hired, $expected, $returned, $overdue);
    $sql->store_result();    

    $datatable = "<table id='customertable' class='table table-bordered table-striped' style='width:100%'>
                    <thead>
                    <tr>
                        <th></th>		
                        <th>Customer</th>
                        <th>Hired</th>
                        <th>Expected</th>
                        <th>Returned</th>
                        <th>Overdue ?</th>                    
                    </tr>
                    </thead>
                    <tbody>";    
    
    // loop through the customers rentals
    while($sql->fetch())
    {
            $datatable.= "<tr>";
            // allow hyperlink to customer if they exist
            if ($memberid == 0)
                $datatable.="<td></td>";
            else
                $datatable.= "<td><a href=viewcustomer.php?memberid=".$memberid.">view</a></td>";
            $datatable.= "<td>" . $customername . "</td>";
            $datatable.= "<td>" . $hired . "</td>";
            $datatable.= "<td>" . $expected . "</td>";
            $datatable.= "<td>" . $returned . "</td>";	
            // check for overdue
            if ($overdue == 1)
                $datatable.= "<td>Yes</td>";
            else
                $datatable.= "<td>No</td>";
            $datatable.= "</tr>"   ;     
    }
    //close connection
    mysqli_close($dbconn);
?>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <meta name="description" content="">
        <meta name="author" content="HND Applied Computing">
        <meta name="generator" content="Jekyll v3.8.5">
        <title>Dashboard Template · Bootstrap</title>  

        <!-- include any relevant css/js files -->
        <?php include "../template/pageheader.php"; ?>

    </head>
    <body>
        <!-- include the top menu -->
        <?php include "../template/topmenu.php"; ?> 
        <!-- end top menu -->

        <div class="container-fluid">
            <div class="row">
                <!-- insert the side menu -->
                <?php include "../template/sidemenu.php"; ?>	
                <!-- end side menu -->

                <!-- webpage content goes here -->
                <main role="main" class="col-md-9 ml-sm-auto col-lg-10 px-4">
                    <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
                        <h1 class="h1">Media Details</h1> 
                    </div>
                    <div class="row">
                        <div class="col-md-1">
                            <label for="medianame">Media Name</label>
                        </div>
                        <div class="col-md-3 mb-3">
                            <input type="text" class="form-control" id="medianame" name="medianame" 
                                   value="<?php echo $medianame?>" 
                                   readonly>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-1">
                            <label for="category">Category</label>
                        </div>
                        <div class="col-md-3 mb-3">
                            <input type="text" class="form-control" id="category" name="category" 
                                   value="<?php echo $category?>" 
                                   readonly>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-1">
                            <label for="director">Director</label>
                        </div>
                        <div class="col-md-3 mb-3">
                            <input type="text" class="form-control" id="director" name="director" 
                                   value="<?php echo $director?>" 
                                   readonly>
                        </div>
                    </div>                    
                     <div class="row">
                        <div class="col-md-1">
                            <label for="genre">Genre</label>
                        </div>
                        <div class="col-md-3 mb-3">
                            <input type="text" class="form-control" id="genre" name="genre" 
                                   value="<?php echo $genredesc?>" 
                                   readonly>
                        </div>
                    </div>                    
                    <div class="row">
                        <div class="col-md-1">
                            <label for="supplier">Supplier</label>
                        </div>
                        <div class="col-md-3 mb-3">
                            <input type="text" class="form-control" id="supplier" name="supplier" 
                                   value="<?php echo $suppliername?>" 
                                   readonly>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-1">
                            <label for="imagename">Image Name</label>
                        </div>
                        <div class="col-md-1">
                            <input type="checkbox" class="form-control-check" id="imagename" name="imagename" 
                                   <?php if($imagename==1) echo "checked='checked'"?> disabled="disabled">
                        </div>
                    </div>                     
                    <div class="row">
                        <div class="col-md-1">
                            <label for="supplier">Rental Price £</label>
                        </div>
                        <div class="col-md-1 mb-1">
                            <input type="text" class="form-control" id="rentalprice" name="rentalprice" 
                                   value="<?php echo $rentalprice?>" 
                                   readonly>
                        </div>
                    </div> 
                    
                    <!-- display rental details of the media -->
                    <div class="row">
                        <div class="col-md-12">
                            <?php echo $datatable ?>
                        </div>
                    </div>
                    
                    <div class="row">
                        <div class="col-md-2 mb-2">
                            <form action="editmedia.php" method="post">                            
                                <input type="hidden" id="mediaid" name="mediaid" value="<?php echo $mediaid?>">
                                <input type="Submit" value="Edit Details" />
                            </form>
                        </div>
                        <div class="col-md-2 mb-2">
                            <form action="delete.php" method="post">
                                <input type="hidden" id="mediaid" name="mediaid" value="<?php echo $mediaid?>">
                                <input type="Submit" value="Delete Media" />
                            </form>
                        </div>
                        <div class="col-md-2 mb-2">
                            <form action="index.php" method="post">
                                <input type="Submit" value="Back to List" />
                            </form>
                        </div>                        
                    </div>
                </main>
            </div>
        </div>

        <!-- include any relevant js files -->
        <?php include "../template/pagefooter.php"; ?>

    </body>
</html>