<?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($_POST['mediaid']))
        $mediaid = $_POST['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, 0, g.GenreID) as genreid, 
                                            IF(s.SupplierID is null, 0, s.SupplierID) as supplierid,
                                            IF(p.PriceCodeID is null, 0, p.PriceCodeID) as pricecodeid,
                                            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, $genreid, $supplierid, $pricecodeid, $imagename);
    $sql->store_result();
    $sql->fetch();
    
    // generate various dropdowns - first genre
    $sql = $dbconn->prepare("select GenreID as genreid, genredesc from genre");

    /* execute the query */
    $sql->execute();
    $sql->bind_result($id, $desc);
    $sql->store_result();
    $genredd = "";
    $genredd.= '<select id="genre" class="form-control" name="genre">';
    if ($genreid == 0)
        $genredd .= '<option value="0" selected>Select Genre</option>';
    else
        $genredd .= '<option value="0">Select Genre</option>';
    while($sql->fetch())
    {
        if ($genreid == $id && $genreid <> 0)
            $genredd.= '<option value='.$id.' selected>'.$desc.'</option>';
        else
            $genredd.= '<option value='.$id.'>'.$desc.'</option>';
    }
    $genredd .=' </select> ';  
    
    // now the supplier
    // generate various dropdowns - first genre
    $sql = $dbconn->prepare("select SupplierID as supplierid, SupplierName as suppliername from suppliers");

    /* execute the query */
    $sql->execute();
    $sql->bind_result($id, $desc);
    $sql->store_result();
    $supplierdd = "";
    $supplierdd.= '<select id="supplier" class="form-control" name="supplier">';
    if ($supplierid == 0)
        $supplierdd .= '<option value="0" selected>Select Supplier</option>';
    else
        $supplierdd .= '<option value="0">Select Supplier</option>';
    while($sql->fetch())
    {
        if ($supplierid == $id && $supplierid <> 0)
            $supplierdd.= '<option value='.$id.' selected>'.$desc.'</option>';
        else
            $supplierdd.= '<option value='.$id.'>'.$desc.'</option>';
    }
    $supplierdd .=' </select> ';
    
    // and finally what about the price code - have a go yourself ?
    $sql = $dbconn->prepare("select PriceCodeID as pricecodeid, CONCAT(pricecode, ' - ', RentalPrice) as rentalprice from pricecode");

    /* execute the query */
    $sql->execute();
    $sql->bind_result($id, $desc);
    $sql->store_result();
    $pricecodedd = "";
    $pricecodedd.= '<select id="pricecode" class="form-control" name="pricecode">';
    if ($pricecodeid == 0)
        $pricecodedd .= '<option value="0" selected>Select Price Code</option>';
    else
        $pricecodedd .= '<option value="0">Select Price Code</option>';
    while($sql->fetch())
    {
        if ($pricecodeid == $id && $pricecodeid <> 0)
            $pricecodedd.= '<option value='.$id.' selected>'.$desc.'</option>';
        else
            $pricecodedd.= '<option value='.$id.'>'.$desc.'</option>';
    }
    $pricecodedd .=' </select> ';
    //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>
                    <form id="mediaform" action="updatemedia.php" method="post">
                        <input type="hidden" id="mediaid" name="mediaid" value="<?php echo $mediaid?>">
                        
                        <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?>">
                            </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?>">
                            </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?>">
                            </div>
                        </div>                    
                         <div class="row">
                            <div class="col-md-1">
                                <label for="genre">Genre</label>
                            </div>
                            <div class="col-md-3 mb-3">
                                <?php echo $genredd; ?>
                            </div>
                        </div>  

                        <div class="row">
                            <div class="col-md-1">
                                <label for="supplier">Supplier</label>
                            </div>
                            <div class="col-md-3 mb-3">
                                <?php echo $supplierdd; ?>
                            </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'"?>>
                            </div>
                        </div> 

                        <div class="row">
                            <div class="col-md-1">
                                <label for="supplier">Rental Price £</label>
                            </div>
                            <div class="col-md-3 mb-3">
                                <?php echo $pricecodedd?>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-2 mb-2">                          
                                    <input type="Submit" value="Update" />
                                </form>
                            </div>
                            <div class="col-md-2 mb-2">
                                <form action="viewmedia.php" method="post">
                                    <input type="hidden" id="mediaid" name="mediaid" value="<?php echo $mediaid?>">
                                    <input type="Submit" value="Cancel" />
                                </form>
                            </div>                       
                        </div>                        
                    </form>
                </main>
            </div>
        </div>

        <!-- include any relevant js files -->
        <?php include "../template/pagefooter.php"; ?>

    </body>
</html>