<?php

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\FinanceController;
use App\Http\Controllers\CourseController;
use App\Http\Controllers\StudentController;
use App\Http\Controllers\CalendarController;
use App\Http\Controllers\ReportController;
use App\Http\Controllers\LogController;
use App\Http\Controllers\ForgotPasswordController;
use App\Http\Controllers\IncomeController;
use App\Http\Controllers\WithdrawController;
use App\Http\Controllers\TeacherController;

Auth::routes();
// หน้าแรก
Route::group(['middleware' => 'auth'], function () {
    Route::get('/', [HomeController::class, 'index'])->name('home.index');
    Route::get('/exam', [HomeController::class, 'exam_student_this_week'])->name('home.showExam');
    Route::get('/tomorrow', [HomeController::class, 'tomorrow'])->name('home.showTomorrow');
    Route::post('/exam/change', [HomeController::class, 'update_exam'])->name('home.updateExam');
});

// ครูสแกนว่าง
Route::get('/teacher-scan', [TeacherController::class, 'scan_index'])->name('teacher_scan.index')->middleware('auth');

Route::post('/teacher-checkout', [TeacherController::class, 'checkout'])->name('teacher.checkout')->middleware('auth');

// ตารางเรียน
Route::group(['middleware' => 'auth'], function () {
    Route::get('/calendar', [CalendarController::class, 'index'])->name('calendar');
    Route::get('/calendar/date', [CalendarController::class, 'submit'])->name('calendar.submit');
    Route::get('/calendar-day', [CalendarController::class, 'indexDay'])->name('calendar.index_day');
    Route::get('/calendar-day/date', [CalendarController::class, 'submitDay'])->name('calendar.submit_day');
    Route::delete('/calendar', [CalendarController::class, 'cancelBooking'])->name('calendar.cancel_booking');
    Route::post('/calendar/booking', [CalendarController::class, 'teacherBooking'])->name('calendar.booking');
    Route::post('/calendar/vehicleBooking', [CalendarController::class, 'vehicleBooking'])->name('calendar.vehicle_booking');

    Route::get('/calendar-test', [CalendarController::class, 'testDay'])->name('calendar.test_day');
});

// รายการรายรับ
Route::group(['middleware' => 'auth'], function () {
    Route::resource('/income', IncomeController::class);
    Route::post('/income', [IncomeController::class, 'submit'])->name('income.submit');
    Route::post('/income/income', [IncomeController::class, 'income'])->name('income.income');
});

// รายการเบิกเงิน
Route::group(['middleware' => 'auth'], function () {
    Route::resource('/withdraw', WithdrawController::class);
    Route::post('/withdraw', [WithdrawController::class, 'submit'])->name('withdraw.submit');
    Route::post('/withdraw/withdraw', [WithdrawController::class, 'withdraw'])->name('withdraw.withdraw');
});

// การจัดการนักเรียน
Route::group(['middleware' => 'auth'], function () {
    Route::resource('/student', StudentController::class);
    Route::put('/student/graduate/{id}', [StudentController::class, 'update_graduate_status'])->name('student.graduate');
    Route::post('/student/payment', [StudentController::class, 'update_payment'])->name('student.payment');
    Route::post('/student/addBooking', [StudentController::class, 'add_booking'])->name('student.add_booking');
    Route::post('/student/editBooking', [StudentController::class, 'edit_booking'])->name('student.edit_booking');
    Route::post('/student/deleteBooking', [StudentController::class, 'delete_booking'])->name('student.delete_booking');
    Route::post('/student/deleteGraduated', [StudentController::class, 'delete_graduated'])->name('student.destroy_graduated');
    Route::post('/student/addExam', [StudentController::class, 'add_exam'])->name('student.add_exam');
    Route::put('/student/exam/{id}', [StudentController::class, 'update_exam'])->name('student.exam');
    Route::post('/student/course/{id}', [StudentController::class, 'update_course'])->name('student.course');
});

// การจัดการคอร์สเรียน
Route::group(['middleware' => 'auth'], function () {
    Route::resource('/course', CourseController::class);
    Route::post('/course/addModule', [CourseController::class, 'add_module'])->name('course.add_module');
    Route::post('/course/deleteModule', [CourseController::class, 'delete_module'])->name('course.delete_module');
});

// การจัดการผู้สอน
Route::resource('/teacher', TeacherController::class)->middleware('auth');

// การจัดการยานพาหนะ
Route::resource('/vehicle', App\Http\Controllers\VehicleController::class)->middleware('auth');

// การจัดการสาขา
Route::resource('/branch', App\Http\Controllers\BranchController::class)->middleware('auth');

// การจัดการค่าจ้าง
Route::resource('/payment', App\Http\Controllers\PaymentController::class)->middleware('auth', 'can:isSuperadmin');

// การจัดการผู้ใช้ระบบ
Route::resource('/user', App\Http\Controllers\UserController::class)->middleware('auth', 'can:isSuperadmin');

// การเงิน
Route::get('/finance', [FinanceController::class, 'index'])->name('finance')->middleware('auth');
Route::post('/finance', [FinanceController::class, 'submit'])->name('finance.submit')->middleware('auth');

// รายงาน
Route::middleware(['auth', 'can:isSuperadmin'])->group(function () {
    Route::get('/report', [ReportController::class, 'index'])->name('report');
    Route::post('/report', [ReportController::class, 'submit'])->name('report.submit');
});

