<?php

namespace App\Controllers;

use App\Models\CustomerModel;
use App\Models\JenisKainModel;
use App\Models\ModelCustomModel;
use App\Models\Models;
use App\Models\PembayaranModel;
use App\Models\PermintaanCustomModel;
use App\Models\Transaction;
use App\Models\TypeOfFabric;
use App\Models\UserModel;
use CodeIgniter\Controller;

class DashboardController extends Controller
{
    public function index()
    {
        helper('auth');
        // Pastikan user sudah login
        if (!auth()->loggedIn()) {
            return redirect()->to('/login');
        }

        $userModel = new UserModel();
        $typeModel = new JenisKainModel();
        $modelModel = new ModelCustomModel();
        $transactionModel = new PermintaanCustomModel();

        // Hitung total data
        $data['total_customer'] = $userModel->where('role', 'customer')->countAllResults();
        $data['total_employee'] = $userModel->where('role', 'kasir')->countAllResults();
        $data['total_type_of_brics'] = $typeModel->countAllResults();
        $data['total_modal'] = $modelModel->countAllResults();
        $data['total_pendapatan'] = $transactionModel->select('SUM(nominal_pelunasan) as total_sum')->first()->total_sum;
        $data['title'] = 'Dashboard';
        // Tampilkan dashboard admin
        return view('dashboard', $data);
    }
}
