<?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 (isset($_POST['activate'])) {
    if (!empty($_POST['id_arr'])) {
        $catid = $_POST['id_arr'];
        foreach ($catid as $id) {
            mysqli_query($dbconn, "UPDATE $table_name SET status = '1' WHERE id = '$id'");
        }
        $_SESSION['session_message'] = '<div class="btn btn-success">' . $record_type . ' has been updated 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;
}

if (isset($_POST['deactivate'])) {
    if (!empty($_POST['id_arr'])) {
        $catid = $_POST['id_arr'];
        foreach ($catid as $id) {
            mysqli_query($dbconn, "UPDATE $table_name SET status = '0'  WHERE id = '$id'");
        }
        $_SESSION['session_message'] = '<div class="btn btn-success">' . $record_type . ' has been updated 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;
}

if (isset($_POST['delete'])) {
    if (!empty($_POST['id_arr'])) {
        $catid = $_POST['id_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("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">
                <form action="" method="post">
                    <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>Type</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><p class="ordern"><?=$arr['code'];?></p></td>
                                    <td><p> <?=$arr['coupon_type'];?> </p></td>
                                    <td><p class="productid-dcs"><?=$arr['discount'];?>%</p></td>
                                    <td>
                                        <p class="rec-date-dcs"> <?=date('d M,Y h:i:a',strtotime($arr['startdate']));?></p>
                                    </td>
                                    <td>
                                        <p class="rec-date-dcs"><?=date('d M,Y h:i:a',strtotime($arr['enddate']));?></p>
                                    </td>
                                    <td>
                                        <?php 
                                        if($arr['enddate'] >= date('Y-m-d H:i:s')){ 
                                            echo ($arr['status'] =='1')?'<label class="badge badge-success badge-pill">Active</label>':'<label class="badge badge-warning badge-pill">In-Active</label>';
                                        }else{
                                            echo '<label class="badge badge-danger badge-pill">Expired</label>';
                                        } 
                                        ?>
                                    </td>
                                    <td  class="download-dcs">
                                        <a
                                            href="<?=SITE_ADMIN_URL?>/addeditcoupan.php?id=<?=$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>
                                    </td>
                                </tr>

                                <?php  $sl++; }?>

                            </tbody>
                        </table>
						<div class="btn-grp">
						<button type="submit" name="activate" onclick="return select_chk()" class="btn btn-success btn-icon-text btn-sm">
						<i class="fas fa-toggle-on mr-1"></i>Activate
                        </button>
						<button  type="submit" name="deactivate" onclick="return select_chk()" class="btn btn-warning btn-icon-text btn-sm">
						<i class="fas fa-toggle-off mr-1"></i>Deactivate
                        </button>
						<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>
                    </div>
                                        </form>
                </div>
            </div>
        </div>
    </div>
</div>
<script language="javascript">
function select_chk(){
var chks = document.getElementsByName('id_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;
?>