<?php

    namespace Modules\Lpj\Http\Controllers;

    use App\Http\Controllers\Controller;
    use Exception;
    use Modules\Location\Models\City;
    use Modules\Location\Models\District;
    use Modules\Location\Models\Province;
    use Modules\Location\Models\Village;
    use Modules\Lpj\Http\Requests\DokumenJaminanRequest;
    use Modules\Lpj\Http\Requests\PemilikJaminanRequest;
    use Modules\Lpj\Models\Debiture;
    use Modules\Lpj\Models\DokumenJaminan;
    use Modules\Lpj\Models\HubunganPemilikJaminan;
    use Modules\Lpj\Models\PemilikJaminan;

    class PemilikJaminanController extends Controller
    {
        public $user;


        public function index($id)
        {
            $debitur        = Debiture::find($id);
            $pemilikJaminan = PemilikJaminan::where('debiture_id', $id)->get();

            return view(
                'lpj::debitur.edit',
                compact('debitur', 'pemilikJaminan'),
            );
        }

        public function store(PemilikJaminanRequest $request, $id)
        {
            $validate = $request->validated();
            if ($validate) {
                try {
                    PemilikJaminan::create($validate);

                    if($request->get('from') == 'create-document'){
                        return redirect()->route('debitur.document.create', $id)->with('success', 'Pemilik Jaminan berhasil ditambahkan');
                    }
                    return redirect()->route('debitur.pemilik.index', $id)->with('success', 'Pemilik Jaminan berhasil ditambahkan');
                } catch (Exception $e) {
                    return redirect()->route('debitur.pemilik.index', $id)->with('error', $e->getMessage());
                }
            }
        }

        public function create($id)
        {
            $debitur         = Debiture::find($id);
            $provinces       = Province::all();
            $hubunganPemilik = HubunganPemilikJaminan::all();

            return view(
                'lpj::pemilik_jaminan.form',
                compact('debitur', 'provinces', 'hubunganPemilik'),
            );
        }

        public function update(PemilikJaminanRequest $request, $id, $pemilik)
        {
            $validate = $request->validated();

            if ($validate) {
                try {
                    $pemilik = PemilikJaminan::find($pemilik);
                    $pemilik->update($validate);

                    if($request->get('from') == 'update-document'){
                        return redirect()->route('debitur.document.edit', [$id, $request->document])->with('success', 'Pemilik Jaminan berhasil diubah');
                    }

                    return redirect()->route('debitur.pemilik.index', $id)->with('success', 'Pemilik Jaminan berhasil diubah');
                } catch (Exception $e) {
                    return redirect()->route('debitur.pemilik.index', $id)->with('error',$e->getMessage());
                }
            }
        }

        public function edit($id, $pemilik)
        {
            $pemilik         = PemilikJaminan::find($pemilik);
            $debitur         = Debiture::find($pemilik->debiture_id);
            $provinces       = Province::all();
            $cities          = City::where('province_code', $pemilik->province_code)->get();
            $districts       = District::where('city_code', $pemilik->city_code)->get();
            $villages        = Village::where('district_code', $pemilik->district_code)->get();
            $hubunganPemilik = HubunganPemilikJaminan::all();
            $detailSertifikat = $pemilik->detail_sertifikat;

            return view(
                'lpj::pemilik_jaminan.form',
                compact(
                    'debitur',
                    'provinces',
                    'cities',
                    'districts',
                    'villages',
                    'hubunganPemilik',
                    'pemilik',
                    'detailSertifikat'
                ),
            );
        }

        public function destroy($id, $pemilik_id)
        {
            try {
                $jaminan = PemilikJaminan::find($pemilik_id);
                $jaminan->delete();
                echo json_encode(['success' => true, 'message' => 'Pemilik Jaminan deleted successfully']);
            } catch (Exception $e) {
                echo json_encode(['success' => false, 'message' => 'Failed to delete Pemilik Jaminan']);
            }
        }
    }
