@php
    function decimalToDms($decimal, $type = 'lat')
    {
        if ($decimal === null || $decimal === '') return '';

        $direction = $decimal >= 0
            ? ($type === 'lat' ? 'N' : 'E')
            : ($type === 'lat' ? 'S' : 'W');

        $decimal = abs($decimal);

        $degree = floor($decimal);
        $minuteFloat = ($decimal - $degree) * 60;
        $minute = floor($minuteFloat);
        $second = round(($minuteFloat - $minute) * 60, 2);

        return "{$degree}°{$minute}'{$second}\"{$direction}";
    }
@endphp

@extends('admin.template')

@section('content')
<div class="content-wrapper">
    <div class="row">
        <div class="col-12 grid-margin stretch-card">
            <div class="card">
                <div class="card-body">
                    <h4 class="card-title">Edit Data TPS</h4>
                    <p class="card-description">
                        Form edit data Tempat Pembuangan Sampah
                    </p>

                    <form action="{{ route('admin.tps.update', $tps->id_tps) }}"
                          method="POST"
                          enctype="multipart/form-data"
                          class="forms-sample">
                        @csrf
                        @method('PUT')

                        {{-- Nama TPS --}}
                        <div class="form-group">
                            <label>Nama TPS</label>
                            <input type="text" name="nama_tps" class="form-control"
                                   value="{{ old('nama_tps', $tps->nama_tps) }}" required>
                        </div>

                        {{-- Alamat --}}
                        <div class="form-group">
                            <label>Alamat</label>
                            <input type="text" name="alamat_tps" class="form-control"
                                   value="{{ old('alamat_tps', $tps->alamat_tps) }}" required>
                        </div>

                        {{-- Kategori TPS --}}
                        <div class="form-group">
                            <label>Kategori TPS</label>
                            <select name="kategori_tps_id" class="form-control" required>
                                <option value="">-- Pilih Kategori --</option>
                                @foreach ($kategori as $item)
                                    <option value="{{ $item->id_kategori_tps }}"
                                        {{ old('kategori_tps_id', $tps->kategori_tps_id) == $item->id_kategori_tps ? 'selected' : '' }}>
                                        {{ $item->nama_kategori }}
                                    </option>
                                @endforeach
                            </select>
                        </div>

                        {{-- Tahun Pembuatan --}}
                        <div class="form-group">
                            <label>Tahun Pembuatan</label>
                            <input type="number" name="tahun_pembuatan" class="form-control"
                                   value="{{ old('tahun_pembuatan', $tps->tahun_pembuatan) }}" required>
                        </div>

                        {{-- Kapasitas TPS --}}
                        <div class="form-group">
                            <label>Kapasitas TPS</label>
                            <input type="text" name="kapasitas_tps" class="form-control"
                                   value="{{ old('kapasitas_tps', $tps->kapasitas_tps) }}" required>
                        </div>

                        {{-- Status TPS --}}
                        <div class="form-group">
                            <label>Status TPS</label>
                            <select name="status_tps" class="form-control" required>
                                <option value="Aktif" {{ old('status_tps', $tps->status_tps) == 'Aktif' ? 'selected' : '' }}>
                                    Aktif
                                </option>
                                <option value="Tidak Aktif" {{ old('status_tps', $tps->status_tps) == 'Tidak Aktif' ? 'selected' : '' }}>
                                    Tidak Aktif
                                </option>
                                <option value="Pembangunan" {{ old('status_tps', $tps->status_tps) == 'Pembangunan' ? 'selected' : '' }}>
                                    Pembangunan
                                </option>
                            </select>
                        </div>

                        {{-- Latitude (DMS) --}}
                        <div class="form-group">
                            <label>Latitude (DMS)</label>
                            <input type="text" name="latitude" class="form-control"
                                   placeholder="Contoh: 7°35'17.25&quot;S"
                                   value="{{ old('latitude', decimalToDms($tps->latitude, 'lat')) }}" required>
                        </div>

                        {{-- Longitude (DMS) --}}
                        <div class="form-group">
                            <label>Longitude (DMS)</label>
                            <input type="text" name="longitude" class="form-control"
                                   placeholder="Contoh: 111°55'0.97&quot;E"
                                   value="{{ old('longitude', decimalToDms($tps->longitude, 'lng')) }}" required>
                        </div>

                        {{-- Foto TPS --}}
                        <div class="form-group">
                            <label>Foto TPS</label>
                            <input type="file" name="foto_tps" class="file-upload-default">

                            <div class="input-group col-xs-12">
                                <input type="text" class="form-control file-upload-info" disabled
                                       placeholder="Upload Foto TPS">
                                <span class="input-group-append">
                                    <button class="file-upload-browse btn btn-primary" type="button">
                                        Upload
                                    </button>
                                </span>
                            </div>

                            {{-- Foto Lama --}}
                            @if ($tps->foto_tps)
                                <div class="mt-2">
                                    <img src="{{ asset('storage/' . $tps->foto_tps) }}"
                                         width="250"
                                         class="img-thumbnail">
                                </div>
                            @endif
                        </div>

                        {{-- Button --}}
                        <button type="submit" class="btn btn-primary mr-2">
                            Simpan
                        </button>
                        <a href="{{ route('admin.tps.index') }}" class="btn btn-light">
                            Batal
                        </a>

                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
