<?php 
// Include main file
include_once('../libs/main.php');
// Check xcare_offer Login
include_once('checkLogin.php');

// Logical section of page
$xcare_offerObj =  new  xcare_offer();
$adminlogObj = new admin_log();
$modulepagesObj = new module_pages();

$filename=basename($_SERVER['SCRIPT_FILENAME']);
if($filename == 'offerlist.php')
{
    $module_id = $adminlogObj->getModuleId($filename);
    $page_id = $adminlogObj->getPageId($filename);
    $allpages = $modulepagesObj->fromId($page_id);
    $pagename = $allpages->page;
}

if(isset($_REQUEST['act']) && $_REQUEST['act']=='change_status' && isset($_REQUEST['xcare_offer_id'])){
    if(isset($_REQUEST['status'])){
        
        $xcare_offerDetail = $xcare_offerObj->fromId($_REQUEST['xcare_offer_id']);
        if($xcare_offerDetail->xcare_offer_status == '1')
        {
            $old_status = 'Active';
        }else{
            $old_status = 'Inactive';
        }

        if($_REQUEST['status'] == '1')
        {
            $new_status = 'Active';
        }else{
            $new_status = 'Inactive';
        }

        $xcare_offerObj->changeStatus($_REQUEST['xcare_offer_id']);

        $offer_detail = " <b>offer status</b>: "." ".$old_status." To ".$new_status." ";
        $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$offer_detail);
        echo RECORD_STATUS;
    }
    return true;
}
if(isset($_REQUEST['act']) && $_REQUEST['act']=='delete' && isset($_REQUEST['xcare_offer_id'])){
    
    
    $xcare_offerDetail = $xcare_offerObj->fromId($_REQUEST['xcare_offer_id']);
    if($xcare_offerDetail->xcare_offer_image != '')
    {
        
        $data = json_decode($xcare_offerDetail->xcare_offer_image,1);
        $size = w::imageSizeArray("xcare_offer");

        if(count($size)>0)
        {
            foreach ($size as $key => $value) {
                $width = $value['width'];
                $height = $value['height'];
                if($width=='max'){
                    $byx = "-original";
                }else{
                    $byx = "-".$width."x".$height;
                }
                if(file_exists(DIR_UPLOAD_OFFER.$data['name'].$byx.".".$data['type'])){
                    unlink(DIR_UPLOAD_OFFER.$data['name'].$byx.".".$data['type']);
                }
            }       
        }
    }

    $xcare_offerDetail = $xcare_offerObj->fromId($_REQUEST['xcare_offer_id']);
    $offer_detail = "Delete Record offer title: "." ".$xcare_offerDetail->xcare_offer_title." and offer link: ".$xcare_offerDetail->xcare_offer_link." and offer Image:".$xcare_offerDetail->xcare_offer_image." and offer Status:".$xcare_offerDetail->xcare_offer_status;

    $offer_del = $xcare_offerObj->deleteFromId($_REQUEST['xcare_offer_id']);
    
    if(isset($offer_del) && $offer_del != '')
    {
        $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$offer_detail);
    }
    $successMsg = base64_encode(RECORD_DELETE);
}

