<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Models\KesehatanPasangan;
use App\Http\Models\IbuHamil;
use App\Http\Models\IdentitasPasangan;
use App\Http\Models\PusatKesehatan;
use App\Http\Models\Users;
use App\Http\Libraries\CompresImage;
use File, Sentinel, Redirect, Validator;

class KesehatanPasanganController extends Controller
{
    public function index(Request $request)
    {
        $data['level'] = Sentinel::getUser();
        $data['mn_active'] = "keluarga";
        $data['title'] = "Data Kesehatan";
        $data['smallTitle'] = "Menampilkan seluruh data Data Kesehatan Keluarga";
        return view('keluargaSehat.kesehatan.main', $data);
    }

    public function datagrid(Request $request)
    {
        $data = KesehatanPasangan::getJson($request);
        return response()->json($data);
    }

    public function formAdd(Request $request)
    {
        $content = view('keluargaSehat.kesehatan.formAdd')->render();
        return ['status' => 'success', 'content' => $content];
    }

    public function cekPernikahanIstri(Request $request)
    {
        $data['bumil'] = IbuHamil::where('nik', $request->nik)->first();
        if (empty($data['bumil'])) {
            $data['pernikahan'] = [];
        }else{
            $data['pernikahan'] = IdentitasPasangan::where('bumil_id',$data['bumil']->id_bumil)->get();
        }
        return response()->json($data);
    }

    public function getDetPernikahan(Request $request)
    {
        $data['idenPas'] = IdentitasPasangan::where('id_identitas_pasangan',$request->id)->with('bumil')->first();
        $data['kesehatan'] = KesehatanPasangan::where('identitas_pasangan_id',$data['idenPas']->id_identitas_pasangan)->first();
        return response()->json($data);
    }

    public function add(Request $request)
    {
        // return $request->all();
        $user_id = Sentinel::getUser()->id;
        $usrPuskesmas = PusatKesehatan::where('users_id',$user_id)->first();
        if ($request->id_identitas_pasangan == 0) {
            $bumil = IbuHamil::where('nik', $request->nik)->first();
            if (empty($bumil)) {
                return Redirect::route('dtKesPas')->with('title', 'Maaf!!!')->with('message', 'NIK Istri Tidak Ditemukan !!')->with('type', 'error');
            }
            $idIdenPas = '0';
            $idBumil = $bumil->id_bumil;

        }else{
            $idenPas = IdentitasPasangan::find($request->id_identitas_pasangan);
            if (empty($idenPas)) {
                return Redirect::route('dtKesPas')->with('title', 'Maaf!!!')->with('message', 'No Registrasi Tidak Ditemukan !!')->with('type', 'error');
            }
            $idIdenPas = $idenPas->id_identitas_pasangan;
            $idBumil = $idenPas->bumil->id_bumil;
        }
        $kesPas = new KesehatanPasangan;
        $kesPas->tbc = $request->tbc;
        $kesPas->diabetes = $request->diabetes;
        $kesPas->jantung = $request->jantung;
        $kesPas->hipertensi = $request->hipertensi;
        $kesPas->infeksi_kandungan = $request->infeksi_kandungan;
        $kesPas->penyakit_lain = $request->penyakit_lain;
        if ($request->penyakit_lain == '1') {
            $kesPas->nama_penyakit = $request->nama_penyakit;
        }
        $kesPas->sistole = $request->sistole;
        $kesPas->diastole = $request->diastole;
        $kesPas->kesehatan_umum = $request->kesehatan_umum;

        $kesPas->imunisasi_tt = $request->imunisasi_tt;
        if ($request->imunisasi_tt == '1') {
            $kesPas->ket_imun_tt = $request->ket_imun_tt;
        }
        $kesPas->kontrasepsi = $request->kontrasepsi;
        if ($request->kontrasepsi == '1') {
            $kesPas->jenis_kontrasepsi = $request->jenis_kontrasepsi;
        }
        $kesPas->ingin_anak_batuta = $request->ingin_anak_batuta;
        $kesPas->hb = $request->hb;
        $kesPas->test_kehamilan = $request->test_kehamilan;
        $kesPas->test_lain = $request->test_lain;
        $kesPas->berat_badan = $request->berat_badan;
        $kesPas->tinggi_badan = $request->tinggi_badan;
        $kesPas->lingkar_lengan_atas = $request->lingkar_lengan_atas;
        $kesPas->lingkar_pinggang = $request->lingkar_pinggang;
        $kesPas->lingkar_panggul = $request->lingkar_panggul;
        $kesPas->vitamin_pengantin = $request->vitamin_pengantin;
        $kesPas->buku_saku = $request->buku_saku;
        $kesPas->tgl_periksa = date('Y-m-d', strtotime($request->tgl_periksa));
        $kesPas->identitas_pasangan_id = $idIdenPas;
        $kesPas->bumil_id = $idBumil;
        $kesPas->pusat_kesehatan_id = $usrPuskesmas->id_pusat_kesehatan;
        $kesPas->save();

        // Start API Add User KUA on Server 1
        // $url_server1 = \Session::get('url_server1');
        // $data = [
        //     'token' => 'N2I2MWNkMmQtZjkyNi00ZDI0LWIyNzQtY2RhZDFjYjNjMWI4',
        //     'nikPus' => Sentinel::getUser()->nik,
        //     'noRegis' => $idenPas->no_registrasi,
        // ];

        // $fields = array(
        //     'secret' => $data,
        //     'data' => $request->all(),
        // );
        
        // $fields = json_encode($fields);
        
        // $ch = curl_init();
        // curl_setopt($ch, CURLOPT_URL, $url_server1."/api/web/kesehatan/addKesehatan");
        // curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8'));
        // curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        // curl_setopt($ch, CURLOPT_HEADER, FALSE);
        // curl_setopt($ch, CURLOPT_POST, TRUE);
        // curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
        // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

        // $response = curl_exec($ch);
        // curl_close($ch);
        // // return response()->json(json_decode($response));
        // $resultAPI = json_decode($response,true);
        // $statusAPI = $resultAPI['status'];
        // End API Add User KUA on Server 1

        if ($kesPas) {
            return Redirect::route('dtKesPas')->with('title', 'Berhasil !')->with('message', 'Data Kesehatan Berhasil Dibuat !!')->with('type', 'success');
        } else {
            return Redirect::route('dtKesPas')->with('title', 'Maaf!!!')->with('message', 'Data Kesehatan Gagal Dibuat !!')->with('type', 'error');
        }
    }

