<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| 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!
|
*/



Auth::routes();
Route::get('/logout', function(){
    Auth::logout();
    return Redirect::to('login');
})->name('logout');

Route::get('storage/images/{from}/{slug}', function($from = null, $slug = null) {
    if ($from == 'shop'){
        $image = \App\Models\UserShop::where('filename', $slug)->first();
        $path = storage_path('app/public').'/images/shop'.'/'.$image->filename;
        if (file_exists($path)) {
            return Response::file($path);
        }
    }

})->name('storage_images');

Route::get('/product/image/{slug}', function($slug = null)
{
    if($image = \App\Models\ProductImage::where('slug', $slug)->first()){
        $path = storage_path('app/public').'/images/product'.'/'.$image->product_id.'/'.$image->filename;
        if (file_exists($path)) {
            return Response::file($path);
        }
    }

})->name('product_image');


Route::get('/iq/image/{slug}', function($slug = null)
{
    if($image = \App\Models\IqImage::where('slug', $slug)->first()){
        $path = storage_path('app/public').'/images/iq_images/'.$image->filename;
        if (file_exists($path)) {
            return Response::file($path);
        }
    }

})->name('iq_image');

Route::get('locale/{locale}', function ($locale) {
    \Session::put('locale', $locale);
    if(Auth::check()){
        $user = Auth::user();
        $user->lang = $locale;
        $user->save();
    }
    return redirect()->back();
})->name('locale');

Route::post('/user_register', 'Web\RegisterController@register')->name('user_register');
Route::get('/user_register/finish', 'Web\RegisterController@finish')->name('user_register_finish');
Route::get('/register/verify/{confirmationCode}', 'HomeController@verify')->name('register_verify');
Route::get('/reg/{member_id?}', 'Web\RegisterController@member')->name('register_user_member');

Route::get('/status/register', 'HomeController@statusRegister')->name('status_register');
Route::get('/status/verify', 'HomeController@statusVerify')->name('status_verify');
Route::get('/status/error', 'HomeController@statusError')->name('status_error');
Route::get('/status/not/found', 'HomeController@notFound')->name('not_found');

Route::post('/loading/modal', 'HomeController@loadingModal')->name('loading_modal');

Route::post('/user/check/mail', 'HomeController@checkMail')->name('user_check_mail');

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

Route::get('/cron/jobs/action/{action}/{key}', 'CronController@action')->name('cron_jobs_action');

Route::get('/user/update_email_confirm/{token}', 'UserUpdateEmailController@activateMail')->name('user_update_email_confirm');

/* AUTH */
Route::group(['middleware' => ['auth']], function() {
    Route::get('/user_blocked', 'HomeController@blocked')->name('user_blocked');
    Route::get('/wizard-create', 'WizardController@create')->name('wizard_create');
    Route::get('/wizard-register', 'WizardController@register')->name('wizard_register');

    Route::post('/wizard/store/create/{step?}', 'WizardController@storeCreate')->name('wizard_store_create');
    Route::post('/wizard/store/register/{step?}', 'WizardController@storeRegister')->name('wizard_store_register');

    Route::get('/wizard/payment', 'WizardController@payment')->name('wizard_payment');
    Route::post('/wizard/store/payment/{step?}', 'WizardController@storePayment')->name('wizard_store_payment');

    Route::get('/wizard/delete/file/{id}/{relation}', 'WizardController@delete')->name('wizard_delete_file');

    Route::get('/storage/file/{id}/{disk}', function($id = null, $disk = null)
    {
        $file = \App\Models\File::findOrFail($id);
        $path = Storage::disk($disk)->path($file->dir.$file->filename);
        if (file_exists($path)) {
            return Response::file($path);
        }
    })->name('storage_file');

});

