<?php
if(!isset($_SESSION['admin_email'])){
    echo "<script>window.open('login.php','_self')</script>";
} else {
?>

<div class="row"><!-- 1 row Starts -->
    <div class="col-lg-12">
        <ol class="breadcrumb">
            <li class="active">
                <i class="fa fa-dashboard"></i> Dashboard / View Categories
            </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> View Categories
                </h3>
            </div>

            <div class="panel-body">
                <div class="table-responsive">
                    <table class="table table-bordered table-hover table-striped">
                        <thead>
                            <tr>
                                <th>#</th>
                                <th>Category Name</th>
                                <th>Parent Category</th>
                                <th>Delete</th>
                                <th>Edit</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php 
                            $i = 0;
                            $get_cats = "SELECT * FROM categories";
                            $run_cats = mysqli_query($con, $get_cats);

                            while($row_cats = mysqli_fetch_array($run_cats)){
                                $cat_id = $row_cats['cat_id'];
                                $cat_name = $row_cats['cat_name'];
                                $parent_cat_id = $row_cats['parent_cat_id'];
                                $i++;

                                // Get parent category name
                                if($parent_cat_id == 0){
                                    $parent_name = "Main Category";
                                } else {
                                    $get_parent = "SELECT cat_name FROM categories WHERE cat_id = '$parent_cat_id'";
                                    $run_parent = mysqli_query($con, $get_parent);
                                    $row_parent = mysqli_fetch_array($run_parent);
                                    $parent_name = $row_parent['cat_name'];
                                }
                            ?>
                            <tr>
                                <td><?php echo $i; ?></td>
                                <td><?php echo $cat_name; ?></td>
                                <td><?php echo $parent_name; ?></td>
                                <td>
                                    <a href="index.php?delete_cat=<?php echo $cat_id; ?>">
                                        <i class="fa fa-trash-o"></i> Delete
                                    </a>
                                </td>
                                <td>
                                    <a href="index.php?edit_cat=<?php echo $cat_id; ?>">
                                        <i class="fa fa-pencil"></i> Edit
                                    </a>
                                </td>
                            </tr>
                            <?php } ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div><!-- 2 row Ends -->

<?php } ?>
