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

class Payments extends CI_Controller {

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

        // Check if user is logged in
        if (!isset($_SESSION['qurbani_user_id'])) {
            redirect('/admin','refresh');
        }

        // Load required models
        $this->load->model('Payment_model', 'payment');
        $this->load->model('User_model', 'user');
        $this->load->model('Settings_model', 'setup');
        $this->load->model('Shifts_model', 'shifts');
    }



    /**
     * Break the dead sidebar link (no standalone add-payment page exists)
     */
    public function add()
    {
        redirect('admin/payments');
    }

    /**
     * Payments list page
     */
    public function index()
    {
        // Payments table
        $data['payments'] = $this->payment->get_all();

        // Today's totals
        $today = $this->payment->get_all_today();
        $data['todays_cash'] = ($today->debit ?? 0) - ($today->credit ?? 0);

        // User-wise today
        $users = $this->user->get_all();
        $user_cash_totals = [];
        foreach ($users as $user) {
            $cash = $this->payment->get_total_cash_by_user($user->id, true); // today = true
            if($cash > 0) {
                $user_cash_totals[$user->id] = $cash;
            }
        }
        $data['users'] = $users;
        $data['user_cash_totals'] = $user_cash_totals;

        // Shift-wise today
        $shifts = $this->shifts->get_all();
        $shift_cash_totals = $this->payment->get_shift_cash_totals_today($shifts);
        $data['shifts'] = $shifts;
        $data['shift_cash_totals'] = $shift_cash_totals;



     $user_id = $this->session->userdata('qurbani_user_id');

// if you want shift-wise cash:
$current_shift = $this->shifts->get_current_shift(); // you’ll write this
$cash_in_hand = $this->payment->get_user_cash_in_hand($user_id, $current_shift);

// or if you want day-wise cash only:
// $cash_in_hand = $this->payment->get_user_cash_in_hand($user_id);

$this->session->set_userdata('cash_in_hand', $cash_in_hand);



        // Page settings
        $data['title'] = 'Payments';
        $data['settings'] = $this->setup->get_by_id(1);
        $data['mainContent'] = 'admin/payments/index';

        $this->load->view('admin/layout/master', $data);
    }

    /**
     * AJAX: Get today cash totals (general, user-wise, shift-wise)
     */
    public function get_today_cash_ajax()
    {
        $response = [];

        // General total
        $todays_cash = $this->payment->get_all_today();
        $response['todays_cash'] = ($todays_cash->debit ?? 0) - ($todays_cash->credit ?? 0);

        // User-wise totals
        $users = $this->user->get_all();
        $user_cash_totals = [];
        foreach ($users as $user) {
            $user_cash_totals[$user->id] = $this->payment->get_total_cash_by_user($user->id, true);
        }
        $response['user_cash_totals'] = $user_cash_totals;

        // Shift-wise totals
        $shifts = $this->shifts->get_all();
        $shift_cash_totals = $this->payment->get_shift_cash_totals_today($shifts);
        $response['shift_cash_totals'] = $shift_cash_totals;

        // Return JSON
        $this->output
             ->set_content_type('application/json')
             ->set_output(json_encode($response));
    }


// AJAX: day-wise totals for a month
public function daily_totals_ajax()
{
    $year  = $this->input->get('year') ?: date('Y');
    $month = $this->input->get('month') ?: date('m');

    $rows = $this->payment->get_daily_totals_by_month($year, $month);

    $days = [];
    $totals = [];
    foreach ($rows as $row) {
        $days[] = date('d-M', strtotime($row->day));
        $totals[] = ($row->debit ?? 0) - ($row->credit ?? 0);
    }

    $this->output
         ->set_content_type('application/json')
         ->set_output(json_encode(['labels' => $days, 'data' => $totals]));
}

// AJAX: month-wise totals for a year
public function monthly_totals_ajax()
{
    $year = $this->input->get('year') ?: date('Y');

    $rows = $this->payment->get_monthly_totals_by_year($year);

    $months = [];
    $totals = [];
    foreach ($rows as $row) {
        $months[] = date('M', mktime(0,0,0,$row->month,1));
        $totals[] = ($row->debit ?? 0) - ($row->credit ?? 0);
    }

    $this->output
         ->set_content_type('application/json')
         ->set_output(json_encode(['labels' => $months, 'data' => $totals]));
}

}
