<?php

namespace App\Http\Services\Backend\DataManagement;

use App\Http\Services\ConstDef\GeneralConst;
use App\Models\Web\CheckinGpAlloc;
use App\Models\Web\Syslog;

class CheckinTmpManagementService
{
    // 相關私有 model 調用器宣告
    private $checkinManagementDb;
    private $syslogManagementDb;
    
    public function __construct()
    {
        date_default_timezone_set("Asia/Taipei");
        // 建構 model 調用器
        $this->checkinManagementDb = new CheckinGpAlloc();
        $this->syslogManagementDb = new Syslog();
    }
    
    public function getCheckins(
        &$cnt = 0,
        $orderColumn,
        $orderDir,
        $start,
        $length,
        $searchValue
    )
    {
        $checkins = $this->checkinManagementDb
            ->select([
                'id',
                \DB::raw("CONCAT('<a href=\"checkinManagement/edit/', id, '\">', IFNULL(day_tmp, ''), '</a>', '<hr>', IFNULL(day, '')) as day"),
                \DB::raw("CONCAT(IFNULL(gp_tmp, ''), '<hr>', IFNULL(gp, '')) as gp"),
                'cdate',
                'mdate',
                \DB::raw("(select name from users where id=checkinGpAlloc.oid) as oid"),
            ]);
        // 過濾搜尋條件
        // 取總筆數
        $cnt = $checkins->count();
        // 排序
        $checkins = $checkins
            ->orderByRaw((int)$orderColumn . ' ' . $orderDir);
        // 彙整
        // 分頁
        $checkins = $checkins
            ->skip($start)->take($length);
        // 實際取資料
        $checkins = $checkins
            ->get()
            ->toArray();
        
        // 整理返回值並返回
        return $checkins;
    }
    
    public function getCheckinById($id)
    {
        // 取得參數
        // 調用資料庫(或者其他業務邏輯)
        $checkins = $this->checkinManagementDb->select([
            'id',
            \DB::raw("day_tmp as day"),
            \DB::raw("gp_tmp as gp"),
            'cdate',
            'mdate',
            \DB::raw("(select name from users where id=checkinGpAlloc.oid) as oid"),
        ])
            ->where('id', $id)
            ->first()
            ->toArray();
        
        // 整理返回值並返回
        return $checkins;
    }
    
    public function insertCheckin($day, $gp, $oid)
    {
        // 取得參數
        $data = [
            'day' . '_tmp' => $day,
            'gp' . '_tmp'  => $gp,
            'cdate'        => date('Y-m-d H:i:s'),
            'mdate'        => date('Y-m-d H:i:s'),
            'oid'          => $oid,
        ];
        // 調用資料庫(或者其他業務邏輯)
        $this->checkinManagementDb
            ->insert($data);
        $id = \DB::getPdo()->lastInsertId();
        // syslog
        $this->syslogManagementDb
            ->insert([
                'type'    => GeneralConst::LOG_ADMIN,
                'func'    => __FUNCTION__,
                'k'       => $oid,
                'memoIn'  => json_encode(['data' => $data], JSON_UNESCAPED_UNICODE),
                'memoOut' => json_encode(['id' => $id], JSON_UNESCAPED_UNICODE),
                'cdate'   => date("Y-m-d H:i:s"),
            ]);
        
        // 整理返回值並返回
        return $id;
    }
    
    public function modifyCheckin($id, $day, $gp, $oid)
    {
        // 取得參數
        $data = [
            'day' . '_tmp' => $day,
            'gp' . '_tmp'  => $gp,
            'mdate'        => date('Y-m-d H:i:s'),
            'oid'          => $oid,
        ];
        // 調用資料庫(或者其他業務邏輯)
        $res = $this->checkinManagementDb
            ->where('id', $id)
            ->update($data);
        $rc = \DB::select("SELECT ROW_COUNT() AS rc;");
        $rc = $rc[0]->rc;
        // syslog
        $this->syslogManagementDb
            ->insert([
                'type'    => GeneralConst::LOG_ADMIN,
                'func'    => __FUNCTION__,
                'k'       => $oid,
                'memoIn'  => json_encode(['id' => $id, 'data' => $data], JSON_UNESCAPED_UNICODE),
                'memoOut' => json_encode(['rc' => $rc], JSON_UNESCAPED_UNICODE),
                'cdate'   => date("Y-m-d H:i:s"),
            ]);
        
        // 整理返回值並返回
        return $res;
    }
    
}