<?php
class Payment {
    private $conn;
    private $table_name = "payments";

    public $id;
    public $invoice_id;
    public $client_id;
    public $payment_date;
    public $amount;
    public $payment_method;
    public $reference_number;
    public $notes;
    public $created_at;
    public $updated_at;

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

    public function create() {
        $query = "INSERT INTO " . $this->table_name . " SET invoice_id=:invoice_id, client_id=:client_id, payment_date=:payment_date, amount=:amount, payment_method=:payment_method, reference_number=:reference_number, notes=:notes, created_at=:created_at, updated_at=:updated_at";

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

        $this->invoice_id = htmlspecialchars(strip_tags($this->invoice_id));
        $this->client_id = htmlspecialchars(strip_tags($this->client_id));
        $this->payment_date = htmlspecialchars(strip_tags($this->payment_date));
        $this->amount = htmlspecialchars(strip_tags($this->amount));
        $this->payment_method = htmlspecialchars(strip_tags($this->payment_method));
        $this->reference_number = htmlspecialchars(strip_tags($this->reference_number));
        $this->notes = htmlspecialchars(strip_tags($this->notes));
        $this->created_at = date('Y-m-d H:i:s');
        $this->updated_at = date('Y-m-d H:i:s');

        $stmt->bindParam(":invoice_id", $this->invoice_id);
        $stmt->bindParam(":client_id", $this->client_id);
        $stmt->bindParam(":payment_date", $this->payment_date);
        $stmt->bindParam(":amount", $this->amount);
        $stmt->bindParam(":payment_method", $this->payment_method);
        $stmt->bindParam(":reference_number", $this->reference_number);
        $stmt->bindParam(":notes", $this->notes);
        $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 p.*, i.invoice_number, c.first_name, c.last_name, c.company_name FROM " . $this->table_name . " p LEFT JOIN invoices i ON p.invoice_id = i.id LEFT JOIN clients c ON p.client_id = c.id ORDER BY p.payment_date DESC, p.created_at DESC";

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

        return $stmt;
    }

    public function readOne() {
        $query = "SELECT p.*, i.invoice_number, c.first_name, c.last_name, c.company_name FROM " . $this->table_name . " p LEFT JOIN invoices i ON p.invoice_id = i.id LEFT JOIN clients c ON p.client_id = c.id WHERE p.id = ? LIMIT 0,1";

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

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

        $this->invoice_id = $row['invoice_id'];
        $this->client_id = $row['client_id'];
        $this->payment_date = $row['payment_date'];
        $this->amount = $row['amount'];
        $this->payment_method = $row['payment_method'];
        $this->reference_number = $row['reference_number'];
        $this->notes = $row['notes'];
        $this->created_at = $row['created_at'];
        $this->updated_at = $row['updated_at'];
    }

    public function update() {
        $query = "UPDATE " . $this->table_name . " SET invoice_id=:invoice_id, client_id=:client_id, payment_date=:payment_date, amount=:amount, payment_method=:payment_method, reference_number=:reference_number, notes=:notes, updated_at=:updated_at WHERE id=:id";

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

        $this->invoice_id = htmlspecialchars(strip_tags($this->invoice_id));
        $this->client_id = htmlspecialchars(strip_tags($this->client_id));
        $this->payment_date = htmlspecialchars(strip_tags($this->payment_date));
        $this->amount = htmlspecialchars(strip_tags($this->amount));
        $this->payment_method = htmlspecialchars(strip_tags($this->payment_method));
        $this->reference_number = htmlspecialchars(strip_tags($this->reference_number));
        $this->notes = htmlspecialchars(strip_tags($this->notes));
        $this->updated_at = date('Y-m-d H:i:s');

        $stmt->bindParam(":invoice_id", $this->invoice_id);
        $stmt->bindParam(":client_id", $this->client_id);
        $stmt->bindParam(":payment_date", $this->payment_date);
        $stmt->bindParam(":amount", $this->amount);
        $stmt->bindParam(":payment_method", $this->payment_method);
        $stmt->bindParam(":reference_number", $this->reference_number);
        $stmt->bindParam(":notes", $this->notes);
        $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 p.*, i.invoice_number, c.first_name, c.last_name, c.company_name FROM " . $this->table_name . " p LEFT JOIN invoices i ON p.invoice_id = i.id LEFT JOIN clients c ON p.client_id = c.id WHERE 
                  p.reference_number LIKE ? OR 
                  p.notes LIKE ? OR 
                  i.invoice_number LIKE ? OR 
                  c.first_name LIKE ? OR 
                  c.last_name LIKE ? OR 
                  c.company_name LIKE ?
                  ORDER BY p.payment_date DESC, p.created_at DESC";

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

        return $stmt;
    }

    public function getPaymentMethodBadge() {
        $badges = [
            'cash' => '<span style="background-color: #28a745; color: white; padding: 2px 6px; border-radius: 4px; font-size: 12px;">Cash</span>',
            'check' => '<span style="background-color: #17a2b8; color: white; padding: 2px 6px; border-radius: 4px; font-size: 12px;">Check</span>',
            'credit_card' => '<span style="background-color: #6f42c1; color: white; padding: 2px 6px; border-radius: 4px; font-size: 12px;">Credit Card</span>',
            'bank_transfer' => '<span style="background-color: #20c997; color: white; padding: 2px 6px; border-radius: 4px; font-size: 12px;">Bank Transfer</span>',
            'other' => '<span style="background-color: #6c757d; color: white; padding: 2px 6px; border-radius: 4px; font-size: 12px;">Other</span>'
        ];
        
        return $badges[$this->payment_method] ?? $this->payment_method;
    }
}
?>
