<?php
include_once("../include/main.inc.php");
check_session('MyCpanel', 'index.php');
?>
<script type="text/javascript">
  function chk_valid_date() {
    if (document.bannerFrm.validFrom.value == 0) {
      alert("Please select Validity Period");
      return false;
    }
    if (document.bannerFrm.validTo.value == 0) {
      alert("Please select Validity Period");
      return false;
    }
  }
</script>
<?php
################ Paging Starts From Here ##################
@extract($_REQUEST);
$start = (intval($start) < 0 or $start == "") ? 0 : $start;
$pagesize = intval($pagesize) == 0 ? $pagesize = 20 : $pagesize;
$res = getResult("admin_details", " where user_type='admin'");

$heading = "Manage Certificate";
$record_type = "Certificate";
$script_name = "manage-certificates.php";
$table_name = "certificate";
####################################################

if ($id == "add_record" && $_FILES['banner']['name'] != "") {
  $count_cat = mysqli_query($GLOBALS['dbcon'], "SELECT * FROM $table_name");
  if (mysqli_num_rows($count_cat) >= $res['banner_count']) {
    $_SESSION['session_message'] = ' <div class="btn btn-danger"> ' . $record_type . ' count exceeds the allowed limit [' . $res[banner_count] . ']  </div>';
  } else {
    if ($_FILES['banner']['name'] != "") {
      $file_extension = strtolower(getExtension($_FILES['banner']['name']));
      if ($file_extension != "jpg" && $file_extension != "gif" && $file_extension != "jpeg" && $file_extension != "png") {
        $form_check_alert .= "<li>Please upload small image(.jpg,.gif,.png Format Only)</li>";
      }
    }
    if (!strlen($form_check_alert) > 0) {
      if ($_FILES['banner']['name'] != "") {
        $img_name = "$personName";
        $img_name = str_replace(" ", "-", $img_name);
        $img1 = upload_file($img_name, "banner", "uploaded-files/banner-image");
      }
      $bannerUrl = "" . $bannerUrl;
      $sql = db_query("insert into $table_name set personName='$personName', bannerUrl='$bannerUrl', bannerImage='$img1', bannerCat='$cat',tagline='$tagline', bannerSubcat='$subcat', status='Y', recvDate=now()");
      $_SESSION['session_message'] = '<div class="btn btn-success">' . $record_type . ' has been added successfully  </div>';

      header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&pagesize=" . $pagesize);
      exit;
    } else {
      $msg = html_head();
      $msg .= $form_check_alert;
      $msg .= html_foot();
    }
  }
}

