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

class Qrimages extends CI_Controller {

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

        if (!isset($_SESSION['qurbani_user_id'])) {
            redirect('/admin','refresh');
        }

        $this->load->helper('url');
    }

    public function index()
    {
        $data['img_url'] = '';

        if ($this->input->post('action') && $this->input->post('action') == "generate_qrcode") {
            $this->load->library('ciqrcode');

            // 📌 Setup QR image filename
            $qr_image = 'qr_' . time() . '.bmp';

            // 📌 Generate QR with full URL
            $link = $this->input->post('qr_text'); // e.g. https://example.com/view/1

            $params['data'] = $link;
            $params['level'] = 'H';
            $params['size'] = 8;
            $params['savename'] = FCPATH . "uploads/qr_image/" . $qr_image;

            if ($this->ciqrcode->generate($params)) {
                $data['img_url'] = base_url('uploads/qr_image/' . $qr_image);
            }
        }

        $data['title'] = 'QR Code Generator';
        $data['mainContent'] = 'admin/qrsystem/index'; // make sure this view exists
        $this->load->view('admin/layout/master', $data);
    }
}
