<?php
require_once('config.php');
$tt_alert = '';

// Add New Category
if (isset($_POST['add_new_category'])) {

  extract($_POST);
  $CategoryParent = !empty($_POST['CategoryParent']) ? (int)$_POST['CategoryParent'] : 0;
  if ($CategoryURL !='') {
    // If get custom category then convert url
    $custom_cat_url =  createUlink($CategoryURL);

    // check this url alredy used another or not
    $url_check_db = getSingleValue('mahamudfutureit_category', 'CategoryURL', $custom_cat_url, 'CategoryURL');
    if ($url_check_db == '') {
      // If unsed then pass this
      $category_url = $custom_cat_url;
    }else {
      // if alredy used then add timestamp
      $category_url = $custom_cat_url.'-'.time();
    }
  }else {

    // If get custom category not provided then create from name
    $custom_cat_url =  createUlink($CategoryName);

    // check this url alredy used another or not
    $url_check_db = getSingleValue('mahamudfutureit_category', 'CategoryURL', $custom_cat_url, 'CategoryURL');
    if ($url_check_db == '') {
      // If unsed then pass this
      $category_url = $custom_cat_url;
    }else {
      // if alredy used then add timestamp
      $category_url = $custom_cat_url.'-'.time();
    }
  }

    // Data Array
    $category_form_data = array(
      "CategoryURL"=>$category_url,
      "CategoryParent"=>htmlentities($CategoryParent, ENT_QUOTES, 'utf-8'),
      "CategoryName"=>htmlentities($CategoryName, ENT_QUOTES, 'utf-8'),
      "CategoryIcon"=>htmlentities($CategoryIcon, ENT_QUOTES, 'utf-8'),
      "CategoryPermission"=>htmlentities($CategoryPermission, ENT_QUOTES, 'utf-8'),
      "CategoryDescription"=>htmlentities($CategoryDescription, ENT_QUOTES, 'utf-8'),
      "CategoryAdd"=>$the_time,
    );

   // Insert Database
   $insert_now = InsertData('mahamudfutureit_category', $category_form_data);

   if ($insert_now) {
     $tt_alert = ttAlert('success', 'Category added successfully !');
   }else {
     $tt_alert = ttAlert('danger', 'Something went wrong to Add Category!');

   }
}
// Delete Category
if (isset($_GET['delid'])) {
  $id_for_delete = $_GET['delid'] ;

  $check_child_cat = showAllttdata("mahamudfutureit_category", "WHERE CategoryParent ='$id_for_delete' ");

  if (empty($check_child_cat)) {
    if (deleteData('mahamudfutureit_category', 'CategoryID', $id_for_delete)){
      header("Location:category.php");
      exit;
    }else {
      $tt_alert = ttAlert('danger', 'Something went wrong to delete category !');
    }
  }else {
    $tt_alert = ttAlert('danger', 'You Cannot Delete This Category !');
  }
}

// Page Navication
if (isset($_GET['edit_cat']) && $_GET['edit_cat'] !='') {
  $edit_cat_id = $_GET['edit_cat'];
  $edit_cat_list = showAllttdata("mahamudfutureit_category", "WHERE CategoryID = '$edit_cat_id'");
  if (!empty($edit_cat_list)) {
    foreach ($edit_cat_list as  $edit_catval);
    $edit_cat_page = true;
  }else {
    header('Location:404.php');
    exit;
  }
}else {
  $edit_cat_page = false;
}


// Edit Category
if (isset($_POST['save_updates'])) {
  extract($_POST);

  $data_for_update = array(
    "CategoryName"=>htmlentities($CategoryName, ENT_QUOTES, 'utf-8'),
    "CategoryDescription"=>htmlentities($CategoryDescription, ENT_QUOTES, 'utf-8'),
    "CategoryIcon"=>htmlentities($CategoryIcon, ENT_QUOTES, 'utf-8'),
    "CategoryURL"=>createUlink($CategoryURL),
    "CategoryPermission"=>htmlentities($CategoryPermission, ENT_QUOTES, 'utf-8'),
  );
  if (UpdateData('mahamudfutureit_category', 'CategoryID', $edit_cat_id, $data_for_update)) {
    header("Location:category.php?edit_cat=".$edit_cat_id);
    exit;
  }else {
    $tt_alert = ttAlert('danger', 'Something went wrong updated category !');
  }
}

