<?php
class InvoiceItem {
    private $conn;
    private $table_name = "invoice_items";

    public $id;
    public $invoice_id;
    public $item_id;
    public $description;
    public $quantity;
    public $unit_price;
    public $line_total;
    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, item_id=:item_id, description=:description, quantity=:quantity, unit_price=:unit_price, line_total=:line_total, created_at=:created_at, updated_at=:updated_at";

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

        $this->invoice_id = htmlspecialchars(strip_tags($this->invoice_id));
        $this->item_id = htmlspecialchars(strip_tags($this->item_id));
        $this->description = htmlspecialchars(strip_tags($this->description));
        $this->quantity = htmlspecialchars(strip_tags($this->quantity));
        $this->unit_price = htmlspecialchars(strip_tags($this->unit_price));
        $this->line_total = htmlspecialchars(strip_tags($this->line_total));
        $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(":item_id", $this->item_id);
        $stmt->bindParam(":description", $this->description);
        $stmt->bindParam(":quantity", $this->quantity);
        $stmt->bindParam(":unit_price", $this->unit_price);
        $stmt->bindParam(":line_total", $this->line_total);
        $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 ii.*, i.name as item_name FROM " . $this->table_name . " ii LEFT JOIN items i ON ii.item_id = i.id WHERE ii.invoice_id = ? ORDER BY ii.id";

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

        return $stmt;
    }

    public function readOne() {
        $query = "SELECT ii.*, i.name as item_name FROM " . $this->table_name . " ii LEFT JOIN items i ON ii.item_id = i.id WHERE ii.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->item_id = $row['item_id'];
        $this->description = $row['description'];
        $this->quantity = $row['quantity'];
        $this->unit_price = $row['unit_price'];
        $this->line_total = $row['line_total'];
        $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, item_id=:item_id, description=:description, quantity=:quantity, unit_price=:unit_price, line_total=:line_total, updated_at=:updated_at WHERE id=:id";

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

        $this->invoice_id = htmlspecialchars(strip_tags($this->invoice_id));
        $this->item_id = htmlspecialchars(strip_tags($this->item_id));
        $this->description = htmlspecialchars(strip_tags($this->description));
        $this->quantity = htmlspecialchars(strip_tags($this->quantity));
        $this->unit_price = htmlspecialchars(strip_tags($this->unit_price));
        $this->line_total = htmlspecialchars(strip_tags($this->line_total));
        $this->updated_at = date('Y-m-d H:i:s');

        $stmt->bindParam(":invoice_id", $this->invoice_id);
        $stmt->bindParam(":item_id", $this->item_id);
        $stmt->bindParam(":description", $this->description);
        $stmt->bindParam(":quantity", $this->quantity);
        $stmt->bindParam(":unit_price", $this->unit_price);
        $stmt->bindParam(":line_total", $this->line_total);
        $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;
    }
}
?>
