@extends('layouts.app')
@push('title', 'Tambah Data Lahan')
@section('content')
    @push('other-css')
        <link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css"
            integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY=" crossorigin="" />

        <!-- Leaflet Draw CSS -->
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet.draw/1.0.4/leaflet.draw.css" />

        <style>
            #map {
                height: 400px;
                width: 100%;
                z-index: 0;
            }
        </style>
    @endpush
    <div class="page-content">
        <div class="container-fluid">
            <!-- start page title -->
            <div class="row">
                <div class="col-12">
                    <div class="page-title-box d-sm-flex align-items-center justify-content-between">
                        <h4 class="mb-sm-0">Tambah Data Lahan</h4>

                        <div class="page-title-right">
                            <ol class="breadcrumb m-0">
                                <li class="breadcrumb-item"><a href="javascript: void(0);">Master Data</a></li>
                                <li class="breadcrumb-item active">Tambah Data Lahan</li>
                            </ol>
                        </div>

                    </div>
                </div>
            </div>
            <!-- end page title -->

            <div class="row">
                <div class="col-lg-12">
                    <div class="card">
                        <div class="card-header">
                            <h4 class="card-title mb-0">Tambah Data Lahan</h4>
                        </div>
                        <div class="card-body">
                            <form id="add-form" class="needs-validation" method="POST" novalidate
                                action="{{ Auth::user()->role == 'admin' ? route('master_data.lahan.store') : route('lahan.store') }}">
                                @csrf
                                <div class="modal-body">

                                    <div class="mb-3" id="modal-id" style="display: none;">
                                        <label for="id-field" class="form-label">ID</label>
                                        <input type="text" id="id-field" class="form-control" placeholder="ID"
                                            readonly />
                                    </div>

                                    <div class="mb-3">
                                        <label for="owner-field" class="form-label">Pemilik Lahan</label>
                                        <input type="text" id="owner-field" class="form-control" name="owner"
                                            value="{{ old('owner') }}" placeholder="Masukan Nama Pemilik Lahan" required />
                                        <div class="invalid-feedback">
                                            Masukan Nama Pemilik Lahan
                                        </div>
                                    </div>

                                    {{-- <div class="mb-3">
                                        <label for="landname-field" class="form-label">Nama Lahan</label>
                                        <input type="text" id="landname-field" class="form-control" name="land_name"
                                            value="{{ old('land_name') }}" placeholder="Masukan Nama Lahan" required />
                                        <div class="invalid-feedback">
                                            Masukan Nama Lahan
                                        </div>
                                    </div> --}}

                                    <div class="mb-3">
                                        <label for="province-field" class="form-label">Provinsi</label>
                                        <select name="province_id" id="province-field" class="form-control" required
                                            onchange="getRegencies(this.value)">
                                            <option value="" selected disabled>Pilih Provinsi</option>
                                            @foreach ($provinces as $province)
                                                <option value="{{ $province->id }}"
                                                    {{ old('province_id') == $province->id ? 'selected' : '' }}>
                                                    {{ $province->name }}</option>
                                            @endforeach
                                        </select>
                                        <div class="invalid-feedback">
                                            Harap Pilih Provinsi
                                        </div>
                                    </div>

                                    <div class="mb-3" id="regency-container" style="display: none">
                                        <label for="regency-field" class="form-label">Kabupaten atau Kota</label>
                                        <select name="regency_id" id="regency-field" class="form-control" required
                                            onchange="getDistricts(this.value)">
                                            <option value="" selected disabled>Pilih Kabupaten atau Kota</option>
                                        </select>
                                        <div class="invalid-feedback">
                                            Harap Pilih Kabupaten atau Kota
                                        </div>
                                    </div>

                                    <div class="mb-3" id="district-container" style="display: none">
                                        <label for="district-field" class="form-label">Kecamatan</label>
                                        <select name="district_id" id="district-field" class="form-control" required
                                            onchange="showmap(this.value)">
                                            <option value="" selected disabled>Pilih Kecamatan</option>
                                        </select>
                                        <div class="invalid-feedback">
                                            Harap Pilih Kecamatan
                                        </div>
                                    </div>

                                    <div id="loading" class="text-center" style="display: none">
                                        <div class="spinner-border text-primary" role="status">
                                            <span class="visually-hidden">Loading...</span>
                                        </div>
                                    </div>

                                    <div class="mb-3" id="address-container" style="display: none">
                                        <label for="address-field" class="form-label">Alamat</label>
                                        <textarea name="address" id="address-field" rows="3" class="form-control"
                                            placeholder="Masukan Alamat, cnth: Jl. Jend. Sudirman No. 12" required></textarea>
                                        <div class="invalid-feedback">
                                            Masukan Alamat
                                        </div>
                                    </div>

                                    <div id="map-container" style="display: none">
                                        <label for="map" class="form-label">Lokasi</label>
                                        <div id="map"></div>

                                        <div class="mt-3">
                                            <input type="hidden" name="lat" id="lat" class="form-control"
                                                readonly>
                                            <input type="hidden" name="lng" id="lng" class="form-control"
                                                readonly>
                                        </div>

                                        <div class="mt-3">
                                            <input type="hidden" name="polygon" id="polygon" class="form-control"
                                                readonly>
                                        </div>
                                    </div>
                                </div>
                                <div class="modal-footer" id="add-footer" style="display: none">
                                    <div class="hstack gap-2 justify-content-end">
                                        <a href="{{ Auth::user()->role == 'admin' ? route('master_data.lahan.index') : route('lahan.index') }}"
                                            class="btn btn-light">Kembali</a>
                                        <button type="submit" class="btn btn-success" id="add-btn">Simpan</button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    @push('other-js')
        <script src="{{ asset('assets/js/pages/customJs/master-data/lahan/create.js') }}"></script>

        <script src="{{ asset('assets/js/pages/form-validation.init.js') }}"></script>

        <!-- Make sure you put this AFTER Leaflet's CSS -->
        <script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"
            integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo=" crossorigin=""></script>

        <script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet.draw/1.0.4/leaflet.draw.js"></script>
    @endpush
@endsection
