<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;

// use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Models\Users;
use App\Http\Models\Bidan;
// use App\Http\Models\CommandCenter;
use App\Http\Models\Provinsi;
use App\Http\Models\Kabupaten;
use App\Http\Models\Kecamatan;
use App\Http\Models\Desa;
use Validator, Sentinel, Redirect;

class APIUserController extends Controller
{
    public function DoLogin(Request $request)
    {
        if (!empty($request->username)) {
            if (!empty($request->password)) {
                $users = Users::whereRaw('nik="'.$request->username.'" || email="'.$request->username.'"')->first();
                if (count($users) != 0) {
                    try
                    {
                        $credentials = array(
                            'login'         => $request->username,
                            'password'      => $request->password
                        );

                        if ($request->has('remember_me')) {
                            $login = Sentinel::authenticateAndRemember($credentials);
                        } else {
                            $login = Sentinel::authenticate($credentials);
                        }

                        if($login) {
                            // if(empty($users->player_id) || ($users->player_id != $request->player_id)){
                            //     $users->player_id = $request->player_id;
                            //     $users->save();
                            // }
                            if ($login->level_user == 2 || $login->level_user == 3) {
                                if ($login->active == 1) {
                                    $return = ['status'=>'success', 'code'=>'200', 'message'=>'Login Berhasil' , 'data'=>$users];
                                }else{
                                    Sentinel::logout();
                                    $return = ['status'=>'error', 'code'=>'403','message'=>'Akun Anda Belum bisa di Gunakan !!', 'data'=>''];
                                }
                            }else{
                                Sentinel::logout();
                                $return = ['status'=>'error', 'code'=>'403','message'=>'Akun Anda Tidak Memiliki Akses !!', 'data'=>''];
                            }
                        } else {
                            $return = ['status'=>'error', 'code'=>'403','message'=>'NIK/Email dan Password Salah, silahkan Coba Kembali !!', 'data'=>''];
                        }
                    }
                    catch (\Cartalyst\Sentinel\Checkpoints\NotActivatedException $e)
                    {
                        $return = ['status'=>'warning', 'code'=>'403','message'=>'Akun Anda Masih BELUM BISA di GUNAKAN','data'=>''];
                    }
                    catch (\Cartalyst\Sentinel\Checkpoints\ThrottlingException $e)
                    {
                        $return = ['status'=>'warning',  'code'=>'403','message'=>'Akun anda dikunci untuk sementara waktu dikarenakan gagal login beberapa kali. Mohon tunggu beberapa menit untuk mencoba kembali.', 'data'=>''];
                    }
                }else{
                    $return = ['status'=>'error', 'code'=>'404', 'message'=>'Akun Tidak Dapat Ditemukan, Silahkan Cek Kembali NIK/Email Anda', 'data'=>''];
                }
            }else{
                $return = ['status'=>'error', 'code'=> '400', 'message'=>'Password Harus Diisi', 'data'=>''];
            }
        }else{
            $return = ['status'=>'error', 'code'=>'400', 'message'=>'NIK/Email Harus Diisi', 'data'=>''];
        }
        return response()->json($return);
    }

    public function getdatauser(Request $request)
    {
        $users = Users::where('token',$request->token)->first();
        if(($users->level_user == 2) || ($users->level_user == 3)){
            ($users->level_user == 2) ? $stutusBidan = 'Bikor' : $stutusBidan = 'Bidan';
            $bidan = Bidan::where('users_id',$users->level_user)->with('kabupaten')->with('kecamatan')->with('kecTugas')->with('pusKes')->first();
            $user = [
                'nik' => $users->nik,
                'nama'=> $users->nama,
                'foto'=> $users->foto,
                'email' => $users->email,
                'tgl_lahir' => $bidan->tgl_lahir,
                'alamat' => $bidan->alamat,
                'kabupaten' => $bidan->kabupaten->nama_kabupaten,
                'kecamatan' => $bidan->kecamatan->nama_kecamatan,
                'gender' => $bidan->jenis_kelamin,
                'status'=> $stutusBidan,
                'no_telp' => $bidan->no_tlp,
                'lokasi_tugas' => $bidan->kecTugas->nama_kecamatan,
                'pusat_kesehatan' => $bidan->pusKes->nama,
            ];
        // }elseif($users->level_user == 1){
        //     $cc = Users::where('level_user',$users->level_user)->first();
        //     $users = CommandCenter::where('users_id',$cc->id)->with('kabupaten')->with('kecamatan')->first();
        //     $user = [
        //         'nik' => $cc->nik,
        //         'nama'=> $cc->nama,
        //         'foto'=> $cc->foto,
        //         'email' => $cc->email,
        //         'tgl_lahir' => $users->tgl_lahir,
        //         'alamat' => $users->alamat,
        //         'kabupaten' => $users->kabupaten->nama_kabupaten,
        //         'kecamatan' => $users->kecamatan->nama_kecamatan,
        //         'gender' => $users->jenis_kelamin,
        //         'status'=> '',
        //         'no_telp' => $users->no_tlp,
        //         'lokasi_tugas' => '',
        //         'pusat_kesehatan' => '',
        //     ];
            $return = ['status'=>'success', 'code'=>'200', 'message'=>'', 'data'=>$user];
        }else{
            $return = ['status'=>'error', 'code'=>'404', 'message'=>'Akun Tidak Ditemukan / Tidak Memiliki Akses !!'];
        }

        return response()->json($return);
    }

    public function getProvinsi(Request $request)
    {
        $provinsi = Provinsi::all();
        $return = ['status'=>'success','code'=>'200','message'=>'','data'=>$provinsi];

        return response()->json($return);
    }

    public function getKabupaten(Request $request)
    {
        $kabupaten = Kabupaten::where('provinsi_id',$request->id)->get();
        $return = ['status'=>'success','code'=>'200','message'=>'','data'=>$kabupaten];

        return response()->json($return);
    }

    public function getKecamatan(Request $request)
    {
        $kecamatan = Kecamatan::where('kabupaten_id',$request->id)->get();
        $return = ['status'=>'success','code'=>'200','message'=>'','data'=>$kecamatan];

        return response()->json($return);
    }

    public function getDesa(Request $request)
    {
        $desa = Desa::where('kecamatan_id',$request->id)->get();
        $return = ['status'=>'success','code'=>'200','message'=>'','data'=>$desa];

        return response()->json($return);
    }
}