if(isset($_REQUEST['action']) && $_REQUEST['action']!=''){
    if($_REQUEST['action']=="activate_all"){
        $data=array("xcare_offer_status" => '1');

        $chk_data = $_POST['check'];

        foreach($chk_data as $akey => $val_actchk)
        {
            $old_data = $xcare_offerObj->fromId($val_actchk);
            if($old_data->xcare_offer_status == '1')
            {
                $old_status = 'Active';
            }else{
                $old_status = 'Inactive';
            }

            if($old_data->xcare_offer_status != '1')
            {
                $new_status = 'Active';
                $offerdata = " <b>Status</b>: "." ".$old_status." <b>To</b> ".$new_status." ";
                $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$offerdata);
            }
        }
        $rs = $xcare_offerObj->updateManyRecords($data, $_POST['check']);
        $successMsg = base64_encode(RECORD_ALL_ACTIVE);
    }else if($_REQUEST['action']=="inactivate_all"){
        $data=array("xcare_offer_status" => '0');
        $chk_data = $_POST['check'];
        foreach($chk_data as $akey => $val_actchk)
        {
            $old_data = $xcare_offerObj->fromId($val_actchk);
            if($old_data->xcare_offer_status == '1')
            {
                $old_status = 'Active';
            }else{
                $old_status = 'Inactive';
            }

            if($old_data->xcare_offer_status != '0')
            {
                $new_status = 'Inactive';
                $offerdata = " <b>Status</b>: "." ".$old_status." <b>To</b> ".$new_status." ";
                $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$offerdata);
            }
        }
        $rs = $xcare_offerObj->updateManyRecords($data, $_POST['check']);
        $successMsg = base64_encode(RECORD_ALL_INACTIVE);
    }else if($_REQUEST['action']=="delete_all"){

        $chk_del = implode(",",$_POST['check']);
        
        $offerdata = " <b>Delete Id</b>: "." ".$chk_del;
        $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$offerdata);
            
        $rs = $xcare_offerObj->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['offer_title']) && $_REQUEST['offer_title'] != '')
    {
        $where .= " AND xcare_offer_title = '".$_REQUEST['offer_title']."' ";
    }
    if(isset($_REQUEST['offer_expire']) && $_REQUEST['offer_expire'] != '')
    {
       if($_REQUEST['offer_expire']=='ongoing'){
            $where .= " AND xcare_offer_expired_at >= '".date("Y-m-d")."' ";
       }else{
            $where .= " AND xcare_offer_expired_at < '".date("Y-m-d")."' ";
       }     
        
    }
    if(isset($_REQUEST['offer_domain_id']) && $_REQUEST['offer_domain_id'] != '')
    {
        $where .= " AND xcare_offer_domain_id = '".$_REQUEST['offer_domain_id']."' ";
    }
    if(isset($_REQUEST['offer_status']) && $_REQUEST['offer_status'] != '')
    {
        $where .= " AND xcare_offer_status = '".$_REQUEST['offer_status']."' ";
    }

    if (isset($_REQUEST['search']['value']) && $_REQUEST['search']['value']!=""){
        $where = " xcare_offer_title LIKE '%".$_REQUEST['search']['value']."%' OR xcare_offer_link LIKE '%".$_REQUEST['search']['value']."%' ";
    }    
        
    // Array of order by 
    $orderby=array();
    if (isset($_REQUEST['order']['0']['column']) && $_REQUEST['order']['0']['column']!=""){
        $orderby = array(" ".$orderFields[$_REQUEST['order']['0']['column']]." ".$_REQUEST['order']['0']['dir']);
    }else{
        $orderby = array(" xcare_offer_id  desc ");
    }
    $data    = array('where' => $where, 'orderBy' => $orderby, 'limit' => $limit);

    $offerDetails = $xcare_offerObj->getAllRecords($data);
    $recordsTotal   =   $xcare_offerObj->getAllCount($data);
        
    if ($offerDetails && count($offerDetails)>0){
        foreach ($offerDetails as $key => $offervalue){

            if($offervalue->xcare_offer_status =='1'){
              $status = '<a href="javascript:void(0)" onclick="change_status('.$offervalue->xcare_offer_id.',0)" title="Inactivate"><span class="badge badge-success">Active</span></a>';
            }else{
              $status = '<a href="javascript:void(0)" onclick="change_status('.$offervalue->xcare_offer_id.',1)" title="Activate"><span class="badge badge-danger">Inactive</span></a>';
            }
            
            $offerimage = json_decode($offervalue->xcare_offer_image,1);
            $imagepath = $offerimage['name']."-100x100.".$offerimage['type'];

        $domainObj = new domain();
        $domainDetails =$domainObj->fromId($offervalue->xcare_offer_domain_id);    
        $domainTitle =$domainDetails->domain_title;
        $image = '<img style="width:150px;" src="'.DIR_UPLOAD_FORM_OFFER_SHOW.$imagepath.'" />';
            $response[] = array('<input type="checkbox" name="check[]" id="check[]" value="'.$offervalue->xcare_offer_id.'" class="check_class" />', 
                    $offervalue->xcare_offer_title."<br/><b>Domain: </b>".$domainTitle,
                    // $offervalue->xcare_offer_link,
                    $offervalue->xcare_offer_button,
                    $offervalue->xcare_offer_desc,
                    $offervalue->xcare_offer_tnc,
                    $offervalue->xcare_offer_expired_at,
                    $offervalue->xcare_offer_city.", ".$offervalue->xcare_offer_state.", ".$offervalue->xcare_offer_postalcode.", ".$offervalue->xcare_offer_country,
                    $image,
                    "<div id='td_status_".$offervalue->xcare_offer_id ."'>".$status."</div>",    
                    '<a href="offeradd.php?act=edit&xcare_offer_id='.$offervalue->xcare_offer_id .'" class="btn btn-success white-btn"><i class="nav-icon i-Pen-2 "></i></a>
                    <a id="delete-confirm" onclick="delete_entry('.$offervalue->xcare_offer_id.');" class="btn btn-danger white-btn"><i class="nav-icon i-Close-Window "></i></a>
                    '

            );
        }
        $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');
?>