<?php

    require_once("entities/product.class.php");
    require_once("entities/category.class.php");

    
    if(isset($_POST["btnsubmit"])){
        $productName = $_POST["txtName"];
        $cateID = $_POST["txtCateID"];
        $price = $_POST["txtprice"];
        $quantity = $_POST["txtquantity"];
        $description = $_POST["txtdesc"];
        $picture = $_FILES["txtpic"];

        if(!isset($_GET["id"])){
            header("Location: edit_product.php?failure");
        }
        else{
            $id = $_GET["id"];
            $temp_prod = Product::get_product($id);
            $prod = reset($temp_prod);

            $prod = new Product($productName, $cateID, $price, $quantity, $description, $picture);

            $result = $prod->update($id);
            if(!$result)
            {
                header("Location: edit_product.php?failure");
            }
            else
            {
                header("Location: edit_product.php?updated");
            }
        }
        
        
        
    }
    
?>

<?php include_once("header.php"); ?>

<style> 
input[type=text] {
  font-family: tahoma;
  font-size: 13px;
  width: 100%;
  padding: 12px 20px;
  margin: 8px 0;
  box-sizing: border-box;
  border: 3px solid #ccc;
  -webkit-transition: 0.5s;
  transition: 0.5s;
  outline: none;
}

input[type=text]:focus {
  border: 3px solid #555;
}

textarea{
    font-family: tahoma;
    width: 100%;
    font-size: 13px;
  padding: 12px 20px;
  margin: 8px 0;
  box-sizing: border-box;
  border: 3px solid #ccc;
  -webkit-transition: 0.5s;
  transition: 0.5s;
  outline: none;
}

select {
    padding: 12px 20px;
    padding-left : 50px;
  background-color: #EEEEEE;
}
select {
  border-radius: 5px;
}


.styled-select {
  border: 1px solid;
  width: 135px; 
}

.styled-select select {
  width: 180px;
}
.button {
  background-color: #333333;
  border: none;
  color: white;
  padding: 15px 100px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  margin: 4px 2px;
  cursor: pointer;
}

</style>
<div class="container">
 <div class="row tm-gallery">
    <div id="tm-gallery-page-pizza" class="tm-gallery-page">
            <h2 class="col-12 text-center tm-section-title" style="padding-top:20px;"><b>CẬP NHẬT SẢN PHẨM</b></h2>
            <?php 
                if(isset($_GET["updated"])){
                    echo"<br /><div class='col-12 text-center'><h2>Cập nhật sản phẩm thành công</h2></div>";
                }
                if(isset($_GET["failure"]))
                {
                    echo"<br /><div class='col-12 text-center'><h2>Cập nhật sản phẩm thất bại</h2></div>";
                }
            ?>
    </div>
    
 </div>
    <form method="post" enctype="multipart/form-data">
        <div style="padding: 0 50px 0 50px;">
        <br />
            <label>Tên sản phẩm</label>
            <input type="text" name="txtName" value="<?php echo isset($_POST["txtName"]) ? $_POST["txtName"] : "" ; ?>" /><br />
            <label>Mô tả</label><br />
            <textarea name="txtdesc" cols="134" rows="10" value="<?php echo isset($_POST["txtdesc"]) ? $_POST["txtdesc"] : "" ; ?>" ></textarea><br />
            <label>Số lượng</label>
            <input type="text" name="txtquantity" value="<?php echo isset($_POST["txtquantity"]) ? $_POST["txtquantity"] : "" ; ?>" /><br />
            <label>Giá</label>
            <input type="text" name="txtprice" value="<?php echo isset($_POST["txtprice"]) ? $_POST["txtprice"] : "" ; ?>" /><br />
            <label>Phân loại</label>
            <select name="txtCateID">
                    <option value="" selected>-- Chọn loại --</option>
                    <?php
                        $cates = Category::list_category();
                        foreach($cates as $item){
                            echo "<option value=".$item["CateID"].">".$item["CategoryName"]."</option>";
                        }
                    ?>
                </select>
            <div style="float: right;">
            <label>Đường dẫn hình</label>
            <input type="file" class="button" id="txtpic" name="txtpic" accept=".PNG,.GIF,.JPG" value="<?php echo isset($_POST["txtpic"]) ? $_POST["txtpic"] : "" ; ?>" />
            </div>
            <div style="padding: 50px 0 50px 0; text-align:center;">
                <input type="submit" class="tm-btn tm-btn-default" name="btnsubmit" value="Cập nhật" />
            </div>
            
        </div>

       
        <div class="row">
            
        </div>
    </form>
</div>
<?php include_once("footer.php"); ?>