<?php
class Vehicle {

    private $conn;
    private $table_name = "vehicle";

    public $id;
    public $license_plate;
    public $vehicle_category;
    public $transmission;
    public $seats;
    public $remark;
    public $entry_by;
    public $update_by;

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

    public function create() {
        $query = "INSERT INTO " . $this->table_name . "
                  SET vehicle_no = :license_plate,
                      vehicle_category = :vehicle_category,
                      vehicle_note = :transmission,
                      seats = :seats,
                      rental_rate = 0,
                      extra_driver = 0,
                      extra_passenger = 0,
                      remark = :remark,
                      entry_by = :entry_by";

        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(":license_plate", $this->license_plate);
        $stmt->bindParam(":vehicle_category", $this->vehicle_category);
        $stmt->bindParam(":transmission", $this->transmission);
        $stmt->bindParam(":seats", $this->seats);
        $stmt->bindParam(":remark", $this->remark);
        $stmt->bindParam(":entry_by", $this->entry_by);

        return $stmt->execute();
    }

    public function update() {
        $query = "UPDATE " . $this->table_name . "
                  SET vehicle_category = :vehicle_category,
                      vehicle_note = :transmission,
                      seats = :seats,
                      rental_rate = 0,
                      extra_driver = 0,
                      extra_passenger = 0,
                      remark = :remark,
                      update_by = :update_by
                  WHERE id = :id";

        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(":id", $this->id);
        $stmt->bindParam(":vehicle_category", $this->vehicle_category);
        $stmt->bindParam(":transmission", $this->transmission);
        $stmt->bindParam(":seats", $this->seats);
        $stmt->bindParam(":remark", $this->remark);
        $stmt->bindParam(":update_by", $this->update_by);

        return $stmt->execute();
    }

    public function delete() {
        $query = "DELETE FROM " . $this->table_name . " WHERE id = ?";
        $stmt = $this->conn->prepare($query);
        $this->id = htmlspecialchars(strip_tags($this->id));
        $stmt->bindParam(1, $this->id);

        return $stmt->execute();
    }

    public function getOneRow() {
        $query = "SELECT id, vehicle_no, vehicle_category, vehicle_note, seats, remark
                  FROM " . $this->table_name . "
                  WHERE id = ?";

        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $this->id);
        $stmt->execute();
        $row = $stmt->fetch(PDO::FETCH_ASSOC);

        if ($row) {
            $this->id = $row['id'];
            $this->license_plate = $row['vehicle_no'];
            $this->vehicle_category = $row['vehicle_category'];
            $this->transmission = $row['vehicle_note'];
            $this->seats = $row['seats'];
            $this->remark = $row['remark'];
        }
    }

    public function getAllRows() {
        $query = "SELECT id, vehicle_no, vehicle_category, vehicle_note, seats, remark
                  FROM " . $this->table_name . "
                  ORDER BY vehicle_no";

        $stmt = $this->conn->prepare($query);
        $stmt->execute();

        return $stmt;
    }

    public function exist() {
        $query = "SELECT id FROM " . $this->table_name . " WHERE vehicle_no = ? LIMIT 0,1";
        $stmt = $this->conn->prepare($query);
        $this->license_plate = htmlspecialchars(strip_tags($this->license_plate));
        $stmt->bindParam(1, $this->license_plate);
        $stmt->execute();

        return $stmt->rowCount() > 0;
    }
}
?>
