<?php

use App\Helpers\CustomDateFilter;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Artisan;
use App\Http\Controllers\Admin\CoController;
use App\Http\Controllers\Admin\LoanController;
use App\Http\Controllers\System\RoleController;
use App\Http\Controllers\System\UserController;
use App\Http\Controllers\Admin\BranchController;
use App\Http\Controllers\Admin\ClientController;
use App\Http\Controllers\Admin\ClosingController;
use App\Http\Controllers\Admin\ReportController;
use App\Http\Controllers\Admin\SavingController;
use App\Http\Controllers\System\AdminController;
use App\Http\Controllers\Admin\CompanyController;
use App\Http\Controllers\Admin\HolidayController;
use App\Http\Controllers\System\BackupController;
use App\Http\Controllers\Admin\BankNoteController;
use App\Http\Controllers\Admin\CurrencyController;
use App\Http\Controllers\System\SettingController;
use App\Http\Controllers\Admin\ComponentController;
use App\Http\Controllers\Admin\CategoryController;
use App\Http\Controllers\Admin\CollateralController;
use App\Http\Controllers\System\DashboardController;
use App\Http\Controllers\Admin\ClientPaymentController;
use App\Http\Controllers\Admin\ManageGroupCoController;
use App\Http\Controllers\Admin\Accounting\AssetController;
use App\Http\Controllers\Admin\Accounting\IncomeController;
use App\Http\Controllers\Admin\Accounting\ExpenseController;
use App\Http\Controllers\Admin\Accounting\JournalController;
use App\Http\Controllers\Admin\Accounting\CashFlowController;
use App\Http\Controllers\Admin\CashTransferRequestController;
use App\Http\Controllers\Admin\Accounting\BalanceSheetController;
use App\Http\Controllers\Admin\Accounting\DepreciationController;
use App\Http\Controllers\Admin\Accounting\TrialBalanceController;
use App\Http\Controllers\Admin\Accounting\GeneralLedgerController;
use App\Http\Controllers\Admin\Accounting\TypeOfAccountController;
use App\Http\Controllers\Admin\Accounting\ChartOfAccountController;
use App\Http\Controllers\Admin\Accounting\NetProfitAndLossController;
use App\Http\Controllers\Admin\Accounting\ComponentAccountingController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
//Clear route cache:
Route::get('/route-cache', function() {
  $exitCode = Artisan::call('route:cache');
  return 'Routes cache cleared';
});

//Clear config cache:
Route::get('/config-cache', function() {
  $exitCode = Artisan::call('config:cache');
  return 'Config cache cleared';
});

Route::get('/', function () {
      // $postId = 1;
      // $getPost = DB::select(
      //   'CALL get_posts_by_userid('.$postId.')'
      // );
      // dd($getPost);
    return view('layouts.app');
})->middleware(['auth'])->name('dashboard');
Auth::routes();

Route::group(['middleware' => ['system_admin']], function() {
  Route::get('system-admin',[AdminController::class,'index'])->name('systemAdmin');
  Route::get('lock',[AdminController::class,'lock'])->name('lock');
  Route::post('sync-role',[AdminController::class,'syncRole'])->name('syncRole');
  // Route::resource('adminbranch', AdminBranchController::class);
  Route::get('adminbranch', [BranchController::class, 'adminbrach']);
  Route::get('adminbranch/create', [BranchController::class, 'admincreate']);
  Route::post('adminbranch', [BranchController::class, 'adminstore']);
  Route::get('adminbranch/{id}/edit', [BranchController::class, 'adminedit']);
  Route::put('adminbranch/{id}', [BranchController::class, 'branchupdate']);
  Route::delete('adminbranch/{id}', [BranchController::class, 'admindestroy']);
  Route::GET('active-action/{id}',[BranchController::class,'activeAction']);
  //currency
  Route::get('currency', [CurrencyController::class, 'admincurrency']);
  Route::get('currency/create', [CurrencyController::class, 'admincreate']);
  Route::post('currency', [CurrencyController::class, 'adminstore']);
  Route::get('currency/{id}/edit', [CurrencyController::class, 'adminedit']);
  Route::put('currency/{id}', [CurrencyController::class, 'currencyupdate']);
  Route::delete('currency/{id}', [CurrencyController::class, 'admindestroy']);

});

