<?php

use App\Pos;
use App\Product;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;



Route::prefix('back')->middleware(['auth'])->group(function () {
    
    /**
     * *********************
     * Bank Account
     * *********************
     */
    Route::get('bank_account/add_money/{bank_account}', 'BankAccountController@add_money')->name('bank_account.add_money');
    Route::post('bank_account/add_money', 'BankAccountController@add_money_store')->name('bank_account.add_money.store');
    Route::get('bank_account/withdraw_money/{bank_account}', 'BankAccountController@withdraw_money')->name('bank_account.withdraw_money');
    Route::post('bank_account/withdraw_money', 'BankAccountController@withdraw_money_store')->name('bank_account.withdraw_money.store');
    Route::resource('bank_account', 'BankAccountController');
    Route::get('bank_account/transfer/{account}', 'BankAccountController@transfer')->name('bank_account.transfer');
    Route::post('bank_account/transfer/', 'BankAccountController@transfer_store')->name('bank_account.transfer_store');
    Route::get('bank_account/history/{account}', 'BankAccountController@history')->name('bank_account.history');

    /**
     * ********
     * Expense 
     * ********
     */
    Route::get('expense/categories', 'ExpenseController@categories')->name('expense.categories');
    Route::get('expense/sub-categories/{id}', 'ExpenseController@subcategories')->name('expense.sub-categories');
    Route::resource('expense-sub-category', 'ExpenseSubcategoryController')->except(['create','show']);
    Route::resource('expense-category', 'ExpenseCategoryController')->except(['create','show']);
    Route::resource('expense', 'ExpenseController')->except('show');

    Route::post('/expense-category/toggle-status', 'ExpenseCategoryController@toggleStatus')->name('expense-category.toggleStatus');
    Route::post('/expense-sub-category/toggle-status', 'ExpenseSubcategoryController@toggleStatus')->name('expense-sub-category.toggleStatus');
    /**
     * ********
     * Customer 
     * ********
     */
    Route::resource('customer', 'CustomerController');
    Route::get('customer/{id}/profile',  'CustomerController@show')->name('customer.profile');
    Route::post('customer/{id}/documents', 'CustomerController@uploadDocuments')->name('customer.documents.upload');

    Route::post('customer/{id}/nominee',  'CustomerController@storeNominee')->name('nominee.store');
    Route::get('nominee/{id}/edit', 'CustomerController@editNominee')->name('nominee.edit');
    Route::put('nominee/{id}', 'CustomerController@updateNominee')->name('nominee.update');
    Route::delete('nominee/{id}',  'CustomerController@destroyNominee')->name('nominee.destroy');
    Route::delete('customer/documents/{id}',  'CustomerController@destroyDocument')->name('customer.documents.destroy');
    Route::get('customer/{customer}/admission-fee', 'CustomerController@showAdmissionFeeForm')->name('customer.admissionFee.form');
    Route::post('customer/{customer}/admission-fee',  'CustomerController@storeAdmissionFee')->name('customer.admissionFee.store');



    /**
     * *********
     * Loan
     * *********
     */
    Route::resource('loans', 'LoanController');
    Route::post('loans/installments/{id}/collect',  'LoanController@collectPayment')->name('loans.installments.collect');
    Route::post('loans/mark-overdue',  'LoanController@markOverdue')->name('loan.markOverdue');
    Route::get('loans/recovery/report',  'LoanController@recoveryReport')->name('loan.recovery.report');
    Route::get('loans/{id}/installments',  'LoanController@installments')->name('loans.installments');
    Route::get('loans/{id}/payments',  'LoanController@paymentReport')->name('loans.payments');
    Route::patch('loans/{loan}/status', 'LoanController@updateStatus')->name('loans.updateStatus');
    Route::get('/loan-report', 'LoanController@loanReport')->name('loans.report');

    // Handle Loan Savings 
    Route::get('/loan/savings/withdraw',  'LoanController@showWithdrawForm')->name('loan.savings.withdraw.form');
    Route::post('/loan/savings/withdraw', 'LoanController@withdrawLoanSavings')->name('loan.savings.withdraw');
    Route::post('/loans/{loan}/savings', 'LoanController@collectSavings')->name('loans.savings.collect');
    Route::delete('/loans/savings/{id}', 'LoanController@deleteSavings')->name('loans.savings.delete');
    Route::delete('/loans/savings-withdrawals/{id}', 'LoanController@deleteSavingsWithdrawal')->name('loans.savingsWithdrawals.delete');
    Route::post('/loans/transfer-savings', 'LoanController@transferSavings')->name('loans.transferSavings');

    /**
     * *********
     * SAVINGS
     * *********
     */

    Route::resource('savings', 'SavingsController')->except(['show']);
    Route::get('/savings/withdraw',  'SavingsController@withdrawForm')->name('savings.withdraw.form');
    Route::get('/savings/customer-savings/{customerId}', 'SavingsController@getCustomerSavings');
    Route::post('/savings/withdraw', 'SavingsController@withdraw')->name('savings.withdraw.submit');
    Route::delete('/savings/{id}',  'SavingsController@deleteSaving')->name('savings.destroy');
    Route::get('/savings/history/{customerId}',  'SavingsController@history')->name('savings.history');
    Route::delete('/savings/withdrawal/{id}',  'SavingsController@deleteWithdrawal')->name('savings.withdrawal.delete');


    /**
     * *********
     * DPS
     * *********
     */
    Route::resource('dps', 'DpsAccountController');
    Route::post('dps/{dps}/close',  'DpsAccountController@close')->name('dps.close');
    Route::get('dps-installments-report', 'DpsAccountController@installmentsReport')->name('dps.installments.report');
    Route::get('dps-report', 'DpsAccountController@report')->name('dps.all.report');
    Route::post('dps/installments/{id}/collect',  'DpsAccountController@collect')->name('dps.installments.collect');
    Route::get('/dps/{id}/withdraw',  'DpsAccountController@withdrawForm')->name('dps.withdraw.form');
    Route::post('/dps/{id}/withdraw', 'DpsAccountController@withdraw')->name('dps.withdraw');
    Route::get('dps/{id}/installments',  'DpsAccountController@installments')->name('dps.installments');

    /**
     * *********
     * all payments delete option
     * *********
     */
    
    Route::get('/all-payments', 'PaymentController@index')->name('payments.index');
    Route::delete('/loan-payments/{id}',  'PaymentController@deleteLoan')->name('payments.loan.delete');
    Route::delete('/dps-payments/{id}',  'PaymentController@deleteDps')->name('payments.dps.delete');
    Route::delete('/payments/bulk-delete', 'PaymentController@bulkDelete')->name('payments.bulk.delete');


    /**
     * ********
     * Reports
     * ********
     */
    Route::get('report/summary-report', 'ReportController@summary_report')->name('summary_report');
    Route::get('loans/installments/report', 'LoanController@installmentsReport')->name('loans.installments.report');
    Route::get('/reports/collection', 'ReportController@collectionReport')->name('reports.collection');


    /**
     * **********
     * Settings
     * **********
     */

    Route::get('setting', 'SettingController@create_pos_setting')->name('pos.pos_setting');
    Route::post('setting', 'SettingController@update_pos_setting')->name('pos.pos_setting_update');
    Route::get('/backup', 'HomeController@backup')->name('backup');

    /**
     * *****************
     * Role & Permission
     * *****************
     */
    Route::resource('roles', 'RoleController')->except('show');
    Route::resource('role_permissions', 'RolePermissionController')->parameters([
        'role_permissions' => 'role'
    ])->only('edit', 'update');

    Route::resource('users','UserController')->except('show');

    /**
     * *********
     * Profile
     * *********
     */

    Route::get('profile', 'ProfileController@index')->name('profile.index');
    Route::post('profile', 'ProfileController@update')->name('profile.update');
    Route::get('change-password', 'ProfileController@change_password')->name('change.password');
    Route::post('update-password', 'ProfileController@update_password')->name('update.password');

});


Route::get('/', 'HomeController@front_home');

Auth::routes();

Route::get('/back', 'HomeController@index')->name('admin');

Route::get('clear', 'MaintenanceController@cache_clear');
Route::get('db_reset', 'MaintenanceController@reset_software');
Route::get('optimize', 'MaintenanceController@optimize');






