<?php
namespace App\Controllers;

use CodeIgniter\RESTful\ResourceController;

class ApiDashboard extends ResourceController {
    public function index() {
        $date1 = $this->request->getVar('date1');
		$date2 = $this->request->getVar('date2');
        
        $db = \Config\Database::connect();
        $sql = "SELECT * from GDC_CMOD.dbo.V_DASHBOARD_DEV where 
            COLLECTIONDATE between '$date1 00:00' and '$date2 23:59' 
            and ODR_DDATE between '$date1 00:00' and '$date2 23:59'";
		$query = $db->query($sql);
		$results = $query->getResultArray();

        // === Variabel counter ===
        $CPending = 0; $CPColl = 0; $CColl = 0; $CPRecv = 0;
        $CRecv = 0; $CInc = 0; $CPenV = 0; $CFin = 0; $CFinV = 0;

        $dataList = [];

        foreach ($results as $row) {
            $reqdate = '';
            if (!empty($row['REQDATE'])) {
                $reqdate = date('Y-m-d H:i', strtotime($row['REQDATE']));
            }

            $patname   = $row['Name'] ?? '';
            $reqnum    = $row['SP_ACCESSNUMBER'] ?? '';
            $hosnum    = $row['HOSTORDERNUMBER'] ?? '';
            $stats     = $row['STATS'] ?? '';
            $tests     = $row['TESTS'] ?? '';
            $isDelete  = $row['ISDELETE'] ?? 0;

            // Bersihkan test string
            $test = str_replace(['(', ')', ',', 'FA'], '', $tests);

            if (!is_numeric($test) && $isDelete == 0) {
                switch ($stats) {
                    case 'Pend':    $statCode = 1; $CPending++; break;
                    case 'PartColl':$statCode = 2; $CPColl++; break;
                    case 'Coll':    $statCode = 3; $CColl++; break;
                    case 'PartRecv':$statCode = 4; $CPRecv++; break;
                    case 'Recv':    $statCode = 5; $CRecv++; break;
                    case 'Inc':     $statCode = 6; $CInc++; break;
                    case 'PenV':    $statCode = 7; $CPenV++; break;
                    case 'Fin':     $statCode = 8; $CFin++; break;
                    case 'FinV':    $statCode = 9; $CFinV++; break;
                    default:        $statCode = 0; break;
                }

                // Simpan ke array
                $dataList[] = [
                    'statCode' => $statCode,
                    'reqdate' => $reqdate,
                    'patname' => $patname,
                    'reqnum' => $reqnum,
                    'hosnum' => $hosnum,
                    'reff' => $row['REFF'] ?? '',
                    'doc' => $row['DOC'] ?? '',
                    'tests' => $row['TESTS'] ?? '',
                    'stats' => $stats,
                    'isPrinted'=> $row['ODR_ISPRINTED'] ?? 0,
                    'isPending'=> $row['ODR_ISPENDING'] ?? 0,
                    'isHardcopy'=> $row['ODR_NFLAGHARDCOPY'] ?? 0,
                    'isVal1' => $row['ISVAL1'] ?? 0,
                    'val1user' => $row['VAL1USER'] ?? '', 
                    'isVal2' => $row['ISVAL2'] ?? 0,
                    'val2user' => $row['VAL2USER'] ?? '', 
                    'isDelete' => $isDelete,
                ];
            }
        }

        // === Total Summary ===
        $summary = [
            'Pending' => $CPending,
            'PartialCollected' => $CPColl,
            'Collected' => $CColl,
            'PartialReceived' => $CPRecv,
            'Received' => $CRecv,
            'Incomplete' => $CInc,
            'PendingValidation' => $CPenV,
            'Final' => $CFin,
            'FinalValidation' => $CFinV,
        ];

        // === Response JSON (misal API) ===
        return $this->respond([
            'status' => 'success',
            'summary' => $summary,
            'data' => $dataList,
        ]);
    }
}