<?php
defined($security_key) or exit;

//------------------------------------------
// Products attributes items
//------------------------------------------

//Connect to DB
$pdo = dbConnect($dbname);

//SoldTo is empty
if (empty($partner->soldto) || $partner->soldto == ''){$soldto_search = '%';} else {$soldto_search = '-%';}

//default whereclause
list($whereclause,$condition) = getWhereclauselvl2("attributes",$permission,$partner,'get');

//NEW ARRAY
$criterias = [];   
$clause = '';

//Check for $_GET variables and build up clause
if(isset($get_content) && $get_content!=''){    
    //GET VARIABLES FROM URL
    $requests = explode("&", $get_content);     
    //Check for keys and values
    foreach ($requests as $y){
        $v = explode("=", $y);
        //INCLUDE VARIABLES IN ARRAY
        $criterias[$v[0]] = $v[1]; 
    
        if ($v[0] == 'page' || $v[0] =='p' || $v[0] =='totals' || $v[0] =='list' || $v[0] =='media'|| $v[0] =='success_msg'){
        //do nothing
        }
        else {//create clause
            $clause .= ' AND pat.'.$v[0].' = :'.$v[0];
        }
    }
    //WHEN LIST = CATALOG change select based on GROUPS instead of ITEMS
    if ($criterias['list'] && $criterias['list'] == 'catalog'){
        $clause = str_replace('pat.group_id','pag.group_id',$clause);
    }
    //CREATE WHERE CLAUSE
    if ($whereclause == '' && $clause !=''){
        $whereclause = 'WHERE '.substr($clause, 4);
    } else {
        $whereclause .= $clause;
    }
} 
//Define Query
if(isset($criterias['totals']) && $criterias['totals'] ==''){
    //Request for total rows
    $sql = 'SELECT count(*) as count FROM products_attributes_items pat '.$whereclause.'';

} elseif ($criterias['media'] && $criterias['media'] =='all'){
    //GET ALL MEDIA ITEMS RELATED TO ATTRIBUTES
    $sql = 'SELECT pat.*, m.full_path, ma.full_path as alternative_media_full_path FROM products_attributes_items pat LEFT JOIN media m ON pat.item_media = m.rowID LEFT JOIN media ma ON pat.alternative_media = ma.rowID '.$whereclause;

} elseif ($criterias['list'] && $criterias['list'] =='catalog'){
    //GET ALL ATTRIBUTE DATA FOR CATALOG GROUPS,ITEMS,MEDIA
    $sql = 'SELECT pag.*, pat.*, m.title, m.full_path, ma.title as alternative_media_title, ma.full_path as alternative_media_full_path FROM products_attributes_groups pag JOIN products_attributes_items pat ON pag.rowID = pat.group_id LEFT JOIN media m ON pat.item_media = m.rowID LEFT JOIN media ma ON pat.alternative_media = ma.rowID '.$whereclause;
 
} else {
    //SQL for Paging 
    $sql = 'SELECT * FROM products_attributes_items pat '.$whereclause.' LIMIT :page,:num_products';
}

$stmt = $pdo->prepare($sql);

//Bind to query
if (str_contains($whereclause, ':condition')){
    $stmt->bindValue('condition', $condition, PDO::PARAM_STR);
}

if (!empty($criterias)){
    foreach ($criterias as $key => $value){
        $key_condition = ':'.$key;
        if (str_contains($whereclause, $key_condition)){
            if ($key == 'search'){
                $search_value = '%'.$value.'%';
                $stmt->bindValue($key, $search_value, PDO::PARAM_STR);
            }
            else {
                $stmt->bindValue($key, $value, PDO::PARAM_STR);
            }
        }
    }
}

//Add paging details
if(isset($criterias['totals']) && $criterias['totals']==''){
    $stmt->execute();
    $messages = $stmt->fetch();
    $messages = $messages[0];
}
elseif($criterias['media'] && $criterias['media'] =='all'){
      //Excute Query
      $stmt->execute();
      //Get results
      $messages = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
elseif($criterias['list'] && $criterias['list'] =='catalog'){
    //Excute Query
    $stmt->execute();
    //Get results
    $messages = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
else {
    $current_page = isset($criterias['p']) && is_numeric($criterias['p']) ? (int)$criterias['p'] : 1;
    $stmt->bindValue('page', ($current_page - 1) * $page_rows_products_attributes, PDO::PARAM_INT);
    $stmt->bindValue('num_products', $page_rows_products_attributes, PDO::PARAM_INT);

    //Excute Query
    $stmt->execute();
    //Get results
    $messages = $stmt->fetchAll(PDO::FETCH_ASSOC);
}

//------------------------------------------
//JSON_ENCODE
//------------------------------------------
$messages = json_encode($messages, JSON_UNESCAPED_UNICODE);

//Send results
echo $messages;

?>