<?php

if(!isset($_SESSION['admin_email'])){

    echo "<script>window.open('login.php','_self')</script>";

} else {

    if(isset($_GET['edit_cat'])){

        $edit_id = $_GET['edit_cat'];

        $edit_cat = "SELECT * FROM categories WHERE cat_id='$edit_id'";
        $run_edit = mysqli_query($con,$edit_cat);
        $row_edit = mysqli_fetch_array($run_edit);

        $c_id = $row_edit['cat_id'];
        $c_type = $row_edit['cat_type'];
    }

?>

<div class="row"><!-- 1 row Starts -->
<div class="col-lg-12">
    <ol class="breadcrumb">
        <li>
            <i class="fa fa-dashboard"></i> Dashboard / Edit Category
        </li>
    </ol>
</div>
</div><!-- 1 row Ends -->

<div class="row"><!-- 2 row Starts -->
<div class="col-lg-12">
    <div class="panel panel-default">
        <div class="panel-heading">
            <h3 class="panel-title">
                <i class="fa fa-money fa-fw"></i> Edit Category
            </h3>
        </div>

        <div class="panel-body">
            <form class="form-horizontal" action="" method="post">
                
                <div class="form-group">
                    <label class="col-md-3 control-label">Category Name</label>
                    <div class="col-md-6">
                        <input type="text" name="cat_type" class="form-control" value="<?php echo $c_type; ?>" required>
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-md-3 control-label"></label>
                    <div class="col-md-6">
                        <input type="submit" name="update" value="Update Category" class="btn btn-primary form-control">
                    </div>
                </div>

            </form>
        </div>
    </div>
</div>
</div><!-- 2 row Ends -->

<?php

    if(isset($_POST['update'])){

        $cat_type = $_POST['cat_type'];

        $update_cat = "UPDATE categories SET cat_type='$cat_type' WHERE cat_id='$c_id'";
        $run_cat = mysqli_query($con,$update_cat);

        if($run_cat){
            echo "<script>alert('Category has been updated successfully')</script>";
            echo "<script>window.open('index.php?view_cats','_self')</script>";
        }
    }

} // end else

?>