Route::get('lock-screen',[AdminController::class,'login']);
Route::post('un-lock',[AdminController::class,'unLock'])->name('unLock');

Route::group(['middleware' => ['auth'],'prefix'=>'system'], function() {
    //=================== BackupController =========================//
    //==============================================================//
    Route::get('/backup', [BackupController::class,'index']);
    Route::get('/backup/create', [BackupController::class,'create']);
    Route::get('/download/{file_name}', [BackupController::class,'download']);
    Route::get('/backup/delete/{file_name}', [BackupController::class,'delete']);

    //=================== UserController =========================//
    //===============================================================//
    Route::resource('users',UserController::class);
    Route::get('get-user',[UserController::class,'getUser']);
    Route::POST('update-user-profile/{id}',[UserController::class,'updateProfile']);
    Route::GET('active-action/{id}',[UserController::class,'activeAction']);
    Route::GET('switch-branch/{id}',[UserController::class,'switchBranch']);
    Route::GET('switch-currency/{id}',[UserController::class,'switchCurrency']);
    Route::get('com-user-branch',[UserController::class,'getUserBranch']);
    Route::get('user-branch-name',[UserController::class,'getBranchName']);

    //=================== SettingController =========================//
    //===============================================================//
    Route::get('/setting', [SettingController::class,'index']);
    Route::POST('/setting', [SettingController::class,'update']);
    Route::get('/check-lang', [SettingController::class,'checkLang']);

    //=================== RoleController =========================//
    //============================================================//
    Route::resource('roles',RoleController::class);
    Route::GET('get-permission/{id}',[RoleController::class,'getPermission']);


    //================= DashboardController =======================//
    //============================================================//
    Route::GET('get-data-dashboard',[DashboardController::class,'getData']);
    Route::GET('get-journal-by-yearly',[DashboardController::class,'getDataJournalByYearly']);
});

