<?php
class Subproject {
    private $conn;
    private $table_name = "subprojects";

    public $id;
    public $project_id;
    public $subproject_name;
    public $description;
    public $status;
    public $start_date;
    public $end_date;
    public $budget;
    public $created_at;
    public $updated_at;

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

    public function create() {
        $query = "INSERT INTO " . $this->table_name . " SET project_id=:project_id, subproject_name=:subproject_name, description=:description, status=:status, start_date=:start_date, end_date=:end_date, budget=:budget, created_at=:created_at, updated_at=:updated_at";

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

        $this->project_id = htmlspecialchars(strip_tags($this->project_id));
        $this->subproject_name = htmlspecialchars(strip_tags($this->subproject_name));
        $this->description = htmlspecialchars(strip_tags($this->description));
        $this->status = htmlspecialchars(strip_tags($this->status));
        $this->start_date = htmlspecialchars(strip_tags($this->start_date));
        $this->end_date = htmlspecialchars(strip_tags($this->end_date));
        $this->budget = htmlspecialchars(strip_tags($this->budget));
        $this->created_at = date('Y-m-d H:i:s');
        $this->updated_at = date('Y-m-d H:i:s');

        $stmt->bindParam(":project_id", $this->project_id);
        $stmt->bindParam(":subproject_name", $this->subproject_name);
        $stmt->bindParam(":description", $this->description);
        $stmt->bindParam(":status", $this->status);
        $stmt->bindParam(":start_date", $this->start_date);
        $stmt->bindParam(":end_date", $this->end_date);
        $stmt->bindParam(":budget", $this->budget);
        $stmt->bindParam(":created_at", $this->created_at);
        $stmt->bindParam(":updated_at", $this->updated_at);

        if($stmt->execute()) {
            return true;
        }

        return false;
    }

    public function read() {
        $query = "SELECT * FROM " . $this->table_name . " ORDER BY created_at DESC";

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

        return $stmt;
    }

    public function readOne() {
        $query = "SELECT * FROM " . $this->table_name . " WHERE id = ? LIMIT 0,1";

        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $this->id);
        $stmt->execute();

        $row = $stmt->fetch(PDO::FETCH_ASSOC);

        $this->project_id = $row['project_id'];
        $this->subproject_name = $row['subproject_name'];
        $this->description = $row['description'];
        $this->status = $row['status'];
        $this->start_date = $row['start_date'];
        $this->end_date = $row['end_date'];
        $this->budget = $row['budget'];
        $this->created_at = $row['created_at'];
        $this->updated_at = $row['updated_at'];
    }

    public function update() {
        $query = "UPDATE " . $this->table_name . " SET project_id=:project_id, subproject_name=:subproject_name, description=:description, status=:status, start_date=:start_date, end_date=:end_date, budget=:budget, updated_at=:updated_at WHERE id=:id";

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

        $this->project_id = htmlspecialchars(strip_tags($this->project_id));
        $this->subproject_name = htmlspecialchars(strip_tags($this->subproject_name));
        $this->description = htmlspecialchars(strip_tags($this->description));
        $this->status = htmlspecialchars(strip_tags($this->status));
        $this->start_date = htmlspecialchars(strip_tags($this->start_date));
        $this->end_date = htmlspecialchars(strip_tags($this->end_date));
        $this->budget = htmlspecialchars(strip_tags($this->budget));
        $this->updated_at = date('Y-m-d H:i:s');

        $stmt->bindParam(":project_id", $this->project_id);
        $stmt->bindParam(":subproject_name", $this->subproject_name);
        $stmt->bindParam(":description", $this->description);
        $stmt->bindParam(":status", $this->status);
        $stmt->bindParam(":start_date", $this->start_date);
        $stmt->bindParam(":end_date", $this->end_date);
        $stmt->bindParam(":budget", $this->budget);
        $stmt->bindParam(":updated_at", $this->updated_at);
        $stmt->bindParam(":id", $this->id);

        if($stmt->execute()) {
            return true;
        }

        return false;
    }

    public function delete() {
        $query = "DELETE FROM " . $this->table_name . " WHERE id = ?";

        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $this->id);

        if($stmt->execute()) {
            return true;
        }

        return false;
    }

    public function search($search_term) {
        $query = "SELECT * FROM " . $this->table_name . " WHERE 
                  subproject_name LIKE ? OR 
                  description LIKE ?
                  ORDER BY created_at DESC";

        $stmt = $this->conn->prepare($query);
        $search_term = "%{$search_term}%";
        $stmt->bindParam(1, $search_term);
        $stmt->bindParam(2, $search_term);
        $stmt->execute();

        return $stmt;
    }

    public function getStatusBadge() {
        $badges = [
            'planning' => '<span style="background-color: #6c757d; color: white; padding: 2px 6px; border-radius: 4px; font-size: 12px;">Planning</span>',
            'in_progress' => '<span style="background-color: #17a2b8; color: white; padding: 2px 6px; border-radius: 4px; font-size: 12px;">In Progress</span>',
            'completed' => '<span style="background-color: #28a745; color: white; padding: 2px 6px; border-radius: 4px; font-size: 12px;">Completed</span>',
            'on_hold' => '<span style="background-color: #ffc107; color: black; padding: 2px 6px; border-radius: 4px; font-size: 12px;">On Hold</span>',
            'cancelled' => '<span style="background-color: #dc3545; color: white; padding: 2px 6px; border-radius: 4px; font-size: 12px;">Cancelled</span>'
        ];
        
        return $badges[$this->status] ?? $this->status;
    }

    public function getProgress() {
        if ($this->start_date && $this->end_date) {
            $start = new DateTime($this->start_date);
            $end = new DateTime($this->end_date);
            $now = new DateTime();
            
            if ($now < $start) {
                return 0; // Not started yet
            } elseif ($now > $end) {
                return 100; // Completed
            } else {
                $total = $end->diff($start)->days;
                $elapsed = $now->diff($start)->days;
                return min(100, round(($elapsed / $total) * 100));
            }
        }
        return 0;
    }
}
?>