// Include Heaser File
require_once('inc/header.php');
?>
<main id="main" class="main ">
  <div class="row">
    <div class="col-6 align-items-center">
      <div class="pagetitle mb-0">
        <h1><?php echo $page_title ?></h1>
      </div>
    </div>
    <div class="col-6 text-end">
      <?php
      if ($edit_cat_page == true) {
        ?>
        <a href="category.php" class="btn btn-primary btn-sm"><i class="ri-arrow-left-line"></i> All Category </a>
        <?php
      }
       ?>
    </div>
  </div>
  <?php
    echo ($tt_alert != '') ? $tt_alert :'';
    if ($edit_cat_page == true) {
      ?>
      <section class="section mt-4">
        <div class="row">
          <div class="col-lg-12  ">
            <div>
              <h5 class="mb-3">Edit Category</h5>
              <form class="row" action="" method="post" enctype="multipart/form-data">
                <div class="col-lg-2 col-md-3">
                  <div class="form-group">
                    <label>Category Name</label>
                  </div>
                </div>
                <div class="col-md-9">
                  <div class="form-group mb-3">
                    <input value="<?php echo $edit_catval['CategoryName'] ?>" name="CategoryName" type="text" class="form-control form-control-sm" id="CategoryName" placeholder="Category Name" required>
                  </div>
                </div>
                <div class="col-lg-2 col-md-3">
                  <div class="form-group">
                    <label>Category Icon Class</label>
                  </div>
                </div>
                <div class="col-md-9  mb-3">
                  <div class="input-group">
                    <span class="input-group-text" id="basic-addon1"> <i class="<?php echo $edit_catval['CategoryIcon'] ?>"> </i> </span>
                    <input value="<?php echo $edit_catval['CategoryIcon'] ?>" name="CategoryIcon" type="text" class="form-control form-control-sm" id="cat_name" placeholder="eg. ri ri-user-line">
                  </div>
                  <small> Get Icon From   <a target="_blank" href="https://icons.getbootstrap.com/">Bootstrap Icon</a>  or <a href="https://fontawesome.com/v6/search" target="_blank">Fontawesome</a> </small>
                </div>
                <div class="col-lg-2 col-md-3">
                  <div class="form-group">
                    <label>Category URL</label>
                  </div>
                </div>
                <div class="col-md-9">
                  <div class="form-group mb-3">
                    <input value="<?php echo rawurldecode($edit_catval['CategoryURL']) ?>" name="CategoryURL" type="text" class="form-control form-control-sm" id="CategoryURL" placeholder="Category URL" required>
                  </div>
                </div>
                <div class="col-lg-2 col-md-3">
                  <div class="form-group">
                    <label>Category Permission</label>
                  </div>
                </div>
                <div class="col-md-9">
                  <div class="form-group mb-3">
                    <select class="form-control" name="CategoryPermission">
                      <option value="Admin" <?php echo ($edit_catval['CategoryPermission'] == 'Admin') ? 'selected' : '' ?>>Admin</option>
                      <option value="Public" <?php echo ($edit_catval['CategoryPermission'] == 'Public') ? 'selected' : '' ?>>Public</option>
                    </select>
                  </div>
                </div>
                <div class="col-lg-2 col-md-3">
                  <div class="form-group">
                    <label>Category Status</label>
                  </div>
                </div>
                <div class="col-md-9">
                  <div class="form-group mb-3">
                    <select class="form-control" name="CategorySataus">
                      <option value="Published" <?php echo ($edit_catval['CategorySataus'] == 'Published') ? 'selected' : '' ?>>Published</option>
                      <option value="Draft" <?php echo ($edit_catval['CategorySataus'] == 'Draft') ? 'selected' : '' ?>>Draft</option>
                    </select>
                  </div>
                </div>
                <div class="col-lg-2 col-md-3">
                  <div class="form-group">
                    <label>Category Details </label>
                   </div>
                </div>
                <div class="col-md-9">
                  <div class="form-group mb-3">
                    <textarea  name="CategoryDescription" class="form-control form-control-sm" rows="3" placeholder="Category Details..."><?php echo $edit_catval['CategoryDescription'] ?></textarea>
                  </div>
                </div>
                <div class="col-lg-2 col-md-3">
                </div>
                <div class="col-md-9">
                  <button type="submit" class="btn btn-primary btn-sm" name="save_updates">Save Changes</button>
                </div>
              </form>
            </div>
          </div>
         </div>
      </section>
      <?php
    }else {
      ?>
      <section class="section mt-4">
        <div class="row">
          <div class="col-lg-4 mb-3">
            <div class="p-3 bg-white rounded-2">
              <h5>Add Category</h5>
              <form class="" action="" method="post" enctype="multipart/form-data">
                <div class="form-group mb-3">
                  <label>Category Name</label>
                  <input name="CategoryName" type="text" class="form-control form-control-sm" id="cat_name" placeholder="Category Name" required>
                </div>
                <div class="form-group mb-3">
                  <label>Category Details </label>
                  <textarea  name="CategoryDescription" class="form-control form-control-sm" rows="3" placeholder="Category Details..."></textarea>
                </div>
                <div class="form-group mb-3">
                  <label>Category Parent</label>
                  <select name="CategoryParent"   class="form-control form-control-sm tt-select-single" >
                    <option selected>Select Parent Category</option>
                    <?php
                    $data_p_cat = showAllttdata("mahamudfutureit_category" , "WHERE CategoryParent IS NULL OR CategoryParent = '' ORDER BY  CategoryID  DESC ");
                    if (!empty($data_p_cat)) {
                      foreach ($data_p_cat as $parent_category) {
                        ?>
                        <option value="<?php echo $parent_category['CategoryID'] ?>"><?php echo $parent_category['CategoryName'] ?></option>
                        <?php
                      }
                    }
                    ?>
                  </select>
                </div>
                <div class="form-group mb-3">
                  <label>Category Permission</label>
                  <select name="CategoryPermission"   class="form-control form-control-sm" required>
                    <option disabled selected>Select Category Permission</option>
                    <option value="Public">Public</option>
                    <option value="Admin">Admin</option>
                  </select>
                </div>
                <div class="form-group mb-3">
                  <label>Category Icon Class</label>
                  <input name="CategoryIcon" type="text" class="form-control form-control-sm" id="cat_icon" placeholder="eg. ri-arrow-right-line">
                  <small> Get Icon From   <a target="_blank" href="https://icons.getbootstrap.com/">Bootstrap Icon</a>  or <a href="https://fontawesome.com/v6/search" target="_blank">Fontawesome</a> </small>
                </div>
                <div class="form-group mb-3">
                  <label>Category URL</label>
                  <input name="CategoryURL" type="text" class="form-control form-control-sm" id="cat_name" placeholder="Category URL">
                </div>
                <button type="submit" class="btn btn-primary btn-sm" name="add_new_category">Add New Category</button>
              </form>
            </div>
          </div>
          <div class="col-lg-8">
            <div class="bg-white rounded-2 p-3">
              <table  class="tt_datatable" style="width:100%">
                <thead>
                    <tr>
                        <th>Name</th>
                        <th class="text-center">Post</th>
                        <th class="text-center" title="Author/Writer Cannot Access Admin Category ">Permission</th>
                        <th class="text-center">Status</th>
                        <th class="text-center">URL</th>
                        <th class="text-end">Action</th>
                    </tr>
                </thead>
                <tbody>
                <?php
                $data_p_cat = showAllttdata("mahamudfutureit_category" , "WHERE CategoryParent IS NULL OR CategoryParent = '' ORDER BY  CategoryID  DESC ");
                if (!empty($data_p_cat)) {
                  foreach ($data_p_cat as $parent_category) {
                    ?>
                    <tr>
                        <td>
                          <a class="fw-bold text-dark" target="_blank" href="<?php echo categoryInfo($parent_category['CategoryID'], 'url') ?>"><?php echo $parent_category['CategoryName'] ?></a>
                        </td>
                        <td>
                          <?php
                            $pcat_ids = $parent_category['CategoryID'];
                            echo count(showAllttdata("mahamudfutureit_article","WHERE (PostCategory = '$pcat_ids' OR PostSubCategory LIKE '%\"$pcat_ids\"%') AND PostStatus  = 'published'"));
                          ?>
                        </td>
                        <td class="text-center">
                          <?php echo $parent_category['CategoryPermission'] ?>
                        </td>
                        <td class="text-center">
                          <?php echo $parent_category['CategorySataus'] ?>
                        </td>
                        <td class="text-center">
                          <?php echo urldecode($parent_category['CategoryURL']) ?>
                        </td>
                        <td class="text-end action-btn">
                          <a class="bg-success" href="category.php?edit_cat=<?php echo $parent_category['CategoryID'] ?>"><i class="bi bi-pencil"></i></a>
                          <a class="bg-danger" data-bs-toggle="modal" data-bs-target="#delete_pop<?php echo $parent_category['CategoryID'] ?>" href="#"><i class="bi bi-trash3"></i></a>
                        </td>
                    </tr>
                    <?php
                    $parent = $parent_category['CategoryID'];
                    $data_p_cat = showAllttdata("mahamudfutureit_category", "WHERE CategoryParent = '$parent' ORDER BY  CategoryID  DESC ");
                    if (!empty($data_p_cat)) {
                      foreach ($data_p_cat as $tt_sub_category) {
                        ?>
                        <tr>
                          <td>
                            --- <a  class=" text-dark" target="_blank" href="<?php echo categoryInfo($tt_sub_category['CategoryID'], 'url') ?>"><?php echo $tt_sub_category['CategoryName'] ?></a>
                          </td>
                          <td>
                            <?php
                              $ccat_ids = $tt_sub_category['CategoryID'];
                              echo count(showAllttdata("mahamudfutureit_article","WHERE (PostCategory = '$ccat_ids' OR PostSubCategory LIKE '%\"$ccat_ids\"%') AND PostStatus  = 'published'"));
                            ?>
                          </td>
                          <td class="text-center">
                            <?php echo $parent_category['CategoryPermission'] ?>
                          </td>
                          <td class="text-center">
                            <?php echo $tt_sub_category['CategorySataus'] ?>
                          </td>
                          <td class="text-center">
                            <?php echo urldecode($tt_sub_category['CategoryURL']) ?>
                          </td>
                            <td class="text-end action-btn">
                              <a class="bg-success" href="category.php?edit_cat=<?php echo $tt_sub_category['CategoryID'] ?>"><i class="bi bi-pencil"></i></a>
                              <a class="bg-danger" data-bs-toggle="modal" data-bs-target="#delete_pop<?php echo $tt_sub_category['CategoryID'] ?>" href="#"><i class="bi bi-trash3"></i></a>
                            </td>
                        </tr>
                        <?php
                        ttPopaction('delete_pop'.$tt_sub_category['CategoryID'],'category.php?delid='.$tt_sub_category['CategoryID']);
                      }
                    }


                    ttPopaction('delete_pop'.$parent_category['CategoryID'],'category.php?delid='.$parent_category['CategoryID']);
                  }
                }
                ?>
                </tbody>
              </table>
            </div>
          </div>
        </div>
      </section>
      <?php
    }
    ?>
</main>
<?php require_once('inc/footer.php'); ?>