Route::group(['middleware' => ['auth'],'prefix'=>'admin-api'], function() {
  //=================== UserController =========================//
  //===============================================================//
  Route::POST('employee',[UserController::class,'employeeStore']);
  Route::POST('emp-attachment/{id}',[UserController::class,'EMPAttachmentStore']);
  Route::GET('get-emp-attachment/{id}',[UserController::class,'getEMPAttachment']);
  Route::DELETE('delete-emp-attachment/{id}',[UserController::class,'deleteEMPAttachment']);
  Route::GET('active-action/{id}',[UserController::class,'activeAction']);
  Route::GET('getLogactivity',[UserController::class,'getLogactivity']);

  //=================== CompanyController =========================//
  //===============================================================//
  Route::resource('company',CompanyController::class);
  Route::get('get-company',[CompanyController::class,'getCompany']);

  Route::resource('branches',BranchController::class);
  Route::resource('currency',CurrencyController::class);

  //=================== ManageGroupCoController ====================//
  //===============================================================//
  Route::resource('manage-group-co',ManageGroupCoController::class);

  Route::GET('/get-keyword-date', function () {
    return CustomDateFilter::getKeywordDate();
  });
  //=================== ComponentController =========================//
  //===============================================================//
  Route::get('get-all-user',[ ComponentController::class,'getAllUser']);
  Route::get('address-autocomplete', [ ComponentController::class,'autoCompleteAddress']);
  Route::get('/get-payment-method', [ ComponentController::class,'getPaymentMethod']);
  Route::get('/get-provinces', [ ComponentController::class,'getProvince']);
  Route::get('/get-districts', [ ComponentController::class,'getDistrict']);
  Route::get('/get-communes', [ ComponentController::class,'getCommune']);
  Route::get('/get-villages', [ ComponentController::class,'getVillage']);
  Route::get('/com-village-client', [ ComponentController::class,'getVillageClient']);
  Route::GET('com-client',[ComponentController::class,'getClient']);
  Route::GET('com-client-name-code',[ComponentController::class,'getClientNameCode']);
  Route::GET('com-co',[ComponentController::class,'getCO']);
  Route::GET('com-get-term-condition',[ComponentController::class,'getTermCondition']);
  Route::GET('com-member-co',[ComponentController::class,'getMemberCO']);
  Route::GET('com-co-loan',[ComponentController::class,'getCOLoan']);
  Route::GET('com-loan-status',[ComponentController::class,'getLoanStatus']);
  Route::GET('com-loan-term-type',[ComponentController::class,'getLoanTermType']);
  Route::GET('com-category',[ComponentController::class,'getCategory']);
  Route::GET('com-collaterals',[ComponentController::class,'getCollateral']);
  Route::GET('com-collaterals-cusstom',[ComponentController::class,'getCollateralCustom']);
  Route::GET('com-user',[ComponentController::class,'getUser']);
  Route::GET('default-currency',[ComponentController::class,'getDefaultCurrency']);
  Route::GET('com-branch',[ComponentController::class,'getBranch']);
  Route::GET('com-branch-transfer',[ComponentController::class,'getBranchTransfer']);
  Route::GET('com-co-by-branch',[ComponentController::class,'getCoByBranch']);
  Route::GET('com-client-loan',[ComponentController::class,'getClientLoan']);
  Route::GET('com-all-client-loan',[ComponentController::class,'getAllClientLoan']);
  Route::GET('show-schedule-payment/{id}',[ComponentController::class,'showSchedulePayment']);
  Route::GET('show-schedule-detail/{id}',[ComponentController::class,'showScheduleDetail']);
  Route::GET('com-collaterals-detail',[ComponentController::class,'getCollateralCustomDetail']);

  Route::GET('user-permissions',[UserController::class,'getUserPermission']);
  Route::resource('holidays',HolidayController::class);
  Route::GET('com-holidays',[HolidayController::class,'comHoliday']);

  Route::resource('clients',ClientController::class);
  // Route::delete('admin-api/clients/{id}', [ClientController::class, 'destroy']);
  Route::resource('categories',CategoryController::class);
  Route::resource('coes',CoController::class);
  //================== CollateralController ======================//
  //=============================================================//
  Route::resource('collaterals',CollateralController::class);
  Route::GET('collateral-list',[CollateralController::class,'getCollateralDetail']);
  Route::GET('collateral-report',[CollateralController::class,'getCollateralReport']);
  Route::GET('create-detail',[CollateralController::class,'getCollateralDetailCreate']);
  Route::POST('store-detail',[CollateralController::class,'storeCollateralDetailCreate']);
  Route::PUT('sub-loan-collateral/{id}',[CollateralController::class,'funSubLoanCollateral']);

  //===================LoanController =========================//
  //===============================================================//
  Route::resource('loans',LoanController::class);
  Route::get('loans/show-payment/{id}',[LoanController::class,'ShowPayment']);
  Route::get('loans/get-payment/{id}',[LoanController::class,'GetPayment']);
  Route::get('getLoanPayment',[LoanController::class,'getLoanPayment']);
  Route::get('contract-day/{id}',[LoanController::class,'getContractDay']);
  Route::post('loan-reschedule',[LoanController::class,'storeReschedule']);
  Route::post('move-co',[LoanController::class,'MoveCo']);
  Route::PUT('move-to-write-off/{id}',[LoanController::class,'moveToWriteOff']);
  Route::post('move-to-write-off-multiple', [LoanController::class, 'moveToWriteOffMultiple']);
  Route::post('multiple-move-write-off-to-loan', [LoanController::class, 'multipleMoveWriteOffToLoan']);
  Route::PUT('submit-loan-loss-provision/{id}',[LoanController::class,'submitLoanLossProvision']);
  Route::get('get-loan-approval-reject',[LoanController::class,'getLoanApprovalRejected']);
  Route::post('submit-loan-transfer',[LoanController::class,'submitLoanTransfer']);
  Route::post('submit-transfer-loan-in-branch',[LoanController::class,'submitTransferLoanInBranch']);
  Route::get('loans/history-payment/{id}',[LoanController::class,'HistoryPayment']);
  Route::get('loans/show-client-payment/{id}',[LoanController::class,'ShowClientPayment']);
  Route::post('LoanReject/{id}', [LoanController::class,'LoanReject']);
  //===================ClientPaymentController =========================//
  //===============================================================//
  Route::resource('client-payment',ClientPaymentController::class);
  Route::post('cashier-approve',[ClientPaymentController::class,'Approve']);
  Route::post('submit-closing-report',[ClientPaymentController::class,'ClosingReport']);
  Route::get('notifications',[ClientPaymentController::class,'Notification']);
  Route::get('show-pay-all/{id}',[ClientPaymentController::class,'ShowPayAll']);
  Route::post('submit-pay-all',[ClientPaymentController::class,'SubmitPayAll']);
  Route::PUT('admin-back-payment/{id}',[ClientPaymentController::class,'adminBackPayment']);

  Route::resource('closing',ClosingController::class);

  //====================== CashTransferRequestController =====================//
  //==================================================================//
  Route::resource('cash-transfer-request',CashTransferRequestController::class);


  //=========================== Report =============================//
  //===============================================================//
  Route::Get('daily-report',[ReportController::class,'DailyReport']);
Route::Get('late-report',[ReportController::class,'LateReport']);
  Route::Get('co-report',[ReportController::class,'CoReport']);
  Route::Get('borrowed-report',[ReportController::class,'BorrowedReport']);
  Route::Get('completed-report',[ReportController::class,'CompletedReport']);
  Route::get('overdue-report',[ReportController::class,'OverdueReport']);
  Route::Get('collection-report',[ReportController::class,'CollectionReport']);
  Route::Get('collected-report',[ReportController::class,'CollectedReport']);
  Route::Get('summary-co-report',[ReportController::class,'SummaryCoReport']);
  Route::Get('summary-co-without-interest-report',[ReportController::class,'summaryCoWtoutInterestReport']);
  Route::Get('summary-co-Disburs-report',[ReportController::class,'summaryCoDisbursReport']);
  Route::Get('summary-co-collected-report',[ReportController::class,'summaryCoCollectedReport']);
  Route::Get('summary-co-collection-report',[ReportController::class,'summaryCoCollectionReport']);



  Route::Get('summary-productivity-by-branch',[ReportController::class,'SummaryProductivityByBranch']);
  Route::Get('export-summary-productivity-by-branch',[ReportController::class,'ExportSummaryProductivityByBranch']);

  Route::Get('collect-good-loan',[ReportController::class,'CollectGoodLoan']);
  Route::Get('collect-late-loan',[ReportController::class,'CollectLateLoan']);
  Route::Get('all-collect-loan',[ReportController::class,'allCollectLoan']);
  
  Route::get('daily-payment',[ReportController::class,'DailyPayment']);
  Route::get('cashier-received',[ReportController::class,'cashierReceived']);
  Route::get('summary-loan-loss-provision',[ReportController::class,'SummaryLoanLossProvision']);
  Route::get('loan-loss-provision-classific',[ReportController::class,'LoanLossProvisionClassific']);

  Route::Get('loan-not-closing-report',[ReportController::class,'loanNotClosingReport']);
  Route::Get('payment-not-closing-report',[ReportController::class,'paymentNotClosingReport']);
  Route::Get('sum-total-client-payment',[ReportController::class,'SumTotalClientPayment']);
  Route::Get('reject-loan-report',[ReportController::class,'RejectLoanReport']);
  Route::Get('deleted-loan-report',[ReportController::class,'deletedLoanReport']);

  //======================= Export Report ==========================//

  //=================== BankNoteController =========================//
  //===============================================================//
  Route::resource('bank-notes',BankNoteController::class);
  Route::Get('get-daily-banknote',[BankNoteController::class,'getDailyBankNote']);
  Route::Get('show-detail-bank-note/{id}',[BankNoteController::class,'showDetailBankNote']);

  //=================== SavingController =========================//
  //=============================================================//
  Route::resource('saving',SavingController::class);
  Route::POST('request-saving',[SavingController::class,'requestSaving']);
  Route::POST('approve-saving',[SavingController::class,'approveSaving']);
});

