<?php

include_once("server/lib/mod_dbm/package.dbm.php");
include_once("server/class/PedidosManager.php");

class DeliveryManager {

    private $idLocal;
    private $deliveryId;

    public function __construct() {
        $this->dao = new dbObject();
    }

    public function setIdLocal($idLocal) {
        $this->idLocal = $idLocal;
    }

    public function setDeliveryId($deliveryId) {
        $this->deliveryId = $deliveryId;
    }

    public function getBusyDeliverys() {
        $deliverys = array();
        $stmt = $this->dao->connection->prepare("SELECT d.id_delivery, iniciales, nombre, telefono, local_actual, tarjeta_actual, current_lat, current_lng, last_update
                    FROM deliverys d
                    WHERE local_actual IS NOT NULL");
        $stmt->execute();
        $result = $stmt->get_result();
        while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
            $row['nombre'] = $this->zeroPad(($row['tarjeta_actual']) ? $row['tarjeta_actual'] : "0", 2) . " " . $row['nombre'];
            $deliverys[] = $row;
        }
        $stmt->close();
        return $deliverys;
    }

    public function getDeliverys() {
        $deliverys = array();
        if ($this->idLocal === null) {
            $stmt = $this->dao->connection->prepare("SELECT d.id_delivery, iniciales, nombre, telefono, local_actual, tarjeta_actual, current_lat, current_lng, last_update, pedidos 
                                                    FROM deliverys d
                                                    LEFT JOIN 
                                                            (SELECT id_delivery, count(id_pedido) as pedidos FROM pedidos WHERE status IN ('PEN','SBY') GROUP BY id_delivery) p 
                                                    ON d.id_delivery = p.id_delivery
                                                    WHERE local_actual IS NULL");
        } else {
            $stmt = $this->dao->connection->prepare("SELECT d.id_delivery, iniciales, nombre, telefono, local_actual, tarjeta_actual, current_lat, current_lng, last_update, pedidos 
                                                        FROM deliverys d
                                                        LEFT JOIN 
                                                                (SELECT id_delivery, count(id_pedido) as pedidos FROM pedidos WHERE status IN ('PEN','SBY') GROUP BY id_delivery) p 
                                                        ON d.id_delivery = p.id_delivery
                                                     WHERE d.local_actual = ?");
            $stmt->bind_param("i", $this->idLocal);
        }
        $stmt->execute();
        $result = $stmt->get_result();
        while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
            $row['nombre'] = $this->zeroPad(($row['tarjeta_actual']) ? $row['tarjeta_actual'] : "0", 2) . " " . $row['nombre'];
            $deliverys[] = $row;
        }
        $stmt->close();
        return $deliverys;
    }

    private function zeroPad($s, $l) {
        while (strlen($s) < $l) {
            $s = "0" . $s;
        }
        return $s;
    }

    public function getDeliveryStatus() {
        $deliverys = array();
        $stmt = $this->dao->connection->prepare("SELECT d.id_delivery, d.nombre, d.telefono as tel_delivery, d.iniciales, d.habilitado_desde, d.habilitado_hasta, 
                                                        d.local_actual, d.current_lat as delivery_lat, d.current_lng as delivery_lng, 
                                                        last_update as coords_timestamp, l.nombre as nombre_local, lat as lat_local, lng as lng_local, l.telefono as tel_local 
                                                FROM deliverys d 
                                                LEFT JOIN locales l ON l.id_local = d.local_actual WHERE id_delivery = ?");
        $stmt->bind_param("i", $this->deliveryId);
        $stmt->execute();
        $result = $stmt->get_result();
        if ($delivery = $result->fetch_array(MYSQLI_ASSOC)) {
            $pm = new PedidosManager();
            $delivery['pedidos'] = $pm->getPedidos($this->deliveryId);
        }
        $stmt->close();
        return $delivery;
    }

}
