<?php

namespace App\Http\Controllers;

use App\Profil;
use App\User;
use Illuminate\Http\Request;
use RealRashid\SweetAlert\Facades\Alert;
use Illuminate\Support\Facades\Storage;

class ProfilController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $id = auth()->user()->id;
        $user = User::findOrFail($id);
        $pageConfigs = ['pageHeader' => true];
        $breadcrumbs = [
            ["link" => "/", "name" => "Home"],["name" => "Profil"]
        ];
        $profil = Profil::findOrFail(auth()->user()->id);

        if ($profil->provinsi != NULL) {
            $provinces = \Indonesia::findProvince($profil->provinsi)->get();
            $cities = \Indonesia::findProvince($profil->provinsi, ['cities'])->cities;
            $districts = \Indonesia::findCity($profil->kabupaten_kota, ['districts'])->districts;
            $villages = \Indonesia::findDistrict($profil->kecamatan, ['villages'])->villages;

            return view('profil.index',[
                'pageConfigs'=>$pageConfigs,
                'breadcrumbs'=>$breadcrumbs,
                'provinces' => $provinces,
                'cities' => $cities,
                'districts' => $districts,
                'villages' => $villages,
                'user' => $user
            ]);

        } else {
            $provinces = \Indonesia::allProvinces();

            return view('profil.index',[
                'pageConfigs'=>$pageConfigs,
                'breadcrumbs'=>$breadcrumbs,
                'provinces' => $provinces,
                'user' => $user
            ]);
        }

        //dd($cities);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Profil  $profil
     * @return \Illuminate\Http\Response
     */
    public function show(Profil $profil)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Profil  $profil
     * @return \Illuminate\Http\Response
     */
    public function edit(Profil $profil)
    {
        //
        $profil = Profil::findOrFail(auth()->user()->id);
        $breadcrumbs = [
            ['link'=>"/home",'name'=>"Dashboard"], ['link'=>"/user",'name'=>"Pengguna"], ['name'=>"Edit Pengguna"]
        ];
        $pageConfigs = ['pageHeader' => true];

        return view('profil.edit',[
            'pageConfigs'=>$pageConfigs,
            'breadcrumbs'=>$breadcrumbs,
            'profil' => $profil
        ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Profil  $profil
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Profil $profil)
    {
        //
        $request->validate([
            'no_kk' => 'required',
            'jenis_kelamin' => 'required',
            'tempat_lahir' => 'required',
            'jenis_kelamin' => 'required',
            'agama' => 'required',
            'alamat' => 'required',
            'rt' => 'required',
            'rw' => 'required',
            'desa_kelurahan' => 'required',
            'kecamatan' => 'required',
            'kabupaten_kota' => 'required',
            'provinsi' => 'required',
            'kode_pos' => 'required',
            'kewarganegaraan' => 'required',
            'telepon' => 'required',
            'pekerjaan' => 'required',
            'status_perkawinan' => 'required',
            'golongan_darah' => 'required',
            'foto' => 'mimes:jpg,png|max:2048',
        ], [
            'foto.mimes' => 'Dokumen harus berupa file dengan jenis: jpg, png.',
            'foto.max' => 'Dokumen tidak boleh lebih besar dari 2Mb.',
        ]);

        $id = auth()->user()->id;
        $user = User::findOrFail($id);
        $user->name = $request->get('name');
        $user->save();

        $user->profil->no_kk = $request->get('no_kk');
        $user->profil->jenis_kelamin = $request->get('jenis_kelamin');
        $user->profil->tempat_lahir = $request->get('tempat_lahir');
        $user->profil->tanggal_lahir = $request->get('tanggal_lahir');
        $user->profil->agama = $request->get('agama');
        $user->profil->alamat = $request->get('alamat');
        $user->profil->rt = $request->get('rt');
        $user->profil->rw = $request->get('rw');
        $user->profil->desa_kelurahan = $request->get('desa_kelurahan');
        $user->profil->kecamatan = $request->get('kecamatan');
        $user->profil->kabupaten_kota = $request->get('kabupaten_kota');
        $user->profil->provinsi = $request->get('provinsi');
        $user->profil->kode_pos = $request->get('kode_pos');
        $user->profil->kewarganegaraan = $request->get('kewarganegaraan');
        $user->profil->telepon = $request->get('telepon');
        $user->profil->pekerjaan = $request->get('pekerjaan');
        $user->profil->status_perkawinan = $request->get('status_perkawinan');
        $user->profil->golongan_darah = $request->get('golongan_darah');
        $user->profil->status = 1;


        if ($request->file('foto'))
        {
            if(file_exists(storage_path('app/public/' . $user->profil->foto))){
                Storage::delete('public/' . $user->profil->foto); 
            }
            $user->profil->foto = $request->file('foto')->store('Profil', 'public');  
        }
        $user->profil->save();

        Alert::success('Berhasil', 'Data Berhasil diperbaharui!');

        return redirect()->route('profil.index');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Profil  $profil
     * @return \Illuminate\Http\Response
     */
    public function destroy(Profil $profil)
    {
        //
    }
}
