<?php

$json = file_get_contents('php://input');   
    $obj = json_decode($json);
    $id = $obj->flag;
    $msg;
    
    // Returns a Json
    header('Content-Type: application/json');
    
    switch($id){
        
        case '0':
            $conn = new mysqli("directoriofitness.com.mx","direct18","L4LL4v3M@3str@",$obj->bd);
            $conn -> set_charset("utf8");

            $array = [];
            $arrayPagos = [];
            $cont = 0;
            $queryLocker = "SELECT 
                    locker.num_locker,
                    lockers_socios.Lockers_idLocker,
                  	lockers_socios.Socios_idSocio as 'id', 
                    lockers.idLocker as 'id_gl', 
                    locker.id_locker as 'idlk_c', 
                    CONCAT(socios.Nombre, ' ', socios.Apellidos) as 'name', 
                    lockers.Clave as 'grupo',
                    locker.num_locker as 'num_locker', 
                    lockers_socios.FechaAsignacion as 'pago', 
                    lockers_socios.FechaVencimiento, 
                    lockers_socios.concepto_pago, 
                    lockers_socios.importe as 'total', 
                    lockers_socios.estado,
                    lockers_socios.cancelado,
                    sucursal.NombreSucursal as 'sucursal',
                    lockers_socios.locker_asignado,
                    lockers.*,
                    IF(lockers_socios.FechaVencimiento < DATE_FORMAT(NOW(), '%d/%m/%Y') 
                		AND lockers_socios.cancelado = 0 AND lockers_socios.estado = 1, 'TRUE', 'FALSE') as 'vencimiento',
                	DATE_FORMAT(lockers_socios.FechaVencimiento, '%e-%c-%Y') AS Fecha_Vencimiento
                FROM 
                    lockers_socios, 
                    socios, 
                    lockers, 
                    sucursal, 
                    locker 
                WHERE 
                    lockers_socios.Lockers_idLocker = lockers.idLocker 
                    AND lockers_socios.Socios_idSocio = socios.idSocio 
                    AND lockers_socios.sucursal_idSucursal = sucursal.idSucursal 
                    AND socios.idSocio = $obj->id
                    AND lockers_socios.locker_asignado = locker.id_locker
                    AND lockers_socios.cancelado = 0
                ORDER BY 
                    lockers_socios.id_lockers_socios DESC LIMIT 1";
                    
                $resultado = $conn->query($queryLocker);
                if($resultado->num_rows > 0){
                    while($row = $resultado->fetch_assoc()){
                        if($row['estado'] == '1'){
                            if($row['costo_dia'] != null){
                            
                                $arrayPagos[] = ['id' => "$cont",'nombre'=>'Dia','costo'=>$row['costo_dia'], 'tipo' => '1'];
                                $cont++;
                                
                            }
                            if($row['costo_sem'] != null){
                                $arrayPagos[] = ['id' => "$cont",'nombre'=>'Semanal','costo'=>$row['costo_sem'], 'tipo' => '2'];
                                $cont++;
                                
                            }
                            if($row['costo_quinc'] != null){
                                $arrayPagos[] = ['id' => "$cont",'nombre'=>'Quincenal','costo'=>$row['costo_quinc'], 'tipo' => '3'];
                                $cont++;
                                
                            }
                            if($row['costo_mes'] != null){
                                $arrayPagos[] = ['id' => "$cont",'nombre'=>'Mensual','costo'=>$row['costo_mes'], 'tipo' => '4'];
                                $cont++;
                                
                            }
                            if($row['costo_semest'] != null){
                                $arrayPagos[] = ['id' => "$cont",'nombre'=>'Semestral','costo'=>$row['costo_semest'], 'tipo' => '5'];
                                $cont++;
                                
                            }
                            if($row['costo_anyo'] != null){
                                $arrayPagos[] = ['id' => "$cont",'nombre'=>'Anual','costo'=>$row['costo_anyo'], 'tipo' => '6'];
                                $cont++;
                                
                            }
                            $array[] = ['locker'=> $row['Lockers_idLocker'], 'num_locker'=> $row['num_locker'], 'FechaVencimiento' => $row['Fecha_Vencimiento'], 
                            'concepto_pago' => $row['concepto_pago'], 'vencimiento' => $row['vencimiento'], 'locker_asignado' => $row['locker_asignado'],
                            'sucursal' => $row['sucursal_idSucursal'],'pagos' => $arrayPagos];
                            
                            echo json_encode($array);
                        }else{
                            $array[] = ['locker'=> 'Sin Locker', 'num_locker'=> 'Sin Locker', 'FechaVencimiento' => 'Sin Locker',
                            'concepto_pago' => 'Sin Locker', 'vencimiento' => 'Sin Locker', 'locker_asignado' => 'Sin Locker',
                            'sucursal' => 'Sin Locker','pagos' => 'no hay'];
                            echo json_encode($array);
                        }
                        
                    }
                    //echo $conn->jsonQuery($queryLocker, "directoriofitness.com.mx","direct18","L4LL4v3M@3str@",$obj->bd);
                    //echo json_encode($array);
                }else{
                     $array[] = ['locker'=> 'Sin Locker', 'num_locker'=> 'Sin Locker', 'FechaVencimiento' => 'Sin Locker',
                            'concepto_pago' => 'Sin Locker', 'vencimiento' => 'Sin Locker', 'locker_asignado' => 'Sin Locker',
                            'sucursal' => 'Sin Locker','pagos' => 'no hay'];
                            
                    echo json_encode($array);
                }
                $conn->close();
            break;
    }

?>