<?php 
// Include main file
include_once('../libs/main.php');
// Check admin Login
include_once('checkLogin.php');
// Logical section of page
$activationKeyObj =  new  activationKey();

if(isset($_REQUEST['act']) && $_REQUEST['act']=='delete' && isset($_REQUEST['activationKey_id'])){
    $activationKeyObj->deleteFromId($_REQUEST['activationKey_id']);
    $successMsg = base64_encode(RECORD_DELETE);
}
if(isset($_REQUEST['action']) && $_REQUEST['action']!=''){
    if($_REQUEST['action']=="activate_all"){
        $data=array("user_status" => '1');
        $rs = $activationKeyObj->updateManyRecords($data, $_POST['check']);
        $successMsg = base64_encode(RECORD_ALL_ACTIVE);
    }else if($_REQUEST['action']=="inactivate_all"){
        $data=array("user_status" => '0');
        $rs = $activationKeyObj->updateManyRecords($data, $_POST['check']);
        $successMsg = base64_encode(RECORD_ALL_INACTIVE);
    }else if($_REQUEST['action']=="delete_all"){
        $rs = $activationKeyObj->deleteManyRecords($_POST['check']);
        $successMsg = base64_encode(RECORD_ALL_DELETE);
    }
}

if (isset($_REQUEST['act']) && $_REQUEST['act']=='load'){
    $records = $_REQUEST['length'];
    $start=0;
    if(isset($_GET['start']) && $_GET['start']!='0'){
        $page = $_GET['start'];
    }
    $start=($page-1)*$limit;
    $limit = array('start'=>$_GET['start'],'record'=>$records);
    
    $where = " 1=1 ";
    if(isset($_REQUEST['activationKey_number']) && $_REQUEST['activationKey_number'] != ''){
        $where .= " AND activationKey_number = '".$_REQUEST['activationKey_number']."'";
    }
    $data    = array('where' => $where,'limit' => $limit);
    $productDetails = $activationKeyObj->getAllRecords($data);
    $recordsTotal   =   $activationKeyObj->getAllCount($data);

        
    if ($productDetails && count($productDetails)>0){
        foreach ($productDetails as $key => $product){
            $adminObj = new admin();
            $admin = $adminObj->fromId($product->activationKey_created_by);
            $used ="No";
            $usedAt="";
            if($product->activationKey_used==1){
                $used ="Yes";
                $usedAt=$product->activationKey_used_at;
            }
            $action = '
                 <a href="activationKeyadd.php?act=edit&activationKey_id='.$product->activationKey_id.'" class="btn btn-success white-btn"><i class="nav-icon i-Pen-2 "></i></a>';

            $response[] = array('<input type="checkbox" name="check[]" id="check[]" value="'.$product->activationKey_id .'" class="check_class" />',
                    $product->activationKey_number,
                    $product->activationKey_key,
                    $admin->admin_fname." ".$admin->admin_lname."<br/>".$product->activationKey_created_at,
                    $used."<br/>".$usedAt,
                    $action,
             );
        }
        $result=array('recordsFiltered'=>$recordsTotal,'recordsTotal'=>$recordsTotal,'data'=>$response);
    }else{
        $result=array('recordsFiltered'=>0,'recordsTotal'=>0,'data'=>array());
    }
    echo json_encode($result);
    exit;
}

// Include Template 
include_once('../libs/routs.php');
?>