<?php

namespace App\Http\Controllers\EndoCAPTURE;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Controllers\Api\FilesendController;

class OperationController extends Controller
{
    public function index()
    {
        if(isset($_GET['casedata'])){
            $casedata = hex2bin($_GET['casedata']);
            $casedata = json_decode($casedata, true);
        }
        $view['configadmin']        = getCONFIG('admin');
        $view['hn'] = $casedata['hn'];
        $view['room_name'] = $casedata['room_name']??'';
        $view['procedurecode'] = $casedata['procedurecode'];
        $view['procedure'] = $casedata['procedure'];
        $view['date'] = $casedata['date'];
        $view['physician'] = $casedata['physician'];
        $view['patientname'] = $casedata['patientname'];
        $view['gender'] = isset($casedata['gender']) ? ($casedata['gender'] == '1' ? 'Male' : ($casedata['gender'] == '2' ? 'Female' : 'ไม่ระบุ')) : 'ไม่ระบุ';
        $view['birthdate'] = $casedata['birthdate'] ?? 'ไม่ระบุ';
        $view['age'] = $casedata['age'] ?? 'ไม่ระบุ';
        $view['imgall'] = $this->imgall($casedata['hn']);
        $view['room_name'] =  $view['configadmin']->room_name??'';
        $view['roomid'] = $view['configadmin']->station_room??0;
        $view['type'] = 'operation';
        $view['drive_color'] = 'danger';
        $view['persen'] = 0;
        $view['ds'] = 'operation';
        $view['drive'] = 'D:';
        $view['cloudid'] = $casedata['cid'];
        $view['cid'] = "not_use";
        $view['case'] = (object) [];
        $view['case']->_id = "not_use";
        $view['case']->case_procedurecode = $casedata['procedurecode'];
        $view['case']->case_procedure = $casedata['procedure'];
        $view['case']->case_date = $casedata['date'];
        $view['case']->case_physician = $casedata['physician'];
        $view['case']->case_patientname = $casedata['patientname'];
        $view['case']->comcreate = "screening";
        $view['url'] = url("operation");
        $view['case_id'] = $casedata['cid'];
        $view['vdo_property'] = "";

        return view('EndoCAPTURE.camera.passdata.main', $view);
    }

    public function imgall($hn){
        $imgall = [];
        $directory = 'd:/laragon/htdocs/allphoto';
        $files = scandir($directory);

        foreach($files as $file) {
            if(pathinfo($file, PATHINFO_EXTENSION) == 'jpg') {
                if(strpos($file, $hn) === 0) {
                    $imgall[] = $file;
                }
            }
        }
        return $imgall;
    }


    public function send_file_to_server()
    {

        $filesend = new FilesendController();
        $filesend->scandir();
    }


    public function update(Request $request, $id)
    {
        //
    }

    public function destroy($id)
    {
        //
    }
}
