<?php

namespace Encore\Admin\Controllers;

use App\Models\Company;
use Encore\Admin\Auth\Database\OperationLog;
use Encore\Admin\Auth\Permission;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Illuminate\Routing\Controller;

class LogController extends Controller
{
    /**
     * Index interface.
     *
     * @param Content $content
     *
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header(trans('admin.operation_log'))
            ->description(trans('admin.list'))
            ->body($this->grid());
    }

    /**
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new OperationLog());

        $grid->model()->orderBy('id', 'DESC');

        $grid->id('ID')->sortable();
        $grid->user()->name('管理员');
//        $grid->method()->display(function ($method) {
//            $color = array_get(OperationLog::$methodColors, $method, 'grey');
//
//            return "<span class=\"badge bg-$color\">$method</span>";
//        });
        $grid->message("操作信息")->display(function ($message){
            return "<a  title='".$message."' style='color:#333; '>".$message."</a>";
        })->width(500);
        $grid->path("访问地址")->label('info');

        $grid->ip("访问IP")->label('primary');
//        $grid->input()->display(function ($input) {
//            $input = json_decode($input, true);
//            $input = array_except($input, ['_pjax', '_token', '_method', '_previous_']);
//            if (empty($input)) {
//                return '<code>{}</code>';
//            }
//
//            return '<pre>'.json_encode($input, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE).'</pre>';
//        });

        $grid->created_at(trans('admin.created_at'));

        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
            if (Permission::isAdministrator()) {
                $actions->disableDelete(false);
            }
        });

        $grid->disableCreation();

        $grid->tools(function (Grid\Tools $tools) {
            $tools->batch(function (Grid\Tools\BatchActions $batch) {
                if (Permission::isAdministrator()) {
                    $batch->disableDelete(false);
                }
            });
        });
        $grid->filter(function ($filter) {

            $filter->column(1/3, function ($filter) {
                $filter->like('path', '访问路径');
            });
            $filter->column(1/3, function ($filter) {
                $userModel = config('admin.database.users_model');
                $filter->equal('user_id', '管理员')->select($userModel::all()->pluck('name', 'id'));
            });
            $filter->column(1/3, function ($filter) {
                $types=include app_path('Admin/logs/type.php');
                $filter->equal('route', '操作类型')->select($types);
            });
        });

        return $grid;
    }


    /**
     * Show interface.
     *
     * @param mixed   $id
     * @param Content $content
     *
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header("日志")
            ->description('详情')
            ->body($this->detail($id));
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(OperationLog::findOrFail($id));

        $show->id('ID');
        $show->ip("IP")->label('primary');
        $show->user("管理员")->as(function ($user) {
            return $user->name;
        });

        $types=include app_path('Admin/logs/type.php');
        $show->route('操作类型')->unescape()->as(function ($route) use ($types) {
            if (isset($types[$route])) {
                return "<span class=\"badge bg-green\">$types[$route]</span>
                        <span class=\"badge bg-green\">$route</span>";
            }
            return "<span class=\"badge bg-green\">$route</span>";
        });

        $show->message("操作信息");

        $show->path("访问路径")->label('info');
        $show->input('提交数据')->unescape()->as(function ($input) {
            $input = json_decode($input, true);
            $input = array_except($input, ['_pjax', '_token', '_method', '_previous_']);
            if (empty($input)) {
                return '<code>{}</code>';
            }

            return '<pre>'.json_encode($input, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE).'</pre>';
        });
        $show->created_at('创建时间');
        return $show;
    }


    /**
     * @param mixed $id
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy($id)
    {
        $ids = explode(',', $id);

        if (OperationLog::destroy(array_filter($ids))) {
            $data = [
                'status'  => true,
                'message' => trans('admin.delete_succeeded'),
            ];
        } else {
            $data = [
                'status'  => false,
                'message' => trans('admin.delete_failed'),
            ];
        }

        return response()->json($data);
    }
}
