<?php

namespace App\Http\Controllers\Backend\DataManagement;

use Illuminate\Http\Request;
use App\Http\Services\Backend\DataManagement\SyslogtManagementService;
use App\Http\Controllers\Controller;
use App\Http\Services\ConstDef\GeneralConst;
use App\Http\Services\CheckParamService;

class SyslogtManagementController extends Controller
{
    // 相關私有服務層調用器宣告
    private $syslogtManagementSv;
    private $checkParamSv;
    
    public function __construct()
    {
        // 建構服務層調用器
        $this->syslogtManagementSv = new SyslogtManagementService();
        $this->checkParamSv = new CheckParamService();
        // 時區調整
        date_default_timezone_set("Asia/Taipei");
    }
    
    public function index(Request $request)
    {
        // 渲染
        return view('admin.DataManagement.SyslogtManagement', [
            'type' => GeneralConst::$syslogtMap,
        ]);
    }
    
    public function grid(Request $request)
    {
        // 取得參數
        $param = $_GET;
        if ($param == null) exit();
        $draw = $param["draw"]; //客戶端傳來的查詢次數，無條件回傳用以核對
        $orderColumn = $param["order"][0]["column"] + 1; //前端從 0 開始送，但 mysql 從 1 開始算
        $orderDir = $param["order"][0]["dir"];
        $start = $param["start"];   // 頁碼
        $length = $param["length"]; // 一頁多大
        $searchValue = $param["search"]["value"];
        //客製化搜尋欄位
        $k = $param["columns"][1]["search"]["value"];
        $type = $param["columns"][2]["search"]["value"];
        $createDate = explode("\n", $param["columns"][3]["search"]["value"]);
        $createDateStart = $createDate[0] ?? null;
        $createDateFinal = $createDate[1] ?? null;
        // 驗證
        if ($k != filter_var($k, FILTER_SANITIZE_SPECIAL_CHARS)) $k = "___CANNOT_FIND_STRING___";
        if (!$this->checkParamSv->LenMToN($k, 0, 50)) $k = "___CANNOT_FIND_STRING___";
        if ($createDateStart == "") $createDateStart = "1900-01-01";
        if ($createDateFinal == "") $createDateFinal = "2050-12-31";
        if (!$this->checkParamSv->validateDate($createDateStart, 'Y-m-d')) $createDateStart = "1900-01-01";
        if (!$this->checkParamSv->validateDate($createDateFinal, 'Y-m-d')) $createDateFinal = "2050-12-31";
        //資料庫
        $recordsTotal = 0;
        $result = $this->syslogtManagementSv->getSyslogts(
            $recordsTotal,
            $orderColumn,
            $orderDir,
            $start,
            $length,
            $searchValue,
            $k,
            $type,
            $createDateStart,
            $createDateFinal
        );
        // 整理返回資料
        $data = array();
        for ($i = 0; $i < count($result); $i++) {
            $data[] = array(
                //一般資料
                $result[ $i ]["id"],
                $result[ $i ]["type"],
                $result[ $i ]["func"],
                $result[ $i ]["k"],
                $result[ $i ]["memoIn"],
                $result[ $i ]["memoOut"],
                $result[ $i ]["cdate"],
            );
        }
        $json = array(
            "draw"            => $draw,
            "recordsTotal"    => $recordsTotal,
            "recordsFiltered" => $recordsTotal, //其實還是填入所有筆數，本次筆數可從陣列取得
            "data"            => $data,
        );
        
        // 返回
        return json_decode(json_encode($json, JSON_NUMERIC_CHECK), true);
    }
    
    public function export(Request $request)
    {
        // 取得參數
        $param = $_POST;
        if ($param == null) exit();
        // 驗證
        if ($param['k'] != filter_var($param['k'], FILTER_SANITIZE_SPECIAL_CHARS)) $param['k'] = "___CANNOT_FIND_STRING___";
        if (!$this->checkParamSv->LenMToN($param['k'], 0, 50)) $param['k'] = "___CANNOT_FIND_STRING___";
        if ($param['createDateStart'] == "") $param['createDateStart'] = "1900-01-01";
        if ($param['createDateFinal'] == "") $param['createDateFinal'] = "2050-12-31";
        if (!$this->checkParamSv->validateDate($param['createDateStart'], 'Y-m-d')) $param['createDateStart'] = "1900-01-01";
        if (!$this->checkParamSv->validateDate($param['createDateFinal'], 'Y-m-d')) $param['createDateFinal'] = "2050-12-31";
        // 製作
        $title = [
            'ID',
            '名義',
            '業務功能',
            '對象 ID',
            '輸入細節',
            '輸出細節',
            '時間戳',
        ];
        $syslogts = $this->syslogtManagementSv->getExports($param);
        $this->syslogtManagementSv->downloadExcel($title, $syslogts, date("YmdHis") . '-SYSLOGT-REPORT');
    }
}