<?php
session_start();
include '../conexion.php';
mysqli_set_charset($connection, "utf8");

$reporte = $_POST['reporte'] ?? null;
$fechaini = $_POST['fechaini'] ?? null;
$fechafin = $_POST['fechafin'] ?? null;
$usados = $_POST['usados'] ?? null;
$detallado = $_POST['detallado'] ?? null;

$response = array();

if ($reporte === "empleado") {
    $query = $connection->query("SELECT * FROM uni_empleados order by num_emp ASC");
    $etiqueta = "Empleado:";
    $valores = "<label style='padding-right:10px'>$etiqueta</label>
    <select id='consultax' style='width:500px'>
    <option value='0'>--Selecciona--</option>
    ";

    if ($query && $query->num_rows > 0) {
        while ($data = $query->fetch_assoc()) {
            $valores .= "<option value='" . htmlspecialchars($data['num_emp'], ENT_QUOTES, 'UTF-8') . "'>" 
                      . htmlspecialchars($data['num_emp'], ENT_QUOTES, 'UTF-8') . " | " 
                      . htmlspecialchars($data['nombre'], ENT_QUOTES, 'UTF-8') . "</option>";
        }
        $valores.="</select>";
        $response['success'] = true;
        $response['valores'] = $valores;
        $response['etiqueta'] = $etiqueta;
    } else {
        $response['success'] = false;
        $response['message'] = "No se encontraron empleados.";
    }
} else if ($reporte === "supervisor") {
    $query = $connection->query("SELECT * FROM uni_empleados group by supervisor order by supervisor ASC");
    $etiqueta = "Supervisor:";
    $valores = "<label style='padding-right:10px'>$etiqueta</label>
    <select id='consultax' style='width:500px'>
    <option value='0'>--Selecciona--</option>";

    if ($query && $query->num_rows > 0) {
        while ($data = $query->fetch_assoc()) {
            $valores .= "<option value='" . htmlspecialchars($data['supervisor'], ENT_QUOTES, 'UTF-8') . "'>" 
                      . htmlspecialchars($data['supervisor'], ENT_QUOTES, 'UTF-8') . "</option>";
        }
        $valores.="</select>";
        $response['success'] = true;
        $response['valores'] = $valores;
        $response['etiqueta'] = $etiqueta;
    } else {
        $response['success'] = false;
        $response['message'] = "No se encontraron empleados.";
    }
} else if ($reporte === "lugar") {
    $query = $connection->query("SELECT * FROM uni_empleados group by lugar_trabajo order by lugar_trabajo ASC");
    $etiqueta = "Lugar de Trabajo:";
    $valores = "<label style='padding-right:10px'>$etiqueta</label>
    <select id='consultax' style='width:500px'>
    <option value='0'>--Selecciona--</option>
    ";

    if ($query && $query->num_rows > 0) {
        while ($data = $query->fetch_assoc()) {
            $valores .= "<option value='" . utf8_encode($data['lugar_trabajo']) . "'>" 
                      . utf8_encode($data['lugar_trabajo']) . "</option>";
        }
        $valores.="</select>";
        $response['success'] = true;
        $response['valores'] = $valores;
        $response['etiqueta'] = $etiqueta;
    } else {
        $response['success'] = false;
        $response['message'] = "No se encontraron empleados.";
    }
} else if ($reporte === "cliente") {
    $query = $connection->query("SELECT * FROM uni_clientes group by cliente order by cliente ASC");
    $etiqueta = "Cliente:";
    $valores = "<label style='padding-right:10px'>$etiqueta</label>
    <select id='consultax' style='width:500px'>
    <option value='0'>--Selecciona--</option>
    ";

    if ($query && $query->num_rows > 0) {
        while ($data = $query->fetch_assoc()) {
            $valores .= "<option value='" . htmlspecialchars($data['iduniCliente'], ENT_QUOTES, 'UTF-8') . "'>" 
                      . htmlspecialchars($data['cliente'], ENT_QUOTES, 'UTF-8') . "</option>";
        }
        $valores.="</select>";
        $response['success'] = true;
        $response['valores'] = $valores;
        $response['etiqueta'] = $etiqueta;
    } else {
        $response['success'] = false;
        $response['message'] = "No se encontraron empleados.";
    }
} else if ($reporte === "sucursal") {
    $query = $connection->query("SELECT * FROM uni_empleados group by ubicacion order by ubicacion ASC");
    $etiqueta = "Cliente:";
    $valores = "<label style='padding-right:10px'>$etiqueta</label>
    <select id='consultax' style='width:500px'>
    <option value='0'>--Selecciona--</option>
    ";

    if ($query && $query->num_rows > 0) {
        while ($data = $query->fetch_assoc()) {
            $valores .= "<option value='" . htmlspecialchars($data['ubicacion'], ENT_QUOTES, 'UTF-8') . "'>" 
                      . htmlspecialchars($data['ubicacion'], ENT_QUOTES, 'UTF-8') . "</option>";
        }
        $valores.="</select>";
        $response['success'] = true;
        $response['valores'] = $valores;
        $response['etiqueta'] = $etiqueta;
    } else {
        $response['success'] = false;
        $response['message'] = "No se encontraron empleados.";
    }
} else if ($reporte === "articulo") {
    $query = $connection->query("SELECT * FROM uni_uniforme group by u_descripcion order by u_descripcion ASC");
    $etiqueta = "Cliente:";
    $valores = "<label style='padding-right:10px'>$etiqueta</label>
    <select id='consultax' style='width:500px'>
    <option value='0'>--Selecciona--</option>
    ";

    if ($query && $query->num_rows > 0) {
        while ($data = $query->fetch_assoc()) {
            $valores .= "<option value='" . htmlspecialchars($data['u_descripcion'], ENT_QUOTES, 'UTF-8') . "'>" 
                      . htmlspecialchars($data['u_descripcion'], ENT_QUOTES, 'UTF-8') . "</option>";
        }
        $valores.="<option value='99'>Todos</option></select>";
        $response['success'] = true;
        $response['valores'] = $valores;
        $response['etiqueta'] = $etiqueta;
    } else {
        $response['success'] = false;
        $response['message'] = "No se encontraron empleados.";
    }
} 



echo json_encode($response);
?>
