
<?php 
  session_start();
  include"inc/config.php";
   $errors = array(); // initialize variables
  $Id = ""; $Category_code="";    $Category_name= "";
  $Id = 0; 
  $update = false;

  if (isset($_GET['edit'])) { 
    $id = $_GET['edit'];
  $record = mysqli_query($db, "SELECT * FROM categories WHERE id= $id ");


    if (count($record) == 1 ) {
    $n = mysqli_fetch_array($record);
    $id = $n['Id'];  $category_name = $n['Category_name'];  $category_code = $n['Category_code']; 
    }

  }

if (isset($_POST['update'])) {
$id = $_POST['id']; $category_code = $_POST['category_code']; $category_name= $_POST['category_name'];

    $sql_a = "SELECT * FROM categories WHERE Category_code='$category_code'";
    $sql_b = "SELECT * FROM categories  WHERE Category_name='$category_name'";
  
    $res_a = mysqli_query($db, $sql_a);
    $res_b = mysqli_query($db, $sql_b);


    if (mysqli_num_rows($res_a) > 1) {
      array_push($errors, "Sorry! category_code arleady exist");
    }else if(mysqli_num_rows($res_b) > 1){
      array_push($errors, "Sorry! category_name arleady exist");
}
    else {


$sql = "UPDATE categories SET Category_code= '$category_code', Category_name='$category_name' WHERE Id =$id";

if ($db->query($sql) === TRUE) {
 echo "<script type= 'text/javascript'>alert('$category_name - Category  updated succesfully.');</script>";
 echo "<script type='text/javascript'>location.href = 'index.php?link=product-categories';</script>";
}
else{echo "<script type= 'text/javascript'>alert('Error!!! while updating.');</script>";}

}
}

?>


<!DOCTYPE>
<html lang="en">
<head>
<title></title>
<meta charset="utf-8">
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
 body { font-family: helvetica, sans-serif; }
* {  box-sizing: border-box;}
body {  margin: 0;}

.top-aa{ padding:15px; text-align: left; width: 100%; font-size: x-small; color: #848482; margin-bottom: 1%;}
 
label{color:#3D2B1F; margin-left:1%;  }

input[type=text],select, input[type=password]{  width: 100%;  padding: 10px;  border: 1px solid #ccc;  border-radius: 4px; color:#848482;box-sizing: border-box;  margin-top: 10px;  margin-bottom: 5px;  resize: vertical; background: white;font-weight: bold;}

input[type=submit] {  background:#007AA5;  color: white; font-weight: bold;  padding: 12px 15px; float: right; border: none;  border-radius: 4px;  cursor: pointer;  margin-top: 4%;}
  
input[type=submit]:hover { opacity: .8;  padding: 13px 20px;}


.error { width: 100%;  padding: 8px;    color: white; margin-bottom: 2% ;background:#C95A49;  border-radius: 5px;  font-size: small;}

label a {color:red; font-weight: bold;}
.c1{width:33.33%; padding: 10px; float:left;}
.c2{width:33.33%; padding: 10px; float:left;}

.container { width:98%; margin: auto; padding: 15px; background:white; float: right;   margin-top: 2%; border-bottom: 2px solid #ED872D ; }



.wrapper{ background:whitesmoke;  width:100%; border: 0px solid #a1a1a1;  margin:auto; resize: none;     overflow:hidden; }

.column-a {  float: left;  width:18%;  padding: 0px; height: 90%; background:white ;border-top: 2px solid whitesmoke;  overflow-x: auto; scrollbar-width: thin; }

.column-b { float: left; width: 82%;  padding: 15px; overflow-x: auto; scrollbar-width: thin; height: 90%; }


/* Clear floats after the columns */
.row:after {
  content: "";
  display: table;
  clear: both;
}

@media screen and (max-width:600px) {
  .column-a { width: 100%; height: auto; }
  .column-b {  width: 100%; height:auto; }


   }

</style>
</head>
<body>


<div class="row">

<div class="wrapper">

  <nav> <?php include "header.php"; ?></nav>


  <div class="column-a"><?php include "menu.php"; ?>  </div>
  
  <div class="column-b">

    <div class="top-aa">
<a href="index.php" style="color: black; text-decoration: none; font-weight: bold;">Home </a>
<a href="index.php?link=product-categories" style="color: black; text-decoration: none; ">// Product Categories </a>
<a>// Update Record</a>
</div>

<?php include ("product-category-edit.php"); ?>

</div>





<?php include ("footer.php"); ?>
</div>
</div>
</body>
</html>