/* AUTH user*/
Route::group(['middleware' => ['auth:user']], function() {

    Route::get('storage/{type?}/{file?}', function($type = null, $file = null)
    {
        if($type == 'xls'){
            $path = storage_path("app/export/");
            $filename = $file.'.xls';
        }

        if (file_exists($path.$filename)) {
            return Response::download($path.$filename, $filename);
        }
    })->name('storage');

    Route::get('/home', 'HomeController@show')->name('home');
    Route::post('/modal/load', 'ModalController@load')->name('modal_load');

    Route::get('/user/edit', 'UserDataController@userEdit')->name('user_edit');
    Route::post('/user/edit', 'UserDataController@userEditStore')->name('user_edit');
    Route::post('/user/data/store', 'UserDataController@userDataStore')->name('user_data_store');

    Route::get('/user/update_password', 'UserUpdatePasswordController@updatePassword')->name('user_update_password');
    Route::post('/user/update_password', 'UserUpdatePasswordController@updatePasswordStore')->name('user_update_password');

    Route::get('/user/update_password_first', 'UserUpdatePasswordController@updatePasswordFirst')->name('user_update_password_first');
    Route::post('/user/update_password_first', 'UserUpdatePasswordController@updatePasswordFirstStore')->name('user_update_password_first');

    Route::get('/user/update_email', 'UserUpdateEmailController@index')->name('user_update_email');
    Route::post('/user/update_email', 'UserUpdateEmailController@update')->name('user_update_email');

    Route::get('/user/delete_account', 'UserDeleteController@deleteAccount')->name('user_delete_account');
    Route::post('/user/delete_account', 'UserDeleteController@deleteAccountAction')->name('user_delete_account');

    Route::post('/user/data/accepted/form', 'UserDataController@userDataAcceptedForm')->name('user_data_accepted_form');

    Route::get('/user/data/free', 'UserDataController@userDataFree')->name('user_data_free');
    Route::post('/user/data/free/form', 'UserDataController@userDataFreeForm')->name('user_data_free_form');


    //user shop Sales
    Route::get('/user/shop/orders', 'User\ShopSalesController@orders')->name('user_shop_orders');
    Route::get('/user/shop/order/detail/{id}', 'User\ShopSalesController@orderDetail')->name('user_shop_order_detail');
    Route::get('/user/shop/orders/datatable', 'User\ShopSalesController@ordersDatatable')->name('user_shop_orders_datatable');

    //user team
    Route::get('/user/team/members', 'User\TeamController@members')->name('user_team_members');

    //user customers
    Route::get('/user/customers', 'User\CustomerController@index')->name('user_customers');
    Route::get('/user/customer/detail/{id}', 'User\CustomerController@detail')->name('user_customer_detail');
    Route::get('/user/customer/edit/{id}', 'User\CustomerController@edit')->name('user_customer_edit');
    Route::get('/user/customer/add/{id}/{step?}', 'User\CustomerController@add')->name('user_customer_add');
    Route::post('/user/customer/edit/{id}', 'User\CustomerController@store')->name('user_customer_edit');
    Route::get('/user/customer/datatable', 'User\CustomerController@getCustomers')->name('user_customer_datatable');

    //user order & do order
    Route::get('/user/orders', 'User\OrderController@index')->name('user_orders');
    Route::get('/user/orders/datatable', 'User\OrderController@ordersDatatable')->name('user_orders_datatable');

    Route::get('/user/order/detail/{id}', 'User\OrderController@detail')->name('user_order_detail');

    Route::get('/user/order/my/delivery/{for}/{id?}', 'User\OrderController@delivery')->name('user_order_my_delivery');
    Route::post('/user/order/my/delivery/{for}/{id?}', 'User\OrderController@delivery')->name('user_order_my_delivery');

    Route::get('/user/order/my/list/{for}/{id?}', 'User\OrderController@list')->name('user_order_my_list');
    Route::post('/user/order/my/list/{for}/{id?}', 'User\OrderController@list')->name('user_order_my_list');

    Route::post('/user/order/my/payment/{for}/{id?}', 'User\OrderController@payment')->name('user_order_my_payment');
    Route::get('/user/order/my/datatable', 'User\OrderController@datatable')->name('user_order_my_datatable');
    Route::post('/user/order/my/perform/request/', 'User\OrderController@performRequest')->name('user_order_my_perform_request');

    Route::get('/user/membership', 'MembershipController@index')->name('user_membership');
    Route::post('/user/membership/store/{action}', 'MembershipController@storePayment')->name('user_membership_store');

    Route::get('/user/checkout/{identifier?}', 'User\CheckoutController@checkout')->name('user_checkout');
    Route::post('/user/checkout_store/{identifier?}', 'User\CheckoutController@store')->name('user_checkout_store');
    Route::get('/user/checkout_final/{payid}/{reference}/{identifier?}', 'User\CheckoutController@final')->name('user_checkout_final');

});