Route::group(['middleware' => ['auth'],'prefix'=>'accounting'], function() {

  //=================== TypeOfAccountController =========================//
  //====================================================================//
  Route::resource('type-of-account',TypeOfAccountController::class);

  //=================== ChartOfAccountController =========================//
  //====================================================================//
  Route::resource('chart-of-account',ChartOfAccountController::class);

  //========================= AssetController ==========================//
  //====================================================================//
  Route::resource('assets',AssetController::class);

  //========================= JournalController =========================//
  //====================================================================//
  Route::resource('journals',JournalController::class);
  //========================= ExpenseController =========================//
  //====================================================================//
  Route::resource('expenses',ExpenseController::class);
  //========================= IncomeController =========================//
  //====================================================================//
  Route::resource('incomes',IncomeController::class);

  //=================== CashFlowController =========================//////
  //====================================================================//
  Route::GET('cash-flow',[CashFlowController::class,'getCashFlow']);

  //=================== BalanceSheetController =========================//
  //====================================================================//
  Route::GET('balance-sheet',[BalanceSheetController::class,'balanceSheet']);

  //=================== NetProfitAndLossController =====================//////
  //====================================================================/////
  Route::GET('netprofit-loss',[NetProfitAndLossController::class,'getProfitLoss']);
  Route::GET('income-statement-by-month',[NetProfitAndLossController::class,'getIncomeStatementByMonth']);
  Route::GET('profit-and-loss-statement-by-month',[NetProfitAndLossController::class,'getProfitAndLossStatementByMonth']);
  Route::GET('export-profit-and-loss-statement-by-month',[NetProfitAndLossController::class,'exportProfitAndLossStatementByMonth']);

  //====================== GeneralLedgerController =====================//////
  //====================================================================/////
  Route::GET('general-ledger',[GeneralLedgerController::class,'getGeneralLedger']);

  //====================== TrialBalanceController =====================//
  //==================================================================//
  Route::GET('trial-balance',[TrialBalanceController::class,'getTrialBalance']);

  //====================== DepreciationController =====================//
  //==================================================================//
  Route::resource('depreciation',DepreciationController::class);
  Route::GET('monthly-depreciation',[DepreciationController::class,'getMonthlyDepreciation']);
  Route::POST('submit-payment-depreciation/{id}',[DepreciationController::class,'submitPaymentDepreciation']);
  Route::POST('submit-all-payment-depreciation',[DepreciationController::class,'submitAllPaymentDepreciation']);
  Route::GET('summary-asset-depreciation-report',[DepreciationController::class,'summaryAssetDepreciationReport']);
  Route::GET('get-depreciation-report',[DepreciationController::class,'getDepreciationReport']);

  //=================== ComponentAccountingController ==================//
  //===================================================================//
  Route::GET('get-asset',[ComponentAccountingController::class,'getAssets']);
  Route::GET('get-type-acc',[ComponentAccountingController::class,'getTypeAccount']);
  Route::GET('get-type-of-acc-details/{id}',[ComponentAccountingController::class,'getTypeAccDetails']);
  Route::GET('com-chart-of-account',[ComponentAccountingController::class,'getChartOfAccount']);
  Route::GET('get-current-asset',[ComponentAccountingController::class,'getChartOfAccCurrentAsset']);
  Route::GET('get-acc-fixed-asset',[ComponentAccountingController::class,'getAccFixedAsset']);
  Route::GET('get-acc-expense',[ComponentAccountingController::class,'getAccExpense']);
  Route::GET('com-cash-bank',[ComponentAccountingController::class,'getCashBank']);
  Route::GET('com-expense',[ComponentAccountingController::class,'getExpense']);
  Route::GET('com-income',[ComponentAccountingController::class,'getIncome']);
});
