<?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\IbuHamil;
use App\Http\Models\PeriodeKehamilan;
use File,Sentinel,Validator;

class APIWEBPeriodeController extends Controller
{
    public function addPeriode_Old(Request $request)
    {
        $infoBumil = IbuHamil::where('nik', $request->secret['nikBumil'])->first();

        $periode = new PeriodeKehamilan;
        $periode->bumil_id = $infoBumil->id_bumil;

        if ($request->data['alamat_domisili'] != null || $request->data['alamat_domisili'] != '') {
            $periode->alamat_domisili = $request->data['alamat_domisili'];
        }else{
            $periode->alamat_domisili = $infoBumil->alamat_ktp;
        }
        if ($request->data['kabupaten_domisili_id'] != null || $request->data['kabupaten_domisili_id'] != '') {
            $periode->kabupaten_domisili_id = $request->data['kabupaten_domisili_id'];
        }else{
            $periode->kabupaten_domisili_id = $infoBumil->kabupaten_ktp_id;
        }
        if ($request->data['kecamatan_domisili_id'] != null || $request->data['kecamatan_domisili_id'] != '') {
            $periode->kecamatan_domisili_id = $request->data['kecamatan_domisili_id'];
        }else{
            $periode->kecamatan_domisili_id = $infoBumil->kecamatan_ktp_id;
        }
        if ($request->data['desa_domisili_id'] != null || $request->data['desa_domisili_id']) {
            $periode->desa_domisili_id = $request->data['desa_domisili_id'];
        }else{
            $periode->desa_domisili_id = $infoBumil->desa_ktp_id;
        }
        $periode->jenis_jkn = $request->data['jenis_jkn'];
        $periode->no_jkn = $request->data['no_jkn'];
        $periode->haid_terakhir = date('Y-m-d', strtotime($request->data['haid_terakhir']));
        $periode->taksiran_lahir = date('Y-m-d', strtotime($request->data['taksiran_lahir']));
        $periode->lingkar_lengan = $request->data['lingkar_lengan'];
        if ($request->data['lingkar_lengan'] < 23.5) {
            $periode->kek = '1';
        }else{
            $periode->kek = '0';
        }
        $periode->tinggi_badan = $request->data['tinggi_badan'];
        $periode->kontrasepsi = $request->data['kontrasepsi'];
        if ($request->data['riwayatPenyakit'] == 'Ada') {
            if (!empty($request->data['riwayat_penyakit']) || $request->data['riwayat_penyakit'] != null || $request->data['riwayat_penyakit'] != '-') {
                $periode->riwayat_penyakit = $request->data['riwayat_penyakit'];
            }
        }
        if ($request->data['riwayatAlergi'] == 'Ada') {
            if (!empty($request->data['riwayat_alergi']) || $request->data['riwayat_alergi'] != null || $request->data['riwayat_alergi'] != '-') {
                $periode->riwayat_alergi = $request->data['riwayat_alergi'];
            }
        }
        $periode->kehamilan = $request->data['kehamilan'];
        $periode->jml_persalinan = $request->data['jml_persalinan'];
        $periode->jml_keguguran_g = $request->data['jml_keguguran_g'];
        $periode->jml_keguguran_p = $request->data['jml_keguguran_p'];
        $periode->jml_keguguran_a = $request->data['jml_keguguran_a'];
        $periode->jarak_kehamilan_thn = $request->data['jarak_kehamilan_thn'];
        $periode->jarak_kehamilan_bln = $request->data['jarak_kehamilan_bln'];
        $periode->penolong = $request->data['penolong'];
        $periode->cara_persalinan = $request->data['cara_persalinan'];
        if ($request->data['cara_persalinan'] == 'Tindakan') {
            if (!empty($request->data['tindakanPenolong']) || $request->data['tindakanPenolong'] != null) {
                $periode->tindakanPenolong = $request->data['tindakanPenolong'];
            }
        }
        $periode->status_kehamilan = 'Hamil';
        $periode->status_risti = 'Rendah';
        $periode->save();

        if ($periode) {
            return ['status'=>'success','code'=>'200','message'=>'Data Periode Berhasil Dibuat !!','data'=>$periode ]; 
        }else{
            return ['status'=>'success','code'=>'200','message'=>'Data Periode Gagal Dibuat !!' ]; 
        }
    }
    