Route::group(['middleware' => ['admin']], function()
{
    //translate
    Route::get('/admin/translate/all', 'TranslationController@index')->name('admin_translate_all');
    Route::get('/admin/translate/all/edit/{lang}/{from?}', 'TranslationController@edit')->name('admin_translate_all_edit');
    Route::post('/admin/translate/all/update/{lang}/{from?}', 'TranslationController@update')->name('admin_translate_all_update');

    Route::get('/admin/translate/file', 'TranslationFileController@index')->name('admin_translate_file');
    Route::get('/admin/translation/file/{file}/{language?}/{langsource?}/{show?}', 'TranslationFileController@edit')->name('admin_translate_file_edit');
    Route::post('/admin/translation/file/{file}/{language?}/{langsource?}/{show?}', 'TranslationFileController@update')->name('admin_translate_file_update');

    //site
    Route::get('/admin/sites/{site}', 'SitesController@show')->name('admin_sites');
    Route::post('/admin/sites/store/{site}', 'SitesController@store')->name('admin_sites_store');
    Route::post('/admin/sites/image/upload/{site}', 'SitesController@imageUpload')->name('admin_sites_image_upload');
    Route::get('/admin/sites/image/delete/{site}/{image_id}', 'SitesController@imageDelete')->name('admin_sites_image_delete');
    Route::get('/admin/sites/image/attribute/{site}/{image_id}/{attr}/{val}', 'SitesController@imageAttribute')->name('admin_sites_image_attribute');


    //products
    Route::get('/admin/product/show', 'ProductController@index')->name('admin_product_show');
    Route::post('/admin/product/store', 'ProductController@store')->name('admin_product_store');
    Route::get('/admin/product/edit/{id}', 'ProductController@edit')->name('admin_product_edit');
    Route::get('/admin/product/copy/{id}', 'ProductController@copy')->name('admin_product_copy');
    Route::get('/admin/product/delete/{id}/{do?}/{did?}', 'ProductController@delete')->name('admin_product_delete');
    //products images
    Route::post('/admin/product/image/upload', 'ProductController@imageUpload')->name('admin_product_image_upload');
    Route::get('/admin/product/image/delete/{image_id}/{product_id}', 'ProductController@imageDelete')->name('admin_product_image_delete');
    Route::get('/admin/product/image/attribute/{image_id}/{attr}/{val}', 'ProductController@imageAttribute')->name('admin_product_image_attribute');

    //products categories
    Route::get('/admin/product/categories', 'CategoryController@index')->name('admin_product_categories');
    Route::get('/admin/product/category/edit/{id}', 'CategoryController@edit')->name('admin_product_category_edit');
    Route::post('/admin/product/category/store', 'CategoryController@store')->name('admin_product_category_store');
    Route::get('/admin/product/category/delete/{id}', 'CategoryController@delete')->name('admin_product_category_delete');

    //products categories
    Route::get('/admin/product/ingredients', 'IngredientController@index')->name('admin_product_ingredients');
    Route::get('/admin/product/ingredient/edit/{id}', 'IngredientController@edit')->name('admin_product_ingredient_edit');
    Route::post('/admin/product/ingredient/store', 'IngredientController@store')->name('admin_product_ingredient_store');
    Route::get('/admin/product/ingredient/delete/{id}', 'IngredientController@delete')->name('admin_product_ingredient_delete');

    //products images
    Route::post('/admin/product/category/image/upload', 'CategoryController@imageUpload')->name('admin_product_category_image_upload');
    Route::get('/admin/product/category/image/delete{image_id}/{category_id}', 'CategoryController@imageDelete')->name('admin_product_category_image_delete');
    Route::get('/admin/product/category/image/attribute/{image_id}/{attr}/{val}', 'CategoryController@imageAttribute')->name('admin_product_category_image_attribute');


    //products attributes
    Route::get('/admin/product/attributes', 'AttributeController@index')->name('admin_product_attributes');
    Route::post('/admin/product/attribute/store', 'AttributeController@store')->name('admin_product_attribute_store');
    Route::get('/admin/product/attribute/delete/{id}', 'AttributeController@delete')->name('admin_product_attribute_delete');


    //Route::get('/admin/products/import', 'ImportProductController@import')->name('admin_product_import');

    //leads
    Route::get('/admin/leads', 'LeadController@index')->name('admin_leads');
    Route::get('/admin/lead/edit/{id}', 'LeadController@edit')->name('admin_lead_edit');
    Route::post('/admin/lead/edit/{id}', 'LeadController@editPost')->name('admin_lead_edit');

    //customers
    Route::get('/admin/customers', 'CustomerController@index')->name('admin_customers');
    Route::get('/admin/customer/detail/{id}', 'CustomerController@detail')->name('admin_customer_detail');
    Route::get('/admin/customer/edit/{id}', 'CustomerController@edit')->name('admin_customer_edit');
    Route::post('/admin/customer/edit/{id}', 'CustomerController@store')->name('admin_customer_edit');
    Route::get('/admin/customer/datatable', 'CustomerController@getCustomers')->name('admin_customer_datatable');

    Route::get('/admin/lead/change_mail/{id}', 'UserUpdateEmailController@adminChangeMail')->name('admin_lead_change_mail');
    Route::post('/admin/lead/change_mail/{id}', 'UserUpdateEmailController@adminUpdateMail')->name('admin_lead_change_mail');
    Route::get('/admin/lead/new_mail/verified/{id}', 'LeadController@newMailVerified')->name('admin_lead_new_mail_verified');
    Route::get('/admin/lead/released/{action}/{id}', 'LeadController@released')->name('admin_lead_released');
    Route::post('/admin/lead/released/{action}/{id}', 'LeadController@released')->name('admin_lead_released');


    Route::get('/admin/lead/delete/file/{user_id}/{file_id}/{relation}', 'LeadController@deleteFile')->name('admin_lead_delete_file');

    Route::post('/admin/lead/store', 'LeadController@store')->name('admin_lead_store');
    Route::get('/admin/leads/datatable', 'LeadController@getLeads')->name('admin_leads_datatable');

    //sales
    Route::get('/admin/sales/users', 'SalesController@users')->name('admin_sales_users');
    Route::get('/admin/sales/users/detail/{id}', 'SalesController@usersDetail')->name('admin_sales_users_detail');
    Route::post('/admin/sales/users/detail/{id}', 'SalesController@usersStore')->name('admin_sales_users_detail');
    Route::get('/admin/sales/users/datatable', 'SalesController@usersDatatable')->name('admin_sales_users_datatable');

    Route::get('/admin/sales/customers', 'SalesController@customers')->name('admin_sales_customers');
    Route::get('/admin/sales/customers/detail/{id}', 'SalesController@customersDetail')->name('admin_sales_customers_detail');
    Route::post('/admin/sales/customers/detail/{id}', 'SalesController@customersStore')->name('admin_sales_customers_detail');
    Route::get('/admin/sales/customers/datatable', 'SalesController@customersDatatable')->name('admin_sales_customers_datatable');

    Route::post('/admin/sales/store', 'SalesController@store')->name('admin_sales_store');


});

