<?php include('../includes/connection.php');
@session_start();

if(!isset($_SESSION['admin_login'])) {
  header("Location: login.php");
  }

 include('header.php'); 

$msg = 0;

if(isset($_POST['delete'])) {
    mysqli_query($DB_CONN,"DELETE from category WHERE id='{$_POST['hide_id']}'");

    $msg = 1;
    $msg_alert = '<br> <div class="alert alert-danger alert-dismissible"> <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
      <strong> Success! </strong>  Category Deleted Successfully </div>';
}


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

    $icon=$_FILES['icon']['name'];
    $tmp_name1=$_FILES['icon']['tmp_name'];

    move_uploaded_file($tmp_name1, "../images/icons/$icon");

    mysqli_query($DB_CONN,"INSERT into `category`(`name`, `fa-icon`) VALUES ('{$_POST['name']}','{$icon}') ");
    $insertid = mysqli_insert_id($DB_CONN);

    $code = $insertid.$_POST['name'];

    mysqli_query($DB_CONN,"UPDATE category set code='{$code}' WHERE id='{$insertid}'");

    $msg = 1;
    $msg_alert = '<br> <div class="alert alert-success alert-dismissible"> <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
      <strong> Success! </strong>  Category Added Successfully </div>';
}


 ?>
      <div id="content-wrapper">

        <div class="container-fluid">

          <!-- Breadcrumbs-->
          <ol class="breadcrumb">
            <li class="breadcrumb-item">
              <a href="#"> Dashboard </a>
            </li>
            <li class="breadcrumb-item active"> Categories </li>
          </ol>

          <?php if ($msg=='1') {
                echo $msg_alert;
              } ?>

          <!-- DataTables Example -->
          <div class="card mb-3">
            <div class="card-header"> <i class="fas fa-table">  </i>  Add New Category </div>

            <div class="card-body">
              <div class="table-responsive">
                    <form method="POST" action="" enctype="multipart/form-data" >
                          <label> Category Name </label>
                          <input type="text" name="name" class="form-control" placeholder="Enter Category Name" > <br>

                          <label> Category Icon </label>
                          <input type="file" name="icon" class="form-control"  >  <br>

                          <center> <button type="submit" style="width: 30%" name="add_category" class="btn btn-primary form-control" > Add Category </button> </center>
                    </form>
              </div>
            </div>
          </div>

                    <!-- DataTables Example -->
          <div class="card mb-3">
            <div class="card-header">
              <i class="fas fa-table"></i>
              Categories </div>

           

            <div class="card-body">
              <div class="table-responsive">
                <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
                  <thead>
                    <tr>
                      <th>Sr# </th>
                      <th> Name </th>
                      <th> Icon </th>
                      <th>Actions</th>
                    </tr>
                  </thead>
                  <tfoot>
                    <tr>
                      <th>Sr# </th>
                      <th> Name </th>
                      <th> Icon </th>
                      <th>Actions</th>
                    </tr>
                  </tfoot>
                  <tbody>

<?php $i = 1; $sql=  mysqli_query($DB_CONN,"SELECT * from category");
      while($rowsql = mysqli_fetch_assoc($sql)) {  ?>
                    <tr>
                      <td> <?php echo $i; ?> </td>
                      <td> <?php echo $rowsql['name']; ?> </td>    
                      <td> <?php echo $rowsql['fa-icon']; ?> </td>
                      <td> 
                      <form method="POST" action="">
                      <input type="hidden" name="hide_id" value="<?php echo $rowsql['id']; ?>" >
                      <button type="submit" title="Delete" name="delete" class="btn btn-sm btn-danger">  Delete </button>
                        </form>
                      </td>
                    </tr>
    <?php $i++; } ?>
                    
                  </tbody>
                </table>
              </div>
            </div>
          </div>

        </div>
      
      
        <footer class="sticky-footer">
          <div class="container my-auto">
            <div class="copyright text-center my-auto">
              <span>Copyright © Your Website 2020 </span>
            </div>
          </div>
        </footer>

      </div>
      <!-- /.content-wrapper -->

    </div>
    <!-- /#wrapper -->

    <!-- Scroll to Top Button-->
    <a class="scroll-to-top rounded" href="#page-top">
      <i class="fas fa-angle-up"></i>
    </a>

<?php  include('footer.php'); ?>