<?php
require_once 'models/TypeVehicule.php';
require_once 'models/Tarif.php';
require_once 'models/BonVers.php';

class VersementController {
    private $db;

    public function __construct($db) {
        $this->db = $db;
    }

    public function getTypeVehicules() {
        $typeVehicule = new TypeVehicule($this->db);
        $stmt = $typeVehicule->getAll();
        $types = $stmt->fetchAll(PDO::FETCH_ASSOC);
        echo json_encode(['data' => $types]);
    }

    public function getOperateurs() {
        $type = $_GET['type'];
        $query = "SELECT id, name FROM operators WHERE type = ?";
        $stmt = $this->db->prepare($query);
        $stmt->bindParam(1, $type);
        $stmt->execute();
        $operateurs = $stmt->fetchAll(PDO::FETCH_ASSOC);
        echo json_encode(['data' => $operateurs]);
    }

    public function getBuses() {
        $id_operateur = $_GET['id_operateur'];
        $type = $_GET['type'];
        $query = "SELECT id, matricule FROM buses WHERE id_operateur = ? AND type = ?";
        $stmt = $this->db->prepare($query);
        $stmt->bindParam(1, $id_operateur);
        $stmt->bindParam(2, $type);
        $stmt->execute();
        $buses = $stmt->fetchAll(PDO::FETCH_ASSOC);
        echo json_encode(['data' => $buses]);
    }

    public function getTarif() {
        $type = $_GET['type'];
        $tarif = new Tarif($this->db);
        $stmt = $tarif->getByType($type);
        $tarifData = $stmt->fetch(PDO::FETCH_ASSOC);
        echo json_encode(['data' => $tarifData]);
    }

    public function checkPeriode() {
        $operateur = $_GET['operateur'];
        $periode = $_GET['periode'];
        $bonVers = new BonVers($this->db);
        $exists = $bonVers->checkPeriodeExistence($operateur, $periode);
        echo json_encode(['success' => true, 'exists' => $exists]);
    }

    public function createBonVers() {
        $data = json_decode(file_get_contents("php://input"));
        $bonVers = new BonVers($this->db);
        
        // Generate num_vers automatically
        $stmt = $this->db->query("SELECT COUNT(*) FROM bons_vers");
        $count = $stmt->fetchColumn();
        $bonVers->num_vers = "BON-" . str_pad($count + 1, 6, '0', STR_PAD_LEFT);
        
        $bonVers->date_vers = $data->date_vers;
        $bonVers->periode_p = $data->periode_p;
        $bonVers->operateur = $data->operateur;
        $bonVers->type_vehicule = $data->type_vehicule;
        $bonVers->mt_ttc = $data->mt_ttc;
        $bonVers->obs = $data->obs;
        $bonVers->creer_par = $data->creer_par;
        $bonVers->date_creer = date('Y-m-d H:i:s');
        
        if ($bonVers->create()) {
            echo json_encode(['success' => true, 'num_vers' => $bonVers->num_vers]);
        } else {
            echo json_encode(['success' => false]);
        }
    }

    public function printBonVers() {
        $data = json_decode(file_get_contents("php://input"));
        // Implémentez la logique d'impression ici
        echo json_encode(['success' => true]);
    }
}
?>