<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCU3tdTex4BWClD0vauHsj01n2pydE0_O4&v=weekly"
      defer></script>
<script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
<div class="content-wrapper">
    <div class="content-header">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <h1 class="m-0">Pengajuan Pangkalan Baru</h1>
                </div><!-- /.col -->
                <div class="col-sm-6">
                    <ol class="breadcrumb float-sm-right">
                    <li class="breadcrumb-item"><a href="<?=base_url()?>home">Home</a></li>
                    <li class="breadcrumb-item"><a href="<?=base_url()?>agen_pangkalan_pengajuan_baru">Data Pengajuan Pangkalan</a></li>
                    <li class="breadcrumb-item active">Pengajuan Pangkalan Baru</li>
                    </ol>
                </div><!-- /.col -->
            </div><!-- /.row -->
        </div><!-- /.container-fluid -->
    </div>
    <hr/>
    <section class="content">
        <div class="container-fluid">
            <div class="row">
                <div class="col-lg-12">
                    <div class="alert alert-info alert-dismissible">
                        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                        <h5><i class="icon fas fa-info"></i> Info</h5>
                        Silahkan mengisi data awal pengajuan pangkalan pada form dibawah ini. Setelah pengisian data awal selesai, anda akan dibawa menuju form pengisian lengkap data pengajuan untuk pengisian kelengkapan lebih lanjut.
                    </div>
                </div>
            </div>
            <form id="f1" action="#" method="post" enctype="multipart/form-data">
            <div class="row">
                <div class="col-lg-6">
                    <div class="card card-outline card-success">
                        <div class="card-header">
                            <div class="card-title">
                                Data Pokok
                            </div>
                        </div>
                        <div class="card-body">
                            <div class="form-group">
                                <label for="id_tipe_pangkalan">Tipe Pangkalan</label>
                                <select class="select2 form-control form-control-border" id="id_tipe_pangkalan" placeholder="Pilih Tipe Pangkalan" name="id_tipe_pangkalan">
                                </select>
                                <span id="eid_tipe_pangkalan" class="error invalid-feedback"></span>
                            </div>
                            <div class="form-group">
                                <label for="id_registrasi">ID Registrasi</label>
                                <input type="text" class="form-control form-control-border" id="id_registrasi" placeholder="ID Registrasi" name="id_registrasi" value="<?php
                                    echo $user_data->data['sold_id'].date('YmdHis');
                                ?>">
                                <span id="eid_registrasi" class="error invalid-feedback"></span>
                            </div>
                            <div class="form-group">
                                <label for="nama_pangkalan">Nama Pangkalan</label>
                                <input type="text" class="form-control form-control-border" id="nama_pangkalan" placeholder="Nama Pangkalan" name="nama_pangkalan">
                                <span id="enama_pangkalan" class="error invalid-feedback"></span>
                            </div>
                            <div class="form-group">
                                <label for="no_telepon">Nomor Telepon</label>
                                <input type="text" class="form-control form-control-border" id="no_telepon" placeholder="Nomor Telepon" name="no_telepon">
                                <span id="eno_telepon" class="error invalid-feedback"></span>
                            </div>
                            <div class="form-group">
                                <label for="no_sku">Nomor SKU</label>
                                <input type="text" class="form-control form-control-border" id="no_sku" placeholder="Nomor SKU" name="no_sku">
                                <span id="eno_sku" class="error invalid-feedback"></span>
                            </div>
                            <div class="form-group">
                                <label for="kuantiti">Qty Tabung / Bulan</label>
                                <input type="number" class="form-control form-control-border" id="kuantiti" placeholder="Kuantiti" name="kuantiti">
                                <span id="ekuantiti" class="error invalid-feedback"></span>
                            </div>
                            <div class="form-group">
                                <label for="tipe_pembayaran">Tipe Pembayaran</label>
                                <select class="select2 form-control form-control-border" id="tipe_pembayaran" placeholder="Pilih Tipe Pembayaran" name="tipe_pembayaran">
                                    <option value="CASH">CASH</option>
                                    <option value="CASHLESS">CASHLESS</option>
                                </select>
                                <span id="etipe_pembayaran" class="error invalid-feedback"></span>
                            </div>
                            <div class="form-group">
                                <label for="nama_pemilik">Nama Pemilik</label>
                                <input type="text" class="form-control form-control-border" id="nama_pemilik" placeholder="Nama Pemilik" name="nama_pemilik">
                                <span id="enama_pemilik" class="error invalid-feedback"></span>
                            </div>
                            <div class="form-group">
                                <label for="no_ktp_pemilik">Nomor KTP Pemilik</label>
                                <input type="text" class="form-control form-control-border" id="no_ktp_pemilik" placeholder="Nomor KTP Pemilik" name="no_ktp_pemilik">
                                <span id="eno_ktp_pemilik" class="error invalid-feedback"></span>
                            </div>
                            <div class="form-group">
                                <label for="no_hp_pemilik">Nomor HP Pemilik</label>
                                <input type="text" class="form-control form-control-border" id="no_hp_pemilik" name="no_hp_pemilik" data-inputmask='"mask": "(+62) 999-99999999"' data-mask>
                                <span id="eno_hp" class="error invalid-feedback"></span>
                            </div>
                            <div class="form-group">
                                <label for="email">Email</label>
                                <input type="email" class="form-control form-control-border" id="email" name="email" placeholder="Email Pemilik">
                                <span id="eemail" class="error invalid-feedback"></span>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-lg-6">
                    <div class="card card-outline card-primary">
                        <div class="card-header">
                            <div class="card-title">
                                Data Lokasi Pangkalan
                            </div>
                        </div>
                        <div class="card-body">
                            <div class="form-group">
                                <label for="alamat">Alamat</label>
                                <textarea class="form-control form-control-border" id="alamat" placeholder="Alamat" name="alamat"></textarea>
                                <span id="ealamat" class="error invalid-feedback"></span>
                            </div>
                            <div class="form-group">
                                <label for="provinsi">Provinsi</label>
                                <select class="select2 form-control form-control-border" id="provinsi" placeholder="Pilih Provinsi" name="provinsi">
                                </select>
                                <span id="eprovinsi" class="error invalid-feedback"></span>
                            </div>
                            <div class="form-group">
                                <label for="kabupaten">Kabupaten</label>
                                <select class="select2 form-control form-control-border" id="kabupaten" placeholder="Pilih Kabupaten" name="kabupaten">
                                </select>
                                <span id="ekabupaten" class="error invalid-feedback"></span>
                            </div>
                            <div class="form-group">
                                <label for="kecamatan">Kecamatan</label>
                                <select class="select2 form-control form-control-border" id="kecamatan" placeholder="Pilih Kecamatan" name="kecamatan">
                                </select>
                                <span id="ekecamatan" class="error invalid-feedback"></span>
                            </div>
                            <div class="form-group">
                                <label for="kelurahan">Kelurahan</label>
                                <select class="select2 form-control form-control-border" id="kelurahan" placeholder="Pilih Kelurahan" name="kelurahan">
                                </select>
                                <span id="ekelurahan" class="error invalid-feedback"></span>
                            </div>
                            <div class="form-group">
                                <label for="kode_pos">Kode Pos</label>
                                <input type="text" class="form-control form-control-border" id="kode_pos" placeholder="Kode Pos" name="kode_pos">
                                <span id="ekode_pos" class="error invalid-feedback"></span>
                            </div>
                            <div class="form-group">
                                <label for="lat">Latitude</label>
                                <input type="text" class="form-control form-control-border" id="lat" name="lat" value="<?=$calon->lat?>">
                                <span id="elat" class="error invalid-feedback"></span>
                            </div>
                            <div class="form-group">
                                <label for="lng">Longitude</label>
                                <input type="text" class="form-control form-control-border" id="lng" name="lng" value="<?=$calon->lng?>">
                                <span id="elng" class="error invalid-feedback"></span>
                            </div>
                            <div class="form-group">
                                <div id="map">
                                </div>
                            </div>
                        </div>
                        <div class="card-footer text-right">
                            <a id="btn_kembali" class="btn bg-gradient-warning" href="<?=base_url()?>agen_pangkalan_pengajuan"><span class="fas fa-arrow-left"></span> Batal</a>
                            <button id="btn_simpan" class="btn bg-gradient-primary"><span class="fas fa-check"></span> Simpan</button>
                        </div>
                    </div>
                </div>
            </div>
            </form>
        </div>
    </section>
    <script>
        $(document).ready(function () {
            var textra_query = '';
            var textra_kab = '';
            var textra_kec = '';
            var textra_kel = '';

            $("select[name='id_tipe_pangkalan']").select2({
                placeholder: "Pilih Tipe Pangkalan",
                ajax: {
                    url: "<?= base_url() ?>select_tipe_pangkalan",
                    dataType: 'json',
                    method: 'POST',
                    data: function(params) {
                        var query = {
                            search: params.term,
                            extra_query: textra_query,
                            page: params.page || 1
                        }
                        return JSON.stringify(query);
                    }
                }
            });

            $("select[name='id_agen']").select2({
                placeholder: "Pilih Agen",
                ajax: {
                    url: "<?= base_url() ?>select_agen",
                    dataType: 'json',
                    method: 'POST',
                    data: function(params) {
                        var query = {
                            search: params.term,
                            extra_query: textra_query,
                            page: params.page || 1
                        }
                        return JSON.stringify(query);
                    }
                }
            });

            $("select[name='provinsi']").select2({
                placeholder: "Pilih Provinsi",
                ajax: {
                    url: "<?= base_url() ?>select_provinsi",
                    dataType: 'json',
                    method: 'POST',
                    data: function(params) {
                        var query = {
                            search: params.term,
                            extra_query: textra_query,
                            page: params.page || 1
                        }
                        return JSON.stringify(query);
                    }
                }
            });

            $("select[name='provinsi']").on('select2:select', function (e) {
                var data = e.params.data;
                textra_kab = "kode_provinsi='"+data.id+"'";

                $("select[name='kabupaten']").val(null).trigger('change');
                $("select[name='kecamatan']").val(null).trigger('change');
                $("select[name='kelurahan']").val(null).trigger('change');
            });

            $("select[name='kabupaten']").select2({
                placeholder: "Pilih Kabupaten",
                ajax: {
                    url: "<?= base_url() ?>select_kabupaten",
                    dataType: 'json',
                    method: 'POST',
                    data: function(params) {
                        var query = {
                            search: params.term,
                            extra_query: textra_kab,
                            page: params.page || 1
                        }
                        return JSON.stringify(query);
                    }
                }
            });

            $("select[name='kabupaten']").on('select2:select', function (e) {
                var data = e.params.data;
                textra_kec = "kode_kabupaten='"+data.id+"'";
                $("select[name='kecamatan']").val(null).trigger('change');
                $("select[name='kelurahan']").val(null).trigger('change');
            });

            $("select[name='kecamatan']").select2({
                placeholder: "Pilih Kecamatan",
                ajax: {
                    url: "<?= base_url() ?>select_kecamatan",
                    dataType: 'json',
                    method: 'POST',
                    data: function(params) {
                        var query = {
                            search: params.term,
                            extra_query: textra_kec,
                            page: params.page || 1
                        }
                        return JSON.stringify(query);
                    }
                }
            });

            $("select[name='kecamatan']").on('select2:select', function (e) {
                var data = e.params.data;
                textra_kel = "kode_kecamatan='"+data.id+"'";
                $("select[name='kelurahan']").val(null).trigger('change');
            });

            $("select[name='kelurahan']").select2({
                placeholder: "Pilih Kelurahan",
                ajax: {
                    url: "<?= base_url() ?>select_kelurahan",
                    dataType: 'json',
                    method: 'POST',
                    data: function(params) {
                        var query = {
                            search: params.term,
                            extra_query: textra_kel,
                            page: params.page || 1
                        }
                        return JSON.stringify(query);
                    }
                }
            });

            $('[data-mask]').inputmask();

            $("#f1").submit(function (e) {
                e.preventDefault();
                Swal.fire({
                    text: 'Menyimpan data.. Silahkan tunggu..',
                    allowOutsideClick: false
                });
                Swal.showLoading();
                var data_kirim = $("#f1").serialize();
                
                $.post("<?=base_url()?>agen_insert_pangkalan",data_kirim).done(function (data) {
                    if (data.status == 'success')
                    {
                        Swal.fire({
                            text:'Pengajuan baru berhasil disimpan sebagai draft, silahkan lengkapi pengisian dokumen selanjutnya.',
                            icon : 'success'
                        }).then(function () {
                            window.location.href = '<?=base_url()?>agen_view_pangkalan_add/'+data.id;
                        });
                    }
                }).fail(function (data) {
                    if(data.statusText == 'input'){
                        Object.keys(data.responseJSON.messages).forEach(function(key){
                            $("#e"+key).html(data.responseJSON.messages[key]);
                            $("#e"+key).show();
                        });
                        Swal.close();
                    }else{
                        Swal.fire({
                            text:'Terjadi kesalahan pada saat registrasi!',
                            icon : 'error'
                        });
                    }
                });
            });
        });
    </script>
    <style>
        #map {
            width: 100%;
            height: 500px;
        }
    </style>
    <script>
        var map;
        var markers = [];
        var iconBase;
        var icons;
        $(document).ready(function () {
            function initMap() {
                map = new google.maps.Map(document.getElementById("map"), {
                    zoom: 12,
                    center: { lat: 3.6424551, lng: 98.5994535 },
                });

                iconBase =
                    "<?=base_url()?>/src/img/";
                icons = {
                    info: {
                        icon: iconBase + "logistic.png",
                    },
                };

                <?php
                if (!empty($calon->lat) && !empty($calon->lng))
                {
                ?>
                    var marker = new google.maps.Marker({
                        position: new google.maps.LatLng(<?=$calon->lat?>, <?=$calon->lng?>),
                        icon: icons['info'].icon,
                        map: map,
                        title:'Lokasi',
                    });
                    markers.push(marker);
                    map.panTo(marker.position);
                <?php
                }
                ?>

                google.maps.event.addListener(map, "click", (event) => {
                    addMarker(event.latLng, map);
                    var LatLng1 = event.latLng.toUrlValue().split(",");
                    var koordinat = LatLng1[0]+","+LatLng1[1];
                });
            }

            initMap();
            
            function addMarker(position, emap) {
                while (markers.length>0)
                {
                    markers[markers.length-1].setMap(null);
                    markers.pop();
                }

                var marker = new google.maps.Marker({
                    position: position,
                    map: emap,
                    icon: icons['info'].icon,
                    title:'Lokasi'
                });
                
                markers.push(marker);
                marker.setAnimation(google.maps.Animation.BOUNCE);
                map.panTo(marker.position);

                var LatLng1 = markers[0].position.toUrlValue().split(",");

                $("input[name='lat']").val(LatLng1[0]);
                $("input[name='lng']").val(LatLng1[1]);
            }
        });
    </script>
</div>