<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PemasukanController;
use App\Http\Controllers\PengeluaranController;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Auth\RegisterController;
use App\Http\Controllers\SosialKasPemasukanController;
use App\Http\Controllers\SosialKasPengeluaranController;
use App\Http\Controllers\SosialKasController;
use App\Http\Controllers\AdminController;
use App\Http\Controllers\JadwalController;
use App\Http\Controllers\JadwalHarianController;
use App\Http\Controllers\PengurusMasjidController;
use App\Http\Controllers\LaporanController;
use App\Http\Controllers\LandingPageController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/
Route::get('/', [LandingPageController::class, 'index'])->name('landing');
Route::post('/sumbangan/store', [LandingPageController::class, 'storeSumbangan'])->name('sumbangan.store');



// Form Login & Register
Route::get('/login', [LoginController::class, 'showLoginForm'])->name('login');
Route::get('/login', [LoginController::class, 'showLoginForm'])->name('login');
Route::post('/login', [LoginController::class, 'login']);
Route::post('/logout', [LoginController::class, 'logout'])->name('logout');

Route::get('/register', [RegisterController::class, 'showRegistrationForm'])->name('register');
Route::post('/register', [RegisterController::class, 'register']);

Route::get('/dashboard', [App\Http\Controllers\DashboardController::class, 'index'])->name('dashboard');

Route::resource('pemasukan', PemasukanController::class);
Route::get('/pemasukan/{id}/accept', [PemasukanController::class, 'accept'])->name('pemasukan.accept');
Route::get('/pemasukan/{id}/reject', [PemasukanController::class, 'reject'])->name('pemasukan.reject');

Route::resource('pengeluaran', PengeluaranController::class);
Route::get('/data-kas', [App\Http\Controllers\DataKasController::class, 'index'])->name('data-kas.index');

Route::post('/logout', function () {
    Auth::logout();

    $role = Auth::user() ? Auth::user()->role : 'pengguna';  // Cek role jika ada

    if ($role == 'admin') {
        return redirect()->route('admin.login')->with('success', 'Anda telah berhasil logout sebagai Admin');
    } else {
        return redirect()->route('login')->with('success', 'Anda telah berhasil logout');
    }
})->name('logout');

// Route::get('/', function () {
//     return view('welcome');
// });

Route::middleware(['auth'])->group(function () {
    Route::prefix('sosial-kas')->name('sosial-kas.')->group(function () {
        Route::resource('pemasukan', SosialKasPemasukanController::class);
        Route::resource('pengeluaran', SosialKasPengeluaranController::class);
    });
});

Route::get('sosial-kas/pengeluaran/{id}/edit', [SosialKasPengeluaranController::class, 'edit'])->name('sosial-kas.pengeluaran.edit');
Route::get('/sosial-kas/pemasukan/{id}/edit', [SosialKasPemasukanController::class, 'edit'])->name('sosial-kas.pemasukan.edit');
Route::get('/sosial-kas/data-kas', [SosialKasController::class, 'index'])->name('sosial-kas.data-kas');
Route::get('/cetak-data-kas', [App\Http\Controllers\DataKasController::class, 'cetak'])->name('cetak.data.kas');

Route::prefix('admin')->middleware(['auth'])->group(function () {
    Route::get('/akun', [AdminController::class, 'index'])->name('admin.index');
    Route::get('/akun/create', [AdminController::class, 'create'])->name('admin.create');
    Route::post('/akun', [AdminController::class, 'store'])->name('admin.store');
    Route::get('/akun/{id}/edit', [AdminController::class, 'edit'])->name('admin.edit');
    Route::put('/akun/{id}', [AdminController::class, 'update'])->name('admin.update');
    Route::delete('/akun/{id}', [AdminController::class, 'destroy'])->name('admin.destroy');
});

Route::middleware(['auth'])->group(function () {
    Route::resource('jadwal', JadwalController::class)->only(['index']);
    Route::resource('jadwal', JadwalController::class);
});

Route::resource('jadwalharian', JadwalHarianController::class);
Route::resource('pengurusmasjid', PengurusMasjidController::class);

Route::get('/laporan/review', [LaporanController::class, 'review'])->name('laporan.review');
Route::get('/laporan/semua', [LaporanController::class, 'semua'])->name('laporan.semua');

Route::get('/laporan/export-pdf', [LaporanController::class, 'exportPdf'])->name('laporan.exportPdf');
Route::prefix('laporan')->group(function () {
    Route::get('/review', [LaporanController::class, 'review'])->name('laporan.review');
    Route::get('/semua', [LaporanController::class, 'semua'])->name('laporan.semua');
});

