<?php
class CertificateSetting {
    private $conn;
    private $table_name = "certificate_settings";

    public $id;
    public $institution_name;
    public $institution_address;
    public $registrar_signature;
    public $rector_signature;
    public $official_seal;
    public $default_issue_date;

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

    public function read() {
        $query = "SELECT * FROM " . $this->table_name . " ORDER BY id DESC LIMIT 1";
        $stmt = $this->conn->prepare($query);
        $stmt->execute();
        
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        
        if($row) {
            $this->id = $row['id'];
            $this->institution_name = $row['institution_name'];
            $this->institution_address = $row['institution_address'];
            $this->registrar_signature = $row['registrar_signature'];
            $this->rector_signature = $row['rector_signature'];
            $this->official_seal = $row['official_seal'];
            $this->default_issue_date = $row['default_issue_date'];
        }
        
        return $row;
    }

    public function update() {
        $query = "UPDATE " . $this->table_name . " 
                  SET institution_name = :institution_name,
                      institution_address = :institution_address,
                      registrar_signature = :registrar_signature,
                      rector_signature = :rector_signature,
                      official_seal = :official_seal,
                      default_issue_date = :default_issue_date,
                      updated_at = NOW()
                  WHERE id = :id";

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

        // Bind values
        $stmt->bindParam(":institution_name", $this->institution_name);
        $stmt->bindParam(":institution_address", $this->institution_address);
        $stmt->bindParam(":registrar_signature", $this->registrar_signature);
        $stmt->bindParam(":rector_signature", $this->rector_signature);
        $stmt->bindParam(":official_seal", $this->official_seal);
        $stmt->bindParam(":default_issue_date", $this->default_issue_date);
        $stmt->bindParam(":id", $this->id);

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