<?php

namespace App\Http\Controllers\Terra;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;


use Illuminate\Support\Facades\DB;


use App\Models\Department;

class HomeController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index()
    {

        $view['moss'] = "";
        $t1 = DB::table("tb_case")
        ->select('case_hn')
        ->distinct()
        ->pluck('case_hn');

        $i=0;
        $arr = array();
        foreach($t1 as $data){
            $tb_case = DB::table('tb_case')->where('case_hn',$data)->first();
            $arr[$i]['cid']     = $tb_case->case_id;
            $arr[$i]['case_hn'] = $tb_case->case_hn;
            $i++;
        }

        // $view['tb_case'] = $arr;

        // ข้อมูลจำลอง -------------------------------------------------------------------------------------------------------------------------------------------
        // $mockup[] = ['name'=>'Suratchanut Chitrat','procedure'=>'EGD Colonoscopy','modality'=>'ES','remark'=>'Anesthetic + Flu','report'=>'Draft','status'=>'3'];
        // $mockup[] = ['name'=>'Sarunwut Tongdee','procedure'=>'Colonoscopy','modality'=>'US','remark'=>'-','report'=>'Final','status'=>'3'];
        // $mockup[] = ['name'=>'Sadayu Thongloi','procedure'=>'Bronchoscopy','modality'=>'US','remark'=>'Flu include','report'=>'','status'=>'1'];
        // $mockup[] = ['name'=>'Lorem Ipsum','procedure'=>'ERCP EUS','modality'=>'ES','remark'=>'H.Bleeding papilla ','report'=>'','status'=>'2'];
        // $mockup[] = ['name'=>'Suvicha Chantarasurin','procedure'=>'EGD','modality'=>'ES','remark'=>'-','report'=>'','status'=>'2'];
        // $mockup[] = ['name'=>'Suvicha Boonmee','procedure'=>'EGD Colonoscopy','modality'=>'OT','remark'=>'Patient came alone','report'=>'','status'=>'1'];
        // $mockup[] = ['name'=>'Boonyasit Surname','procedure'=>'ERCP','modality'=>'CT','remark'=>'Send to Ward6','report'=>'Final','status'=>'3'];
        // $mockup[] = ['name'=>'Nhum Meejai','procedure'=>'EGD','modality'=>'US','remark'=>'-','report'=>'','status'=>'2'];
        // $view['mockup'] = $mockup;
        // $view['view'] = $view;
        // ข้อมูลจำลอง -------------------------------------------------------------------------------------------------------------------------------------------


        $view['procedure'] = DB::table('tb_procedure')->select('procedure_id','procedure_code','procedure_name')->get();
        $view['tb_case'] = DB::table('tb_case')
        ->join('patient','patient.hn','tb_case.case_hn')
        ->where('studydate','!=',0)
        ->get();



        return view('terra.home.index',$view);
        // return view('endocapture.home6.index', $view);
    }


    public function store(Request $r)
    {
        switch($r->event){
            case "config_type"  : $this->config_type($r);   break;
            case "clearview"    : $this->clearview($r);     break;
            case "queue"        : $this->queue($r);         break;
        }
    }

    public function config_type($r){
        $str            = file_get_contents("D:/laragon/htdocs/config/project/$r->config_type.txt");
        $json           = (array) jsonDecode($str);
        $json[$r->id]   = $r->value;
        file_put_contents($_SERVER['DOCUMENT_ROOT']."/config/project/$r->config_type.txt", jsonEncode($json));
    }


    public function show($id){


        $view['id'] = $id;
        return view("endocapture.superadmin.page.$id",$view);
    }




    public function clearview(){
        $dir = storage_path('framework\views');
        function getDirContents($dir, &$results = array()) {
            $files = scandir($dir);
            foreach ($files as $key => $value) {
                $path = realpath($dir . DIRECTORY_SEPARATOR . $value);
                if (!is_dir($path)) {
                    $find = strpos($path,'.gitignore');
                    if($find>0){
                        echo $path;
                    }else{
                        unlink($path);
                        $results[] = $path;
                    }
                } else if ($value != "." && $value != "..") {
                    getDirContents($path, $results);
                    $results[] = $path;
                }
            }
            return $results;
        }
        getDirContents($dir);
    }

}
