<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Models\IbuHamil;
use App\Http\Models\Provinsi;
use App\Http\Models\Kabupaten;
use App\Http\Models\Kecamatan;
use App\Http\Models\Desa;
use App\Http\Libraries\CompresImage;
use File, Sentinel, Redirect, Validator;

class IstriController extends Controller
{
    public function index(Request $request)
    {
        $data['level'] = Sentinel::getUser();
        $data['mn_active'] = "istri";
        $data['title'] = "Data Istri";
        $data['smallTitle'] = "Menampilkan seluruh data Ibu Istri";
        return view('istri.main', $data);
    }
    public function datagrid(Request $request)
    {
        $data = IbuHamil::getJson($request);
        return response()->json($data);
    }
    public function formAdd(Request $request)
    {
        // $data['kabupatens'] = Kabupaten::where('provinsi_id','35')->get();
        $data['provinsis'] = Provinsi::all();
        $content = view('istri.formAdd', $data)->render();
        return ['status' => 'success', 'content' => $content];
    }
    public function cekNIK(Request $request)
    {
        $bumil = IbuHamil::where('nik',$request->nik)->get();
        return response()->json($bumil);
    }
    public function add(Request $request)
    {
        $bumil = new IbuHamil;
        $bumil->nik = $request->nik;
        $bumil->nama_bumil = $request->nama_bumil;
        $bumil->tmp_lahir = $request->tmp_lahir;
        $bumil->tgl_lahir = date('Y-m-d', strtotime($request->tgl_lahir));
        $bumil->agama = $request->agama;
        $bumil->suku = $request->suku;
        $bumil->pendidikan = $request->pendidikan;
        $bumil->pekerjaan = $request->pekerjaan;
        if (!empty($request->nama_suami)) {
            $bumil->nama_suami = $request->nama_suami;
        }
        $bumil->alamat_ktp = $request->alamat_ktp;
        $bumil->provinsi_ktp_id = $request->provinsi_ktp_id;
        $bumil->kabupaten_ktp_id = $request->kabupaten_ktp_id;
        $bumil->kecamatan_ktp_id = $request->kecamatan_ktp_id;
        $bumil->desa_ktp_id = $request->desa_ktp_id;
        $bumil->no_tlp = $request->no_tlp;
        if (!empty($request->foto)) {
            date_default_timezone_set('Asia/Jakarta');
            $ukuranFile = filesize($request->foto);
            if ($ukuranFile <= 500000) {
                $ext_foto = $request->foto->getClientOriginalExtension();
                $filename = "Istri_".date('Ymd-His')."_".$request->nama_bumil.".".$ext_foto;
                $temp_foto = 'dist/img/istri/';
                $proses = $request->foto->move($temp_foto, $filename);
                $bumil->foto = $filename;
            }else{
                $file=$_FILES['foto']['name'];
                if(!empty($file)){
                    $direktori="dist/img/istri/"; //tempat upload foto
                    $name='foto'; //name pada input type file
                    $namaBaru="Istri_".date('Ymd-His')."_".$request->nama_bumil; //name pada input type file
                    $quality=50; //konversi kualitas gambar dalam satuan %
                    $upload = CompresImage::UploadCompress($namaBaru,$name,$direktori,$quality);
                }
                $ext_foto = $request->foto->getClientOriginalExtension();
                $bumil->foto = $namaBaru.".".$ext_foto;
            }
        }
        $bumil->save();

        if ($bumil) {
            return Redirect::route('istri')->with('title', 'Berhasil !')->with('message', 'Data Istri Berhasil Dibuat !!')->with('type', 'success');
        } else {
            return Redirect::route('istri')->with('title', 'Maaf!!!')->with('message', 'Data Istri Gagal Dibuat !!')->with('type', 'error');
        }
    }
    public function detail(Request $request)
    {
        $data['bumil'] = IbuHamil::find($request->id);
        $content = view('istri.detail', $data)->render();
        return ['status' => 'success', 'content' => $content];
    }
    public function formEdit(Request $request)
    {
        // $data['kabupatens'] = Kabupaten::where('provinsi_id','35')->get();
        $data['provinsis'] = Provinsi::all();
        $data['bumil'] = IbuHamil::find($request->id);
        $data['kabupatens'] = Kabupaten::where('provinsi_id',$data['bumil']->provinsi_ktp_id)->get();
        $data['kecamatans'] = Kecamatan::where('kabupaten_id', $data['bumil']->kabupaten_ktp_id)->get();
        $data['desas'] = Desa::where('kecamatan_id', $data['bumil']->kecamatan_ktp_id)->get();
        $content = view('istri.formEdit',$data)->render();
        return ['status' => 'success', 'content' => $content];
    }
    public function edit(Request $request)
    {
        $bumil = IbuHamil::find($request->id_bumil);
        $bumil->nik = $request->nik;
        $bumil->nama_bumil = $request->nama_bumil;
        $bumil->tmp_lahir = $request->tmp_lahir;
        $bumil->tgl_lahir = date('Y-m-d', strtotime($request->tgl_lahir));
        $bumil->agama = $request->agama;
        $bumil->suku = $request->suku;
        $bumil->pendidikan = $request->pendidikan;
        $bumil->pekerjaan = $request->pekerjaan;
        if (!empty($request->nama_suami)) {
            $bumil->nama_suami = $request->nama_suami;
        }
        $bumil->alamat_ktp = $request->alamat_ktp;
        $bumil->provinsi_ktp_id = $request->provinsi_ktp_id;
        $bumil->kabupaten_ktp_id = $request->kabupaten_ktp_id;
        $bumil->kecamatan_ktp_id = $request->kecamatan_ktp_id;
        $bumil->desa_ktp_id = $request->desa_ktp_id;
        $bumil->no_tlp = $request->no_tlp;
        if (!empty($request->foto)) {
            if ($bumil->foto != null) {
                File::delete('dist/img/istri/'.$bumil->foto);
            }
            date_default_timezone_set('Asia/Jakarta');
            $ukuranFile = filesize($request->foto);
            if ($ukuranFile <= 500000) {
                $ext_foto = $request->foto->getClientOriginalExtension();
                $filename = "Istri_".date('Ymd-His')."_".$request->nama_bumil.".".$ext_foto;
                $temp_foto = 'dist/img/istri/';
                $proses = $request->foto->move($temp_foto, $filename);
                $bumil->foto = $filename;
            }else{
                $file=$_FILES['foto']['name'];
                if(!empty($file)){
                    $direktori="dist/img/istri/"; //tempat upload foto
                    $name='foto'; //name pada input type file
                    $namaBaru="Istri_".date('Ymd-His')."_".$request->nama_bumil; //name pada input type file
                    $quality=50; //konversi kualitas gambar dalam satuan %
                    $upload = CompresImage::UploadCompress($namaBaru,$name,$direktori,$quality);
                }
                $ext_foto = $request->foto->getClientOriginalExtension();
                $bumil->foto = $namaBaru.".".$ext_foto;
            }
        }
        $bumil->save();

        if ($bumil) {
            return Redirect::route('istri')->with('title', 'Berhasil !')->with('message', 'Data Istri Berhasil Diperbaharui !!')->with('type', 'success');
        } else {
            return Redirect::route('istri')->with('title', 'Maaf!!!')->with('message', 'Data Istri Gagal Diperbaharui !!')->with('type', 'error');
        }
    }
    public function delete(Request $request)
    {
        $id_bumil = $request->id;
        $bumil = IbuHamil::find($id_bumil);
        if(!empty($bumil)){
            $nama_file = $bumil->foto;
            if ($bumil->foto != null) {
                File::delete('dist/img/istri/'.$nama_file);
            }
            $bumil->delete();
            return ['status' => 'success', 'message' => 'Data berhasil dihapus'];
        }else{
            return ['status'=>'error', 'message'=>'Invalid user.'];
        }
    }
}