//login pages for superadmin
Route::group(['middleware' => ['superadmin']], function() {
    //leads
    Route::get('/admin/users', 'AdminUserController@index')->name('admin_users');
    Route::get('/admin/user/edit/{user_id}', 'AdminUserController@edit')->name('admin_user_edit');
    Route::post('/admin/user/store', 'AdminUserController@store')->name('admin_user_store');
    Route::get('/admin/user/delete/{user_id}', 'AdminUserController@deleteUser')->name('admin_user_delete');
    Route::get('/admin/user/login_as/{userId?}', 'AdminUserController@userLoginAs')->name('admin_user_login_as');


    Route::get('/admin/shippings', 'ShippingController@index')->name('admin_shippings');
    Route::get('/admin/shipping/edit/{shipping_id}', 'ShippingController@edit')->name('admin_shipping_edit');
    Route::post('/admin/shipping/store', 'ShippingController@store')->name('admin_shipping_store');
    Route::get('/admin/shipping/delete/{shipping_id}', 'ShippingController@deleteShipping')->name('admin_shipping_delete');
    Route::get('/admin/shipping/price/delete/{price_id}', 'ShippingController@deletePrice')->name('admin_shipping_price_delete');
    Route::get('/admin/shipping/country/delete/{price_id}', 'ShippingController@deleteCountry')->name('admin_shipping_country_delete');

    Route::get('data_table', 'DataTableController@datatable')->name('data_table');
    // Route::get('datatables/leads', 'DataTableController@getLeads')->name('datatables-leads');
    Route::get('data/table/users', 'DataTableController@getUsers')->name('data_table_users');

    Route::get('/admin/payment_methods', 'PaymentMethodController@index')->name('admin_payment_methods');
    Route::post('/admin/payment_method/store', 'PaymentMethodController@store')->name('admin_payment_method_store');

    Route::get('/admin/countries', 'CountryController@index')->name('admin_countries');
    Route::get('/admin/country/edit/{id}', 'CountryController@edit')->name('admin_country_edit');
    Route::post('/admin/country/store', 'CountryController@store')->name('admin_country_store');

    Route::get('/admin/levels', 'UserLevelController@index')->name('admin_levels');
    Route::get('/admin/level/edit/{id}', 'UserLevelController@edit')->name('admin_level_edit');
    Route::post('/admin/level/store', 'UserLevelController@store')->name('admin_level_store');
    Route::get('/admin/level/delete/{do}/{id}', 'UserLevelController@delete')->name('admin_level_delete');


    Route::get('/admin/settings', 'SettingController@index')->name('admin_settings');
    Route::post('/admin/setting/store', 'SettingController@store')->name('admin_setting_store');

});

