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

Route::get('/', function () {

    return view('welcome');
});

// 登录
Route::get('/login', 'Auth\LoginController@showLoginForm')->name('login');
Route::post('/login', 'Auth\LoginController@login')->name('login.post');

Route::prefix('admin')
    ->middleware('auth:web')
    ->namespace('Admin')
    ->group(function () {
        Route::get('/', function () {
            return view('admin.index');
        });
        Route::get('/welcome', function () {
            return view('admin.welcome');
        });

        // 管理员管理
        Route::get('/users', 'UsersController@index')->name('users');
        Route::post('/users/store', 'UsersController@store')->name('users.store');
        Route::get('/users_add', 'UsersController@create')->name('users.add');
        Route::get('/users/edit/{id}', 'UsersController@show')->name('users.edit');
        Route::post('/users/update/{id}', 'UsersController@update')->name('users.update');
        Route::delete('/users/delete/{id}', 'UsersController@destroy')->name('users.delete');


        // 通用管理
        Route::get('/{model}', 'CommonsController@index')->name('list');
        Route::get('/{model}/add', 'CommonsController@create')->name('add');

        Route::get('/{model}/edit/{id}', 'CommonsController@show')->name('edit');
        Route::post('/{model}/store', 'CommonsController@store')->name('store');
        Route::post('/{model}/update/{id}', 'CommonsController@update')->name('update');
        Route::delete('/{model}/delete/{id}', 'CommonsController@destroy')->name('delete');

        // 设置
        Route::get('/setting', 'ConfigsController@index')->name('configs');
        Route::post('/setting/{key}', 'ConfigsController@setValue')->name('configs.set');
        Route::post('/setting', 'ConfigsController@set')->name('configs.post');

        // 文件上传
        Route::post('/file/upload','UploadController@upload')->name('file.upload');

        Route::post('{model}/reply/{id}', 'CommonsController@update');

        // 通用操作
        Route::get('/actions/{id}', function (\Illuminate\Http\Request $request, $id) {
            $modelClass = str_replace('.', '\\', $request->input('model'));
            $key = $request->input('key');
            $model = new $modelClass();
            return view('admin.action', [
                'id' => $id,
                'fields' => $model->getExtensionFields($key),
                'route' => $request->input('route')
            ]);
        })->name('actions.open');
    });

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

// 文件上传
Route::post('/file/upload','UploadController@upload');
