<?php

use app\controller\admin\system\OrganizationController;
use app\entity\SysMenu;
use app\entity\SysOrganization;
use app\http\middleware\ClientMiddleware;
use app\model\SysDictionary;
use app\controller\admin\{auth,
    system\CacheController,
    system\CacheDataController,
    system\ConfigController,
    system\DictionaryController,
    system\DictionaryDataController,
    system\FileController,
    system\LoginRecordController,
    system\MenuController,
    system\OperateRecordController,
    system\RequestRecordController,
    system\RoleController,
    system\UserController
};
use app\http\middleware\AuthMiddleware;
use think\facade\Route;
use think\middleware\AllowCrossDomain;

Route::group("adminapi", function () {
    /*
     * 用户
     */
    Route::get("captcha", [auth\LoginController::class, "captcha"])->name("admin.LoginCaptcha");

    Route::post("login", [auth\LoginController::class, "index"])->name("admin.SysUserLogin")->middleware([ClientMiddleware::class]);


    Route::group(function () {


        Route::group("auth", function () {
            Route::post("logout", [auth\AuthController::class, "logout"])->name("admin.SysUserLogout");
            Route::get("user", [auth\AuthController::class, "user"])->name("admin.SysUserInfo");
        });


        Route::group("file", function () {
            /*
             * 文件管理
             */
            Route::get('page', [FileController::class, "page"])->name("filesystem.pageFiles");
            Route::delete('remove/batch', [FileController::class, "batchRemove"])->name("filesystem.batchRemoveFile");
            Route::post('upload', [FileController::class, "upload"])->name("filesystem.uploadFile");
        })->name('文件接口');


        Route::group("system", function () {
            Route::get('login-record/page', [LoginRecordController::class, "page"])->name("system.pageLoginRecords");
            Route::get('operate-record/page', [OperateRecordController::class, "page"])->name("system.pageOperateRecords");
            Route::get('request-record/page', [RequestRecordController::class, "page"])->name("system.pageRequestRecords");
            /*
             * 缓存管理
             */
            Route::get('cache', [CacheController::class, "list"])->name("system.listCache");
            Route::put('cache', [CacheController::class, "update"])->name("system.updateCache");
            /*
             * 缓存数据管理
             */
            Route::get('cache-data', [CacheDataController::class, "list"])->name("system.listCacheData");
            Route::delete('cache-data/batch', [CacheDataController::class, "batchDelete"])->name("system.batchRemoveCacheData");
            Route::put('cache-data', [CacheDataController::class, "update"])->name("system.updateCacheData");
            /*
            * 配置管理
            */
            Route::get('config$', [ConfigController::class, "list"])->name("system.listConfig");
            Route::get("config/info", [ConfigController::class, "info"])->name("system.getConfigInfo");
            Route::get('config/data', [ConfigController::class, "data"])->name("system.getConfigData");
            Route::get('config/existence', [ConfigController::class, "existence"])->name("system.configExistence");
            Route::post("config", [ConfigController::class, "add"])->name("system.addConfig");
            Route::put("config$", [ConfigController::class, "update"])->name("system.updateConfig");
            Route::delete("config/batch", [ConfigController::class, "batchDelete"])->name("system.batchDeleteConfig");
            Route::put("config/sync", [ConfigController::class, "sync"])->name("system.syncConfig");
            Route::put("config/data", [ConfigController::class, "updateData"])->name("system.updateConfigData");
            Route::put("config/status", [ConfigController::class, "updateStatus"])->name("system.updateConfigStatus");
            Route::put("config/sort", [ConfigController::class, "updateSort"])->name("system.updateConfigSort");


            /*
            * 用户管理
            */
            Route::get("user/page", [UserController::class, "page"])->name("system.pageUsers");
            Route::delete("user/batch", [UserController::class, "batchRemove"])->name("system.batchRemoveUser");
            Route::post("user", [UserController::class, "add"])->name("system.addUser");
            Route::put("user$", [UserController::class, "update"])->name("system.updateUser");
            Route::get("user/existence", [UserController::class, "existence"])->name("system.userExistence");
            Route::put("user/status", [UserController::class, "updateStatus"])->name("system.updateUserStatus");
            Route::put("user/password", [UserController::class, "updatePassword"])->name("system.updateUserPassword");
            /*
            * 角色管理
            */
            Route::get("role$", [RoleController::class, "list"])->name("system.listRoles");
            Route::get("role/page", [RoleController::class, "page"])->name("system.pageRoles");
            Route::delete("role/batch", [RoleController::class, "removeRoles"])->name("system.removeRoles");
            Route::get("role-menu/:role_id", [RoleController::class, "getMenu"])->name("system.getRoleMenu");
            Route::put("role-menu/:role_id", [RoleController::class, "updateMenu"])->name("system.updateRoleMenu");
            /*
            * 菜单管理
            */
            Route::get("menu$", [MenuController::class, "list"])->name("system.listMenus");
            Route::post("menu", [MenuController::class, "add"])->name("system.addMenu");
            Route::put("menu", [MenuController::class, "update"])->name("system.updateMenu");
            Route::delete("menu/:menu_id", [MenuController::class, "remove"])
                ->model('menu_id', SysMenu::class)
                ->name("system.removeMenu");

            /*
             * 机构管理
             */
            Route::get("organization", [OrganizationController::class, "lists"])->name("system.getOrganizationList");
            Route::post("organization", [OrganizationController::class, "add"])->name("system.addOrganization");
            Route::put("organization", [OrganizationController::class, "update"])->name("system.updateOrganization");
            Route::delete("organization/:organization_id", [OrganizationController::class, "remove"])
                ->model('organization_id', SysOrganization::class)
                ->name("system.removeOrganization");
            /*
             * 字典管理
             */
            Route::get("dictionary$", [DictionaryController::class, "lists"])->name("system.getDictionaryList");
            Route::post("dictionary", [DictionaryController::class, "add"])->name("system.addDictionary");
            Route::put("dictionary", [DictionaryController::class, "update"])->name("system.updateDictionary");
            Route::delete("dictionary/:dict_id", [DictionaryController::class, "remove"])->model('dict_id', SysDictionary::class)->name("system.removeDictionary");
            /*
             * 字典数据管理
             */
            Route::get("dictionary-data$", [DictionaryDataController::class, "lists"])->name("system.getDictionaryData");
            Route::get("dictionary-data/page", [DictionaryDataController::class, "page"])->name("system.pageDictionaryData");
            Route::post("dictionary-data", [DictionaryDataController::class, "add"])->name("system.addDictionaryData");
            Route::put("dictionary-data", [DictionaryDataController::class, "update"])->name("system.updateDictionaryData");
            Route::delete("dictionary-data/batch", [DictionaryDataController::class, "batchRemove"])->name("system.batchRemoveDictionaryData");

        })->name('系统接口');

    })->middleware([ClientMiddleware::class, AuthMiddleware::class]);

})->layer('admin')
    ->middleware([AllowCrossDomain::class], [
        'Access-Control-Allow-Headers' => 'Client, Client-Version, Client-Id, Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-CSRF-TOKEN, X-Requested-With'
    ]);
// AllowCrossDomain 缺一个预检检查.
//if($request->isOptions()) {
//    return \response()->header($header)->code(204);
//}