<?php

declare(strict_types=1);

namespace app\admin\controller\crud;

use app\admin\model\CrudLog;
use app\common\controller\Backend;
use Webman\Http\Request;
use support\Response;

class Log extends Backend
{
    protected ?object $model = null;

    protected array|string $preExcludeFields = ['id', 'create_time'];
    protected array|string $quickSearchField = ['id', 'table_name', 'comment'];
    protected array $noNeedPermission = ['index'];

    protected function initController(Request $request): ?Response
    {
        $this->model = new CrudLog();

        if (!$this->auth->check('crud/crud/index')) {
            return $this->error(__('You have no permission'), [], 401);
        }
        return null;
    }

    public function index(Request $request): Response
    {
        $response = $this->initializeBackend($request);
        if ($response !== null) return $response;

        if ($request->get('select') || $request->post('select')) {
            return $this->select($request);
        }

        list($where, $alias, $limit, $order) = $this->queryBuilder();
        $res = $this->model
            ->withJoin($this->withJoinTable ?? [], $this->withJoinType ?? 'LEFT')
            ->alias($alias)
            ->where($where)
            ->order($order)
            ->paginate($limit);

        return $this->success('', [
            'list' => $res->items(),
            'total' => $res->total(),
            'remark' => get_route_remark(),
        ]);
    }
}
