<?php

namespace App\Http\Controllers\EndoBOOK;

use Illuminate\Http\Request;
use DB;
use Illuminate\Support\Facades\Hash;

class UsersController extends Controller
{

    public function __construct(Request $r){checklogin();}
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * 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->type=='tb_bookprocedure'){
            $check = DB::table('tb_bookprocedure')->where('bookprocedure_name',$r->bookprocedure_name)->count();
            if($check == 0){
                $data['bookprocedure_name']   = $r->bookprocedure_name;
                DB::table('tb_bookprocedure')->insert($data);
            }
            $procedure_code = DB::table('tb_bookprocedure')->where('bookprocedure_name',$r->bookprocedure_name)->first();
            $value['book_procedure']    = $procedure_code->bookprocedure_code;
            $value['book_user']         = $r->user_id;
            DB::table('tb_booking')->insert($value);
        }
        if($r->type=='tb_bookroom'){
            $check = DB::table('tb_bookroom')->where('bookroom_name',$r->bookroom_name)->count();
            if($check == 0){
                $data['bookroom_name']   = $r->bookroom_name;
                $data['bookroom_color']  = $r->bookroom_color;
                DB::table('tb_bookroom')->insert($data);
            }
            $room_id = DB::table('tb_bookroom')->where('bookroom_name',$r->bookroom_name)->first();
            $value['book_room'] = $room_id->bookroom_id;
            $value['book_user'] = $r->user_id;
            DB::table('tb_booking')->insert($value);
        }
        if($r->type=='tb_bookdoctor'){
            $check = DB::table('tb_bookdoctor')->where('bookdoctor_name',$r->bookdoctor_name)->count();
            if($check == 0){
                $data['bookdoctor_name']   = $r->bookdoctor_name;
                DB::table('tb_bookdoctor')->insert($data);
            }
            $doctor_id = DB::table('tb_bookdoctor')->where('bookdoctor_name',$r->bookdoctor_name)->first();
            $value['book_doctor']   = $doctor_id->bookdoctor_id;
            $value['book_user']     = $r->user_id;
            DB::table('tb_booking')->insert($value);
        }
        if($r->type=='add_user'){
            $data['name']   = $r->email;
            $data['email']  = $r->email;
            $data['user_type']   = 'user';
            DB::table('users')->insert($data);
        }
        return redirect()->back();
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

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

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $r, $id)
    {
        // dd($r);
        if((isset($r->email)) && (isset($r->password))){
            $pass = Hash::make($r->password);
            $data['name']       = $r->email;
            $data['email']      = $r->email;
            $data['password']   = $pass;
            DB::table('users')->where('id',$id)->update($data);
        }
        return redirect()->back();
    }

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