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

// Logical section of page
$giftcardObj =  new  giftcard();

$adminlogObj = new admin_log();
$modulepagesObj = new module_pages();

$filename=basename($_SERVER['SCRIPT_FILENAME']);
if($filename == 'giftcardlist.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['giftcard_id'])){
    if(isset($_REQUEST['status'])){

        $giftDetail = $giftcardObj->fromId($_REQUEST['giftcard_id']);
        if($giftDetail->giftcard_status == '1')
        {
            $old_status = 'Active';
        }else{
            $old_status = 'Inactive';
        }

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

        $giftcardObj->changeStatus($_REQUEST['giftcard_id']);

        $gift_detail = " <b>Gift Status</b>: "." ".$old_status." <b>To</b> ".$new_status." ";
        $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$gift_detail);

        echo RECORD_STATUS;
    }
    return true;
}
if(isset($_REQUEST['act']) && $_REQUEST['act']=='delete' && isset($_REQUEST['giftcard_id'])){
    $giftcardObj->deleteFromId($_REQUEST['giftcard_id']);
    $successMsg = base64_encode(RECORD_DELETE);
}

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

        $chk_data = $_POST['check'];
        foreach($chk_data as $akey => $val_actchk)
        {
            $old_data = $giftcardObj->fromId($val_actchk);
            if($old_data->giftcard_status == '1')
            {
                $old_status = 'Active';
            }else{
                $old_status = 'Inactive';
            }

            if($old_data->giftcard_status != '1')
            {
                $new_status = 'Active';
                $giftcarddata = " <b>Status</b>: "." ".$old_status." <b>To</b> ".$new_status." ";
                $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$giftcarddata);
            }
        }

        $rs = $giftcardObj->updateManyRecords($data, $_POST['check']);
        $successMsg = base64_encode(RECORD_ALL_ACTIVE);
    }else if($_REQUEST['action']=="inactivate_all"){
        $data=array("giftcard_status" => '0');

        $chk_data = $_POST['check'];
        foreach($chk_data as $akey => $val_actchk)
        {
            $old_data = $giftcardObj->fromId($val_actchk);
            if($old_data->giftcard_status == '1')
            {
                $old_status = 'Active';
            }else{
                $old_status = 'Inactive';
            }

            if($old_data->giftcard_status != '0')
            {
                $new_status = 'Inactive';
                $giftcarddata = " <b>Status</b>: "." ".$old_status." <b>To</b> ".$new_status." ";
                $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$giftcarddata);
            }
        }

        $rs = $giftcardObj->updateManyRecords($data, $_POST['check']);
        $successMsg = base64_encode(RECORD_ALL_INACTIVE);
    }else if($_REQUEST['action']=="delete_all"){

        $chk_del = implode(",",$_POST['check']);
        
        $giftcarddata = " <b>Delete Id</b>: "." ".$chk_del;
        $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$giftcarddata);
        
        $rs = $giftcardObj->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['gift_code']) && $_REQUEST['gift_code'] != '')
    {
        $where .= " AND giftcard_code LIKE '%".$_REQUEST['gift_code']."%'";
    }
    if(isset($_REQUEST['gift_title']) && $_REQUEST['gift_title'] != '')
    {
        $where .= " AND giftcard_title LIKE '%".$_REQUEST['gift_title']."%'";
    }
    if(isset($_REQUEST['gift_cost']) && $_REQUEST['gift_cost'] != '')
    {
        $where .= " AND giftcard_cost LIKE '%".$_REQUEST['gift_cost']."%'";
    }
    if(isset($_REQUEST['gift_createat']) && $_REQUEST['gift_createat'] != '')
    {
        $where .= " AND date(giftcard_created_at) = '".$_REQUEST['gift_createat']."'";
    }
    if(isset($_REQUEST['gift_status']) && $_REQUEST['gift_status']!=""){
         $where .= " and giftcard_status='".$_REQUEST['gift_status']."' "; 
    }


    if (isset($_REQUEST['search']['value']) && $_REQUEST['search']['value']!=""){
        $where = " giftcard_code LIKE '%".$_REQUEST['search']['value']."%' OR giftcard_title LIKE '%".$_REQUEST['search']['value']."%' OR giftcard_cost LIKE '%".$_REQUEST['search']['value']."%' OR giftcard_created_at LIKE '%".$_REQUEST['search']['value']."%' ";
    }    
        
    $orderFields = array('','giftcard_code','giftcard_title');
    
    // 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(" giftcard_id desc ");
    }
    $data    = array('where' => $where, 'orderBy' => $orderby, 'limit' => $limit);

    $giftcardDetails = $giftcardObj->getAllRecords($data);
    $recordsTotal   =   $giftcardObj->getAllCount($data);
        
    if ($giftcardDetails && count($giftcardDetails)>0){
        foreach ($giftcardDetails as $key => $giftcard){

            if($giftcard->giftcard_status =='1'){
              $status = '<a href="javascript:void(0)" onclick="change_status('.$giftcard->giftcard_id.',0)" title="Inactivate"><span class="badge badge-success">Active</span></a>';
            }else{
              $status = '<a href="javascript:void(0)" onclick="change_status('.$giftcard->giftcard_id.',1)" title="Activate"><span class="badge badge-danger">Inactive</span></a>';
            }
            $image = '<img style="width:80px;" src="'.DIR_UPLOAD_GIFT_SHOW.$giftcard->giftcard_image.'" />';
            if($giftcard->giftcard_updated_at != '' && $giftcard->giftcard_updated_at != '0000-00-00 00:00:00'){
                $gift_updated_at =  date('Y-m-d',strtotime($giftcard->giftcard_updated_at));
            }else{
                $gift_updated_at = '';
            } 
            $response[] = array('<input type="checkbox" name="check[]" id="check[]" value="'.$giftcard->giftcard_id.'" class="check_class" />', 
                    $giftcard->giftcard_code,
                    $giftcard->giftcard_title,
                    $giftcard->giftcard_cost,
                    $image,
                    date('Y-m-d',strtotime($giftcard->giftcard_created_at)),
                    //date('Y-m-d',strtotime($gift_updated_at)),
                    $gift_updated_at,
                    "<div id='td_status_".$giftcard->giftcard_id."'>".$status."</div>",    
                    '<a href="giftcardadd.php?act=edit&giftcard_id='.$giftcard->giftcard_id.'" class="btn btn-success white-btn"><i class="nav-icon i-Pen-2 "></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');
?>