<?php

namespace app\admin\controller\sys_manage;

use app\admin\validate\sys_manage\ApiValidate;
use app\controller\Curd;
use app\model\SysApi;
use app\model\SysSerial;
use support\Request;
use support\Response;

class ApiController extends Curd
{
    public function __construct()
    {
        $this->model = new SysApi();
        $this->validate = true;
        $this->validateClass = new ApiValidate();
    }

    public function select(Request $request): Response
    {
        [$where, $format, $limit, $field, $order] = $this->selectInput($request);
        $order = $request->get('order', 'desc');
        $field = $field ?? 'api_addtimes';
        $query = $this->doSelect($where, $field, $order);

        return $this->doFormat($query, $format, $limit);
    }

    protected function insertInput(Request $request): array
    {
        $data = $this->inputFilter($request->post());
        $data['api_id'] = "AP" . date('ymdHi') . random_string(4, 'up');
        $data['api_extend_json'] = $data['api_extend_json'] ?? '{}';
        $data['api_request_json'] = $data['api_request_json'] ?? '{}';
        $data['api_response_json'] = $data['api_response_json'] ?? '{}';

        return $data;
    }
}