<?php

namespace App\Http\Controllers\EndoBOOK;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

use App\Models\Mongo;
use App\Models\Department;


class HomeController extends Controller
{
    public function __construct(Request $r){checklogin();}
    public function index(Request $r){
        $userID     = uid();
        $userTYPE   = uget("user_type");
        return redirect("book/doctor/$userID");

        $view['userID']         = uid();
        $view['procedure']      = Department::procedure(uid());
        $view['doctor']         = Department::user('doctor');
        if($r->event == 'filters'){
            $view['search']             = $r->search;
            $view['search_physician']   = $r->search_physician;
            $view['search_procedure']   = $r->search_procedure;
            $w1[] = array('status','!=','');
            $w2[] = array('status','!=','');
            $w3[] = array('status','!=','');
            if(isset($r->search)){
                $w1[] = array('hn','like',"%$r->search%");
                $w2[] = array('firstname','like',"%$r->search%");
                $w3[] = array('lastname','like',"%$r->search%");
            }
            if(isset($r->search_physician)){
                $w1[] = array('physician',"$r->search_physician");
                $w2[] = array('physician',"$r->search_physician");
                $w3[] = array('physician',"$r->search_physician");
            }
            if(isset($r->search_procedure)){
                $w1[] = array('procedure','like',"%$r->search_procedure%");
                $w2[] = array('procedure','like',"%$r->search_procedure%");
                $w3[] = array('procedure','like',"%$r->search_procedure%");
            }
            if(isset($r->search_day)){
                $date="$r->search_year-$r->search_month-$r->search_day";
                $view['s_year'] = $r->search_year;
                $view['s_month'] = $r->search_month;
                $view['s_day'] = $r->search_day;
            }else{$date = date('Y-m-d');}
            $view['tb_booking'] = Mongo::table('tb_booking')
            ->where($w1)
            ->orWhere($w2)
            ->orWhere($w3)
            ->whereDate('date',$date)
            ->orderby('bk_id','desc')
            ->get();

        }else{
            $date = date('Y-m-d');
            $view['tb_booking'] = Mongo::table('tb_booking')
            ->whereDate('date',$date)
            ->orderby('bk_id','desc')
            ->get();
        }

        $date = date_create($date);
        $view['date_str']   = date_format($date, "D, d F Y");
        $view['tb_bookset_calendar_department'] = Mongo::table('tb_bookset_calendar_department')->get();

        return view('EndoBOOK.home.index',$view);
    }

    public function create(){
        return view('EndoBOOK.home.create');
    }


    public function store(Request $r){}
    public function show($id){return view('EndoBOOK.home.show');}
    public function edit($id){}
    public function update($id,Request $r){}
    public function destroy($id){}
}
