<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\User;
use App\Pegawai;
use App\Periksa;
use App\ResultSample;

use Session;
use Auth;
use Validator;

class AuthController extends Controller
{
	public function login() {
		$data		= [];
		$previlage 	= Session('previlage');
		if ($previlage == '' OR $previlage === null) {
			$getdomain = url()->current();
			$data['getdomain'] = $getdomain;
			return view('login', $data);
		} else {
			return redirect('/');
		}
    }
    public function authenticate(Request $request) {
        $validator  =   Validator::make($request->all(), [
            'username'  => 'required',
            'password'  => 'required',
        ]);

        if($validator->fails()) {
            Session::flash('message', 'Username dan Password Harus diisi');
            return back();
        } else {
            $username   =   $request->username;
            $password   =   $request->password;
            $firebaseid	=   $request->firebaseid;
			if (is_null($firebaseid)){ $firebaseid = ''; }
            $auth 		= 	Auth::attempt([
                'username' => $username,
                'password' => $password
            ]);

            if(!$auth) {
				$cekada = User::whereNotNull('username')->count();
				if ($cekada == 0){
					User::create([
						'nama'      	=>  'Administrator',
						'username' 		=>  'admin',
						'password' 		=>  bcrypt('semangat'),
						'previlage' 	=> 	'developer'
					]);
				}
            	Session::flash('message', 'Username atau password anda salah');
            	return back();
            }
            $user 	= Auth::user();
			$photo 	= $user->getPhoto->xfile ?? '/doctor.png';
			if ($firebaseid != ''){
				User::where('username', $request->username)->update([
					'firebase'	=> $firebaseid
				]);
			}
			
			Session::put('id', $user->id);
			Session::put('nama', $user->nama);
			Session::put('username', $user->username);
			Session::put('previlage', $user->previlage);
			Session::put('photo', $photo);
			Session::save();
			User::where('id', $user->id)->update([
				'active_status' => 1,
			]);
			return redirect('/');
        }
    }
	public function cekandroid($firebaseid){
		$cekperiksa = Periksa::all();
		if (!empty($cekperiksa)){
			$tahun 		= date('y');
			$getdata 	= ResultSample::where('accession_number', 'LIKE', $tahun.'.%')->get();
			foreach($getdata as $rows){
				$norm 	= $rows->patient_id;
				$nama2 	= $rows->patient_name_last;
				$nama1 	= $rows->patient_name_first;
				$nama 	= $nama1.' '.$nama2;
				$alamat = $rows->address_street;
				$nofoto = $rows->accession_number;
				
				Pasien::updateOrCreate(
					[
						'norm'	=> $norm,
					],
					[
						'nama'          =>  $nama,
						'jk'            =>  $jk,
						'tgl_lahir'     =>  $tgllahir,
						'kota'          =>  config('global.subdomainapps'),
						'telpon'        =>  $telpon,
						'alamat'        =>  $alamat,
						'nik'        	=>  $nik,
						'bpjs'        	=>  $bpjs,
					]
				);
			}
		}
		$cekuser =   User::where('firebase', $firebaseid)->count();
		if ($cekuser != 0){
			$user   = User::where('firebase', $firebaseid)->first();
			$photo 	= $user->getPhoto->xfile ?? '/doctor.png';
			Session::put('id', $user->id);
			Session::put('nama', $user->nama);
			Session::put('username', $user->username);
			Session::put('previlage', $user->previlage);
			Session::put('photo', $photo);
			Session::save();
			User::where('id', $user->id)->update([
				'active_status' => 1,
			]);
			return redirect('/');

		} else {
			Session::flash('message', 'Firebase ID Not Saved, Please Login');
			Session::flash('status', $firebaseid);
			return redirect('/login'); 
		}
    }
    public function logout(Request $request) {
        Auth::logout();
        $request->session()->regenerate();
        $request->session()->flush();
		session()->flush();
        return redirect('/');
    }
}
