<?php
include_once("../include/main.inc.php");
check_session('MyCpanel', 'index.php');

@extract($_REQUEST);
if (empty($catID)) {
  $heading = "Add New Certificate";
} else {
  $heading = "Edit Certificate";
}
$res = getResult("admin_details", " where user_type='admin'");

$record_type = "New Certificate";
$script_name = "addeditcertificate.php";
$redirect_script_name = "manage-certificates.php";
$table_name = "certificate";
#################### Add Record ##################
if ($id == 'remove_image') {
  mysqli_query($dbconn, "UPDATE $table_name SET $field='' where id = '$catID'");
  $_SESSION['session_message'] = $record_type . ' has been updated successfully';
  header("Location: " . SITE_ADMIN_URL . "/" . $script_name);
  exit;
}

if ($_REQUEST['action'] == "add_record" && $name != "") {
  $numCnt = getCount($table_name, " where name='$name'");
  $count_cat = mysqli_query($dbconn, "SELECT * FROM $table_name");
  if (mysqli_num_rows($count_cat) >= $res['category_count']) {
    $_SESSION['session_message'] = '<div class="btn btn-danger"> ' . $record_type . ' count exceeds the allowed limit ['. $res[category_count] .']  </div>';
  } else {
    if ($numCnt > 0) {
      $_SESSION['session_message'] = '<div class="btn btn-warning">' . $record_type . ' already exist  </div>';
    } else {
    
      if ($_FILES['certificateimg']['name']!=" ") {
        $file_extension = strtolower(getExtension($_FILES['certificateimg']['name']));
        if ($_FILES['certificateimg']['name'] != "") {
          $certificate = str_replace(" ", "-", $name);
          $img_name = $certificate;
          $img = upload_file($img_name, "certificateimg", "uploaded-files/certificate/images");
        }
      }

      $sql = mysqli_query($GLOBALS['dbconn'],"insert into `$table_name` set name='$name',caption='$caption',image='$img'");
      $_SESSION['session_message'] ='<div class="btn btn-success">' . $name . ' has been added successfully  </div> ';

      header("Location: " . SITE_ADMIN_URL . "/" . $redirect_script_name);
      exit;
    }
  }
}
################ Update Record ##################

if ($_REQUEST['action'] == "update_record" && $name != "") {
  $numCnt = getCount($table_name, " where name='$name' and id!='$catID'");
  if ($numCnt > 0) {
    $_SESSION['session_message'] = '<div class="btn btn-warning">' . $record_type . ' already exist  </div>';
    header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&parentID=" . $parentID . "&catID=" . $catID . "&pagesize=" . $pagesize);
    exit;
  }

  if ($_FILES['certificateimg']['name'] != "") {
    $file_extension = strtolower(getExtension($_FILES['certificateimg']['name']));
    if ($file_extension != "jpg" && $file_extension != "gif" && $file_extension != "jpeg" && $file_extension != "png") {
      $_SESSION['session_message'] = '<div class="btn btn-warning"> Please upload image (.jpg,.gif,.png Format Only) </div>';
      header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&parentID=" . $parentID . "&catID=" . $catID . "&pagesize=" . $pagesize);
      exit;
    }
    if ($_FILES['certificateimg']['name'] != "") {
      $categoryRes = getResult($table_name, " where id='$catID'", "image");
      $filename = $categoryRes['image'];
      if ($filename != "") {
        $file = site_url . "/uploaded-files/certificate/images/" . $filename;
        if (file_exists($file)) {
          unlink($file);
        }

      $certificate = str_replace(" ", "-", $name);
      $img_name = $certificate;
      $img = upload_file($img_name, "certificateimg", "uploaded-files/certificate/images");
      updateTable($table_name, " image='$img' where id='$catID'");
    }
  }
}
  
  updateTable($table_name, " name='$name',caption='$caption' where id='$catID'");
  $_SESSION['session_message'] = '<div class="btn btn-success">' . $record_type . ' has been added successfully  </div>';
   
  header("Location: " . SITE_ADMIN_URL . "/" . $redirect_script_name);
  exit;
}
########################################################
admin_header();
$res = getResult($table_name, " where id='$catID'");
?>
           <div class="content-wrapper">
          <div class="page-header">
            <h3 class="page-title">
			<?=$heading;?>
            </h3>
            <nav aria-label="breadcrumb">
              <ol class="breadcrumb">
			  <li class="nav-item d-none d-lg-flex">
            <a class="nav-link" href="<?=$redirect_script_name?>">
              <span class="btn btn-primary">Go Back</span>
            </a>
          </li>
              </ol>
            </nav>
          </div>
          <div class="row">
          
            <div class="col-12 grid-margin stretch-card">
              <div class="card">
                <div class="card-body">
                  <form class="forms-sample" method="POST" action=""  enctype="multipart/form-data">
                  <div class="form-group row">
                    <div class="col">
                      <label>Certificate Name</label>
                      <div id="the-basics">
                        <input class="typeahead" type="text" name="name" value="<?=$res['name']?>" placeholder="Certificate Name" required>
                      </div>
                    </div>
                    <div class="col">
                      <label>Certificate Caption Name</label>
                      <div id="bloodhound">
                        <input class="typeahead" type="text"  name="caption" value="<?=$res['caption']?>" placeholder="Certificate Caption Name" Required>
                      </div>
                    </div>
                  </div>
                  <?php
          if ($res['id'] == "") { ?>
                  <div class="form-group">
                     <label for="exampleTextarea1">Certificate Image</label>
                      <input type="file" class="dropify" name="certificateimg">
                  </div>
          <?php } else{ ?>
            <div class="form-group row">
              <div class="col">
                  <label>Certificate Image</label>
                      <input type="file" class="dropify" name="certificateimg">
             </div>
            <div class="col">
                <img src="<?=site_url."/uploaded-files/certificate/images/".$res['image']?>" alt="image" style="width: 60%;">
            </div>
          </div>
       <?php   } ?>
                  <?php
          if ($res['id'] == "") {
            ?>
            <button type="submit" class="btn btn-primary mr-2" name="add_record">Submit</button>
            <input type="hidden" name="action" value="add_record">
    <?php
          } else {
            ?>
              <button type="submit" class="btn btn-primary mr-2" name="update_record">Update</button>
              <input type="hidden" name="action" value="update_record">
    <?php
          }
          ?>
          <button class="btn btn-light" onclick="javascript: window.location.href = '<?=SITE_ADMIN_URL;?>/<?= $redirect_script_name; ?>?parentID=<?= $parentID; ?>&catID=<?= $catID; ?>'">Cancel</button>
                  </form>
                </div>
              </div>
            </div>
          </div>
        </div>
        <!-- content-wrapper ends -->
<?php
admin_footer();
exit;
?>