if ($id == "update_record") {
  if ($_FILES['banner']['name'] != "") {
    $file_extension = strtolower(getExtension($_FILES['banner']['name']));
    if ($file_extension != "jpg" && $file_extension != "gif" && $file_extension != "jpeg" && $file_extension != "png") {
      $form_check_alert .= "<li>Please upload small image(.jpg,.gif,.png Format Only)</li>";
    }
  }
  if (!strlen($form_check_alert) > 0) {
    if ($_FILES['banner']['name'] != "") {
      $res1 = getResult($table_name, " where slno='$recordID'");
      $filenm1 = SITE_DIR_PATH . "/uploaded-files/banner-image/" . $res1['bannerImage'];
      if (file_exists($filenm1)) {
        unlink($filenm1);
      }

      $img_name = "$personName";
        $img_name = str_replace(" ", "-", $img_name);
      $img1 = upload_file($img_name, "banner", "uploaded-files/banner-image");
      updateTable($table_name, " bannerImage='$img1' where slno='$recordID'");
    }

    $bannerUrl = $bannerUrl;
    updateTable($table_name, " personName='$personName', bannerUrl='$bannerUrl', bannerCat='$cat',tagline='$tagline', bannerSubcat='$subcat' where slno='$recordID'");
    $_SESSION['session_message'] = $record_type . ' has been updated successfully';
    header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&pagesize=" . $pagesize);
    exit;
  } else {

    $msg = html_head();
    $msg .= $form_check_alert;
    $msg .= html_foot();
  }
}
if (isset($_POST['delete'])) {
  if (!empty($_POST['cer_arr'])) {
      $catid = $_POST['cer_arr'];
      foreach ($catid as $id) {
          mysqli_query($dbconn, "DELETE FROM $table_name WHERE id = '$id'");
      }
      $_SESSION['session_message'] = '<div class="btn btn-success">' . $record_type . ' has been deleted successfully</div>';
  } else {
      $_SESSION['session_message'] = '<div class="btn btn-danger">No Category selected</div>';
  }
  header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize);
  exit;
}
admin_header("");
$qry= mysqli_query($dbconn,"select * from $table_name");	
?>
   <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="<?=admin_url?>addeditcertificate.php">
              <span class="btn btn-primary">+ Add New Certificate</span>
            </a>
          </li>
              </ol>
            </nav>
          </div>
          <div class="card">
            <div class="card-body">
              <div class="row">
                <div class="col-12">
                <form action="" method="post">
                  <div class="table-responsive">
                    <table id="order-listing" class="table table-bordered table-hover">
                      <thead>
                        <tr>
                            <th>
                            <div class="form-check mt-0">
										<label class="form-check-label">
										<input type="checkbox" class="form-check-input" name="check_all" id="check_all" value="1" onclick="checkall(this.form)">
										<i class="input-helper"></i>
										</label>
                                    </div>
                            </th>
							<th>S.No.</th>
                            <th>Certificate Name</th>
                            <th>Certificate Image</th>
                            <th>Actions</th>
                        </tr>
                      </thead>
                      <tbody>
					  <?php
 $sl=$start+1;
 while($arr=mysqli_fetch_array($qry)) { ?>
 <tr>
                            <td><input type="checkbox" name="cer_arr[]" value="<?= $arr['id'] ?>"></td>
                            <td><?=$sl?></td>
     <td><p class="ordern"><?=$arr['name'];?></p></td>
         <td><p class="dongle-dcs">  
                <a href="javascript:void(0);" onClick="openwin('<?=SITE_ADMIN_URL;?>/popup_new.php?img=certificate/images/<?=$arr['image'];?>', '<?=$dimens[0];?>', '<?=$dimens[1];?>')"><span> View
                    Image <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-eye-slash" viewBox="0 0 16 16"> <path d="M13.359 11.238C15.06 9.72 16 8 16 8s-3-5.5-8-5.5a7.028 7.028 0 0 0-2.79.588l.77.771A5.944 5.944 0 0 1 8 3.5c2.12 0 3.879 1.168 5.168 2.457A13.134 13.134 0 0 1 14.828 8c-.058.087-.122.183-.195.288-.335.48-.83 1.12-1.465 1.755-.165.165-.337.328-.517.486l.708.709z"/> <path d="M11.297 9.176a3.5 3.5 0 0 0-4.474-4.474l.823.823a2.5 2.5 0 0 1 2.829 2.829l.822.822zm-2.943 1.299.822.822a3.5 3.5 0 0 1-4.474-4.474l.823.823a2.5 2.5 0 0 0 2.829 2.829z"/> <path d="M3.35 5.47c-.18.16-.353.322-.518.487A13.134 13.134 0 0 0 1.172 8l.195.288c.335.48.83 1.12 1.465 1.755C4.121 11.332 5.881 12.5 8 12.5c.716 0 1.39-.133 2.02-.36l.77.772A7.029 7.029 0 0 1 8 13.5C3 13.5 0 8 0 8s.939-1.721 2.641-3.238l.708.709zm10.296 8.884-12-12 .708-.708 12 12-.708.708z"/> </svg></span></a></p>
       </td>
                            <td> <p class="download-dcs">
                                <a href="<?=SITE_ADMIN_URL?>/addeditcertificate.php?catID=<?=$arr['id'];?>"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-edit-3"><path d="M12 20h9"></path><path d="M16.5 3.5a2.121 2.121 0 0 1 3 3L7 19l-4 1 1-4L16.5 3.5z"></path></svg></a></p>
                            </td>
						</tr>

 <?php  $sl++; } ?>
                       
                      </tbody>
                    </table>
                    <div class="btn-grp">
						<button type="submit" name="delete" onclick="return select_chk()" class="btn btn-danger btn-icon-text btn-sm">
							<i class="fas fa-trash-alt mr-1"></i>Delete
                        </button>
                        </div>
                        <form action="" method="post">
                  </div>
                  
                </div>
              </div>
            </div>
          </div>
        </div>
        <script language="javascript">

checked=false;
function checkall(frm1){
	var aa= frm1;
	if(checked == false){
		checked = true
	}
	else{
		checked = false
	}
	for (var i =0; i < aa.elements.length; i++){
		aa.elements[i].checked = checked;
	}
} 
function select_chk(){
var chks = document.getElementsByName('cer_arr[]');
var hasChecked = false;
for (var i = 0; i < chks.length; i++){
if (chks[i].checked){
  hasChecked = true;
  break;
  }
}
if (hasChecked == false){
alert("Please Select At Least One.");
return false;
}
}
</script>
                  <?php
                  admin_footer();
                  exit;
                  ?>