<?php 
session_start();
error_reporting(E_ERROR | E_WARNING | E_PARSE);

function formatearFechaHora($fecha) {
	return date('d/m/Y - h:i A', strtotime($fecha));
}

// echo '<br /><br /><br /><br /><br />';
//datos de login
$_host =		 'localhost';
$_usuario =	 	 'jcpack_sistema';
$_contrasena =	 'C[kL2eqSsqN*';
$_nombre =		 'jcpack_sistema';
$_db =			 null;
$_db = new PDO("mysql:host=$_host;dbname=$_nombre;charset=utf8", $_usuario, $_contrasena);
$_db->exec("SET NAMES UTF8");
$_db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$estatus;

//si intentas logearte
if($_POST["login"]) {
	$_nombreUsuario = $_POST['nombreUsuario'];
	
	//consulta para ver si el usuario existe
	$sth = $_db->prepare("SELECT id, nombre, apellidos, email, sitio, nombreUsuario, salt, contrasena, puesto FROM usuarios WHERE nombreUsuario = ? and ext = 1 LIMIT 1");
	$sth->bindParam(1, $_nombreUsuario);
	if(!$sth->execute()) throw New Exception();
	$datos = $sth->fetch();

	//comparar si la contrase�a corresponde al usuario que introduciste
	if (hash("sha256", $_POST["contrasena"].$datos['salt']) == $datos['contrasena']) {
		//para comparar si ya est�s logeado
		$_SESSION['login_flag'] = 1;
		$_SESSION['login_usuario_xpress'] = 1;
		$_SESSION['login_id'] = $datos['id'];
		$_SESSION['login_nombreUsuario'] = $datos['nombreUsuario'];
		$_SESSION['login_nombre'] = $datos['nombre'];
		$_SESSION['login_apellidos'] = $datos['apellidos'];
		$_SESSION['login_correo'] = $datos['email'];
		$_SESSION['login_sitio'] = $datos['sitio'];
		$_SESSION['login_puesto'] = $datos['puesto'];
	} else {
		//si el te equivocas con la contrase�a
		$mensajes[] = "Contrase&ntilde;a incorrecta";
	}
	//si el dato de la contrase�a est� vac�o
} elseif (isset($_POST["login"]) && !empty($_POST['nombreUsuario']) && empty($_POST['contrasena'])) {
    $mensajes[] = "Campo de contrase&ntilde;a vacio";
}

