<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Render extends CI_Controller {

    public function __construct()
    {
        parent::__construct();

        // QRcode() invoice QR images ke liye PUBLIC rehna zaroori hai:
        //  - tokens/print.php server-side loopback se inhe fetch karta hai (bina session)
        //  - public bill_print_view ke <img> tags bhi inhe use karte hain
        $current_method = $this->router->fetch_method();
        if (strcasecmp($current_method, 'QRcode') !== 0 && !isset($_SESSION['qurbani_user_id'])) {
            redirect('/admin', 'refresh');
        }

        $this->load->model('Settings_model', 'setup');

        // Load QR Code Library
        $this->load->library('Ciqrcode');
    }

    
    

    // Optional: QR Preview Page
    public function index()
    {
        $data['title'] = 'My QR Code';
        $data['settings'] = $this->setup->get_all(); // Assuming you have a settings table/model
        $this->load->view('admin/render/rander', $data);
    }

    // ✅ This method will generate QR Code from invoice ID
    public function QRcode($invoice_id = null)
    {
        if (!$invoice_id) {
            show_error('Invoice ID is missing.', 400);
        }

        // Construct the full invoice URL to encode
        $url = base_url('admin/tokens/bill_print_view/' . $invoice_id);

        // Generate QR Code for the invoice link
        // QRcode::png(
        //     $url,
        //     $outfile = false,
        //     $level = QR_ECLEVEL_H,
        //     $size = 350,
        //     $margin = 2
        // );
QRcode::png(
    $url,
    false,
    QR_ECLEVEL_L,
    2, // size (module size)
    1  // margin
);
    }
}
