<?php

namespace App\Http\Controllers\EndoINDEX;
use App\Http\Controllers\Controller;
use App\Models\tb_department;
use App\Models\tb_procedure;
use App\Models\tb_room;
use App\Models\tb_scope;
use App\Models\users;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class Department6Controller extends Controller
{

    public function __construct(Request $r){checklogin();}

    public function index(Request $r)
    {
        $view['department']  = tb_department::get_department();
        if(isset($r->type) && isset($r->department)){
            $view['data']     = tb_department::search_department($r);
            $view['rtn_dep']  = $r->department;
            $view['rtn_type'] = $r->type;
        }
        return view('admin.department.new',$view);
    }

    public function show($id)
    {
        $view['id'] = $id;
        if(isset($_GET['type'])){
            $view['type'] = $_GET['type'];
        }else{
            return redirect('department');
        }
        $view['department'] = $_GET['department'];

        $view['department_all']  = tb_department::get_department();
        $is_uesr = array('endo','doctor','anesthesia','nurse','reprocess','register', 'nurse_anes');
        if(in_array($view['type'],$is_uesr)){
            $view['data'] = DB::table('users')->where('id',$id)->first();
            if(isset($view['data']->user_pic)){
                $view['image'] = picurl('user').'/'.$view['data']->user_pic;
            }else{
                $view['image'] = picurl('user').'/avatar.png';
            }

        }elseif($view['type']=='procedure'){
            $view['data'] = tb_procedure::get_procedure_by_id($id);
        }elseif($view['type']=='room'){
            $view['data'] = tb_room::get_room_by_id($id, 'first');
        }elseif($view['type']=='scope'){
            $view['data'] = tb_scope::get_scope_by_id($id);
        }
        $view['view']   = $view;

        return view('admin.department.show_new',$view);

    }

    public function create()
    {
        $view['type'] = $_GET['type'];
        $view['department'] = $_GET['department'];
        $view['department_all']  = tb_department::get_department();
        $view['image'] = picurl('user').'/avatar.png';
        $view['view'] = $view;
        return view('admin.department.create',$view);
    }

    public function store(Request $r)
    {
        if($r->event=='add_department'){
            $this->department_add($r);
            return redirect('department');
        }else{
            $is_uesr = array('endo','doctor','anesthesia','nurse','reprocess','register', 'nurse_anes');
            if(in_array($r->user_type,$is_uesr)){
                $this->user_add($r);
            }elseif($r->user_type=='procedure'){
                $this->procedure_add($r);
            }elseif($r->user_type=='room'){
                $this->room_add($r);
            }elseif($r->user_type=='scope'){
                $this->scope_add($r);
            }
        }
        return redirect("department?_method=PUT&department=".$r->department2."&type=".$r->user_type);
    }


    public function update(Request $r, $id)
    {
        $is_uesr = array('endo','doctor','anesthesia','nurse','reprocess','register');
        if(in_array($r->user_type,$is_uesr)){
            $this->user_update($r);
        }elseif($r->user_type=='procedure'){
            $this->procedure_update($r);
        }elseif($r->user_type=='room'){
            $this->room_update($r);
        }elseif($r->user_type=='scope'){
            $this->scope_update($r);
        }
        // return redirect('department');
        return redirect("department?_method=PUT&department=".$r->department2."&type=".$r->user_type);
    }

    public function user_add($r){
        $r = users::add_user6($r);
        createTEMPMASTERDATA('users');
        $this->department_update($r,'department_user');
    }
    public function department_add($r){
        tb_department::add_department($r);
        createTEMPMASTERDATA('tb_department');
    }

    public function department_update($r,$field){
        tb_department::department_update6($r, $field);
        createTEMPMASTERDATA('tb_department');
    }
    public function room_add($r){
        $r = tb_room::add_room6($r);
        createTEMPMASTERDATA('tb_room');
        $this->department_update($r,'department_room');
    }
    public function scope_add($r){
        $scope_autocrop = 0;
        if(isset($r->scope_autocrop)){
            $scope_autocrop = 1;
        }
        $r = tb_scope::add_scope6($r, $scope_autocrop);
        createTEMPMASTERDATA('tb_scope');
        // $r->department = jsonEncode($r->department_id);
        $this->department_update($r,'department_scope');
    }
    public function procedure_add($r){
        $r = tb_procedure::add_procedure($r);
        $this->department_update($r,'department_procedure');
    }

    public function user_update($r){
        users::update_user6($r);
        createTEMPMASTERDATA('users');
        $this->department_update($r,'department_user');
    }
    public function procedure_update($r){
        tb_procedure::update_procedure6($r);
        createTEMPMASTERDATA('tb_procedure');
        $r->id = $r->procedure_code;
        $this->department_update($r,'department_procedure');
    }

    public function room_update($r){
        tb_room::update_room6($r);
        createTEMPMASTERDATA('tb_room');
        $this->department_update($r,'department_room');
    }

    public function scope_update($r){
        tb_scope::update_scope_in_department($r);
        createTEMPMASTERDATA('tb_scope');
        $this->department_update($r,'department_scope');
    }
}