//login pages for sysadmin
Route::group(['middleware' => ['sysadmin']], function() {
    Route::get('/sysadmin/tools/customers', 'SyS\AdminToolsController@customers')->name('sysadmin_tools_customers');
    Route::post('/sysadmin/tools/customers', 'SyS\AdminToolsController@customerStore')->name('sysadmin_tools_customers');

    Route::get('/sysadmin/tools/domainssl', 'SyS\AdminToolsController@domainSSL')->name('sysadmin_tools_domainssl');
    Route::post('/sysadmin/tools/domainssl', 'SyS\AdminToolsController@domainSSLStore')->name('sysadmin_tools_domainssl');

    Route::get('/sysadmin/tools/cronjobs', 'SyS\AdminToolsController@cronjobs')->name('sysadmin_tools_cronjobsl');
    Route::post('/sysadmin/tools/cronjobs', 'SyS\AdminToolsController@cronjobsStore')->name('sysadmin_tools_cronjobs');

    Route::get('/sysadmin/tools/shopping_orders', 'SyS\AdminToolsController@shoppingOrders')->name('sysadmin_tools_shopping_orders');
    Route::post('/sysadmin/tools/shopping_orders', 'SyS\AdminToolsController@shoppingOrdersStore')->name('sysadmin_tools_shopping_orders');

    Route::get('/sysadmin/import', 'SyS\ImportController@import')->name('sysadmin_import');
    Route::post('/sysadmin/import/store', 'SyS\ImportController@importStore')->name('sysadmin_import_store');
    Route::get('/sysadmin/import/show/{type}/{filename}/{skip?}/{limit?}', 'SyS\ImportController@importShow')->name('sysadmin_import_show');

    Route::get('/sysadmin/settings', 'SyS\SettingController@index')->name('sysadmin_settings');
    Route::post('/sysadmin/tools/setting/store', 'SyS\SettingController@store')->name('sysadmin_setting_store');
});

/*
 * Route::group(['middleware' => ['checkout']], function() {

    Route::get('/datenschutz', 'HomeController@legalDataProtected')->name('datenschutz');
    Route::get('/impressum', 'HomeController@legalImprint')->name('impressum');
    Route::get('/datenschutz', 'HomeController@legalDataProtected')->name('data_protected');
    Route::get('/impressum', 'HomeController@legalImprint')->name('imprint');
    Route::get('/agb', 'HomeController@legalAGB')->name('agb');


    Route::get('/checkout/card/{identifier?}', 'Web\CheckoutController@checkout')->name('checkout.checkout_card');
    Route::post('/checkout/card/final', 'Web\CheckoutController@checkoutFinal')->name('checkout.checkout_card_final');

    Route::get('/transaction/status/{status?}/{reference?}', 'Web\CheckoutController@transactionStatus')->name('checkout.transaction_status');
    Route::post('/transaction/status/{status?}/{reference?}', 'Web\CheckoutController@transactionStatus')->name('checkout.transaction_status');

    Route::get('/transaction/approved/{transactionId}/{reference}', 'Web\CheckoutController@transactionApproved')->name('checkout.transaction_approved');

});
*/