<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Filetemp;
use App\User;
use App\Periksa;
use App\Pasien;
use GuzzleHttp\Client;
use Validator;
use DateTime;
use Carbon\Carbon;
use Session;

class PasienController extends Controller
{
    public function index() {
		if (Session::get('previlage') == ''){
			return redirect('/login');
		} else {
			$data 				= [];
			return view('admin.pasien', $data);
		}
    }
    public function store(Request $request) {
        $validator  =   Validator::make($request->all(), [
            'nama'          =>  'required',
            'jk'            =>  'required',
            'tgl_lahir'     =>  'required',
            'kota'          =>  'required',
            'alamat'        =>  'required',
			'telpon'        =>  'required'
        ]);
        $old_date   = $request->input('tgl_lahir');
        $date 		= date("Y-m-d", strtotime($old_date));
        if($validator->fails()) {
            return response()->json(['status' => 'error', 'message' => 'Please fill input field or fill with right input']);
        } else {
			$norm = $request->input('norm');
			$norm = strtoupper($norm);
			Pasien::create([
				'norm'          =>  $norm,
				'nama'          =>  $request->input('nama'),
				'jk'            =>  $request->input('jk'),
				'tgl_lahir'     =>  $date,
				'telpon'        =>  $request->input('telpon'),
				'kota'          =>  $request->input('kota'),
				'alamat'        =>  $request->input('alamat')
			]);
            Session::flash('message', 'Data berhasil disimpan'); 
            Session::flash('alert-class', 'alert-success');
            return back();
        }
    }
    public function getPasien(Request $request) {
        $id         =   $request->input('id');
        $result     =   DB::table('pasien')->where('id', $id)->first();
		$tgllahir	= 	$result->tgl_lahir;
		$arrtgl		= 	explode('-', $tgllahir);
		$yy			= 	$arrtgl[0];
		$mm			= 	$arrtgl[1];
		$dd			= 	$arrtgl[2];
		$tgllahir	= 	$dd.'-'.$mm.'-'.$yy;
		$arrayfnk[] = 	array(
			'telpon'        => $result->telpon,
			'nama'       	=> $result->nama,
			'tgl_lahir'    	=> $result->tgl_lahir,
			'alamat'    	=> $result->alamat,
			'id'    		=> $result->id,
			'norm'    		=> $result->norm,
			'jk'    		=> $result->jk,
			'kota'    		=> $result->kota,
			'nik'    		=> $result->nik,
			'bpjs'    		=> $result->bpjs,
		);
        echo json_encode($arrayfnk);
    }
	public function getPatienalllist(Request $request) {
		$data =   Pasien::all();
		echo json_encode($data);
    }
	public function getPasienbynorm(Request $request) {
        $norm         	= $request->input('id');
		$getdatalokal	= Pasien::where('norm', $norm)->first();
		if (isset($getdatalokal->nama)){
			$tgl_lahir	= $getdatalokal->tgl_lahir;
			$arrdate 	= explode('-', $tgl_lahir);
			$yy 		= $arrdate[0];
			$mm 		= $arrdate[1];
			$dd 		= $arrdate[2];
			$tgl_lahir	= $dd.'-'.$mm.'-'.$yy;
			return response()->json([
				'id' 		=> $getdatalokal->id, 
				'norm' 		=> $getdatalokal->norm,
				'telpon' 	=> $getdatalokal->telpon, 
				'nama'      => $getdatalokal->nama, 
				'tgl_lahir' => $getdatalokal->tgl_lahir,
				'jk'    	=> $getdatalokal->jk,
				'alamat'    => $getdatalokal->alamat,
				'kota'		=> $getdatalokal->kota,
				'nik'		=> $getdatalokal->nik,
				'bpjs'		=> $getdatalokal->bpjs,
			]);
		} else {
			try {
				$client         = new Client();
                $res            = $client->request('GET', 'https://gomed.rssa.my.id/api/account/pasien', ['query' => ['limit' => 10, 'nomedis' => $norm]]);
                $response_data  = json_decode($res->getBody()->getContents());
                if (isset($response_data->response[0])) {
                    $hasil      = $response_data->response[0];
                    $nama       = $hasil->Nama;
                    $alamat     = $hasil->Alamat;
                    $telpon     = $hasil->Telp;
                    $kota       = $hasil->Kota;
                    $kelamin    = $hasil->JenisKelamin;
                    $tgllahir   = $hasil->tanggal_lahir;
                    return response()->json([
                        'id'        => 'new',
                        'norm'      => $norm,
                        'telpon'    => $telpon,
                        'nama'      => $nama,
                        'tgl_lahir' => date('d-m-Y', strtotime($tgllahir)),
                        'jk'        => $kelamin,
                        'alamat'    => $alamat,
                        'kota'      => $kota,
                    ]);
                } else {
                    return response()->json([
                        'id'            => '',
                        'telpon'        => '000000000',
                        'norm'          => $norm,
                        'nama'          => '',
                        'tgl_lahir'     => date("d-m-Y"),
                        'jk'            => 'L',
                        'alamat'        => 'Malang',
                        'kota'          => 'Malang',
                    ]);
                }
			}catch (Exception $e) {
				return response()->json([
					'id' 		=> '', 
					'telpon' 	=> '000000000',
					'norm' 		=> $norm,
					'nama'      => '', 
					'tgl_lahir' => date("d-m-Y"),
					'jk'    	=> 'L',
					'alamat'    => $e->getMessage(),
					'kota'		=> 'Malang',
					'nik'		=> '',
					'bpjs'		=> '',
				]);
			}
			
		}
    }
    public function update(Request $request) {
        $validator  =   Validator::make($request->all(), [
            'nama'          =>  'required',
            'jk'            =>  'required',
            'tgl_lahir'     =>  'required',
            'kota'          =>  'required',
	    	'telpon'        =>  'required',
            'alamat'        =>  'required'
        ]);

        if($validator->fails()) {
          return response()->json(['status' => 'error', 'message' => 'Please fill input field or fill with right input']);
        } else {
            $id         = $request->input('id_pasien');
            $old_date   = $request->input('tgl_lahir');
            $date       = date("Y-m-d", strtotime($old_date));
            $norm 		= $request->input('norm');
			$norm 		= strtoupper($norm);
			$siapa		= Session('nama');
			$kapan		= date('Y-m-d H:i:s');
			$keterangan = 'Diupdate Oleh '.$siapa.' Pada '.$kapan;
			try {
				Pasien::updateOrCreate(
					[
						'norm'			=> $norm,
					],
					[
						'nama'          =>  $request->input('nama'),
						'jk'            =>  $request->input('jk'),
						'tgl_lahir'     =>  $date,
						'telpon'        =>  $request->input('telpon'),
						'kota'          =>  $request->input('kota'),
						'alamat'        =>  $request->input('alamat'),
						'keterangan'    =>  $keterangan
					]
				);
				Session::flash('message', 'Data berhasil disimpan'); 
				Session::flash('alert-class', 'alert-success');
				return back();
			} catch (\Exception $e) {
				Session::flash('message', $e->getMessage()); 
				Session::flash('alert-class', 'alert-succdangeress');
				return back();
			}
        }
    }
    public function delete(Request $request) {
        $id         =   $request->pasien_id;
        $pasien     =   Pasien::find($id);
        $hapus 		= 	$pasien->delete();
		if ($hapus) {
			return response()->json(['icon' => 'success', 'warna' => '#5ba035', 'status' => 'Success', 'message' => 'Data Deleted..!!!']);
			return back();
		} else {
			return response()->json(['icon' => 'error', 'warna' => '#bf441d', 'status' => 'Error.!!', 'message' => 'System Down, please try again in a few years....']);
			return back();
		}
    }
}
