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

################ Paging Starts From Here ##################
@extract($_REQUEST);
$start = (intval($start)==0 or $start=="")?$start=0:$start;
$pagesize = intval($pagesize)==0?$pagesize=100:$pagesize;
if($parentID=="") {
	$parentID=0;
}

$heading="Manage Coupan Code";
$record_type="Coupan Code";
$script_name="manage-coupan-codes.php";
$table_name="coupan_codes_tbl";
#################### DELETE Record ##################

if($id=="delete_record") {
		$categoryRes=getResult($table_name," where catID='$catID'");
		$filename=$categoryRes['catImage'];
		if($filename!="") {
			$file=SITE_DIR_PATH."/uploaded-files/blog/images/".$filename;
			if(file_exists($file)) {
				unlink($file);
			}
			
			$file_thumb=SITE_DIR_PATH."/uploaded-files/blog/images/thumbs/".$filename;
			if(file_exists($file_thumb)) {
				unlink($file_thumb);
			}
		}
		mysqli_query($dbconn,"delete from $table_name where catID='$catID'");
		$_SESSION['session_message']=$record_type.' has been deleted successfully';
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize);
	exit;	 
}

if($id=="deactivate_record") {
	updateTable($table_name," status='N' where catID='$catID' and status='Y' ");
	$_SESSION['session_message']=$record_type.' has been deactivated successfully.';
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize);
	exit;
}
if($id=="activate_record") {
	updateTable($table_name," status='Y' where catID='$catID' and status='N' ");
	$_SESSION['session_message']=$record_type.' has been activated successfully.';
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize);
	exit;
}
if($id=="set_top_business_category") {
	updateTable($table_name," topBusinessCategory='Y', status='Y' where catID='$recordID'");
	$_SESSION['session_message']=$record_type.' has been set as top business category successfully.';
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize);
	exit;
}
if($id=="unset_top_business_category") {
	
	$homeCnt=getCount($table_name," where topBusinessCategory='Y' and parentID='0'");
	if ($homeCnt < 4) {
		$_SESSION['session_message']=$record_type.' is required hence can not be unset as top business category.';
		header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize);
		exit;
	}
	
	updateTable($table_name," topBusinessCategory='N' where catID='$recordID'");
	$_SESSION['session_message']=$record_type.' has been unset as top business category successfully.';
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize);
	exit;	
}

########################################################
admin_header("Manage $record_type");	
$qry= db_query("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?>addeditcoupan.php">
                        <span class="btn btn-primary">+ Add New Coupan Code</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 table-bordered table-hover">
                            <thead>
                                <tr>
                                    <th class="_reab">#</th>
                                    <th>S.No.</th>
                                    <th>Code</th>
                                    <th>DisCount</th>
                                    <th>Start Date</th>
                                    <th>Expire Date</th>
                                    <th>Status</th>
                                    <th>Actions</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
 $sl=$start+1;
 while($arr=mysqli_fetch_array($qry)) { ?>
                                <tr>
                                    <td>
                                        <div class="form-check mt-0"><label class="form-check-label"><input
                                                    type="checkbox" class="form-check-input" name="id_arr[]"
                                                    value="<?= $arr['id'] ?>"><i class="input-helper"></i></label>
                                        </div>
                                    </td>
                                    <td><?=$sl?></td>
                                    <td><?=$arr['code'];?></td>
                                    <td><?=$arr['discount'];?></td>
                                    <td>
                                       <?=date('d M,Y h:i:s',strtotime($arr['startdate']));?>
                                    </td>
                                    <td>
                                       <?=date('d M,Y h:i:s',strtotime($arr['enddate']));?>
                                    </td>
                                    <td><?php if($arr['status']=='1') { ?><label class="badge badge-success badge-pill">Active</label><?php }
	   									else if($arr['status']=='0') { ?> <label class="badge badge-danger badge-pill">In-Active</label>
                                        <?php } ?>
                                    </td>
                                    <td>
                                        <a
                                            href="<?=SITE_ADMIN_URL?>/addeditcoupan.php?id=<?=$arr['id'];?>"><i
                                                class="icon-sm fa fa-edit mr-2"></i></a>
                                    </td>
                                </tr>

                                <?php  $sl++; }?>

                            </tbody>
                        </table>
						<div class="btn-grp">
						<button type="button" class="btn btn-success btn-icon-text btn-sm">
						<i class="fas fa-toggle-on mr-1"></i>Activate
                        </button>
						<button type="button" class="btn btn-warning btn-icon-text btn-sm">
						<i class="fas fa-toggle-off mr-1"></i>Deactivate
                        </button>
						<button type="button" class="btn btn-danger btn-icon-text btn-sm">
							<i class="fas fa-trash-alt mr-1"></i>Delete
                        </button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<?php 
admin_footer();
exit;
?>