// นักเรียนที่เรียนจบแล้ว
Route::get('/graduated', [StudentController::class, 'graduated'])->name('student.graduated')->middleware('auth', 'can:isSuperadmin');
Route::post('/graduated', [StudentController::class, 'graduated_submit'])->name('student.graduated_submit')->middleware('auth', 'can:isSuperadmin');
Route::get('/graduated/{id}', [StudentController::class, 'show_graduated'])->name('student.show_graduated')->middleware('auth', 'can:isSuperadmin');
Route::get('/graduated/reactivate/{id}', [StudentController::class, 'reactivate'])->name('student.reactivate')->middleware('auth', 'can:isSuperadmin');

// บันทึกการเข้าระบบ
Route::get('/accesslog', [App\Http\Controllers\LogController::class, 'access_log'])->name('log.access')->middleware('can:isSuperadmin');
Route::post('/accesslog', [App\Http\Controllers\LogController::class, 'access_log_post'])->name('log.access_post')->middleware('auth', 'can:isSuperadmin');

// บันทึกการใช้งานระบบ
Route::get('/log', [App\Http\Controllers\LogController::class, 'event_log'])->name('log.event')->middleware('auth', 'can:isSuperadmin');
Route::post('/log', [App\Http\Controllers\LogController::class, 'event_log_post'])->name('log.event_post')->middleware('auth', 'can:isSuperadmin');

// ข้อมูลส่วนตัว
Route::get('/info', [App\Http\Controllers\UserController::class, 'info'])->name('user.info')->middleware('auth');
Route::post('/info/edit', [App\Http\Controllers\UserController::class, 'info_edit'])->name('user.info_edit')->middleware('auth');
Route::post('/info/update', [App\Http\Controllers\UserController::class, 'info_update'])->name('user.info_update')->middleware('auth');
Route::post('/user/changepass', [App\Http\Controllers\UserController::class, 'change_password'])->name('user.change_password')->middleware('auth');
Route::post('/user/changepass/update', [App\Http\Controllers\UserController::class, 'update_change_password'])->name('user.update_change_password')->middleware('auth');
Route::get('/user/{id}/changepass', [App\Http\Controllers\UserController::class, 'admin_change_password'])->name('user.admin_change_password')->middleware('auth', 'can:isSuperadmin');
Route::put('/user/changepass/{id}', [App\Http\Controllers\UserController::class, 'admin_update_change_password'])->name('user.admin_update_change_password')->middleware('auth', 'can:isSuperadmin');

Route::post('/teacher/month', [App\Http\Controllers\TeacherController::class, 'show_month_selected'])->name('teacher.showSelected')->middleware('auth');
Route::post('/teacher/withdraw', [App\Http\Controllers\TeacherController::class, 'withdraw'])->name('teacher.withdraw')->middleware('auth');

Route::group(['middleware' => 'auth'], function () {
    Route::get('/getVehicles/{type}/{gear}/{date}/{starttime}/{endtime}/{drive}/{purpose}/{category}', [App\Http\Controllers\VehicleController::class, 'checkVehicleAvailable']);
    Route::get('/getModule/{course}', [App\Http\Controllers\CourseController::class, 'checkCourseModule']);
    Route::get('/getTeacher/{date}', [App\Http\Controllers\WithdrawController::class, 'checkTeacher']);
    Route::get('/getTeacherEarn/{teacher}/{date}', [App\Http\Controllers\WithdrawController::class, 'checkTeacherEarn']);
    Route::get('/getStudent/{purpose}/{type}', [StudentController::class, 'get_student']);
    Route::get('/getModuleForBooking/{enrollID}', [App\Http\Controllers\CourseController::class, 'checkCourseModuleForBooking']);
    Route::get('/getModuleForCalendar/{studentID}/{purpose}/{type}/{vehicleID}/{date}/{time}', [CourseController::class, 'checkCourseModuleForCalendar']);
});

// พิมพ์ pdf
Route::group(['middleware' => 'auth'], function () {
    Route::get('/pdf/student/{id}', [App\Http\Controllers\PDFController::class, 'student'])->name('pdf.student');
    Route::get('/pdf/receipt/{id}', [App\Http\Controllers\PDFController::class, 'receipt'])->name('pdf.receipt');
    Route::get('/pdf/schedule/{id}', [App\Http\Controllers\PDFController::class, 'schedule'])->name('pdf.schedule');
    Route::get('/pdf/evaluation/{id}', [App\Http\Controllers\PDFController::class, 'evaluation'])->name('pdf.evaluation');
    Route::get('/pdf/registration/{id}', [App\Http\Controllers\PDFController::class, 'registration'])->name('pdf.registration');
    Route::get('/pdf/finance/{branch}/{month}/{year}/{type}', [App\Http\Controllers\PDFController::class, 'finance'])->name('pdf.finance');
});

// Forget password
Route::get('forget-password', [ForgotPasswordController::class, 'showForgetPasswordForm'])->name('forget.password.get');
Route::post('forget-password', [ForgotPasswordController::class, 'submitForgetPasswordForm'])->name('forget.password.post');
Route::get('reset-password/{username}/{token}', [ForgotPasswordController::class, 'showResetPasswordForm'])->name('reset.password.get');
Route::post('reset-password', [ForgotPasswordController::class, 'submitResetPasswordForm'])->name('reset.password.post');
