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

// Logical section of page
$brandObj =  new  brand();

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

$filename=basename($_SERVER['SCRIPT_FILENAME']);
if($filename == 'brandlist.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['brand_id'])){
    if(isset($_REQUEST['status'])){
        if($_REQUEST['status'] == '1'){
            $new_status = 'active';
        }else{
            $new_status = 'inactive';
        }
        $data =array(
            'xcarestatus' => $new_status,
        );
        $brandObj->updateRecords($data,$_REQUEST['brand_id']);
        echo RECORD_STATUS;
    }
    return true;
}


if (isset($_REQUEST['act']) && $_REQUEST['act']=='assign'){
    if(isset($_POST['brandIds']) && !empty($_POST['brandIds'])){
        foreach($_POST['brandIds'] as $brandId) {
            $brandData =array(
                "oneDollarPoints"=>$_POST['brand_points']
            );
            $brandObj->updateRecords($brandData,$brandId);
        }
    }
}
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['brandName']) && $_REQUEST['brandName'] != '')
    {
        $where .= " AND brandName LIKE '%".$_REQUEST['brandName']."%'";
    }
    if(isset($_REQUEST['xcarestatus']) && $_REQUEST['xcarestatus'] != '')
    {
        $where .= " AND xcarestatus LIKE '%".$_REQUEST['xcarestatus']."%'";
    }
    if(isset($_REQUEST['oneDollarPoints']) && $_REQUEST['oneDollarPoints'] != '')
    {
        $where .= " AND oneDollarPoints LIKE '%".$_REQUEST['oneDollarPoints']."%'";
    }
    

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

    $brandDetails = $brandObj->getAllRecords($data);
    $recordsTotal   =   $brandObj->getAllCount($data);
        
    if ($brandDetails && count($brandDetails)>0){
        foreach ($brandDetails as $key => $brand){

            if($brand->xcarestatus =='active'){
              $status = '<a href="javascript:void(0)" onclick="change_status('.$brand->brand_id.',0)" title="Inactivate"><span class="badge badge-success">Active</span></a>';
            }else{
              $status = '<a href="javascript:void(0)" onclick="change_status('.$brand->brand_id.',1)" title="Activate"><span class="badge badge-danger">Inactive</span></a>';
            }

            $images =json_decode($brand->imageUrls,1);
            if($images !="" && count($images)>0){
                foreach($images as $key =>$value){
                    $image = '<img style="width:80px;" src="'.$value.'" />';
                    break;
                }
            }
            $response[] = array( 
                    $brand->brandName."<br/>".$brand->brandKey,
                    $image,
                    $brand->status,
                    $brand->xcarestatus,
                    $brand->oneDollarPoints,
                    "Min: ".$brand->min."<br/>Max: ".$brand->max,
                    "created Date: ".date('Y-m-d',strtotime($brand->createdDate))."</br>last Update Date: ".date('Y-m-d',strtotime($brand->lastUpdateDate)),
                    $status
            );
        }
        $result=array('recordsFiltered'=>$recordsTotal,'recordsTotal'=>$recordsTotal,'data'=>$response);
    }else{
        $result=array('recordsFiltered'=>0,'recordsTotal'=>0,'data'=>array());
    }
    echo json_encode($result);
    exit;
}

$brandObj=new brand();
$fields =array('DISTINCT(oneDollarPoints)');
$arg =array('fields'=>$fields);
$allPoints =$brandObj->getAllRecords($arg);

$brandObj=new brand();
$fields =array('brand_id','brandName');
$arg =array('fields'=>$fields);
$allbrands =$brandObj->getAllRecords($arg);
// Include Template 
include_once('../libs/routs.php');
?>