    public function addPeriode(Request $request)
    {
        // $data = [
        //     'token' => $request->secret['token'],
        //     'kelahiran' => $request->secret['kelahiran'],
        //     'nikBumil' => $request->secret['nikBumil'],
        // ];
        // return ['status'=>'success','code'=>'200','message'=>'Data Periode Berhasil Dibuat !!','data'=>$data ];
        if ($request->secret['kelahiran'] != null) {
            $bumil = IbuHamil::where('nik',$request->secret['nikBumil'])->first();
            $periode = new PeriodeKehamilan;
            $periode->bumil_id = $bumil->id_bumil;
            $periode->alamat_domisili = $bumil->alamat_ktp;
            $periode->kabupaten_domisili_id = $bumil->kabupaten_ktp_id;
            $periode->kecamatan_domisili_id = $bumil->kecamatan_ktp_id;
            $periode->desa_domisili_id = $bumil->desa_ktp_id;
            $periode->kehamilan = $request->secret['kelahiran'];
            $periode->status_kehamilan = 'Melahirkan';
            $periode->save();
            
            if ($periode) {
                return ['status'=>'success','code'=>'200','message'=>'Data Periode Berhasil Dibuat !!','data'=>$periode ];
            }else{
                return ['status'=>'failed','code'=>'500','message'=>'Data Periode Gagal Dibuat !!' ]; 
            }
        }else{
            return ['status' => 'required','code'=>'400','messages' => 'Angka Kelahiran Harus Di isi !!'];
        }
    }

    public function addPeriodeFromAndro(Request $request)
    {
        $bumils = IbuHamil::where('nik',$request->secret['nikBumil'])->first();
        $periode = new PeriodeKehamilan;
        $periode->bumil_id = $bumils->id_bumil;
        $periode->alamat_domisili = ($request->data['alamat_domisili'] == 0) ? $bumils->alamat_ktp : $request->data['alamat_domisili'];
        $periode->kabupaten_domisili_id = ($request->data['kabupaten_domisili_id'] == 0) ? $bumils->kabupaten_ktp_id : $request->data['kabupaten_domisili_id'];
        $periode->kecamatan_domisili_id = ($request->data['kecamatan_domisili_id'] == 0) ? $bumils->kecamatan_ktp_id : $request->data['kecamatan_domisili_id'];
        $periode->desa_domisili_id = ($request->data['desa_domisili_id'] == 0) ? $bumils->desa_ktp_id : $request->data['desa_domisili_id'];
        $periode->jenis_jkn = $request->data['jenis_jkn'];
        $periode->no_jkn = $request->data['no_jkn'];
        $periode->haid_terakhir = date('Y-m-d', strtotime($request->data['haid_terakhir']));
        $periode->taksiran_lahir = date('Y-m-d', strtotime($request->data['taksiran_lahir']));
        $periode->lingkar_lengan = $request->data['lingkar_lengan'];
        $periode->kek = ($request->data['lingkar_lengan'] < 23.5) ? '1' : '0';
        $periode->tinggi_badan = $request->data['tinggi_badan'];
        $periode->kontrasepsi = $request->data['kontrasepsi'];
        $periode->riwayat_penyakit = $request->data['riwayat_penyakit'];
        $periode->riwayat_alergi = $request->data['riwayat_alergi'];
        $periode->kehamilan = $request->data['kehamilan'];
        $periode->jml_persalinan = $request->data['jml_persalinan'];
        $periode->jml_keguguran_g = $request->data['jml_keguguran_g'];
        $periode->jml_keguguran_p = $request->data['jml_keguguran_p'];
        $periode->jml_keguguran_a = $request->data['jml_keguguran_a'];
        $periode->jarak_kehamilan_thn = $request->data['jarak_kehamilan_thn'];
        $periode->jarak_kehamilan_bln = $request->data['jarak_kehamilan_bln'];
        $periode->penolong = $request->data['penolong'];
        $periode->cara_persalinan = $request->data['cara_persalinan'];
        if ($request->data['cara_persalinan'] == 'Tindakan') {
            $periode->tindakanPenolong = $request->data['tindakanPenolong'];
        }
        $periode->status_kehamilan = 'Hamil';
        $periode->status_risti = 'Rendah';
        $periode->save();

        if ($periode) {
            return ['status'=>'success','code'=>'200','message'=>'Data Periode Berhasil Dibuat !!','data'=>$periode ]; 
        }else{
            return ['status'=>'success','code'=>'200','message'=>'Data Periode Gagal Dibuat !!' ]; 
        }
    }

    public function editPeriodKunjung(Request $request)
    {
        $updatePeriode = PeriodeKehamilan::find($request->secret['periode_id']);
        $updatePeriode->status_kehamilan = $request->secret['status_kehamilan'];
        $updatePeriode->status_risti = $request->secret['status_risti'];
        $updatePeriode->keterangan_kehamilan = $request->secret['keterangan_kehamilan'];
        $updatePeriode->save();

        if ($updatePeriode) {
            return ['status'=>'success','code'=>'200','message'=>'Data Periode Berhasil Diperbaharui !!','data'=>$updatePeriode ]; 
        }else{
            return ['status'=>'success','code'=>'200','message'=>'Data Periode Gagal Diperbaharui !!' ]; 
        }
    }
}
