<?php

/*
 * Backend
 */
Route::group(['middleware' => ['role:Admin|Super Admin']], function () {
    Route::get('backend', 'BackendController@index')->name('backend');

    Route::namespace('Backend')->prefix('backend')->name('backend.')->group(function () {

        Route::group(['middleware' => ['role:Super Admin']], function () {
            Route::resources([
                'pages' => 'PagesController',
                'users' => 'UsersController',
                'slides' => 'SlidesController',
            ], [
                'except' => 'show'
            ]);

            // Options
            Route::get('options', 'OptionsController@index')->name('options');
            Route::post('options', 'OptionsController@update')->name('options.update');
            Route::post('options/order', 'OptionsController@order')->name('options.order');
        });

        Route::resources([
            'articles' => 'ArticlesController',
            'products' => 'ProductsController',
            'categories' => 'CategoriesController',
            'sections' => 'SectionsController',
            'media' => 'MediaController',
            'orders' => 'OrdersController',
            'payment_methods' => 'PaymentMethodsController',
            'shipping_methods' => 'ShippingMethodsController',
        ], [
            'except' => 'show'
        ]);

        // Blocks
        Route::get('blocks/{id}', 'BlocksController@index')->name('blocks.index');
        Route::get('blocks/{id}/create', 'BlocksController@create')->name('blocks.create');
        Route::post('blocks/{id}/update', 'BlocksController@update')->name('blocks.update');
        Route::get('blocks/{id}/delete', 'BlocksController@delete')->name('blocks.delete');

        // Order
        Route::post('posts/order', 'PostsController@order')->name('posts.order');
        Route::post('categories/order', 'CategoriesController@order')->name('categories.order');
        Route::post('slides/order', 'SlidesController@order')->name('slides.order');
        Route::post('payment_methods/order', 'PaymentMethodsController@order')->name('payment_methods.order');
        Route::post('shipping_methods/order', 'ShippingMethodsController@order')->name('shipping_methods.order');

        // Media
        Route::get('products/media', 'ProductsController@media')->name('products.media');

        // Delete image
        Route::post('backend/posts/deleteImage', 'PostsController@deleteImage')->name('posts.deleteImage');

        // Edit image
        Route::get('backend/posts/editImage', 'PostsController@editImage')->name('posts.editImage');

        // Update image
        Route::post('backend/posts/updateImage', 'PostsController@updateImage')->name('posts.updateImage');

        // Orders
        Route::get('backend/orders/{order}/process', 'OrdersController@process')->name('orders.process');
    });
});

/*
 * CMS
 */
Route::get('flush', 'CMSController@flush')->name('flush');
Route::get('cron', 'CMSController@cron')->name('cron');
Route::get('analytics', 'CMSController@analytics')->name('analytics');
Route::get('symlink', 'CMSController@symlink')->name('symlink');

/*
 * Web
 */
Route::get('detalle/{slug}', 'MainController@product')->name('product');
Route::get('categoria/{slug}', 'MainController@category')->name('category');
Route::get('cotizar', 'MainController@cotizar')->name('cotizar');
Route::post('contacto', 'MainController@contacto')->name('contacto');
Route::name('cart.')->group(function() {
    Route::post('carrito', 'CartController@action')->name('action');
    Route::get('carrito', 'CartController@index')->name('index');
    Route::get('checkout', 'CartController@checkout')->name('checkout');
    Route::post('checkout', 'CartController@place')->name('place');
    Route::get('gracias/{hash}', 'CartController@finish')->name('finish');
});
Route::get('cuenta', 'AccountController@index')->name('account');
Route::post('cuenta', 'AccountController@update')->name('account');

/*
 * Auth
 */
Auth::routes();
Route::get('login/{provider}', 'Auth\LoginController@redirectToProvider');
Route::get('login/{provider}/callback', 'Auth\LoginController@handleProviderCallback');

Route::get('logout', 'Auth\LoginController@logout')->name('logout');

Route::get('temp', function() {
    $order = \App\Order::has('items')->first();

    return view('emails.confirm', [
        'order' => $order
    ]);
});

/*
|--------------------------------------------------------------------------
| 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!
|
*/
use App\Page;

$pages = Page::get();
foreach($pages as $page) {
    Route::get($page->post->slug, 'PageController@show')->name($page->post->slug);
}