    public function detail(Request $request)
    {
        $data['kesPas'] = KesehatanPasangan::find($request->id);
        $data['bumil'] = IbuHamil::find($data['kesPas']->bumil_id);
        $content = view('keluargaSehat.kesehatan.detail', $data)->render();
        return ['status' => 'success', 'content' => $content];
    }

    public function formEdit(Request $request)
    {
        $data['kesPas'] = KesehatanPasangan::find($request->id);
        $data['bumil'] = IbuHamil::find($data['kesPas']->bumil_id);
        $content = view('keluargaSehat.kesehatan.formEdit',$data)->render();
        return ['status' => 'success', 'content' => $content];
    }

    public function edit(Request $request)
    {
        $kesPas = KesehatanPasangan::find($request->id_kesehatan_pasangan);
        $kesPas->tbc = $request->tbc;
        $kesPas->diabetes = $request->diabetes;
        $kesPas->jantung = $request->jantung;
        $kesPas->hipertensi = $request->hipertensi;
        $kesPas->infeksi_kandungan = $request->infeksi_kandungan;
        $kesPas->penyakit_lain = $request->penyakit_lain;
        if ($request->penyakit_lain == '1') {
            $kesPas->nama_penyakit = $request->nama_penyakit;
        }else{
            $kesPas->nama_penyakit = "";
        }
        $kesPas->sistole = $request->sistole;
        $kesPas->diastole = $request->diastole;
        $kesPas->kesehatan_umum = $request->kesehatan_umum;

        $kesPas->imunisasi_tt = $request->imunisasi_tt;
        if ($request->imunisasi_tt == '1') {
            $kesPas->ket_imun_tt = $request->ket_imun_tt;
        }else{
            $kesPas->ket_imun_tt = "";
        }
        $kesPas->kontrasepsi = $request->kontrasepsi;
        if ($request->kontrasepsi == '1') {
            $kesPas->jenis_kontrasepsi = $request->jenis_kontrasepsi;
        }else{
            $kesPas->jenis_kontrasepsi = "";
        }
        $kesPas->ingin_anak_batuta = $request->ingin_anak_batuta;
        $kesPas->hb = $request->hb;
        $kesPas->test_kehamilan = $request->test_kehamilan;
        $kesPas->test_lain = $request->test_lain;
        $kesPas->berat_badan = $request->berat_badan;
        $kesPas->tinggi_badan = $request->tinggi_badan;
        $kesPas->lingkar_lengan_atas = $request->lingkar_lengan_atas;
        $kesPas->lingkar_pinggang = $request->lingkar_pinggang;
        $kesPas->lingkar_panggul = $request->lingkar_panggul;
        $kesPas->vitamin_pengantin = $request->vitamin_pengantin;
        $kesPas->buku_saku = $request->buku_saku;
        $kesPas->tgl_periksa = date('Y-m-d', strtotime($request->tgl_periksa));
        $kesPas->save();

        // Start API Add User KUA on Server 1
        // $url_server1 = \Session::get('url_server1');
        // $data = [
        //     'token' => 'N2I2MWNkMmQtZjkyNi00ZDI0LWIyNzQtY2RhZDFjYjNjMWI4',
        // ];

        // $fields = array(
        //     'secret' => $data,
        //     'data' => $request->all(),
        // );
        
        // $fields = json_encode($fields);
        
        // $ch = curl_init();
        // curl_setopt($ch, CURLOPT_URL, $url_server1."/api/web/kesehatan/editKesehatan");
        // curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8'));
        // curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        // curl_setopt($ch, CURLOPT_HEADER, FALSE);
        // curl_setopt($ch, CURLOPT_POST, TRUE);
        // curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
        // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

        // $response = curl_exec($ch);
        // curl_close($ch);
        
        // $resultAPI = json_decode($response,true);
        // $statusAPI = $resultAPI['status'];
        // End API Add User KUA on Server 1

        if ($kesPas) {
            return Redirect::route('dtKesPas')->with('title', 'Berhasil !')->with('message', 'Data Kesehatan Berhasil Diperbaharui !!')->with('type', 'success');
        } else {
            return Redirect::route('dtKesPas')->with('title', 'Maaf!!!')->with('message', 'Data Kesehatan Gagal Diperbaharui !!')->with('type', 'error');
        }
    }
}