<?php

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

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\Datacase;
use App\Models\Mongo;

class ViewerController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
    // dd(1);

        $view['test'] = 'test';
        if(isMobile()){

        }
        return view('Terra.wait.viewer');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $r)
    {
        if($r->event=="capture_image")       {$this->capture_image($r);}

    }

    public function capture_image($r){
        if(isset($r->image)){
            $hn = $r->hn;
            $date = $r->date;
            $upload_location = "D:/laragon/htdocs/store/$hn/$date";

            $image = $r->file('image');
            $image_name = $image->getClientOriginalName();
            $image->move($upload_location,$image_name);
        }
        return redirect()->back();
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show(Request $r, $id)
    {
        $view['type'] = 'edit';
        if(isset($_GET['type'])){
            $view['type'] = 'view';
        }

        if(isset($r->hn) && isset($r->appointment)){
            $w[0] = array('hn', 'like', $r->hn);
            $w[1] = array('appointment', 'LIKE', '%'.$r->appointment.'%');
            $tb_case = Mongo::table('tb_case')->where($w)->get();
            $tb_case = (object) $tb_case;
            $arr     = [];
            $vdo_arr = [];
            foreach ($tb_case as $data) {
                $data = (object) $data;
                if(isset($data->procedurename)){
                    if(isset($data->photo)){
                        foreach($data->photo as $p){
                            $arr[$data->procedurename][] = $p;
                        }
                    }

                    if(isset($data->video)){
                        foreach($data->video as $v){
                            $vdo_arr[$data->procedurename][] = $v;
                        }
                    }
                }
            }

            $view['count']          = 2;
            $view['case_id']        = '';
            $view['tb_case']        = (object) $tb_case[0];
            $view['images']         = $arr;
            $view['videos']         = $vdo_arr;


        } else {
            $case_id = $id;
            $tb_case = Mongo::table('tb_case')->where('_id', $id)->first();
            $view['count']          = 1;
            $view['case_id']        = $case_id;
            $view['tb_case']        = isset($tb_case) ? (object) $tb_case : null;
            $view['images']         = isset($view['tb_case']->photo) ? $view['tb_case']->photo : [];
            $view['videos']         = isset($view['tb_case']->video) ? $view['tb_case']->video : [];
        }

        $view['hn']             = isset($view['tb_case']->hn) ? $view['tb_case']->hn : '';
        $view['appointment']    = isset($view['tb_case']->appointment) ? $view['tb_case']->appointment : '';
        $view['studydate']      = isset($view['tb_case']->studydate) ? $view['tb_case']->studydate : '';
        $view['url']            = str_replace('endoindex', '', url(''));
        $view['patientdicom']   = '';
        $view['id']             = $id;



        return view('terra.viewer.show', $view);
        // return view('test.test_layout', $view);

    }


    public function getfile($dir,$type){
        $arr   = array();
        if (is_dir($dir)) {
            if ($dh = opendir($dir)) {
                while (($file = readdir($dh)) !== false) {
                    $temp = $file;
                    if(substr($file,-4)==$type){
                        $arr[] = $temp;
                    }

                }
                closedir($dh);
            }
        }
        return $arr;
    }



    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        // return view('test.test_layout', $view);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
