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

class SlipSettings extends CI_Controller {

    public function __construct() {
        parent::__construct();
        // Printer/slip settings only for logged-in admins
        if (!isset($_SESSION['qurbani_user_id']) || $_SESSION['qurbani_role'] != 'sa') {
            redirect('/admin', 'refresh');
        }
        $this->load->model('Slip_settings_model', 'slipsetup');
    }

    public function index() {
        $data['settings'] = $this->slipsetup->get_by_id(1); // always id = 1
        $data['title'] = 'Slip / Printer Settings';
        $data['breadcrumb'] = [
            ['label' => 'Settings', 'url' => base_url('admin/settings')],
            ['label' => 'POS Printer', 'url' => '']
        ];
        $data['mainContent'] = 'admin/settings/tabs/pos-printer/index';
        $this->load->view('admin/layout/master', $data);
    }

// public function save()
// {
//     $id = 1; // always id = 1

//     $data = [
//         'printer_name' => $this->input->post('printer_name'),
//         'slip_footer'  => $this->input->post('slip_footer'),
//         'updated_at'   => date('Y-m-d H:i:s')
//     ];

//     // Directory
//     $upload_path = FCPATH . 'uploads/settings/';
//     if (!is_dir($upload_path)) {
//         mkdir($upload_path, 0755, true);
//     }

//     // Handle logo upload
//     if (!empty($_FILES['slip_logo']['name'])) {

//         // accept both bmp and BMP
//         $config = [
//             'upload_path'   => $upload_path,
//             'allowed_types' => 'bmp|BMP',
//             'file_name'     => 'Icon.bmp', // force exact Icon.bmp name
//             'overwrite'     => true
//         ];

//         $this->load->library('upload');
//         $this->upload->initialize($config); // **important**

//         if ($this->upload->do_upload('slip_logo')) {
//             $uploadData = $this->upload->data();
//             $data['slip_logo'] = $uploadData['file_name']; // should be Icon.bmp
//         } else {
//             $this->session->set_flashdata('error', $this->upload->display_errors());
//             redirect('admin/settings#posprinterTab');
//             return;
//         }
//     }

//     // Save settings row
//     $existing = $this->slipsetup->get_by_id($id);

//     if ($existing) {
//         $this->slipsetup->update($id, $data);
//     } else {
//         $data['id'] = $id;
//         $data['created_at'] = date('Y-m-d H:i:s');
//         $this->slipsetup->insert($data);
//     }

//     $this->session->set_flashdata('success', 'Slip settings saved successfully');
//     redirect('admin/settings#posprinterTab');
// }

























public function save()
{
    // Always use id = 1 for settings
    $id = 1;

    // collect post values
    $data = [
        'printer_name' => $this->input->post('printer_name'),
        'slip_footer'  => $this->input->post('slip_footer'),
        'updated_at'   => date('Y-m-d H:i:s')
    ];

    // handle file uploads
    $upload_fields = ['slip_logo'];

    // absolute path on server
    $config['upload_path']   = FCPATH . 'uploads/settings/';   // e.g. /var/www/html/project/uploads/settings/
    $config['allowed_types'] = 'bmp|BMP';
    $this->load->library('upload');

    // make sure directory exists
    if (!is_dir($config['upload_path'])) {
        mkdir($config['upload_path'], 0755, true);
    }

    foreach ($upload_fields as $field) {
        if (!empty($_FILES[$field]['name'])) {
            $config['file_name'] = time() . '_' . preg_replace('/\s+/', '_', $_FILES[$field]['name']);
            $this->upload->initialize($config);
            if ($this->upload->do_upload($field)) {
                $upload_data      = $this->upload->data();
                $data[$field] = $upload_data['file_name'];
            } else {
                log_message('error', 'Upload error for '.$field.': '.$this->upload->display_errors());
            }
        }
    }

    // Check if row id=1 exists
    $existing = $this->slipsetup->get_by_id($id); // create a simple get_by_id() in your model

    if ($existing) {
        $this->slipsetup->update($id, $data);
    } else {
        $data['id'] = $id; // force id = 1
        $data['created_at'] = date('Y-m-d H:i:s');
        $this->slipsetup->insert($data);
    }

    $this->session->set_flashdata('success', 'Settings saved successfully');
    redirect('admin/settings/#posprinterTab');
}



}
