<?php
namespace backend\modules\admin\controllers;

use Yii;
use backend\models\LogsApi;

/**
 * 系统日志
 */
class LogsController extends BaseController
{
    /**
     * 列表页
     * @return string
     */
    public function actionIndex()
    {
        return $this->render('index');
    }

    /**
     * AJAX获取数据
     */
    public function actionAjax()
    {
        $get = Yii::$app->request->get();
        $orderInt = isset($get['order'][0]['column']) ? (int) $get['order'][0]['column'] : 1;
        $order = isset($get['columns'][$orderInt]['data']) ? $get['columns'][$orderInt]['data'] : '';
        $orderBy = isset($get['order'][0]['dir']) ? $get['order'][0]['dir'] : 'desc';
        $search = isset($get['search']['value']) ? trim($get['search']['value']) : '';

        $start = isset($get['start']) ? (int) $get['start'] : 0;
        $length = isset($get['length']) ? (int) $get['length'] : 20;
        $draw = isset($get['draw']) ? $get['draw'] : 1;

        $params = [
            'order' => $order,
            'orderBy' => $orderBy,
            'search' => $search,
            'start' => $start,
            'length' => $length,
            'draw' => $draw,
        ];

        $api = new LogsApi();
        $result = $api->getList($params);
        echo json_encode($result['data']);
    }
    
    /**
     * 查看日志
     */
    public function actionView()
    {
        $path = trim(Yii::$app->request->get('path'));
        $api = new LogsApi();
        $result = $api->view(['path' => $path]);
        echo $result['data']['contents'];
    }
    
    /**
     * 清空日志
     */
    public function actionTruncate()
    {
        $path = trim(Yii::$app->request->get('path'));
        $api = new LogsApi();
        $result = $api->truncate(['path' => $path]);
    }
    
}
