<?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-certificate.php";
$table_name = "banner_tbl";
####################################################

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 ($id == "delete_record") {
  db_query("delete from $table_name where slno='$recordID'");
  $_SESSION['session_message'] = $record_type . ' has been deleted successfully';
  header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&pagesize=" . $pagesize);
  exit;
}


if ($action == "Update") {


  while (list($key, $val) = each($_POST)) {

    if (substr($key, 0, 6) == "order_") {

      $key1 = substr($key, 6, strlen($key) - 6);

      $qry1 = db_query("update $table_name set bannerorder='$val' where slno='$key1'");
    }
  }

  $_SESSION['session_message'] = $record_type . ' order has been updated successfully';

  header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&parentID=" . $parentID . "&slno=" . $slno . "&pagesize=" . $pagesize);
  exit;
}
admin_header("");
?>
   <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">
                  <div class="table-responsive">
                    <table id="order-listing" class="table">
                      <thead>
                        <tr>
                            <th>#</th>
							<th>S.No.</th>
                            <th>Certificate Name</th>
                            <th>Certificate Image</th>
                            <th>Order</th>
                            <th>Status</th>
                            <th>Actions</th>
                        </tr>
                      </thead>
                      <tbody>
					  <?php
 $sl=$start+1;
 while($arr=mysqli_fetch_array($qry)) {
	 $catID = $arr['catID'];
	 $sub_cat_query = mysqli_query($dbconn,"SELECT * from $table_name where parentID = '$catID'");
	 $faq_query = mysqli_query($dbconn,"SELECT * from faq_tbl where category_id = '$catID'"); ?>
 <tr>
                            <td><input type="checkbox" name="cat_arr[]" value="<?= $arr['catID'] ?>"></td>
                            <td><?=$sl?></td>
                            <td><?=$arr['catName'];?>
							<?php
	   if($arr['status']=="N") {
		   echo "&nbsp;<span class='icon'>Inactive</span>";
	   }
	   echo ($arr['home_category'] == '1') ? 'Set As Home Category' : "";?>

	   	<br/><a href="<?=SITE_ADMIN_URL;?>/<?=$script_name;?>?parentID=<?=$arr['catID'];?>">Sub Categories[<?= mysqli_num_rows($sub_cat_query) ?>]</a>&nbsp; 
	   	<a href="<?=SITE_ADMIN_URL;?>/manage_faq.php?category_id=<?=$arr['catID'];?>">FAQ[<?= mysqli_num_rows($faq_query) ?>] </a>
   		<a href="<?=SITE_ADMIN_URL;?>/manage_location_content.php?category_id=<?=$arr['catID'];?>">Sub Domain Content </a>
						</td>
							<td> <?php
	   if($arr['catImage']!="") {
		   $file1=SITE_DIR_PATH."/uploaded-files/category/images/".$arr['catImage'];

			   $dimens=GetImageSize($file1);
			   if($dimens[0]>130) {
				   $dimens[0]=$dimens[0]+30;
				   $dimens[1]=$dimens[1]+80;
			   }
			   else{
				   $dimens[0]=200;
				   $dimens[1]=200;
			   }
			   ?>
			   <a href="javascript:void(0);" onClick="openwin('<?=SITE_ADMIN_URL;?>/popup_new.php?img=category/images/<?=$arr['catImage'];?>', '<?=$dimens[0];?>', '<?=$dimens[1];?>')" class="linkgray fontbold"><strong>View Image</strong>
			   <?php
	
	   }
	   ?>	   </td>
                            <td><input name="order_<?=$arr['catID'];?>" class="txtfieldadmin" type="text" size="2" value="<?=$arr['catOrder'];?>"></td>
                            <td>
                              <label class="badge badge-info">  <?php if($arr['status']=='Y' && $parentID!=0) { ?>
	   &nbsp;<a href="<?=SITE_ADMIN_URL?>/<?=$script_name;?>?id=deactivate_record&start=<?=$start;?>&catID=<?=$arr['catID'];?>&parentID=<?=$parentID;?>&pagesize=<?=$pagesize;?>"><img src="<?=SITE_ADMIN_URL?>/images/active.gif" alt="Deactivate This <?=$record_type;?>" title="Deactivate This <?=$record_type;?>" border="0"></a>
	   <?php }
	   if($arr['status']=='N' && $parentID!=0) { ?>
	   &nbsp;<a href="<?=SITE_ADMIN_URL?>/<?=$script_name;?>?id=activate_record&start=<?=$start;?>&catID=<?=$arr['catID'];?>&parentID=<?=$parentID;?>&pagesize=<?=$pagesize;?>"><img src="<?=SITE_ADMIN_URL?>/images/deactive.gif" alt="Activate This <?=$record_type;?>" title="Activate This <?=$record_type;?>" border="0"></a>
	   <?php } ?></label>
                            </td>
                            <td>
                            <a href="<?=SITE_ADMIN_URL?>/addeditcategory.php?start=<?=$start;?>&catID=<?=$arr['catID'];?>&parentID=<?=$parentID;?>&pagesize=<?=$pagesize;?>"><i class="icon-sm fa fa-edit mr-2"></i></a>
                            </td>
						</tr>

 <?php  $sl++; }?>
                       
                      </tbody>
                    </table>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
                  <?php
                  admin_footer();
                  exit;
                  ?>