<?php

namespace app\Http\Controllers\Api\Web;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Models\Users;
use App\Http\Models\Bidan;
use File,Sentinel,Validator;

class APIWEBBidanController extends Controller
{
    public function addBidan(Request $request)
    {
        $user = Sentinel::registerAndActivate([
            'email' => $request->data['email'],
            'password' => $request->data['nik'],
        ]);
        $user->nik = $request->data['nik'];
        $user->token = $request->secret['tokenUser'];
        $user->nama = $request->data['nama'];
        $user->level_user = $request->secret['level_user'];
        $user->active = '1';
        $user->foto = $request->secret['namaGambar'];
        $user->save();

        $bidan = new Bidan;
        $bidan->nama_bidan = $request->data['nama'];
        $bidan->tgl_lahir = date('Y-m-d', strtotime($request->data['tgl_lahir']));
        $bidan->alamat = $request->data['alamat'];
        $bidan->kabupaten_id = $request->data['kabupaten_id'];
        $bidan->kecamatan_id = $request->data['kecamatan_id'];
        $bidan->jenis_kelamin = $request->data['jenis_kelamin'];
        $bidan->no_tlp = $request->data['no_tlp'];
        $bidan->pusat_kesehatan_id = $request->data['pusat_kesehatan_id'];
        $bidan->users_id = $user->id;
        $bidan->save();

        if ($bidan) {
            return ['status'=>'success','code'=>'200','message'=>'Data Bidan Berhasil Ditambah !!','data'=>$bidan ]; 
        }else{
            return ['status'=>'success','code'=>'200','message'=>'Data Bidan Gagal Ditambah !!' ]; 
        }
    }
    
    public function nonActiveBidan(Request $request)
    {
        $user = Users::where('nik', $request->secret['nikUser'])->first();
        $user->active = '0';
        $user->save();

        if ($user) {
            return ['status'=>'success','code'=>'200','message'=>'Data Bidan Berhasil di Non Active kan !!','data'=>$user ]; 
        }else{
            return ['status'=>'error','code'=>'500','message'=>'Data Bidan Gagal di Non Active kan !!' ]; 
        }
    }
    
    public function activeBidan(Request $request)
    {
        $user = Users::where('nik', $request->secret['nikUser'])->first();
        $user->active = '1';
        $user->save();

        if ($user) {
            return ['status'=>'success','code'=>'200','message'=>'Data Bidan Berhasil di Non Active kan !!','data'=>$user ]; 
        }else{
            return ['status'=>'error','code'=>'500','message'=>'Data Bidan Gagal di Non Active kan !!' ]; 
        }
    }
    
    public function resetBidan(Request $request)
    {
        $infoUser = Users::where('nik', $request->secret['nikUser'])->first();

        $user = Sentinel::findById($infoUser->id);
        $nik = $user->nik;
        $credentials = [
            'password' => $nik,
        ];
        $user = Sentinel::update($user, $credentials);

        if ($user) {
            return ['status'=>'success','code'=>'200','message'=>'Password Bidan Berhasil di Reset !!','data'=>$user ]; 
        }else{
            return ['status'=>'error','code'=>'500','message'=>'Password Bidan Gagal di Reset !!' ]; 
        }
    }

    public function gantiPassword(Request $request)
    {
        $infoUser = Users::where('nik', $request->secret['nikUser'])->first();
        $user = Sentinel::findById($infoUser->id);
        Sentinel::update($user, array('password' => $request->secret['password']));

        if ($user) {
            return ['status'=>'success','code'=>'200','message'=>'Password Bidan Berhasil di Diperbaharui !!','data'=>$user ]; 
        }else{
            return ['status'=>'error','code'=>'500','message'=>'Password Bidan Gagal di Diperbaharui !!' ]; 
        }
    }
    
    public function editBidan(Request $request)
    {
        $bidan = Bidan::find($request->data['id_bidan']);
        $bidan->nama_bidan = $request->data['nama'];
        $bidan->tgl_lahir = date('Y-m-d', strtotime($request->data['tgl_lahir']));
        $bidan->alamat = $request->data['alamat'];
        $bidan->kabupaten_id = $request->data['kabupaten_id'];
        $bidan->kecamatan_id = $request->data['kecamatan_id'];
        $bidan->jenis_kelamin = $request->data['jenis_kelamin'];
        $bidan->no_tlp = $request->data['no_tlp'];
        if (empty($request->data['asal'])) {
            $bidan->pusat_kesehatan_id = $request->data['pusat_kesehatan_id'];
        }
        $bidan->save();

        $user = Users::find($bidan->users_id);
        $user->nama = $request->data['nama'];
        $user->level_user = $request->secret['level_user'];
        $user->foto = $request->secret['namaGambar'];
        $user->save();

        if ($user) {
            return ['status'=>'success','code'=>'200','message'=>'Data Bidan Berhasil di Diperbaharui !!','data'=>$user ]; 
        }else{
            return ['status'=>'error','code'=>'500','message'=>'Data Bidan Gagal di Diperbaharui !!' ]; 
        }
    }
}