//cerrar sesi�n
if ($_GET['cs'] == 1) {
	setcookie('sId', '', time()-60*60*24*365);
	
    $_SESSION = array();
	session_regenerate_id(); 
	//borrar datos de sesi�n
    session_destroy();
    header("Location:./tracking.php");
}//Si no est�s logeado, te manda a un login
if(isset($_POST['pdfRegresos'])){
    //convertir a PDF
    require_once('plugins/tcpdf/tcpdf.php');
	$pdf = new tcpdf(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
	$pdf->SetCreator(PDF_CREATOR);
	$pdf->SetAuthor('BLS CRM');
	$pdf->SetTitle('BLS CRM');
	$pdf->SetSubject('BLS CRM');
	$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
	$pdf->SetFont('helvetica', '', 10);
	$pdf->AddPage();

	//traer los bols de cierto id
	$sth = $_db->prepare("SELECT reg.id, reg.fecha_solicitud, reg.fecha_inicial, reg.fecha_reprogramacion, reg.fecha_transferencia, reg.fecha_bodega,
    reg.fecha_entrega, reg.fecha_cancelacion, reg.localidad, reg.observaciones, rec.orden, rec.nombre_cliente,
    CONCAT(us.nombre, ' ', us.apellidos) as chofer, s.id as id_status FROM regresos reg LEFT JOIN recolecciones rec
    ON reg.recoleccion_id = rec.id LEFT JOIN status_recoleccion s ON reg.status_id = s.id LEFT JOIN usuarios us ON
    reg.user_recoleccion = us.id WHERE orden = ? ORDER BY reg.fecha_solicitud ASC, reg.fecha_inicial ASC");
	$sth->bindParam(1, $_POST['ordenId']);
	if(!$sth->execute()) throw New Exception();
	$datos = $sth->fetch();

	switch($datos['id_status']) {
		case 0: $status = 'PENDIENTE'; break;
		case 1: $status = 'EN TIEMPO'; break;
		case 2: $status = 'EN BODEGA'; break;
		case 3: $status = 'EN TRASNFERENCIA'; break;
        case 4: $status = 'ENTREGADO A ULINE'; break;
        case 6: $status = 'CANCELADA'; break;
		case 7: $status = 'INTENTO DE RECOLECCI�N'; break;
	}

	//consulta para traer los datos del historial de estatus
	$statusquery = $_db->prepare("SELECT id_regreso, fecha, s.status as status, nombre, responsable from historia_status h, status_recoleccion s where s.id = h.status and id_regreso = ? and activo = 1 order by fecha desc");

	$statusquery->bindParam(1, $_POST['regresosId'] );
	//ejecutar consulta para historial de estatus
	if(!$statusquery->execute()) throw New Exception();
	
    while ($datos3 = $statusquery->fetch()) {
        $datosStatus = array(
            'regreso' => $datos3['id_regreso'],
            'fecha' => mb_strtoupper($datos3['fecha'], 'UTF-8'),
            'status' => mb_strtoupper($datos3['status']),
            'responsable' => mb_strtoupper($datos3['nombre']),
            'observaciones' => mb_strtoupper($datos3['responsable'])
        );
        $datosdelstatus[] = $datosStatus;
    }
	
	$orden = $datos['orden'];
	$customer = $datos['nombre_cliente'];
	$date = $datos['fecha_solicitud'];
	$location = $datos['localidad'];
	$driver = $datos['chofer'];
    
    $html = '<style>
    *{
        margin:0;
        padding:0;
    }
    .headerButton{
        height: 40px;
        border: none;
        margin-top: 34px;
        background: #fff;  /* fallback for old browsers */
        border: 1px 1px 1px #111 solid;
        border-radius: 50%;
        transition: 0.6s;
    }
    .headerButton:hover{
        background: #ccc;
        
    }
    .headerImg{
        height: 40px;
        border-radius: 50%;
    }
    .headerInput{
        margin-top:40px;
        width: 120px;
        height: 38px;
        margin-left: 8px;
        border-radius: 3px;        
        border: 1px solid #ccc;        
        padding:10px;
    }
    .headerInput::placeholder{
        font-size: 12pt;
        color : #3E4095;
        font-weight: 600;
        text-align: center;
    }
    .headerSesion{
        margin-top:40px;
        margin-left: 50%;
    }
    .bodyHeader{
        margin-top: 20px;
        border: #ccc solid 1px;
        border-radius:7px;
        width: 50%;
        margin-left:25%;
        margin-right:25%;
        height: 150px;
    }
    .bodyHeaderHeader{
        width: calc(100% - 20%);
        margin-left: 10%;
        margin-right: 10%;
        margin-top: 10px;
    }
    .headers{
        color: #aaa;
        width: 50%;
        float: left;
        position: relative;
        font-size: 15pt;
    }
    .headersValue{
        font-size: 14pt;
        font-weight: bold;
        width: 50%;
        float: left;
        position: relative;
    }
    .bodies{
        width: 33.33%;
        position: relative;
        float: left;
    }
    .bodiesValue{
        font-weight: bold;
        font-size: 20pt;
        color: #4CAF50;
    }
    .bodiesValueCancel{
        font-weight: bold;
        font-size: 20pt;
        color:#F78F8F;
    }
    .bodyHeaderFooter{
        width: 100%;
        float: left;
        position: relative;
    }
    .footerButton{
        margin-top: 5px;
        width: 100%;
        height: 20px;
        font-size: 13px;
        text-align: left;
        padding-left: 10px;
        background: #fff;
        border: #ccc 1px solid;
    }
    .bodyHistorial{
        border: 1px solid #ccc;
        width: 100%;
        position: relative;
        float: left;
    }
    .bodyFotos{
        border: 1px solid #ccc;
        width: 100%;
        position: relative;
        float:left;
    }
   
    .historialClm{
        width: 25%;
        position: relative;
        float: left;
        background: #bbb;
    }
    .historial{
        position: relative;
        float: left;
        border: 1px solid #ccc;
        width: 100%;
    }
    .bodyFotoDate{
        position: relative;
        width: 100%;
        float: left;
        font-size: 20pt;
        text-align: center;
        font-weight: bold;
        color:#4CAF50;
    }
    </style>
        <body>
            <h1 style="color:#555; text-align:center">Orden 24993101</h1>
            <table style="border: 1px solid #aaa;">
                <tbody>
                    <tr style="border-bottom:1px solid #aaa">
                        <td>

                            <div class="headers" style="width:25%; float:left; position:relative">
                                Order
                            </div>
                        </td>
                        <td>
                            <div class="headers" style="width:25%; float:left; position:relative">
                                Location
                            </div>
                        </td>
                        <td>
                            <div class="headers" style="width:25%; float:left; position:relative">
                                Customer
                            </div>
                        </td>
                        <td>
                            <div class="headers" style="width:25%; float:left; position:relative">
                                Driver
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <div class="headersValues" style="width:25%; float:left; position:relative; font-size:16pt">
                            '.$orden.'
                            </div>
                        </td>
                        <td>
                            <div class="headersValues" style="width:25%; float:left; position:relative; font-size:14pt">
                                '.$location.'
                            </div>
                        </td>
                        <td>
                            <div class="headersValues" style="width:25%; float:left; position:relative; font-size:14pt">
                                '.$customer.'
                            </div>
                        </td>
                        <td>
                            <div class="headersValues" style="width:25%; float:left; position:relative; font-size:14pt">
                                '.$driver.'
                            </div>
                        </td>
                    </tr>
                
                    <tr>
                        <td>';
                        switch($status) {
                            case 'PENDIENTE': $html .= '<img src="warning.png" style="margin-left:10px; margin-top:50px">';
                            break;
                            case 'EN RUTA': $html .= '<img src="caja.png" style="margin-left:10px; margin-top:50px">';
                            break;
                            case 'ENTREGADO A ULINE':$html .= '<img src="check.png" style="margin-left:10px; margin-top:50px">';
                            break;
                            case 'CANCELADA':$html .= '<img src="cancel.png" style="margin-left:10px; margin-top:50px">';
                            break;
                            case 'EN TRANSFERENCIA':$html .= '<img src="caja.png" style="margin-left:10px; margin-top:50px">';
                            break;
                            case 'INTENTO DE RECOLECCI�N': $html .= '<img src="warning.png" style="margin-left:10px; margin-top:50px">'; break;
                        }
                        $html.='
                        </td>
                        <td>'.$status.'
                        </td>
                    </tr>
                    <tr>
                        <td style="font-size:14pt;">
                            <div style="font-size:14pt; color:#bbb">
                            </div>
                            
                        </td>
                        <td style="font-size:14pt;">
                            <div style="font-size:14pt; color:#bbb">
                            </div>
                        </td>
                    </tr>
                </tbody>
            </table>
            <h1 style="color:#555">Historial</h1>';
            foreach($datosdelstatus as $status){
                $chofer = $status['responsable'];
                $observaciones = $status['observaciones'];
                $fecha = $status['fecha'];
                $statuschido = $status['status'];
                switch($status['status']) {
                    case 'PENDIENTE': $status .= '<div class="bodiesValue" style="width: 100%; color:#C5AB61">PENDIENTE</div>';
                    break;
                    case 'EN TRANSFERENCIA': $status .= '<div class="bodiesValue" style="width: 100%; color:#C5AB61">EN TRANSFERENCIA</div>';
                    break;
                    case 'EN BODEGA': $status .='<div class="bodiesValue" style="width: 100%; color:#C5AB61">EN BODEGA</div>'; break;
                    case 'ENTREGADO A ULINE': $status .= '<div class="bodiesValue" style="width: 100%">ENTREGADA</div>';
                    break;
                    case 'CANCELADO': $status .= '<div class="bodiesValue" style="width: 100%; color:#F78F8F">CANCELADA</div>';
                    break;
                    case 'INTENTO DE ENTREGA': $status .= '<div class="bodiesValue" style="width: 100%; color:#C5AB61">INTENTO DE RECOLECCI�N</div>';
                    break;
                }
                $html.= '<table style="border: 1px solid #aaa;"><tr>
                <td>  
                </td>
                <td>
                    '.$status.'
                </td>
            </tr>
            <tr>
                <td>
                    <div class="headers" style="width: 50%">
                    Chofer asignado: <div class="headers" style="color: #111;">
                                '.$chofer.'
                            </div>
                    </div>
                </td>
                <td>
                    <div class="headers" style="width: 50%">
                        Observaciones: <div class="headers" style="color: #111;">'.$observaciones.'</div>
                    </div>
                </td>
            </tr>
            <tr>
                <td style="border-bottom:1px solid #aaa">
                    <div class="headers" style="width: 50%; font-size:12pt">
                        '.$fecha.'
                    </div>
                </td>
                <td style="border-bottom:1px solid #aaa">
                </td>
            </tr>';
            }
            $html.='
            </table>
        </br > </body></html>';
        // echo $html;
        $pdf->writeHTML($html, true, false, true, false, '');
        $pdf->Output('xd.pdf', 'FI');
        exit();
}
if (isset($_POST['pdf'])) {
    //convertir a PDF
        require_once('plugins/tcpdf/tcpdf.php');
        $pdf = new tcpdf(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
        $pdf->SetCreator(PDF_CREATOR);
        $pdf->SetAuthor('BLS CRM');
        $pdf->SetTitle('BLS CRM');
        $pdf->SetSubject('BLS CRM');
        $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
        $pdf->SetFont('helvetica', '', 10);
        $pdf->AddPage();
    
        //traer los bols de cierto id
        $sth = $_db->prepare("SELECT * FROM bols WHERE id = ?");
        $sth->bindParam(1, $_POST['id']);
        if(!$sth->execute()) throw New Exception();
        $datos = $sth->fetch();
        $statusquery = $_db->prepare("SELECT * FROM historia_bols WHERE bol = ? and activo = 1 order by fecha desc limit 1");
        $statusquery->bindParam(1, $_POST['id']);
        if(!$statusquery->execute()) throw New Exception();

        $consult_for_image = $_db->prepare("SELECT order_d FROM bols WHERE id = ?");
        $consult_for_image->bindParam(1, $_POST['id']);
        $consult_for_image->execute();
        if(!$statusquery->execute()) throw New Exception();
        $re = $consult_for_image->fetchColumn();
        $sthImagenes = $_db->prepare("SELECT b.id, bi.imagen, bi.fecha, bi.id_bol 
                                    FROM bols_imagenes bi 
                                    JOIN bols b ON b.id = bi.id_bol 
                                    WHERE b.order_d = ? ORDER BY b.id DESC");
        $sthImagenes->bindParam(1, $re);
        $sthImagenes->execute();
            
            $imagenes = array();
            $x=0;
            while ($datos2 = $sthImagenes->fetch()) {
                $imagenes[$x]['imagen'] = $datos2['imagen'];
                $imagenes[$x]['fecha'] = $datos2['fecha'];
                $x++;
            }

        $choferquery = $_db->prepare("SELECT * FROM usuarios WHERE id = ?");
        $choferquery->bindParam(1, $datos['id_chofer']);
        $choferquery->execute();
        if($choferxd = $choferquery->fetch()){
            $chofer = mb_strtoupper($choferxd['nombre'].' '.$choferxd['apellidos']);
        }
        
        
    
        switch($datos['status']) {
            case 0: $status = 'PENDIENTE'; break;
            case 1: $status = 'EN RUTA'; break;
            case 2: $status = 'ENTREGADA'; break;
            case 3: $status = 'CANCELADA'; break;
            case 4: $status = 'RECHAZADA'; break;
            case 7: $status = 'INTENTO DE ENTREGA'; break;
        }
    
        switch($datos['motivo_rechazo']) {
            case 1: $motivo = 'Falta de PO'; break;
            case 2: $motivo = 'Falta de Factura'; break;
            case 3: $motivo = 'Material equivocado'; break;
            case 4: $motivo = 'Mal direccionada'; break;
            case 5: $motivo = 'Domicilio cerrado'; break;
            case 6: $motivo = 'Cliente cancel  la orden'; break;
            case 7: $motivo = 'Personal de recibos no disponible'; break;
            case 8: $motivo = 'Uline cancel  la orden'; break;
            case 9: $motivo = 'Error en la factura'; break;
            case 10: $motivo = 'Error en el PO'; break;
            case 11: $motivo = 'No se alcanz  la entrega por demora con otro cliente'; break;
            case 12: $motivo = 'No recibe por inventario'; break;
            case 13: $motivo = 'Problemas de tr nsito'; break;
            case 14: $motivo = 'No se recibi  material en JC'; break;
            case 15: $motivo = 'Accidente vehicular'; break;
            case 19: $motivo = 'Fuerza mayor'; break;
            default: $motivo = '';
        }
    
        $pro = $datos['pro'];
        $order_d = $datos['order_d'];
        $cust_num = $datos['cust_number'];
        $cust_po = $datos['cust_po'];
        $package_id = $datos['package_id'];
        $to_name = $datos['to_name'];
        //$motivo  = $datos['motivo'];

  
        //estilos para el pdf
        $html.= '<style>
        *{
            margin:0;
            padding:0;
        }
        .headerButton{
            height: 40px;
            border: none;
            margin-top: 34px;
            background: #fff;  /* fallback for old browsers */
            border: 1px 1px 1px #111 solid;
            border-radius: 50%;
            transition: 0.6s;
        }
        .headerButton:hover{
            background: #ccc;
            
        }
        .headerImg{
            height: 40px;
            border-radius: 50%;
        }
        .headerInput{
            margin-top:40px;
            width: 120px;
            height: 38px;
            margin-left: 8px;
            border-radius: 3px;        
            border: 1px solid #ccc;        
            padding:10px;
        }
        .headerInput::placeholder{
            font-size: 12pt;
            color : #3E4095;
            font-weight: 600;
            text-align: center;
        }
        .headerSesion{
            margin-top:40px;
            margin-left: 50%;
        }
        .bodyHeader{
            margin-top: 20px;
            border: #ccc solid 1px;
            border-radius:7px;
            width: 50%;
            margin-left:25%;
            margin-right:25%;
            height: 150px;
        }
        .bodyHeaderHeader{
            width: calc(100% - 20%);
            margin-left: 10%;
            margin-right: 10%;
            margin-top: 10px;
        }
        .headers{
            width: 50%;
            float: left;
            position: relative;
            font-size: 11pt;
        }
        .headersValue{
            font-size: 14pt;
            font-weight: bold;
            width: 50%;
            float: left;
            position: relative;
        }
        .bodies{
            width: 33.33%;
            position: relative;
            float: left;
        }
        .bodiesValue{
            font-weight: bold;
            font-size: 20pt;
            color: #4CAF50;
        }
        .bodiesValueCancel{
            font-weight: bold;
            font-size: 20pt;
            color:#F78F8F;
        }
        .bodyHeaderFooter{
            width: 100%;
            float: left;
            position: relative;
        }
        .footerButton{
            margin-top: 5px;
            width: 100%;
            height: 20px;
            font-size: 13px;
            text-align: left;
            padding-left: 10px;
            background: #fff;
            border: #ccc 1px solid;
        }
        .bodyHistorial{
            border: 1px solid #ccc;
            width: 100%;
            position: relative;
            float: left;
        }
        .bodyFotos{
            border: 1px solid #ccc;
            width: 100%;
            position: relative;
            float:left;
        }
       
        .historialClm{
            width: 25%;
            position: relative;
            float: left;
            background: #bbb;
        }
        .historial{
            position: relative;
            float: left;
            border: 1px solid #ccc;
            width: 100%;
        }
        .bodyFotoDate{
            position: relative;
            width: 100%;
            float: left;
            font-size: 12pt;
            text-align: center;
            font-weight: bold;
            color:#4CAF50;
        }
        .tableBorderWhite{
            border: 1px solid #fff; 
            text-align:left; 
            display:table;
        }
        .headerPDFInfoTitle{
            color:#f36b07; 
            font-size:16pt;
        }
        .headerPDFInfo{
            color:#404291;
            font-size:12pt;
        }
        .estadoPENDIENTE{
            font-size:15pt;
            color: #fff;
            weight:600;
        }
        .estadoENTREGADA{
            font-size:15pt;
            color: #fff;
            background-color: #4CAF50;
            weight:600;
        }
        .estadoCANCELADA{
            font-size:15pt;
            color: #fff;
            background-color: #F78F8F;
            weight:600;
        }

        .historialHeader{
            font-size:15pt;
            color: #fff;
            background-color: #f98b3b;
            weight:600;
        }

        .cuadroEntregado{
            background-color:#d3fea1;
            border: solid 1px #a9da6f;
            border-radius:50px;
            margin: 10px;
        }

        .cuadroCancelado{
            background-color:#f9dada;
            border: solid 1px #F78F8F;
            border-radius:50px;
            margin: 10px;
        }

        .cuadroPendiente{
            background-color:#fae6ae;
            border: solid 1px #C5AB61;
            border-radius:50px;
            margin: 10px;
        }

        </style>';

        $html .= '
        <body>
            <table>
                <tbody>
                    <tr style="border: solid 1px #000;">
                        <td>
                        <span class="headerPDFInfoTitle">JC Pack  Soluciones Logisticas</span>
                        <br/>
                        <span class="headerPDFInfo">Parque Industrial Girasoles 2<br/>Aguila Azteca #19308 Int 3, Tijuana, B.C.<br/>Horario 6am - 6pm<br/>625-56-33</span>
                        </td>
                        <td style="text-align:right; border: 1px solid #fff;">
                            <img src="http://www.jc-pack.net/images/logo.png" style="width:100px;">
                        </td>
                    </tr>
                </tbody>
            </table>
                <div></div>
            <table style="border: 1px solid #aaa;">
                <tbody>
                    <tr>
                    ';
                    switch($status) {
                        case 'PENDIENTE': $html .= '<th class="estadoPENDIENTE" colspan="3">&nbsp;<b>ESTADO: PENDIENTE</b></th>';
                        break;
                        case 'EN RUTA': $html .= '<th class="estadoPENDIENTE" colspan="3">&nbsp;<b>ESTADO: EN RUTA</b></th> ';
                        break;
                        case 'ENTREGADA':$html .= '<th class="estadoENTREGADA" colspan="3">&nbsp;<b>ESTADO: ENTREGADA</b></th>';
                        break;
                        case 'CANCELADA':$html .= '<th class="estadoCANCELADA" colspan="3">&nbsp;<b>ESTADO: CANCELADA</b></th>';
                        break;
                        case 'RECHAZADA':$html .= '<th class="estadoCANCELADA" colspan="3">&nbsp;<b>ESTADO: RECHAZADA</b></th>';
                        break;
                        case 'INTENTO DE ENTREGA': $html .= '<th class="estadoPENDIENTE" colspan="3">&nbsp;<b>ESTADO: INTENTO DE ENTREGA</b></th>'; break;
                    }
                    $html.=
                    '                        
                    </tr>
                    <tr style="border:1px solid #aaa;">
                        <td>
                            <span class="headers" >
                                Orden: &nbsp; <b>'.$order_d.'</b>
                            </span>
                        </td>
                        <td>
                            <span class="headers" style=" float:left; padding:20px; position:relative;">PRO: &nbsp;<b>'.$pro.'</b>
                            </span>
                        </td>
                        <td>
                            <span class="headers" style=" float:left; position:relative;">Cliente PO: &nbsp;<b>'.$cust_po.'</b></span>
                        </td>
                    </tr>
                    <tr style="border:1px solid #aaa;">
                        <td>
                            <span class="headers" style="width:30%; float:left; padding:20px; position:relative;">
                                No. de cliente: &nbsp;<b>'.$cust_num.'</b>
                            </span>
                        </td>
                        <td>
                            <span class="headers" style="width:30%; float:left; padding:20px; position:relative;">
                                ID Paquete: &nbsp;<b>'.$package_id.'</b>
                            </span>
                        </td>
                    </tr>
                    <tr style="border:1px solid #aaa;">
                        <td colspan="2">
                            <span class="headers" style="width:60%; float:left; position:relative;">
                                Destino: &nbsp;<b>'.$to_name.'</b>
                            </span>
                        </td>
                    </tr> 
                    <tr>
                        <td >
                        ';
                        switch($status) {
                            case 'PENDIENTE': $html .= '<div class="cuadroPendiente" > 
                            <div class="bodiesValue" ><img src="warning.png" style="width:35px;">&nbsp;'.$status.'</div>                                
                        </div>';
                            break;
                            case 'EN RUTA': $html .= '<div class="cuadroPendiente" > 
                            <div class="bodiesValue" style="text-align:center;"><img src="check.png" style="width:100%;">&nbsp;'.$status.'</div>                                
                        </div>';
                            break;
                            case 'ENTREGADA':$html .= '
                            <div  style="text-align:center; padding-bottom:20px !important;" ><img src="btnEntrega_pdf.png" style="width:100px;">
                            </div>';
                            break;
                            case 'CANCELADA':$html .= '<div  style="color:#F78F8F;" ><img src="cancel.png" style="width:20px;">'.$status.'</div>';
                            break;
                            case 'RECHAZADA':$html .= '<div class="cuadroCancelado" > 
                            <div class="bodiesValue" ><img src="check.png" style="width:35px;">&nbsp;'.$status.'</div>                                
                        </div>';
                            break;
                            case 'INTENTO DE ENTREGA': $html .= '<div class="cuadroPendiente" > 
                            <div class="bodiesValue" ><img src="check.png" style="width:35px;">&nbsp;'.$status.'</div>                                
                        </div>'; break;
                        }
                        $html.=
                        '
                        </td>
                        <td colspan="2">
                        ';
                        switch($status) {
                            case 'PENDIENTE': $html .= '
                            <div class="headers" style="color:#aaaaaa; text-align:right;text-decoration: underline; margin-right:30px !important; position:relative;">&nbsp;Motivo de Cancelación / Rechazo<b>&nbsp;'.$motivo.'</b></div>';
                            break;
                            case 'EN RUTA': $html .= '
                            <div class="headers" style="color:#aaaaaa; text-align:right;text-decoration: underline; margin-right:30px !important; position:relative;">&nbsp;Motivo de Cancelación / Rechazo<b>&nbsp;'.$motivo.'</b></div>';
                            break;
                            case 'ENTREGADA':$html .= '<div></div>';
                            break;
                            case 'CANCELADA':$html .= '<div class="headers" style="color:#aaaaaa; text-align:right;text-decoration: underline; margin-right:30px !important; position:relative;">&nbsp;Motivo de Cancelación / Rechazo<b>&nbsp;'.$motivo.'</b></div>  ';
                            break;
                            case 'RECHAZADA':$html .= '<div class="headers" style="color:#aaaaaa; text-align:right;text-decoration: underline; margin-right:30px !important; position:relative;">&nbsp;Motivo de Cancelación / Rechazo<b>&nbsp;'.$motivo.'</b></div>  ';
                            break;
                            case 'INTENTO DE ENTREGA': $html .= '<div class="headers" style="color:#aaaaaa; text-align:right;text-decoration: underline; margin-right:30px !important; position:relative;">&nbsp;Motivo de Cancelación / Rechazo<b>&nbsp;'.$motivo.'</b></div>';
                            break;
                        }
                        $html.=
                        '                        
                        </td>                       
                    </tr>
                </tbody>
            </table>
            <div></div>
            <table style="border: 1px solid #aaa;">
                <tr> 
                    <th class="historialHeader" colspan="3">&nbsp;<b>DATOS DE LA ENTREGA</b>
                    </th>
                </tr>
            ';
            while($datos2 = $statusquery->fetch()){
                $datosdelstatus[] = $datosStatus = array(
                    'status' => mb_strtoupper ( $datos2['status'], 'UTF-8'),
                    'fecha' => mb_strtoupper($datos2['fecha'], 'UTF-8'),
                    'nombre' => mb_strtoupper($datos2['nombre'], 'UTF-8'),
                    'observaciones' =>  mb_strtoupper($datos2['responsable'], 'UTF-8')
                );
            }
            foreach($datosdelstatus as $pkg_status){
                $chofer = $pkg_status['nombre'];
                $observaciones = $pkg_status['observaciones'];
                $fecha = $pkg_status['fecha'];
               /* switch( $pkg_status['status']) {
                    case 0: $pkg_status .= '<div class="bodiesValue" style="width: 33%; color:#C5AB61;">PENDIENTE</div>';
                    break;
                    case 1: $pkg_status .= '<div class="bodiesValue" style="width: 33%"; color:#C5AB61;">EN RUTA</div>';
                    break;
                    case 2: $pkg_status .= '<div class="bodiesValue" style="width: 33%;">ENTREGADA</div>';
                    break;
                    case 3: $pkg_status .= '<div class="bodiesValue" style="width: 33%; color:#F78F8F;">CANCELADA</div>';
                    break;
                    case 4: $pkg_status .= '<div class="bodiesValue" style="width: 33%; color:#F78F8F";>RECHAZADA</div>';
                    break;
                    case 7: $pkg_status .= '<div class="bodiesValue" style="width: 33%; color:#C5AB61;">INTENTO DE ENTREGA</div>';
                    break;
                }*/
               
            }
            $html.='
            </table>
        <div></div>
        <table >
        <tr> <th class="historialHeader" colspan="3">&nbsp;<b>PRUEBA DE ENTREGA</b></th></tr>
        </table>';          
      // if(file_exists('http://sistema.jc-pack.net/data/privada/imagenes/'.$imagenes['imagen'])){
            $html .= '
        <div class="bodyFotos" id="bodyFotos">';
            foreach ($imagenes as $imagens) {
                $fecha = $imagens['fecha'];
                $imagen = $imagens['imagen'];
                $fichero = 'http://sistema.jc-pack.net/data/privada/imagenes/'.$imagen;
                    $html.='
                        <img class="img-thumbnail" width="300" src="http://sistema.jc-pack.net/data/privada/imagenes/'.$imagen.'"/>
                        <span class="bodyFotoDate">'.$fecha.' </span>
                        ';  
            }
            $html .='</div></div>';
       //}
        $pdf->writeHTML($html, true, false, true, false, '');
        $pdf->Output($_POST['id'].'.pdf', 'FI');
        exit();
    }

?>
<html>

<head>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
        integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
        integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous">
    </script>
    <style>
    * {
        margin: 0;
        padding: 0;
    }

    body {
        display: flex;
        min-height: 100vh;
        flex-direction: column;
    }

    #loginHeader {
        color: #3E4095;
        margin-top: 20px;
        text-align: center;
        font-size: 28pt;
    }

    .header {
        /*background: #2C3E50;  /* fallback for old browsers */
        /*background: -webkit-linear-gradient(to right, #4CA1AF, #2C3E50);  /* Chrome 10-25, Safari 5.1-6 */
        /*background: linear-gradient(to right, #4CA1AF, #2C3E50); /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
        background: none;
        /* fallback for old browsers */
        border-bottom: 1px solid #eee;
        width: 100%;
        min-height: 120px;
        height: auto;
    }

    .footer {
        margin-top: auto;
        width: 100%;
        position: fixed;
        left: 0;
        right:0;
        bottom: 0;
        background: #fff;
        height: 120px;
        border-top: 1px solid #eee;
    }

    .headerElement {
        width: 100%;
        position: relative;
        float: left;
        display: flex;
        justify-content: end;
        align-items: center;

    }

    .headerSelect {
        margin-left: 15%;
        margin-right: 15%;
        margin-top: 0px;
    }

    .headerButton {
        width: 25px;
        height: 25px;
        border: none;
        background: #fff;
        /* fallback for old browsers */
        border: 1px 1px 1px #111 solid;
        border-radius: 50%;
        transition: 0.6s;
    }

    .headerButton:hover {
        background: #ccc;

    }

    .headerImg {
        height: 25px;
        border-radius: 50%;
    }

    .headerInput {
        width: 120px;
        height: 38px;
        margin-left: 8px;
        border-radius: 3px;
        border: 1px solid #ccc;
        padding: 10px;
    }

    .headerInput::placeholder {
        font-size: 10pt;
        color: #3E4095;
        font-weight: 600;
        text-align: center;
    }

    .headerSesion {
        margin-top: 40px;
        margin-left: 50%;
    }

    .bodyHeader {
        width: 50%;
        height: auto;
        margin-top: 20px;
        border: #ccc solid 1px;
        border-radius: 7px;
        margin-left: 25%;
        margin-right: 25%;

    }

    .bodyHeaderHeader {
        width: calc(100% - 20%);
        height: auto;
        margin-left: 10%;
        margin-right: 10%;
        margin-top: 10px;
    }

    .headers {
        color: #aaa;
        width: 100%;
        float: left;
        position: relative;
        font-size: 15pt;
    }

    .headersValue {
        font-size: 14pt;
        font-weight: bold;
        width: 100%;
        float: left;
        position: relative;
    }

    .bodies {
        width: 100%;
        position: relative;
        float: left;
    }

    .bodiesValue {
        font-weight: bold;
        font-size: 20pt;
        color: #4CAF50;
    }

    .bodiesValueCancel {
        font-weight: bold;
        font-size: 20pt;
        color: #F78F8F;
    }

    .bodiesButton {
        border: #f9f9f9 solid 1px;
        border-radius: 10%;
        background: none;
    }

    .bodiesButton:hover {
        background: #ccc;
    }

    .bodyHeaderFooter {
        width: 100%;
        float: left;
        position: relative;
    }

    .footerButton {
        margin-top: 5px;
        width: 100%;
        height: 20px;
        font-size: 13px;
        text-align: left;
        padding-left: 10px;
        background: #fff;
        border: #ccc 1px solid;
    }

    .bodyHistorial {
        border: 1px solid #ccc;
        width: 100%;
        position: relative;
        float: left;
        display: none;
    }

    .bodyFotos {
        width: 100%;
        border: 1px solid #ccc;
        position: relative;
        text-align: center;
        float: left;
        display: none;
    }

    .historialClm {
        width: 25%;
        position: relative;
        float: left;
        background: #bbb;
    }

    .historial {
        position: relative;
        float: left;
        border: 1px solid #ccc;
        width: 100%;
    }

    .bodyFotoDate {
        position: relative;
        width: 100%;
        float: left;
        font-size: 20pt;
        text-align: center;
        font-weight: bold;
        color: #4CAF50;
    }

    /*Extra small device*/
    @media (min-width: 320px) {

        .header {
            background: none;
            border-bottom: 1px solid #eee;
            width: 100%;
            min-height: 150px;
            height: auto;
        }

        .headerInput {
            width: 100px;
            height: 38px;
            margin-left: 8px;
            border-radius: 3px;
            border: 1px solid #ccc;
            padding: 10px;
        }

        .specialMargin {
            margin-bottom: 10px;
        }

        .bodyHeader {
            width: 90%;
            height: auto;
            margin-top: 20px;
            border: #ccc solid 1px;
            border-radius: 7px;
            margin-left: 5%;
            margin-right: 5%;
            margin-bottom: 200px;
        }

        .mobileView_cerrarSecion {
            top: -195px;
            left: 47%;
        }

        .mobileBtn-danger {
            top: -90px;
            right: 0px;
        }

        .mobileAlign {
            text-align: center;
        }

        .mobileMargin {
            margin-left: 38px;
        }

    }

    /* */
    @media (min-width: 500px) {
        .margin500px {
            margin-bottom: 20px;
        }

        .mobileView_cerrarSecion {
            position: absolute !important;
            top: 10px !important;
            left: 65% !important;
        }

        .headerInput {
            width: 85px;
            height: 38px;
            margin-left: 8px;
            border-radius: 3px;
            border: 1px solid #ccc;
            padding: 10px;
        }
    }


    /*  Small devices (landscape phones, 576px and up)*/
    @media (min-width: 576px) {
        .headerInput {
            width: 105px;
            height: 38px;
            margin-left: 8px;
            border-radius: 3px;
            border: 1px solid #ccc;
            padding: 10px;
        }

        .parent {
            height: auto;
        }

        .child {
            position: relative;
            top: 50%;
            transform: translateY(-50%; );
        }

        .mobileView_cerrarSecion {
            position: absolute !important;
            top: 10px !important;
            right: 0 !important;
        }
    }

    /* Medium devices (tablets, 768px and up)*/
    @media (min-width: 768px) {

        .specialMargin {
            margin-bottom: 0px;
        }

        .mobileView_cerrarSecion {
            position: static !important;
            top: 0px !important;
            right: 0px !important;
        }

        .mobileBtn-danger {}

        .mobileAlign {
            text-align: left;
        }

        .mobileMargin {
            margin: 15px 0px 0px 37px;
        }


    }

    @media (min-width: 900px) {
        .mobileMargin {
            margin: 0px 0px 0px 0px;
        }
    }

    /*Large devices (desktops, 992px and up)*/
    @media (min-width: 992px) {
        .headerInput {
            width: 100px;
            height: 38px;
            margin-left: 8px;
            border-radius: 3px;
            border: 1px solid #ccc;
            padding: 10px;
        }

        .bodyHeader {
            width: 50%;
            height: auto;
            margin-top: 20px;
            border: #ccc solid 1px;
            border-radius: 7px;
            margin-left: 25%;
            margin-right: 25%;

        }

        .mobileView_cerrarSecion {}

        .mobileBtn-danger {}

        .mobileMargin {
            margin: 0px 0px 0px 0px;
        }

    }

    @media (min-width: 1200px) {

        .header {
            background: none;
            border-bottom: 1px solid #eee;
            width: 100%;
            min-height: 120px !important;
            height: auto;
        }

        .headerInput {
            width: 110px;
            height: 38px;
            margin-left: 8px;
            border-radius: 3px;
            border: 1px solid #ccc;
            padding: 10px;
        }

    }
    </style>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
        integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <title>Tracking</title>
</head>

<body>


    <?php
if (!isset($_SESSION['login_flag'])) {
    ?>
    <div class="header">
        <div class="headerElement">
            <img src="http://www.jc-pack.net/images/logo.png" style="float:left; position:relative;" height="120"
                width="210">
            <h1 id="loginHeader">| Inicio de sesi&oacute;n</h1>
        </div>

    </div>
    <section style="background-color: #FFF; margin: 130px auto; width: 350px; text-align:center;">
        <div style="border:1px solid #aaa; height:auto; padding-top: 10px; padding-bottom:10px;">
            <fieldset>
                <legend style="color:#3E4095">Iniciar sesi&oacute;n</legend>
                <form method="post" action="">
                    <div>
                        <div class="admin-caja-separador">
                            <label for="nombreUsuario">Usuario</label>
                            <div class="admin-caja-texto" style="margin-bottom:25px;">
                                <input name="nombreUsuario" type="text" />
                            </div>
                        </div>

                        <div class="admin-caja-separador">
                            <label for="contrasena">Contrase&ntilde;a</label>
                            <div class="admin-caja-texto">
                                <input name="contrasena" type="password" />
                            </div>
                        </div>
                        <br />
                        <input type="hidden" class="btn btn-default" value="1" name="login" />
                        <input type="submit" class="btn btn-primary" value="Accesar" />
                    </div>
                </form>
            </fieldset>

            <?php
			if (!empty($mensajes)) {
				foreach ($mensajes as $mensaje) {
					echo '<div id="mensajes">' . $mensaje . '</div>';
				}
			}
			?>
        </div>
    </section>

    <?php } ?>


    <?php 
    if (isset($_SESSION['login_flag'])) {
        if(!isset($_POST['buscar'])  && !isset($_POST['buscarregresos'])){
    ?>
    <div class="header">
        <div class="col-12 col-sm-12 col-md-12 col-lg-12">
            <div class="row">
                <div class="col-6 col-sm-3 col-md-2 col-lg-2">
                    <img src="http://www.jc-pack.net/images/logo.png" width="100%">
                </div>
                <div class="col-12 col-sm-4 col-md-2 col-lg-2 parent specialMargin">
                    <div class="child">
                        <select class="form-control " id="headerSelect">
                            <option value="entregas" selected>Entregas</option>
                            <option value="pickup">Return pickups</option>
                        </select>
                    </div>
                </div>

                <div class="col-12 col-sm-12 col-md-6 col-lg-6 parent" id="entregasElement">
                    <div class="col-12 col-sm-12 col-md-12 col-lg-12 child margin500px">
                        <form action="" method="post">
                            <button class="headerButton" name="buscar" type="submit">
                                <img src="busqueda.png" class="headerImg">
                            </button>
                            <input type="text" class="headerInput specialMargin" placeholder="Pro" name="pro">
                            <input type="text" class="headerInput specialMargin " placeholder="Order" name="order">
                            <input type="text" class="headerInput mobileMargin" placeholder="Customer #"
                                name="customerNum">
                            <input type="text" class="headerInput" placeholder="Package ID" name="PackageId">
                        </form>
                    </div>
                </div>

                <div class="col-12 col-sm-12 col-md-6 col-lg-6 parent " style="display:none" id="pickupElement">
                    <form class="child" method="POST" action="">
                        <input type="hidden" value="1" id="idRegresos" />
                        <div class="col-12 col-sm-12 col-md-12 col-lg-12 child margin500px">
                            <button class="headerButton" name="buscarregresos" type="submit">
                                <img src="busqueda.png" class="headerImg">
                            </button>
                            <input type="text" class="headerInput" style="width:120px" placeholder="Order"
                                name="ordenRegresos">
                            <input type="text" class="headerInput" style="width:120px" placeholder="Date"
                                name="fechaRegresos">
                        </div>
                    </form>
                </div>
                <div class="col-12 col-sm-6 col-md-2 col-lg-2 parent mobileView_cerrarSecion">
                    <div class="child">
                        <a class="btn btn-danger child mobileBtn-danger" href="tracking.php?cs=1"> Cerrar sesi&oacute;n
                            <img src="logout.png" height="25px" /></a>
                    </div>
                </div>
            </div>

        </div>
    </div>
    <!-- section-->
    <div class="footer">
        <img src="http://www.jc-pack.net/images/logo.png" style="float:left; position: relative;" width="210"
            height="120" />
        <h5 style="color:#3E4095; font-weight: bold">625-56-33</h5>
        <h4 style="color:#F36B07">Horario 6am - 6pm</h4>
        <h5>Tijuana, B.C.</h5>
        <address style="margin-top: -5px; color: #999">Parque Industrial Girasoles 2,
            Aguila Azteca #19308 Int 3</address>
    </div>
    <?php }elseif(isset($_POST['buscar'])){
       //poblar variables
        $pro = $_POST['pro'];
        $order = $_POST['order'];
        $customerNum = $_POST['customerNum'];
        $PackageId = $_POST['PackageId'];
        //$customerPo = $_POST['customerPo'];

        //si product # no est� vac�o, se ejecutar� esta parte del c�digo
        if (!empty($pro)) {
            $sth = $_db->prepare("SELECT b.id, bi.imagen, bi.fecha, bi.id_bol
                FROM bols_imagenes bi
                JOIN bols b
                ON b.id = bi.id_bol
                WHERE b.pro = ?
                ORDER BY b.id DESC");
            $sth->bindParam(1, $pro);
            $sth->execute();
            
            $imagenes = array();
            $x=0;
            while ($datos = $sth->fetch()) {
                $imagenes[$x]['imagen'] = $datos['imagen'];
                $imagenes[$x]['fecha'] = $datos['fecha'];

                $x++;
            }

            $sth = $_db->prepare("SELECT b.id
                FROM bols b
                WHERE b.pro = ? ORDER BY b.id DESC LIMIT 1");
            $sth->bindParam(1, $pro);
            $sth->execute();
            $datos = $sth->fetch();
            $idBol = $datos['id'];
            
            //consulta para traer los datos del historial de estatus
            $statusquery = $_db->prepare("SELECT fecha, h.status as status, nombre, responsable from historia_bols h where h.bol = ? and activo = 1 order by fecha desc");
            $statusquery->bindParam(1, $idBol);
            //ejecutar consulta para historial de estatus
            if(!$statusquery->execute()) throw New Exception();
            $datosStatus = array();							
            while($datos3 = $statusquery->fetch()){
                switch($datos3['status']){
                    case 0: $status = 'PENDIENTE'; break;
                        case 1: $status = 'EN RUTA'; break;
                        case 2: $status = 'ENTREGADA'; break;
                        case 3: $status = 'CANCELADA'; break;
                        case 4: $status = 'RECHAZADA'; break;
                        case 7: $status = 'INTENTO DE ENTREGA'; break;
                }
                $nuevoMotivo = 'N/A';
                if($datos3['status'] == 7){
                    $motivo = $_db->prepare("SELECT descripcion FROM incidencias i, intento_bol ib where i.id = ib.incidencia and ib.fecha 
                in (SELECT h.fecha FROM historia_bols h where h.bol = ?) and ib.bol in
                 (SELECT h.bol FROM historia_bols h where h.bol = ?) and ib.fecha = ?");
                    $motivo->bindParam(1, $idBol);
                    $motivo->bindParam(2, $idBol);
                    $motivo->bindParam(3, $datos3['fecha']);
                    $motivo->execute();
                    if($datoMotivo = $motivo->fetch()){
                        $nuevoMotivo = $datoMotivo['descripcion'];
                    }
                }
                if($datos3['status'] == 3){
                    $motivo = $_db->prepare("SELECT descripcion FROM incidencias i, cancelacion_bol ib, asignacion_bol a where i.id = ib.incidencia_id and a.id = ib.asignacion_id and ib.fecha in (SELECT h.fecha FROM historia_bols h where h.bol = ?)
                     and a.bol_id in (SELECT h.bol FROM historia_bols h where h.bol = ?) and a.bol_id = ? and ib.fecha = ?");
                    $motivo->bindParam(1, $idBol);
                    $motivo->bindParam(2, $idBol);
                    $motivo->bindParam(3, $idBol);
                    $motivo->bindParam(4, $datos3['fecha']);
                    $motivo->execute();
                    if($datoMotivo = $motivo->fetch()){
                        $nuevoMotivo = $datoMotivo['descripcion'];
                    }
                }
                
                
                    $datosStatus = array (
                        'fecha' => mb_strtoupper($datos3['fecha'], 'UTF-8'),
                        'status' => $status,
                        'responsable' => mb_strtoupper($datos3['nombre']),
                        'observaciones' => mb_strtoupper($datos3['responsable'], 'UTF-8'),
                        'motivo' => mb_strtoupper($nuevoMotivo, 'UTF-8')
                    );
                    $datosdelstatus[] = $datosStatus;
            }

            $motivoIntento = "";
            $sth = $_db->prepare("SELECT b.id, b.pro, b.order_d, b.cust_number, b.package_id, b.cust_po, b.to_name, u.id AS id_chofer, b.status, b.motivo_rechazo
                FROM bols b
                LEFT JOIN usuarios u
                ON u.id = b.id_chofer
                WHERE b.id = ?
                ORDER BY b.id DESC LIMIT 1");
            $sth->bindParam(1, $idBol);
            $sth->execute();

            $datos = $sth->fetch();
            switch($datos['status']) {
                case 0: $status = 'PENDIENTE'; break;
                case 1: $status = 'EN RUTA'; break;
                case 2: $status = 'ENTREGADA'; break;
                case 3: $status = 'CANCELADA'; break;
                case 4: $status = 'RECHAZADA'; break;
                case 7: $status = 'INTENTO DE ENTREGA';
                $intento = $_db->prepare("SELECT ic.descripcion descripcion FROM intento_bol i, incidencias ic where bol = ? and ic.id = i.incidencia order by fecha desc limit 1");
                $intento->bindParam(1, $idBol);
                $intento->execute();
                if($dato = $intento->fetch()){
                    $motivoIntento = $dato['descripcion'];
                }
                break;
            }

            switch($datos['motivo_rechazo']) {
                case 1: $motivo = 'Falta de PO'; break;
                case 2: $motivo = 'Falta de Factura'; break;
                case 3: $motivo = 'Material equivocado'; break;
                case 4: $motivo = 'Mal direccionada'; break;
                case 5: $motivo = 'Domicilio cerrado'; break;
                case 6: $motivo = 'Cliente cancel&#243; la orden'; break;
                case 7: $motivo = 'Personal de recibos no disponible'; break;
                case 8: $motivo = 'Uline cancel&#243; la orden'; break;
                case 9: $motivo = 'Error en la factura'; break;
                case 10: $motivo = 'Error en el PO'; break;
                case 11: $motivo = 'No se alcanz&#243; la entrega por demora con otro cliente'; break;
                case 12: $motivo = 'No recibe por inventario'; break;
                case 13: $motivo = 'Problemas de tr�nsito'; break;
                case 14: $motivo = 'No se recibi&#243; material en JC'; break;
                case 15: $motivo = 'Accidente vehicular'; break;
                case 19: $motivo = 'Fuerza mayor'; break;
                default: $motivo = '';
            }

            // if (!empty($datos['status'])) {
                $datosBol = array('id' => $datos['id'],
                                'existe' => $existe,
                                'pro' => $datos['pro'],
                                'order_d' => $datos['order_d'],
                                'cust_number' => $datos['cust_number'],
                                'package_id' => $datos['package_id'],
                                'cust_po' => $datos['cust_po'],
                                'to_name' => $datos['to_name'],
                                'status' => $status,
                                'motivo' => $motivo,
                                'motivoIntento' => $motivoIntento
                            );
            // }
        }

        //si la orden no est� vac�a, se ejecutar� esta parte del c�digo
        if (!empty($order)) {
            // Contar caracteres de la orden
            $conteo = strlen($order);
            $orderTemp = $order;

            if ($conteo == 8) {
                $order = '00' . $order;
            }

            //datos del bol
            $sth = $_db->prepare("SELECT b.id, bi.imagen, bi.fecha, bi.id_bol
                FROM bols_imagenes bi
                JOIN bols b
                ON b.id = bi.id_bol
                WHERE b.order_d = ?
                ORDER BY b.id DESC");
            $sth->bindParam(1, $order);
            $sth->execute();
            $datos = $sth->fetch();

            $imgx = $datos['imagen'];
            
            //si dentro de los datos del bol, la foto est� vac�a, se ejecutar� esta parte del c�digo
            if (empty($imgx)) {
                $x=0;

                $sth = $_db->prepare("SELECT b.id, bi.imagen, bi.fecha, bi.id_bol
                    FROM bols_imagenes bi
                    JOIN bols b
                    ON b.id = bi.id_bol
                    WHERE b.order_d = ?
                    ORDER BY b.id DESC");
                $sth->bindParam(1, $order);
                $sth->execute();

                $imagenes = array();
                while ($datos = $sth->fetch()) {
                    $imagenes[$x]['imagen'] = $datos['imagen'];
                    $imagenes[$x]['fecha'] = $datos['fecha'];

                    $x++;
                }

                $order = $orderTemp;
                $sth = $_db->prepare("SELECT b.id, bi.imagen, bi.fecha, bi.id_bol
                    FROM bols_imagenes bi
                    JOIN bols b
                    ON b.id = bi.id_bol
                    WHERE b.order_d = ?
                    ORDER BY b.id DESC");
                $sth->bindParam(1, $order);
                $sth->execute();

                $imagenes = array();
                while ($datos = $sth->fetch()) {
                    $imagenes[$x]['imagen'] = $datos['imagen'];
                    $imagenes[$x]['fecha'] = $datos['fecha'];

                    $x++;
                }
                //si dentro de los datos del bol, la foto no est� vac�a, se ejecutar� esta parte del c�digo
            } else {
                $x=0;

                $sth = $_db->prepare("SELECT b.id, bi.imagen, bi.fecha, bi.id_bol
                    FROM bols_imagenes bi
                    JOIN bols b
                    ON b.id = bi.id_bol
                    WHERE b.order_d = ?
                    ORDER BY b.id DESC");
                $sth->bindParam(1, $order);
                $sth->execute();

                $imagenes = array();
                while ($datos = $sth->fetch()) {
                    $imagenes[$x]['imagen'] = $datos['imagen'];
                    $imagenes[$x]['fecha'] = $datos['fecha'];

                    $x++;
                }

                $order = $orderTemp;
                $sth = $_db->prepare("SELECT b.id, bi.imagen, bi.fecha, bi.id_bol
                    FROM bols_imagenes bi
                    JOIN bols b
                    ON b.id = bi.id_bol
                    WHERE b.order_d = ?
                    ORDER BY b.id DESC");
                $sth->bindParam(1, $order);
                $sth->execute();

                $imagenes = array();
                while ($datos = $sth->fetch()) {
                    $imagenes[$x]['imagen'] = $datos['imagen'];
                    $imagenes[$x]['fecha'] = $datos['fecha'];

                    $x++;
                }
            }

            $sth = $_db->prepare("SELECT b.id
                FROM bols b
                WHERE b.order_d = ? ORDER BY b.id DESC LIMIT 1");
            $sth->bindParam(1, $order);
            $sth->execute();
            $datos = $sth->fetch();
            $idBol = $datos['id'];
        
            //consulta para traer los datos del historial de estatus
            $statusquery = $_db->prepare("SELECT fecha, h.status AS status, nombre, responsable 
            FROM historia_bols h 
            WHERE h.bol = ? AND activo = 1 ORDER BY fecha DESC");
            $statusquery->bindParam(1, $idBol);
            //ejecutar consulta para historial de estatus
            if(!$statusquery->execute()) throw New Exception();
            $datosStatus = array();							
            while($datos3 = $statusquery->fetch()){
                switch($datos3['status']){
                    case 0: $status = 'PENDIENTE'; break;
                        case 1: $status = 'EN RUTA'; break;
                        case 2: $status = 'ENTREGADA'; break;
                        case 3: $status = 'CANCELADA'; break;
                        case 4: $status = 'RECHAZADA'; break;
                        case 7: $status = 'INTENTO DE ENTREGA'; break;
                }
                $nuevoMotivo = 'N/A';
                if($datos3['status'] == 7){
                    $motivo = $_db->prepare("SELECT descripcion FROM incidencias i, intento_bol ib where i.id = ib.incidencia and ib.fecha 
                in (SELECT h.fecha FROM historia_bols h where h.bol = ?) and ib.bol in
                 (SELECT h.bol FROM historia_bols h where h.bol = ?) and ib.fecha = ?");
                    $motivo->bindParam(1, $idBol);
                    $motivo->bindParam(2, $idBol);
                    $motivo->bindParam(3, $datos3['fecha']);
                    $motivo->execute();
                    if($datoMotivo = $motivo->fetch()){
                        $nuevoMotivo = $datoMotivo['descripcion'];
                    }
                }
                if($datos3['status'] == 3){
                    $motivo = $_db->prepare("SELECT descripcion FROM incidencias i, cancelacion_bol ib, asignacion_bol a where i.id = ib.incidencia_id and a.id = ib.asignacion_id and ib.fecha in (SELECT h.fecha FROM historia_bols h where h.bol = ?)
                     and a.bol_id in (SELECT h.bol FROM historia_bols h where h.bol = ?) and a.bol_id = ? and ib.fecha = ?");
                    $motivo->bindParam(1, $idBol);
                    $motivo->bindParam(2, $idBol);
                    $motivo->bindParam(3, $idBol);
                    $motivo->bindParam(4, $datos3['fecha']);
                    $motivo->execute();
                    if($datoMotivo = $motivo->fetch()){
                        $nuevoMotivo = $datoMotivo['descripcion'];
                    }
                }
                    $datosStatus = array (
                        'fecha' => mb_strtoupper($datos3['fecha'], 'UTF-8'),
                        'status' => $status,
                        'responsable' => mb_strtoupper($datos3['nombre']),
                        'observaciones' => mb_strtoupper($datos3['responsable'], 'UTF-8'),
                        'motivo' => mb_strtoupper($nuevoMotivo, 'UTF-8')
                    );
                    $datosdelstatus[] = $datosStatus;
            }


            $sth = $_db->prepare("SELECT b.id, b.pro, b.order_d, b.cust_number, b.package_id, b.cust_po, b.to_name, u.id AS id_chofer, b.status, b.motivo_rechazo
                FROM bols b
                LEFT JOIN usuarios u
                ON u.id = b.id_chofer
                WHERE b.id = ?
                ORDER BY b.id DESC LIMIT 1");
            $sth->bindParam(1, $idBol);
            $sth->execute();

            $datos = $sth->fetch();
            switch($datos['status']) {
                case 0: $status = 'PENDIENTE'; break;
                case 1: $status = 'EN RUTA'; break;
                case 2: $status = 'ENTREGADA'; break;
                case 3: $status = 'CANCELADA'; break;
                case 4: $status = 'RECHAZADA'; break;
                case 7: $status = 'INTENTO DE ENTREGA';
                $intento = $_db->prepare("SELECT ic.descripcion descripcion FROM intento_bol i, incidencias ic where bol = ? and ic.id = i.incidencia order by fecha desc limit 1");
                $intento->bindParam(1, $idBol);
                $intento->execute();
                if($dato = $intento->fetch()){
                    $motivoIntento = $dato['descripcion'];
                }
                break;
            }

            switch($datos['motivo_rechazo']) {
                case 1: $motivo = 'Falta de PO'; break;
                case 2: $motivo = 'Falta de Factura'; break;
                case 3: $motivo = 'Material equivocado'; break;
                case 4: $motivo = 'Mal direccionada'; break;
                case 5: $motivo = 'Domicilio cerrado'; break;
                case 6: $motivo = 'Cliente cancel&#243; la orden'; break;
                case 7: $motivo = 'Personal de recibos no disponible'; break;
                case 8: $motivo = 'Uline cancel&#243; la orden'; break;
                case 9: $motivo = 'Error en la factura'; break;
                case 10: $motivo = 'Error en el PO'; break;
                case 11: $motivo = 'No se alcanz&#243; la entrega por demora con otro cliente'; break;
                case 12: $motivo = 'No recibe por inventario'; break;
                case 13: $motivo = 'Problemas de tr�nsito'; break;
                case 14: $motivo = 'No se recibi&#243; material en JC'; break;
                case 15: $motivo = 'Accidente vehicular'; break;
                case 19: $motivo = 'Fuerza mayor'; break;
                default: $motivo = '';
            }

            // if (!empty($datos['status'])) {
                $datosBol = array('id' => $datos['id'],
                                'existe' => $existe,
                                'pro' => $datos['pro'],
                                'order_d' => $datos['order_d'],
                                'cust_number' => $datos['cust_number'],
                                'package_id' => $datos['package_id'],
                                'cust_po' => $datos['cust_po'],
                                'to_name' => $datos['to_name'],
                                'status' => $status,
                                'motivo' => $motivo,
                                'motivoIntento' => $motivoIntento
                            );
            // }
        }

        if (!empty($customerNum)) {
            $sth = $_db->prepare("SELECT b.id, bi.imagen, bi.fecha, bi.id_bol
                FROM bols_imagenes bi
                JOIN bols b
                ON b.id = bi.id_bol
                WHERE b.cust_number = ?
                ORDER BY b.id DESC");
            $sth->bindParam(1, $customerNum);
            $sth->execute();
            
            $imagenes = array();
            $x=0;
            while ($datos = $sth->fetch()) {
                $imagenes[$x]['imagen'] = $datos['imagen'];
                $imagenes[$x]['fecha'] = $datos['fecha'];

                $x++;
            }

            $sth = $_db->prepare("SELECT b.id
                FROM bols b
                WHERE b.cust_number = ? ORDER BY b.id DESC LIMIT 1");
            $sth->bindParam(1, $customerNum);
            $sth->execute();
            $datos = $sth->fetch();
            $idBol = $datos['id'];

            //consulta para traer los datos del historial de estatus
            $statusquery = $_db->prepare("SELECT fecha, h.status as status, nombre, responsable from historia_bols h where h.bol = ? and activo = 1 order by fecha desc");
            $statusquery->bindParam(1, $idBol);
            //ejecutar consulta para historial de estatus
            if(!$statusquery->execute()) throw New Exception();
            $datosStatus = array();							
            while($datos3 = $statusquery->fetch()){
                switch($datos3['status']){
                    case 0: $status = 'PENDIENTE'; break;
                        case 1: $status = 'EN RUTA'; break;
                        case 2: $status = 'ENTREGADA'; break;
                        case 3: $status = 'CANCELADA'; break;
                        case 4: $status = 'RECHAZADA'; break;
                        case 7: $status = 'INTENTO DE ENTREGA'; break;
                }
                $nuevoMotivo = 'N/A';
                if($datos3['status'] == 7){
                    $motivo = $_db->prepare("SELECT descripcion FROM incidencias i, intento_bol ib where i.id = ib.incidencia and ib.fecha 
                in (SELECT h.fecha FROM historia_bols h where h.bol = ?) and ib.bol in
                 (SELECT h.bol FROM historia_bols h where h.bol = ?) and ib.fecha = ?");
                    $motivo->bindParam(1, $idBol);
                    $motivo->bindParam(2, $idBol);
                    $motivo->bindParam(3, $datos3['fecha']);
                    $motivo->execute();
                    if($datoMotivo = $motivo->fetch()){
                        $nuevoMotivo = $datoMotivo['descripcion'];
                    }
                }
                if($datos3['status'] == 3){
                    $motivo = $_db->prepare("SELECT descripcion FROM incidencias i, cancelacion_bol ib, asignacion_bol a where i.id = ib.incidencia_id and a.id = ib.asignacion_id and ib.fecha in (SELECT h.fecha FROM historia_bols h where h.bol = ?)
                     and a.bol_id in (SELECT h.bol FROM historia_bols h where h.bol = ?) and a.bol_id = ? and ib.fecha = ?");
                    $motivo->bindParam(1, $idBol);
                    $motivo->bindParam(2, $idBol);
                    $motivo->bindParam(3, $idBol);
                    $motivo->bindParam(4, $datos3['fecha']);
                    $motivo->execute();
                    if($datoMotivo = $motivo->fetch()){
                        $nuevoMotivo = $datoMotivo['descripcion'];
                    }
                }
                    $datosStatus = array (
                        'fecha' => mb_strtoupper($datos3['fecha'], 'UTF-8'),
                        'status' => $status,
                        'responsable' => mb_strtoupper($datos3['nombre']),
                        'observaciones' => mb_strtoupper($datos3['responsable'], 'UTF-8'),
                        'motivo' => mb_strtoupper($nuevoMotivo, 'UTF-8')
                    );
                    $datosdelstatus[] = $datosStatus;
            }


            $sth = $_db->prepare("SELECT b.id, b.pro, b.order_d, b.cust_number, b.package_id, b.cust_po, b.to_name, u.id AS id_chofer, b.status, b.motivo_rechazo
                FROM bols b
                LEFT JOIN usuarios u
                ON u.id = b.id_chofer
                WHERE b.id = ?
                ORDER BY b.id DESC LIMIT 1");
            $sth->bindParam(1, $idBol);
            $sth->execute();

            $datos = $sth->fetch();
            switch($datos['status']) {
                case 0: $status = 'PENDIENTE'; break;
                case 1: $status = 'EN RUTA'; break;
                case 2: $status = 'ENTREGADA'; break;
                case 3: $status = 'CANCELADA'; break;
                case 4: $status = 'RECHAZADA'; break;
                case 7: $status = 'INTENTO DE ENTREGA';
                $intento = $_db->prepare("SELECT ic.descripcion descripcion FROM intento_bol i, incidencias ic where bol = ? and ic.id = i.incidencia order by fecha desc limit 1");
                $intento->bindParam(1, $idBol);
                $intento->execute();
                if($dato = $intento->fetch()){
                    $motivoIntento = $dato['descripcion'];
                }
                break;
            }

            switch($datos['motivo_rechazo']) {
                case 1: $motivo = 'Falta de PO'; break;
                case 2: $motivo = 'Falta de Factura'; break;
                case 3: $motivo = 'Material equivocado'; break;
                case 4: $motivo = 'Mal direccionada'; break;
                case 5: $motivo = 'Domicilio cerrado'; break;
                case 6: $motivo = 'Cliente cancel&#243; la orden'; break;
                case 7: $motivo = 'Personal de recibos no disponible'; break;
                case 8: $motivo = 'Uline cancel&#243; la orden'; break;
                case 9: $motivo = 'Error en la factura'; break;
                case 10: $motivo = 'Error en el PO'; break;
                case 11: $motivo = 'No se alcanzo la entrega por demora con otro cliente'; break;
                case 12: $motivo = 'No recibe por inventario'; break;
                case 13: $motivo = 'Problemas de tr�nsito'; break;
                case 14: $motivo = 'No se recibi&#243; material en JC'; break;
                case 15: $motivo = 'Accidente vehicular'; break;
                case 19: $motivo = 'Fuerza mayor'; break;
                default: $motivo = '';
            }

            // if (!empty($datos['status'])) {
                $datosBol = array('id' => $datos['id'],
                                'existe' => $existe,
                                'pro' => $datos['pro'],
                                'order_d' => $datos['order_d'],
                                'cust_number' => $datos['cust_number'],
                                'package_id' => $datos['package_id'],
                                'cust_po' => $datos['cust_po'],
                                'to_name' => $datos['to_name'],
                                'status' => $status,
                                'motivo' => $motivo,
                                'motivoIntento' => $motivoIntento
                            );
            // }
    }
    if (!empty($PackageId)) {
		$sth = $_db->prepare("SELECT b.id, bi.imagen, bi.fecha, bi.id_bol
			FROM bols_imagenes bi
			JOIN bols b
			ON b.id = bi.id_bol
			WHERE b.package_id = ? ORDER BY b.id DESC");
		$sth->bindParam(1, $PackageId);
		$sth->execute();
		
		$imagenes = array();
		$x=0;
		while ($datos = $sth->fetch()) {
			$imagenes[$x]['imagen'] = $datos['imagen'];
			$imagenes[$x]['fecha'] = $datos['fecha'];

			$x++;
		}

		$sth = $_db->prepare("SELECT b.id
			FROM bols b
			WHERE b.package_id = ? ORDER BY b.id DESC LIMIT 1");
		$sth->bindParam(1, $PackageId);
		$sth->execute();
		$datos = $sth->fetch();
		$idBol = $datos['id'];

		//consulta para traer los datos del historial de estatus
		$statusquery = $_db->prepare("SELECT fecha, h.status as status, nombre, responsable from historia_bols h where h.bol = ? and activo = 1 order by fecha desc");
		$statusquery->bindParam(1, $idBol);
		//ejecutar consulta para historial de estatus
		if(!$statusquery->execute()) throw New Exception();
		$datosStatus = array();							
		while($datos3 = $statusquery->fetch()){
			switch($datos3['status']){
				case 0: $status = 'PENDIENTE'; break;
					case 1: $status = 'EN RUTA'; break;
					case 2: $status = 'ENTREGADA'; break;
					case 3: $status = 'CANCELADA'; break;
					case 4: $status = 'RECHAZADA'; break;
					case 7: $status = 'INTENTO DE ENTREGA'; break;
            }
            $nuevoMotivo = 'N/A';
                if($datos3['status'] == 7){
                    $motivo = $_db->prepare("SELECT descripcion FROM incidencias i, intento_bol ib where i.id = ib.incidencia and ib.fecha 
                in (SELECT h.fecha FROM historia_bols h where h.bol = ?) and ib.bol in
                 (SELECT h.bol FROM historia_bols h where h.bol = ?) and ib.fecha = ?");
                    $motivo->bindParam(1, $idBol);
                    $motivo->bindParam(2, $idBol);
                    $motivo->bindParam(3, $datos3['fecha']);
                    $motivo->execute();
                    if($datoMotivo = $motivo->fetch()){
                        $nuevoMotivo = $datoMotivo['descripcion'];
                    }
                }
                if($datos3['status'] == 3){
                    $motivo = $_db->prepare("SELECT descripcion FROM incidencias i, cancelacion_bol ib, asignacion_bol a where i.id = ib.incidencia_id and a.id = ib.asignacion_id and ib.fecha in (SELECT h.fecha FROM historia_bols h where h.bol = ?)
                     and a.bol_id in (SELECT h.bol FROM historia_bols h where h.bol = ?) and a.bol_id = ? and ib.fecha = ?");
                    $motivo->bindParam(1, $idBol);
                    $motivo->bindParam(2, $idBol);
                    $motivo->bindParam(3, $idBol);
                    $motivo->bindParam(4, $datos3['fecha']);
                    $motivo->execute();
                    if($datoMotivo = $motivo->fetch()){
                        $nuevoMotivo = $datoMotivo['descripcion'];
                    }
                }
				$datosStatus = array (
					'fecha' => mb_strtoupper($datos3['fecha'], 'UTF-8'),
					'status' => $status,
                    'responsable' => mb_strtoupper($datos3['nombre']),
                    'observaciones' => mb_strtoupper($datos3['responsable'], 'UTF-8'),
                    'motivo' => mb_strtoupper($nuevoMotivo)
				);
				$datosdelstatus[] = $datosStatus;
		}


		$sth = $_db->prepare("SELECT b.id, b.pro, b.order_d, b.cust_number, b.package_id, b.cust_po, b.to_name, u.id AS id_chofer, b.status, b.motivo_rechazo
			FROM bols b
			LEFT JOIN usuarios u
			ON u.id = b.id_chofer
			WHERE b.id = ?
			ORDER BY b.id DESC LIMIT 1");
		$sth->bindParam(1, $idBol);
		$sth->execute();

		$datos = $sth->fetch();
		switch($datos['status']) {
			case 0: $status = 'PENDIENTE'; break;
			case 1: $status = 'EN RUTA'; break;
			case 2: $status = 'ENTREGADA'; break;
			case 3: $status = 'CANCELADA'; break;
			case 4: $status = 'RECHAZADA'; break;
			case 7: $status = 'INTENTO DE ENTREGA';
                $intento = $_db->prepare("SELECT ic.descripcion descripcion FROM intento_bol i, incidencias ic where bol = ? and ic.id = i.incidencia order by fecha desc limit 1");
                $intento->bindParam(1, $idBol);
                $intento->execute();
                if($dato = $intento->fetch()){
                    $motivoIntento = $dato['descripcion'];
                }
                break;
		}

		switch($datos['motivo_rechazo']) {
			case 1: $motivo = 'Falta de PO'; break;
			case 2: $motivo = 'Falta de Factura'; break;
			case 3: $motivo = 'Material equivocado'; break;
			case 4: $motivo = 'Mal direccionada'; break;
			case 5: $motivo = 'Domicilio cerrado'; break;
			case 6: $motivo = 'Cliente cancel&#243; la orden'; break;
			case 7: $motivo = 'Personal de recibos no disponible'; break;
			case 8: $motivo = 'Uline cancel&#243; la orden'; break;
			case 9: $motivo = 'Error en la factura'; break;
			case 10: $motivo = 'Error en el PO'; break;
			case 11: $motivo = 'No se alcanz&#243; la entrega por demora con otro cliente'; break;
			case 12: $motivo = 'No recibe por inventario'; break;
			case 13: $motivo = 'Problemas de tr�nsito'; break;
			case 14: $motivo = 'No se recibi&#243; material en JC'; break;
            case 15: $motivo = 'Accidente vehicular'; break;
            case 19: $motivo = 'Fuerza mayor'; break;
			default: $motivo = '';
		}

		// if (!empty($datos['status'])) {
			$datosBol = array('id' => $datos['id'],
							 'existe' => $existe,
							 'pro' => $datos['pro'],
							 'order_d' => $datos['order_d'],
							 'cust_number' => $datos['cust_number'],
							 'package_id' => $datos['package_id'],
							 'cust_po' => $datos['cust_po'],
							 'to_name' => $datos['to_name'],
							 'status' => $status,
							 'motivo' => $motivo,
                             'motivoIntento' => $motivoIntento
                            );
		// }
	}

    }elseif(isset($_POST['buscarregresos'])){
            //poblar variables
            $ordenRegresos = $_POST['ordenRegresos'];
			if(!empty($ordenRegresos)){
			try {	
                // Activos
                $sth = $_db->prepare("SELECT reg.id, reg.fecha_solicitud, reg.fecha_inicial, reg.fecha_reprogramacion, reg.fecha_transferencia, reg.fecha_bodega,
                reg.fecha_entrega, reg.fecha_cancelacion, reg.localidad, reg.observaciones, rec.orden, rec.nombre_cliente,
                CONCAT(us.nombre, ' ', us.apellidos) as chofer, s.id as id_status FROM regresos reg LEFT JOIN recolecciones rec
                ON reg.recoleccion_id = rec.id LEFT JOIN status_recoleccion s ON reg.status_id = s.id LEFT JOIN usuarios us ON
                reg.user_recoleccion = us.id WHERE orden = ? ORDER BY reg.fecha_solicitud ASC, reg.fecha_inicial ASC");
                
                if (!$sth->execute([$ordenRegresos])) throw new Exception();

                
                /*
                //consulta para traer los datos del historial de estatus
                $statusquery = $_db->prepare("SELECT id_regreso, fecha, s.status as status, nombre from historia_status h, status_recoleccion s where s.id = h.status order by fecha desc");
                
                
                //ejecutar consulta para historial de estatus
                if(!$statusquery->execute()) throw New Exception();
                $datosStatus = array();

                while($datos3 = $statusquery->fetch()){
                    $datosStatus = array (
                        'regreso' => $datos3['id_regreso'],
                        'fecha' => mb_strtoupper($datos3['fecha'], 'UTF-8'),
                        'status' => mb_strtoupper($datos3['status']),
                        'responsable' => mb_strtoupper($datos3['nombre'])
                    );
                    $datosdelstatus[] = $datosStatus;
                }
                */
                $datosVista = array();
                
                //consulta para traer los datos del historial de estatus
                $statusquery = $_db->prepare("SELECT id_regreso, fecha, s.status as status, nombre, responsable from historia_status h, status_recoleccion s where s.id = h.status and activo = 1 order by fecha desc");
                //ejecutar consulta para historial de estatus
                if(!$statusquery->execute()) throw New Exception();
                $datosStatus = array();

                while($datos3 = $statusquery->fetch()){
                    $datosStatus = array (
                        'regreso' => $datos3['id_regreso'],
                        'fecha' => mb_strtoupper($datos3['fecha'], 'UTF-8'),
                        'status' => mb_strtoupper($datos3['status']),
                        'responsable' => mb_strtoupper($datos3['nombre']),
                        'observaciones' => mb_strtoupper($datos3['responsable'])
                    );
                    $datosdelstatus[] = $datosStatus;
                }

                while ($datos = $sth->fetch()) {
                    //trae datos del estatus
                    $sth2 = $_db->query("SELECT id, status FROM status_recoleccion WHERE id <> 5 ");
                    if(!$sth2->execute()) throw New Exception();
                    
                    // relacionar estatus de regreso con los estatus, para seleccionar el estatus correcto
                    
                    while ($datos2 = $sth2->fetch()) {
                        if ($datos2['id'] == $datos['id_status']) {
                            $status = $datos2['status'];
                        } 
                    }
                    
                    $datosRegresos = array(
                        'id' => $datos['id'],
                        'fecha_solicitud' => mb_strtoupper($datos['fecha_solicitud'], 'UTF-8'),
                        'chofer' => mb_strtoupper($datos['chofer'], 'UTF-8'),
                        'fecha_inicial' => mb_strtoupper($datos['fecha_inicial'], 'UTF-8'),
                        'fecha_reprogramacion' => mb_strtoupper($datos['fecha_reprogramacion'], 'UTF-8'),
                        'localidad' => mb_strtoupper($datos['localidad'], 'UTF-8'),
                        'observaciones' => mb_strtoupper($datos['observaciones'], 'UTF-8'),
                        'orden' => mb_strtoupper($datos['orden'], 'UTF-8'),
                        'nombre_cliente' => mb_strtoupper($datos['nombre_cliente'], 'UTF-8'),
                        'status_id' => $datos['id_status'],
                        'motivo' => $motivo,
                        'status' => $status
                    );
    
                    $datosVista[] = $arreglo;
                    }

                    
                } catch (Exception $e) {
                    echo $e;
                }
            }
    }

   if((empty($datosBol) && empty($datosRegresos) ) ){
        /**/?>
    <div class="col-12 col-sm-12 col-md-12 col-lg-12" style="width:100%; height: 580px;  text-align:center;">
        <img src="jc_envios.png" style="width:80%; height: auto; text-align:center;maring-top:30px;" />
    </div>
    <?php
    }
    if(!empty($datosBol) ) {
        ?>
    <div class="header">
        <div class="col-12 col-sm-12 col-md-12 col-lg-12">
            <div class="row">
                <div class="col-6 col-sm-3 col-md-2 col-lg-2">
                    <img src="http://www.jc-pack.net/images/logo.png" width="100%">
                </div>
                <div class="col-12 col-sm-4 col-md-2 col-lg-2 parent specialMargin">
                    <div class="child">
                        <select class="form-control " id="headerSelect">
                            <option value="entregas" selected>Entregas</option>
                            <option value="pickup">Return pickups</option>
                        </select>
                    </div>
                </div>

                <div class="col-12 col-sm-12 col-md-6 col-lg-6 parent" id="entregasElement">
                    <div class="col-12 col-sm-12 col-md-12 col-lg-12 child margin500px">
                        <form action="" method="post">
                            <button class="headerButton" name="buscar" type="submit">
                                <img src="busqueda.png" class="headerImg">
                            </button>
                            <input type="text" class="headerInput" placeholder="Pro" name="pro">
                            <input type="text" class="headerInput" placeholder="Order" name="order">
                            <input type="text" class="headerInput mobileMargin" placeholder="Customer #"
                                name="customerNum">
                            <input type="text" class="headerInput" placeholder="Package ID" name="PackageId">
                        </form>
                    </div>
                </div>

                <div class="col-12 col-sm-12 col-md-6 col-lg-6 parent " style="display:none" id="pickupElement">
                    <form class="child" method="POST" action="">
                        <input type="hidden" value="1" id="idRegresos" />
                        <div class="col-12 col-sm-12 col-md-12 col-lg-12 child margin500px">
                            <button class="headerButton" name="buscarregresos" type="submit">
                                <img src="busqueda.png" class="headerImg">
                            </button>
                            <input type="text" class="headerInput" style="width:120px" placeholder="Order"
                                name="ordenRegresos">
                            <input type="text" class="headerInput" style="width:120px" placeholder="Date"
                                name="fechaRegresos">
                        </div>
                    </form>
                </div>
                <div class="col-12 col-sm-6 col-md-2 col-lg-2 parent mobileView_cerrarSecion">
                    <div class="child">
                        <a class="btn btn-danger child mobileBtn-danger" href="tracking.php?cs=1"> Cerrar sesi&oacute;n
                            <img src="logout.png" height="25px" /></a>
                    </div>
                </div>
            </div>

        </div>
    </div>

    <!--Primera Linea de la informacion del envio-->
    <div class="bodyHeader">
        <div class="col-12 col-sm-12 col-md-12 col-lg-12">
            <div class="row" style="margin-top:20px;">
                <div class="col-12 col-sm-5 col-md-5 col-lg-4">
                    <div class="headers">
                        PRO
                    </div>
                    <br />
                    <div class="headersValue">
                        <?php echo $datosBol['pro'];?>
                    </div>
                </div>
                <!---->
                <div class="col-6 col-sm-6 col-md-4 col-lg-3">
                    <div class="headers">
                        Orden
                    </div>
                    <br />
                    <div class="headersValue">
                        <?php echo $datosBol['order_d'];?>
                    </div>
                </div>
                <!---->
                <!---->
                <div class="col-6 col-sm-6 col-md-4 col-lg-4">
                    <div class="headers">
                        Package ID
                    </div>
                    <br />
                    <div class="headersValue">
                        <?php echo $datosBol['package_id'];?>
                    </div>
                </div>
                <!---->
                <div class="col-12 -col-sm-6 col-md-4 col-lg-12" style="margin-top:1em;">
                    <div class="headers">
                        Destino
                    </div>
                    <br />
                    <div class="headersValue">
                        <?php echo $datosBol['to_name'];?>
                    </div>
                </div>
            </div>
            <!--Second Row-->
            <div class="row" style="margin-top:30px;">

                <div class="col-6 col-sm-6 col-md-3 col-lg-3">
                    <div class="headers">
                        N&uacute;mero de cliente
                    </div>
                    <br />
                    <div class="headersValue">
                        <?php echo $datosBol['cust_number'];?>
                    </div>
                </div>
                <div class="col-6 col-sm-6 col-md-4 col-lg-4">
                    <div class="headers">
                        Cliente PO
                    </div>
                    <br />
                    <div class="headersValue">
                        <?php echo $datosBol['cust_po'];?>
                    </div>
                </div>
                <div class="col-12 col-sm-12 col-md-5 col-lg-5">

                    <div class="">
                        <?php if($datosBol['status'] == 'CANCELADA'){ ?>
                        <div class="headers">
                            Motivo de Cancelaci&oacute;n / Rechazo
                        </div>
                        <div class="headersValue"> <?php echo $datosBol['motivo'];?></div>
                        <?php } ?>

                        <?php if($datosBol['status'] == 'ENTREGADA'){ ?>
                        <div class="headers"> </div>
                        <?php } ?>

                        <?php if($datosBol['status'] == 'EN RUTA'){?>
                        <div class="headers">
                            Motivo de Cancelaci&oacute;n / Rechazo
                        </div>
                        <div class="headersValue"> <?php echo $datosBol['motivo'];?></div>
                        <?php } ?>

                        <?php if($datosBol['status'] == 'En Bodega'){?>
                        <div class="headers">
                            Motivo de Cancelaci&oacute;n / Rechazo
                        </div>
                        <div class="headersValue"> <?php echo $datosBol['motivo'];?></div>
                        <?php } ?>

                        <?php if($datosBol['status'] == 'EN TRANSFERENCIA'){?>
                        <div class="headers">
                            Motivo de Cancelaci&oacute;n / Rechazo
                        </div>
                        <div class="headersValue"> <?php echo $datosBol['motivo'];?></div>
                        <?php } ?>
                        <?php if($datosBol['status'] == 'INTENTO DE ENTREGA'){?>
                        <div class="headers">
                            Motivo de Cancelaci&oacute;n / Rechazo
                        </div>
                        <div class="headersValue"> <?php echo $datosBol['motivoIntento'];?></div>
                        <?php } ?>
                    </div>
                </div>

            </div>
            <!--Second Row-->
            <!--Third Row-->
            <div class="row" style="margin-top:30px; margin-bottom:20px;">
                <div class="col-4 col-sm-3 col-md-3 col-lg-3">
                    <div class="">
                        <?php if($datosBol['status'] == 'CANCELADA'){ ?>
                        <div class="" style="text-align: center;">
                            <img src="cancel.png" style="margin-left:10px; margin-top:10px; width: 50px;">
                        </div>
                        <?php } ?>

                        <?php if($datosBol['status'] == 'ENTREGADA'){ ?>
                        <div class="" style="text-align: center;">
                            <img src="check.png" style="margin-left:10px; margin-top:10px; width: 50px;">
                        </div>
                        <?php } ?>

                        <?php if($datosBol['status'] == 'EN RUTA'){?>
                        <div class="" style="text-align: center;">
                            <img src="caja.png" style="margin-left:10px; margin-top:10px; width: 50px;">
                        </div>
                        <?php } ?>

                        <?php if($datosBol['status'] == 'En Bodega'){?>
                        <div class="" style="text-align: center;">
                            <img src="bodega.png" style="margin-left:10px; margin-top:10px; width: 50px;">
                        </div>
                        <?php } ?>

                        <?php if($datosBol['status'] == 'EN TRANSFERENCIA'){?>
                        <div class="" style="text-align: center;">
                            <img src="transferencia.png" style="margin-left:20px; margin-top:10px; width: 50px;">
                        </div>
                        <?php } ?>
                        <?php if($datosBol['status'] == 'INTENTO DE ENTREGA'){?>
                        <div class="" style="text-align: center;">
                            <img src="warning.png" style="margin-left:20px; margin-top:10px; width: 50px;">
                        </div>
                        <?php } ?>
                    </div>
                </div>
                <div class="col-8 col-sm-5 col-md-5 col-lg-5">
                    <div class="bodies">
                        <div class="headers" style="width: 100%;">
                            Estado
                        </div>
                        <?php if($datosBol['status'] == 'ENTREGADA') { ?>
                        <div class="bodiesValue" style="width: 100%">
                            <?php echo $datosBol['status'];?>
                        </div>
                        <?php } ?>

                        <?php if($datosBol['status'] == 'CANCELADA') { ?>
                        <div class="bodiesValue" style="width: 100%; color:#F78F8F">
                            <?php echo $datosBol['status'];?>
                        </div>
                        <?php } ?>

                        <?php if($datosBol['status'] != 'CANCELADA' & $datosBol['status'] != 'ENTREGADA'){?>
                        <div class="bodiesValue" style="width: 100%; color:#C5AB61;">
                            <?php echo $datosBol['status'];?>
                        </div>
                        <?php } ?>

                        <div class="headers" style="width: 100%; font-size:12pt">
                        </div>
                    </div>
                </div>
                <div class="col-12 col-sm-4 col-md-4 col-lg-4 mobileAlign">
                    <div>
                        <!--PDF Solo disponible si el estatus es "Entregado"-->
                        <?php if($datosBol['status'] == 'ENTREGADA') { ?>
                        <div class="headers">
                            Descargar PDF
                            <form action="" method="POST">
                                <input type="hidden" value="1" name="pdf">
                                <input type="hidden" value="<?php echo $datosBol['id'];?>" name="id">
                                <button class="bodiesButton" style="margin-right:10px;" type="submit">
                                    <img src="pdf.png" width="50px" style="border-radius: 10%ñ">
                                </button>
                            </form>
                        </div>
                        <?php } ?>
                        <?php if($datosBol['status'] == 'CANCELADA') { ?>
                        <div class="headers">
                        </div>
                        <?php } ?>
                    </div>

                    <!--PDF Solo disponible si el estatus es "Entregado"-->
                </div>
            </div>
            <!--Third Row-->
        </div>
        <!--Ultima Linea de la informacion del envio-->
        <div class="bodyHeaderFooter">
            <button class="footerButton" id="footerButton">Historial &#10010;</button>
            <button class="footerButton" id="fotosButton">Prueba de entrega &#10010;</button>
        </div>
        <?php if(!empty($datosdelstatus)){ ?>
        <div class="bodyHistorial" id="bodyHistorial">
            <?php foreach($datosdelstatus as $status){ ?>
            <div class="historial">
                <!--test-->
                <div class="col-12 col-sm-12 col-md-12 col-lg-12">
                    <div class="row">
                        <div class="col-5 col-sm-5 col-md-4 col-lg-2">
                            <?php if($status['status'] == 'CANCELADA'){ ?>
                            <div class="bodies">
                                <img src="cancel.png" style="margin-left:10px; margin-top:10px; width: 50px;">
                            </div>
                            <?php } ?>

                            <?php if($status['status'] == 'EN RUTA'){ ?>
                            <div class="bodies">
                                <img src="warning.png" style="margin-left:10px; margin-top:10px; width: 50px;">
                            </div>
                            <?php } ?>

                            <?php if($status['status'] == 'ENTREGADA'){ ?>
                            <div class="bodies">
                                <img src="check.png" style="margin-left:10px; margin-top:10px; width: 50px;">
                            </div>
                            <?php } ?>

                            <?php if($status['status'] == 'EN RUTA'){?>
                            <div class="bodies">
                                <img src="caja.png" style="margin-left:10px; margin-top:10px; width: 50px;">
                            </div>
                            <?php } ?>

                            <?php if($status['status'] == 'En Bodega'){?>
                            <div class="bodies">
                                <img src="bodega.png" style="margin-left:10px; margin-top:10px; width: 50px;">
                            </div>
                            <?php } ?>

                            <?php if($status['status'] == 'EN TRANSFERENCIA'){?>
                            <div class="bodies">
                                <img src="transferencia.png" style="margin-left:10px; margin-top:10px; width: 50px;">
                            </div>
                            <?php } ?>
                            <?php if($status['status'] == 'INTENTO DE ENTREGA'){?>
                            <div class="bodies">
                                <img src="warning.png" style="margin-left:10px; margin-top:10px; width: 50px;">
                            </div>
                            <?php } ?>
                            <?php if($status['status'] == 'RECHAZADA'){?>
                            <div class="bodies">
                                <img src="warning.png" style="margin-left:10px; margin-top:10px; width: 50px;">
                            </div>
                            <?php } ?>
                        </div>
                        <div class="col-6 col-sm-6 col-md-4 col-lg-6">
                            <div class="bodies">
                                <div class="headers">
                                    Estado
                                </div>
                                <?php if($status['status'] == 'CANCELADA') { ?>
                                <div class="bodiesValueCancel" style="width: 50%; float: left; position: relative;">
                                    <?php echo $status['status']; ?>
                                </div>
                                <?php } ?>

                                <?php if($status['status'] == 'ENTREGADA') { ?>
                                <div class="bodiesValue" style="width: 50%; float: left; position: relative;">
                                    <?php echo $status['status']; ?>
                                </div>
                                <?php } ?>

                                <?php if($status['status'] != 'CANCELADA' & $status['status'] != 'ENTREGADA'){?>
                                <div class="bodiesValue" style="width: 100%; color:#C5AB61;">
                                    <?php echo $status['status'];?>
                                </div>
                                <?php } ?>

                            </div>
                        </div>
                        <div class="col-12 col-sm-12 col-md-4 col-lg-4">
                            <div class="bodies">
                                <div class="headers" style="width: 100%">
                                    Chofer asignado: <div class="headers" style="color: #111;">
                                        <?php echo $status['responsable']; ?>
                                    </div>
                                </div>
                                <div class="headers" style="width: 100%; font-size:12pt">
                                    <?php echo $status['fecha']; ?>
                                </div>
                            </div>

                        </div>

                    </div>
                    <div class="row">
                        <div class="col-12 col-sm-12 col-md-6 col-lg-8">
                            <div class="headers" style="margin-left:50px">Motivo de rechazo/cancelación:</div> <div style="margin-left:50px"><?php echo $status['motivo']; ?></div>
                        </div>
                        <div class="col-12 col-sm-12 col-md-6 col-lg-4">
                       
                        <?php if($status['status'] == 'CANCELADA' || $status['status'] == 'INTENTO DE ENTREGA'){ ?>
                                <div class="headers" style="width: 100%">
                                Observaciones: <div class="headers" style="color: #111;">
                                        <?php if($status['observaciones'] != '') {
                                            echo $status['observaciones'];
                                        }else{
                                        ?> N/A <?php 
                                        }?>
                                    </div>
                                </div>
                                <?php } ?>
                        </div>

                    </div>
                </div>
            </div>
            <?php } ?>
        </div>

        <?php }  ?>
        
        <div>
            <div class="bodyFotos" id="bodyFotos">
                <?php if(!empty($imagenes)){
                            foreach($imagenes as $imagen) {?>
                <img class="img-thumbnail" width="600"
                    src="http://sistema.jc-pack.net/data/privada/imagenes/<?php echo $imagen['imagen']?>" />
                <div class="bodyFotoDate"><?php echo $imagen['fecha']; ?></div>
                <?php 
                                }
                            }else{?>
                <div style="width:100%; color:#111; background:#aaa">No hay pruebas de entrega</div>
                <?php } ?>
            </div>
        </div>
    </div>

    <div class="footer">
        <img src="http://www.jc-pack.net/images/logo.png" style="float:left; position: relative;" width="210"
            height="120" />
        <h5 style="color:#3E4095; font-weight: bold">625-56-33</h5>
        <h4 style="color:#F36B07">Horario 6am - 6pm</h4>
        <h5>Tijuana, B.C.</h5>
        <address style="margin-top: -5px; color: #999">Parque Industrial Girasoles 2,
            Aguila Azteca #19308 Int 3</address>
    </div>
    <?php
    }

    else if(!empty($datosRegresos)){
        ?>
    <!---->
    <div class="header">
        <div class="col-12 col-sm-12 col-md-12 col-lg-12">
            <div class="row">
                <div class="col-6 col-sm-3 col-md-2 col-lg-2">
                    <img src="http://www.jc-pack.net/images/logo.png" width="100%">
                </div>
                <div class="col-12 col-sm-4 col-md-2 col-lg-2 parent specialMargin">
                    <div class="child">
                        <select class="form-control " id="headerSelect">
                            <option value="entregas" selected>Entregas</option>
                            <option value="pickup">Return pickups</option>
                        </select>
                    </div>
                </div>

                <div class="col-12 col-sm-12 col-md-6 col-lg-6 parent" id="entregasElement">
                    <div class="col-12 col-sm-12 col-md-12 col-lg-12 child margin500px">
                        <form action="" method="post">
                            <button class="headerButton" name="buscar" type="submit">
                                <img src="busqueda.png" class="headerImg">
                            </button>
                            <input type="text" class="headerInput" placeholder="Pro" name="pro">
                            <input type="text" class="headerInput" placeholder="Order" name="order">
                            <input type="text" class="headerInput mobileMargin" placeholder="Customer #"
                                name="customerNum">
                            <input type="text" class="headerInput" placeholder="Package ID" name="PackageId">
                        </form>
                    </div>
                </div>

                <div class="col-12 col-sm-12 col-md-6 col-lg-6 parent " style="display:none" id="pickupElement">
                    <form class="child " method="POST" action="">
                        <input type="hidden" value="1" id="idRegresos" />
                        <div class="col-12 col-sm-12 col-md-12 col-lg-12 child margin500px">
                            <button class="headerButton" name="buscarregresos" type="submit">
                                <img src="busqueda.png" class="headerImg">
                            </button>
                            <input type="text" class="headerInput" style="width:120px" placeholder="Order"
                                name="ordenRegresos">
                            <input type="text" class="headerInput" style="width:120px" placeholder="Date"
                                name="fechaRegresos">
                        </div>
                    </form>
                </div>
                <div class="col-12 col-sm-6 col-md-2 col-lg-2 parent mobileView_cerrarSecion">
                    <div class="child">
                        <a class="btn btn-danger child mobileBtn-danger" href="tracking.php?cs=1"> Cerrar sesi&oacute;n
                            <img src="logout.png" height="25px" /></a>
                    </div>
                </div>
            </div>

        </div>
    </div>
    <div class="bodyHeader">
        <div class="col-12 col-sm-12 col-md-12 col-lg-12">
            <div class="row" style="margin-top:20px;">
                <div class="col-6 col-sm-5 col-md-3">
                    <div class="headers">
                        Order
                    </div>
                    <br />
                    <div class="headersValue">
                        <?php echo $datosRegresos['orden']; ?>
                    </div>
                </div>
                <div class="col-6 col-sm-7 col-md-3">
                    <div class="headers">
                        Ubicaci&oacute;n
                    </div>
                    <br />
                    <div class="headersValue">
                        <?php echo $datosRegresos['localidad']; ?>
                    </div>
                </div>
                <div class="col-6 col-sm-5 col-md-3">
                    <div class="headers">
                        Cliente
                    </div>
                    <br />
                    <div class="headersValue">
                        <?php echo $datosRegresos['nombre_cliente']; ?>
                    </div>
                </div>
                <div class="col-6 col-sm-7 col-md-3">
                    <div class="headers">
                        Chofer
                    </div>
                    <br />
                    <div class="headersValue">
                        <?php echo $datosRegresos['chofer']; ?>
                    </div>
                </div>
            </div>
            <!--Second Row-->
            <div class="row" style="margin-top:30px; margin-bottom:20px;">
                <div class="col-4 col-sm-2 col-md-3">
                    <div class="bodies">
                        <?php if($datosRegresos['status'] == 'CANCELADA'){ ?>
                        <div class="bodies" style="text-align:center;">
                            <img src="cancel.png" style="margin-left:10px; margin-top:10px; width: 50px;">
                        </div>
                        <?php } ?>
                        <?php if($datosRegresos['status'] == 'Entregado a ULINE'){?>
                        <div class="bodies" style="text-align:center;">
                            <img src="transferencia.png" style="margin-left:10px; margin-top:10px; width: 50px;">
                        </div>
                        <?php } ?>
                        <?php if($datosRegresos['status'] == 'EN RUTA'){ ?>
                        <div class="bodies" style="text-align:center;">
                            <img src="warning.png" style="margin-left:10px; margin-top:10px; width: 50px;">
                        </div>
                        <?php } ?>

                        <?php if($datosRegresos['status'] == 'ENTREGADA'){ ?>
                        <div class="bodies" style="text-align:center;">
                            <img src="check.png" style="margin-left:10px; margin-top:10px; width: 50px;">
                        </div>
                        <?php } ?>

                        <?php if($datosRegresos['status'] == 'EN RUTA'){?>
                        <div class="bodies" style="text-align:center;">
                            <img src="caja.png" style="margin-left:10px; margin-top:10px; width: 50px;">
                        </div>
                        <?php } ?>

                        <?php if($datosRegresos['status'] == 'En Bodega'){?>
                        <div class="bodies" style="text-align:center;">
                            <img src="bodega.png" style="margin-left:10px; margin-top:10px; width: 50px;">
                        </div>
                        <?php } ?>

                        <?php if($datosRegresos['status'] == 'EN TRANSFERENCIA'){?>
                        <div class="bodies" style="text-align:center;">
                            <img src="transferencia.png" style="margin-left:10px; margin-top:10px; width: 50px;">
                        </div>
                        <?php } ?>
                    </div>
                </div>
                <div class="col-8 col-sm-3 col-md-3">
                    <div class="bodyHeaderBody">
                        <div class="bodies">
                            <div class="headers" style="width: 100%;">
                                Estado
                            </div>
                            <?php if($datosRegresos['status'] == 'CANCELADA') { ?>
                            <div class="bodiesValueCancel" style="width: 50%; float: left; position: relative;">
                                <?php echo $datosRegresos['status']; ?>
                            </div>
                            <?php } ?>
                            <?php if($datosRegresos['status'] == 'ENTREGADA') { ?>
                            <div class="bodiesValue" style="width: 50%; float: left; position: relative;">
                                <?php echo $datosRegresos['status']; ?>
                            </div>
                            <?php } ?>
                            <?php if($datosRegresos['status'] != 'CANCELADA' & $datosRegresos['status'] != 'ENTREGADA'){?>
                            <div class="bodiesValue" style="width: 100%; color:#C5AB61;">
                                <?php echo $datosRegresos['status'];?>
                            </div>
                            <?php } ?>

                            <div class="headers" style="width: 100%; font-size:12pt">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-12 col-sm-7 col-md-6">
                    <div class="headers">
                    </div>
                    <br />
                    <div class="headersValue">
                        <?php echo $datosRegresos['motivo'];?>
                    </div>
                </div>

                <div class="col-12 col-sm-2 col-md-3 mobileAlign">
                    <div>
                        <!--PDF Solo disponible si el estatus es "Entregado"-->
                        <?php if($datosBol['status'] == 'ENTREGADA') { ?>
                        <div class="headers">
                            Descargar PDF

                            <form method="POST" action="" enctype="multipart/form-data">
                                <input type="hidden" value="1" name="pdfRegresos">
                                <input type="hidden" name="ordenId" value="<?php echo $datosRegresos['orden'];?>">
                                <input type="hidden" value="<?php echo $datosRegresos['id'];?>" name="regresosId">
                                <button class="bodiesButton" style="margin-right:10px;" type="submit">
                                    <img src="pdf.png" height="50px" style="border-radius: 50%">
                                </button>
                            </form>
                        </div>
                        <?php } ?>
                        <?php if($datosBol['status'] == 'CANCELADA') { ?>
                        <div class="headers">
                        </div>
                        <?php } ?>
                    </div>
                </div>
                <div class="footer">
                    <img src="http://www.jc-pack.net/images/logo.png" style="float:left; position: relative;"
                        width="210" height="120" />
                    <h5 style="color:#3E4095; font-weight: bold">625-56-33</h5>
                    <h4 style="color:#F36B07">Horario 6am - 6pm</h4>
                    <h5>Tijuana, B.C.</h5>
                    <address style="margin-top: -5px; color: #999">Parque Industrial Girasoles 2,
                        Aguila Azteca #19308 Int 3</address>
                </div>
            </div>
            <!--Second Row-->
        </div>

        <!--test part-->


        <div class="bodyHeaderFooter">
            <button class="footerButton" id="footerButton">Historial &#10010;</button>
        </div>
        <div class="bodyHistorial" id="bodyHistorial">
            <?php foreach($datosdelstatus as $status){ 
                    if($status['regreso'] == $datosRegresos['id']){
                    ?>

            <div class="historial">
                <div class="col-12 col-sm-12 col-md-12 col-lg-12">
                    <div class="row">
                        <div class="col-5 col-sm-5 col-md-4 col-lg-2">
                            <?php if($status['status'] == 'CANCELADA'){ ?>
                            <div class="bodies">
                                <img src="cancel.png" style="width: 70%;">
                            </div>
                            <?php } ?>

                            <?php if($status['status'] == 'EN RUTA'){ ?>
                            <div class="bodies">
                                <img src="warning.png" style="width: 70%;">
                            </div>
                            <?php } ?>

                            <?php if($status['status'] == 'EN RUTA'){?>
                            <div class="bodies">
                                <img src="caja.png" style="width: 70%;">
                            </div>
                            <?php } ?>

                            <?php if($status['status'] == 'EN BODEGA'){?>
                            <div class="bodies">
                                <img src="bodega.png" style="width: 70%;">
                            </div>
                            <?php } ?>

                            <?php if($status['status'] == 'EN TRANSFERENCIA'){?>
                            <div class="bodies">
                                <img src="transferencia.png" style="width: 70%;">
                            </div>
                            <?php } ?>
                            <?php if($status['status'] == 'ENTREGADO A ULINE'){?>
                            <div class="bodies">
                                <img src="transferencia.png" style="width: 70%;">
                            </div>
                            <?php } ?>
                        </div>
                        <div class="col-6 col-sm-6 col-md-4 col-lg-6">
                            <div class="bodies">
                                <div class="headers" style="width: 100%;">
                                    Estado
                                </div>

                                <?php if($status['status'] == 'CANCELADA') { ?>
                                <div class="bodiesValueCancel" style="width: 100% float: left; position: relative;">
                                    <?php echo $status['status']; ?>
                                </div>
                                <?php } ?>

                                <?php if($status['status'] == 'ENTREGADA') { ?>
                                <div class="bodiesValue" style="width: 100% float: left; position: relative;">
                                    <?php echo $status['status']; ?>
                                </div>
                                <?php } ?>

                                <?php if($status['status'] != 'CANCELADA' & $status['status'] != 'ENTREGADA'){?>
                                <div class="bodiesValue" style="width: 100%; color:#C5AB61;">
                                    <?php echo $status['status']; ?>
                                </div>
                                <?php } ?>
                            </div>
                        </div>
                        <div class="col-12 col-sm-12 col-md-4 col-lg-4">
                            <div class="bodies">
                                <div class="headers">
                                    Chofer asignado: <div class="headers" style="color: #111;">
                                        <?php echo $status['responsable']; ?>
                                    </div>
                                </div>

                                <div class="headers" style="font-size:12pt">
                                    <?php echo $status['fecha']; ?>
                                </div>

                                <div class="headers">
                                    Observaciones: <div class="headers" style="color: #111;">
                                        <?php echo $status['observaciones']; ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <?php } 
                } ?>

            </div>
        </div>
    </div>
    <div class="footer">
        <img src="http://www.jc-pack.net/images/logo.png" style="float:left; position: relative;" width="210"
            height="120" />
        <h5 style="color:#3E4095; font-weight: bold">625-56-33</h5>
        <h4 style="color:#F36B07">Horario 6am - 6pm</h4>
        <h5>Tijuana, B.C.</h5>
        <address style="margin-top: -5px; color: #999">Parque Industrial Girasoles 2,
            Aguila Azteca #19308 Int 3</address>
    </div>
    <?php

    }
}
    
?>
</body>
<script>
$(function() {
    console.log('loading...');
});
$('#headerSelect').change(function() {
    if ($(this).val() == 'entregas') {
        $('#entregasElement').show();
        $('#pickupElement').hide();

    }
    if ($(this).val() == 'pickup') {
        $('#entregasElement').hide();
        $('#pickupElement').show();

    }
});
$('#bodyHistorial').show();
$('#footerButton').click(function() {
    if ($('#bodyHistorial').is(':visible')) {
        $('#bodyHistorial').hide();
    } else {
        $('#bodyHistorial').show();
    }

});

$('#bodyFotos').show();
/**/
$('#fotosButton').click(function() {
    if ($('#bodyFotos').is(':visible')) {
        $('#bodyFotos').hide();
    } else {
        $('#bodyFotos').show();
    }

});
</script>

</html>