
<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>
<script src="<?=base_url()?>/assets/plupload/moxie.min.js"></script>
<script src="<?=base_url()?>/assets/plupload/plupload.dev.js"></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">Detail Agen</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()?>all_agen">Data Agen</a></li>
                    <li class="breadcrumb-item active">View Detail Agen</li>
                    </ol>
                </div><!-- /.col -->
            </div><!-- /.row -->
        </div><!-- /.container-fluid -->
    </div>
    <hr/>
    <style>
        .select2-container .select2-selection--single{
            height: 38px !important;
        }
    </style>
    <section class="content">
        <div class="container-fluid">
            <div class="row">
                <div class="col-lg-12">
                    <div class="card">
                        <div class="card-body">
                            <div class="row">
                                <div class="col-lg-3 col-sm-3">
                                    <div class="nav flex-column nav-tabs h-100" id="vert-tabs-tab" role="tablist" aria-orientation="vertical">
                                        <a class="nav-link active" id="vert-tabs-data-pokok-tab" data-toggle="pill" href="#vert-tabs-data-pokok" role="tab" aria-controls="vert-tabs-data-pokok" aria-selected="true"><i class="fas fa-users"></i> Data Pokok</a>
                                        <a class="nav-link" id="vert-tabs-lokasi-tab" data-toggle="pill" href="#vert-tabs-lokasi" role="tab" aria-controls="vert-tabs-lokasi" aria-selected="false"><i class="far fa-file-alt"></i> Lokasi</a>
                                        <a class="nav-link" id="vert-tabs-dokumen-tab" data-toggle="pill" href="#vert-tabs-dokumen" role="tab" aria-controls="vert-tabs-dokumen" aria-selected="false"><i class="far fa-file-alt"></i> Dokumen</a>
                                        <a class="nav-link" id="vert-tabs-pangkalan-tab" data-toggle="pill" href="#vert-tabs-pangkalan" role="tab" aria-controls="vert-tabs-pangkalan" aria-selected="false"><i class="far fa-file-alt"></i> Pangkalan</a>
                                    </div>
                                </div>

                                <div class="col-lg-9 col-sm-9">
                                    <div class="tab-content" id="vert-tabs-tabContent">
                                        <div class="tab-pane text-left fade show active" id="vert-tabs-data-pokok" role="tabpanel" aria-labelledby="vert-tabs-data-pokok-tab">
                                            <div class="card card-outline card-success">
                                                <div class="card-header">
                                                    <div class="card-title">
                                                        Data Pokok Agen
                                                    </div>
                                                </div>
                                                <div class="card-body">
                                                    <form id="f1" action="#" method="post" enctype="multipart/form-data">
                                                        <input type="hidden" name="id" value="<?=$data->id_agen?>">
                                                        <div class="form-group">
                                                            <label for="nomor">Nomor</label>
                                                            <input type="text" class="form-control form-control-border" id="nomor" placeholder="Nomor" name="nomor" value="<?=$data->nomor?>">
                                                            <span id="enomor" class="error invalid-feedback"></span>
                                                        </div>
                                                        <div class="form-group">
                                                            <label for="nama_perusahaan">Nama Perusahaan</label>
                                                            <input type="text" class="form-control form-control-border" id="nama_perusahaan" placeholder="Nama Perusahaan" name="nama_perusahaan" value="<?=$data->nama_perusahaan?>">
                                                            <span id="enama_perusahaan" class="error invalid-feedback"></span>
                                                        </div>
                                                        <div class="form-group">
                                                            <label for="nama_pemohon">Nama Pemohon</label>
                                                            <input type="text" class="form-control form-control-border" id="nama_pemohon" placeholder="Nama Pemohon" name="nama_pemohon" value="<?=$data->nama_pemohon?>">
                                                            <span id="enama_pemohon" class="error invalid-feedback"></span>
                                                        </div>
                                                        <div class="form-group">
                                                            <label for="sold_id">Sold ID</label>
                                                            <input type="text" class="form-control form-control-border" id="sold_id" placeholder="Sold ID" name="sold_id" value="<?=$data->sold_id?>">
                                                            <span id="esold_id" 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" placeholder="Email" name="email" value="<?=$data->email?>">
                                                            <span id="eemail" class="error invalid-feedback"></span>
                                                        </div>
                                                        <div class="form-group">
                                                            <label for="password">Password</label>
                                                            <input type="password" class="form-control form-control-border" id="password" placeholder="Password Login Agen" name="password" value="<?=$data->password?>">
                                                            <span id="epassword" class="error invalid-feedback"></span>
                                                        </div>
                                                        <div class="form-group">
                                                            <label for="no_hp">Nomor HP</label>
                                                            <input type="text" class="form-control form-control-border" id="no_hp" name="no_hp" data-inputmask='"mask": "(+62) 999-9999999"' data-mask value="<?=$data->no_hp?>">
                                                            <span id="eno_hp" class="error invalid-feedback"></span>
                                                        </div>
                                                    </form>
                                                </div>
                                                <div class="card-footer text-right">
                                                    <a id="btn_kembali" class="btn bg-gradient-warning" href="<?=base_url()?>all_agen"><span class="fas fa-arrow-left"></span> Batal</a>
                                                    <button id="btn_simpan1" class="btn bg-gradient-primary"><span class="fas fa-check"></span> Update Data</button>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="tab-pane text-left fade" id="vert-tabs-lokasi" role="tabpanel" aria-labelledby="vert-tabs-lokasi-tab">
                                            <div class="card card-outline card-info">
                                                <div class="card-header">
                                                    <div class="card-title">
                                                        Lokasi
                                                    </div>
                                                </div>
                                                <div class="card-body">
                                                    <div class="card">
                                                        <div class="card-body">
                                                            <form id="f2" action="#" method="post" enctype="multipart/form-data">
                                                                <div class="form-group">
                                                                    <label for="alamat">Alamat</label>
                                                                    <textarea class="form-control form-control-border" id="alamat" placeholder="Alamat" name="alamat"><?=$data->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" style="width: 100%" id="provinsi" placeholder="Pilih Provinsi" name="provinsi">
                                                                        <option value="<?=$data->provinsi?>" selected><?=$data->nama_provinsi?></option>
                                                                    </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" style="width: 100%" id="kabupaten" placeholder="Pilih Kabupaten" name="kabupaten">
                                                                        <option value="<?=$data->kabupaten?>" selected><?=$data->nama_kabupaten?></option>
                                                                    </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" style="width: 100%" id="kecamatan" placeholder="Pilih Kecamatan" name="kecamatan">
                                                                        <option value="<?=$data->kecamatan?>" selected><?=$data->nama_kecamatan?></option>
                                                                    </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" style="width: 100%" id="kelurahan" placeholder="Pilih Kelurahan" name="kelurahan">
                                                                        <option value="<?=$data->kelurahan?>" selected><?=$data->nama_kelurahan?></option>
                                                                    </select>
                                                                    <span id="ekelurahan" 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="<?=$data->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="<?=$data->lng?>">
                                                                    <span id="elng" class="error invalid-feedback"></span>
                                                                </div>
                                                                <div class="form-group">
                                                                    <div id="map" class="map">
                                                                    </div>
                                                                </div>
                                                            </form>
                                                        </div>
                                                        <div class="card-footer text-right">
                                                            <a id="btn_kembali2" class="btn bg-gradient-warning" href="<?=base_url()?>all_agen"><span class="fas fa-arrow-left"></span> Batal</a>
                                                            <button id="btn_simpan2" class="btn bg-gradient-primary"><span class="fas fa-check"></span> Update Lokasi</button>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="tab-pane text-left fade" id="vert-tabs-dokumen" role="tabpanel" aria-labelledby="vert-tabs-dokumen-tab">
                                            <div class="card card-outline card-info">
                                                <div class="card-header">
                                                    <div class="card-title">
                                                        Dokumen
                                                    </div>
                                                </div>
                                                <div class="card-body">
                                                    <div class="card">
                                                        <div class="card-body d-flex">
                                                            <div class="ml-2">
                                                                <p>
                                                                    <b>KTP</b><br>
                                                                    <?php
                                                                        $status1 = false;
                                                                        if (!empty($data->ktp))
                                                                        {
                                                                            $status1=true;
                                                                            ?>
                                                                            <img class="img-fluid" src="<?=base_url()?>uploads/agen/dokumen/ktp/<?=$data->ktp?>">
                                                                            <?php
                                                                        }else{
                                                                            echo 'No File Available';
                                                                        }
                                                                    ?>
                                                                </p>
                                                            </div>
                                                            <div class="ml-auto">
                                                                <?php
                                                                if (!$status1){
                                                                ?>
                                                                <button class="btn btn-app bg-info" id="btn_browse_ktp">
                                                                    <i class="fas fa-file-upload"></i> Upload
                                                                </button>
                                                                <?php
                                                                }else{
                                                                    ?>
                                                                    <a class="btn btn-app bg-danger" href="javascript:delete_file('<?=$data->ktp?>','ktp');">
                                                                        <i class="fas fa-trash"></i> Delete
                                                                    </a>
                                                                    <a class="btn btn-app bg-success" href="javascript:download_file('<?=$data->ktp?>','ktp');">
                                                                        <i class="fas fa-download"></i> Download
                                                                    </a>
                                                                    <?php
                                                                }
                                                                ?>
                                                            </div>
                                                        </div>
                                                    </div>

                                                    <div class="card">
                                                        <div class="card-body d-flex">
                                                            <div class="ml-2">
                                                                <p>
                                                                    <b>SK</b><br>
                                                                    <?php
                                                                        $status2 = false;
                                                                        if (!empty($data->sk))
                                                                        {
                                                                            $status2=true;
                                                                            echo 'File : '.$data->sk;
                                                                        }else{
                                                                            echo 'No File Available';
                                                                        }
                                                                    ?>
                                                                </p>
                                                            </div>
                                                            <div class="ml-auto">
                                                                <?php
                                                                if (!$status2){
                                                                ?>
                                                                <button class="btn btn-app bg-info" id="btn_browse_sk">
                                                                    <i class="fas fa-file-upload"></i> Upload
                                                                </button>
                                                                <?php
                                                                }else{
                                                                    ?>
                                                                    <a class="btn btn-app bg-danger" href="javascript:delete_file('<?=$data->sk?>','sk');">
                                                                        <i class="fas fa-trash"></i> Delete
                                                                    </a>
                                                                    <a class="btn btn-app bg-success" href="javascript:download_file('<?=$data->sk?>','sk');">
                                                                        <i class="fas fa-download"></i> Download
                                                                    </a>
                                                                    <?php
                                                                }
                                                                ?>
                                                            </div>
                                                        </div>
                                                    </div>

                                                    <div class="card">
                                                        <div class="card-body d-flex">
                                                            <div class="ml-2">
                                                                <p>
                                                                    <b>Kontrak Pangkalan</b><br>
                                                                    <?php
                                                                        $status3 = false;
                                                                        if (!empty($data->kontrak_pangkalan))
                                                                        {
                                                                            $status3=true;
                                                                            echo 'File : '.$data->kontrak_pangkalan;
                                                                        }else{
                                                                            echo 'No File Available';
                                                                        }
                                                                    ?>
                                                                </p>
                                                            </div>
                                                            <div class="ml-auto">
                                                                <?php
                                                                if (!$status3){
                                                                ?>
                                                                <button class="btn btn-app bg-info" id="btn_browse_kontrak_pangkalan">
                                                                    <i class="fas fa-file-upload"></i> Upload
                                                                </button>
                                                                <?php
                                                                }else{
                                                                    ?>
                                                                    <a class="btn btn-app bg-danger" href="javascript:delete_file('<?=$data->kontrak_pangkalan?>','kontrak_pangkalan');">
                                                                        <i class="fas fa-trash"></i> Delete
                                                                    </a>
                                                                    <a class="btn btn-app bg-success" href="javascript:download_file('<?=$data->kontrak_pangkalan?>','kontrak_pangkalan');">
                                                                        <i class="fas fa-download"></i> Download
                                                                    </a>
                                                                    <?php
                                                                }
                                                                ?>
                                                            </div>
                                                        </div>
                                                    </div>

                                                    <div class="card">
                                                        <div class="card-body d-flex">
                                                            <div class="ml-2">
                                                                <p>
                                                                    <b>Foto Lokasi</b><br>
                                                                    <?php
                                                                        $status4 = false;
                                                                        if (!empty($data->foto_lokasi))
                                                                        {
                                                                            $status4=true;
                                                                            ?>
                                                                            <img class="img-fluid" src="<?=base_url()?>uploads/agen/dokumen/foto_lokasi/<?=$data->foto_lokasi?>">
                                                                            <?php
                                                                        }else{
                                                                            echo 'No File Available';
                                                                        }
                                                                    ?>
                                                                </p>
                                                            </div>
                                                            <div class="ml-auto">
                                                                <?php
                                                                if (!$status4){
                                                                ?>
                                                                <button class="btn btn-app bg-info" id="btn_browse_foto_lokasi">
                                                                    <i class="fas fa-file-upload"></i> Upload
                                                                </button>
                                                                <?php
                                                                }else{
                                                                    ?>
                                                                    <a class="btn btn-app bg-danger" href="javascript:delete_file('<?=$data->foto_lokasi?>','foto_lokasi');">
                                                                        <i class="fas fa-trash"></i> Delete
                                                                    </a>
                                                                    <a class="btn btn-app bg-success" href="javascript:download_file('<?=$data->foto_lokasi?>','foto_lokasi');">
                                                                        <i class="fas fa-download"></i> Download
                                                                    </a>
                                                                    <?php
                                                                }
                                                                ?>
                                                            </div>
                                                        </div>
                                                    </div>

                                                    <div class="card">
                                                        <div class="card-body d-flex">
                                                            <div class="ml-2">
                                                                <p>
                                                                    <b>Foto Selfie KTP</b><br>
                                                                    <?php
                                                                        $status5 = false;
                                                                        if (!empty($data->foto_selfie_ktp))
                                                                        {
                                                                            $status5=true;
                                                                            ?>
                                                                            <img class="img-fluid" src="<?=base_url()?>uploads/agen/dokumen/foto_selfie_ktp/<?=$data->foto_selfie_ktp?>">
                                                                            <?php
                                                                        }else{
                                                                            echo 'No File Available';
                                                                        }
                                                                    ?>
                                                                </p>
                                                            </div>
                                                            <div class="ml-auto">
                                                                <?php
                                                                if (!$status5){
                                                                ?>
                                                                <button class="btn btn-app bg-info" id="btn_browse_foto_selfie_ktp">
                                                                    <i class="fas fa-file-upload"></i> Upload
                                                                </button>
                                                                <?php
                                                                }else{
                                                                    ?>
                                                                    <a class="btn btn-app bg-danger" href="javascript:delete_file('<?=$data->foto_selfie_ktp?>','foto_selfie_ktp');">
                                                                        <i class="fas fa-trash"></i> Delete
                                                                    </a>
                                                                    <a class="btn btn-app bg-success" href="javascript:download_file('<?=$data->foto_selfie_ktp?>','foto_selfie_ktp');">
                                                                        <i class="fas fa-download"></i> Download
                                                                    </a>
                                                                    <?php
                                                                }
                                                                ?>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="tab-pane text-left fade" id="vert-tabs-pangkalan" role="tabpanel" aria-labelledby="vert-tabs-pangkalan-tab">
                                            <div class="card card-outline card-info">
                                                <div class="card-header">
                                                    <div class="card-title">
                                                        Pangkalan
                                                    </div>
                                                </div>
                                                <div class="card-body">
                                                    <div id="map2" class="map">
                                                    </div>
                                                    <div class="card">
                                                        <div class="card-body">
                                                            <table id="tbl_data1" class="display compact cell-border row-border nowrap" style="width:100%">
                                                                <thead>
                                                                    <tr>
                                                                        <th>Nama Pangkalan</th>
                                                                        <th>Tipe Pangkalan</th>
                                                                        <th>ID Registrasi</th>
                                                                        <th>Provinsi</th>
                                                                        <th>Kabupaten</th>
                                                                        <th>Kecamatan</th>
                                                                        <th>Kelurahan</th>
                                                                        <th>Status</th>
                                                                        <th class="not-export">Action</th>
                                                                    </tr>
                                                                </thead>
                                                                <tbody>

                                                                </tbody>
                                                            </table>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
    <style>
        .map {
            width: 100%;
            height: 500px;
        }
    </style>
    <script>
        $(document).ready(function () {
            function init_uploader(target) {
                var uploader = new plupload.Uploader({
                    'multi_selection' : false,
                    'unique_names' : true,
                    'browse_button' : 'btn_browse_'+target,
                    'chunk_size':'1024kb',
                    'max_retries' : 3,
                    'url' : '<?=base_url()?>agen_upload/'+target+'/<?=$data->id_agen?>',
                });

                uploader.init();

                uploader.bind("BeforeUpload", function(up,file) {
                    file.name = <?=$data->id_agen?>+'_'+file.target_name;

                    var params = up.settings.multipart_params;
                    params.key = file.name;
                    params.Filename = file.name;
                });

                uploader.bind('UploadProgress', function(up, file) {
                    /*document.getElementById(file.id).getElementsByTagName('b')[0].innerHTML = '<span>' + file.percent + "%</span>";*/
                    $("#filelist_inner").html(file.name + ' : ' + file.percent + '%');
                    $("#filelist").attr('aria-valuenow',file.percent);
                    $("#filelist_inner").attr('style','width:'+file.percent+'%');
                });

                uploader.bind('UploadComplete', function(up, file, info) {
                    Swal.fire({
                        text:'Uploading Sukses',
                        icon : 'success',
                        allowOutsideClick:false
                    }).then(function () {
                        window.location.href = '<?=base_url()?>view_agen/<?=$data->id_agen?>';
                    });
                });

                uploader.bind('Error', function(up, err) {
                    up.stop();
                    console.log(err);
                    Swal.fire({
                        text:'Uploading Failed! Error : '+err.message,
                        icon : 'error',
                        allowOutsideClick:false
                    });
                });

                uploader.bind('FilesAdded', function(up, files) {
                    if(up.files.length > 1)
                    {
                        tfile = up.files[0];
                        up.removeFile(tfile);
                    }

                    upload(up);
                });

                return uploader;
            }

            function upload(up)
            {
                if (up.files.length <= 0)
                {
                    Swal.fire({
                        title:'Upload',
                        html:'Tidak ada file yang akan diupload..',
                        icon : 'error',
                        allowOutsideClick:false
                    }).then(function () {
                        return false;
                    });
                }

                Swal.fire({
                    title:'Uploading..',
                    html:'<div id="filelist" class="progress" role="progressbar" aria-label="test" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100"><div class="progress-bar progress-bar-striped progress-bar-animated" id="filelist_inner" style="width: 0%">0%</div></div>',
                    allowOutsideClick:false
                });
                Swal.showLoading();
                up.start();
            }

            var uploader1 = init_uploader('ktp');
            var uploader2 = init_uploader('sk');
            var uploader3 = init_uploader('kontrak_pangkalan');
            var uploader4 = init_uploader('foto_lokasi');
            var uploader5 = init_uploader('foto_selfie_ktp');
        });
    </script>
    
    <script>
        function download_file(filename,target)
        {
            var link = document.createElement('a');
            link.download = filename;
            link.href ="<?=base_url()?>/uploads/agen/dokumen/"+target+"/"+filename;  
            link.click();
        }

        function delete_file(filename,target)
        {
            Swal.fire({
                title:'Delete File..',
                html: '<p>Yakin ingin menghapus file ini?</p>',
                showDenyButton: true,
                showCancelButton: true,
                confirmButtonText: 'Yes',
                denyButtonText: 'No',
            }).then((result) => {
                if (result.isConfirmed) {
                    Swal.fire({
                        text: 'Menghapus file.. Silahkan tunggu..',
                        allowOutsideClick: false
                    });
                    Swal.showLoading();
                    $.post("<?=base_url()?>agen_delete_upload/"+target+"/<?=$data->id_agen?>",{file_name:filename}).done(function (data) {
                        if (data.message == 'delete success')
                        {
                            Swal.fire({
                                text:'File berhasil dihapus!',
                                icon : 'success'
                            }).then(function () {
                                window.location.href = '<?=base_url()?>view_agen/<?=$data->id_agen?>';
                            });
                        }
                    }).fail(function (data) {
                        Swal.fire({
                            text:'Terjadi kesalahan pada saat operasi delete!',
                            icon : 'error'
                        });
                    });
                } else if (result.isDenied) {
                    Swal.fire('Operasi dibatalkan..', '', 'info')
                }
            });
        }
    </script>
    <script>
        $(document).ready(function () {
            var textra_query = '';
            var textra_kab = "kode_provinsi='<?=$data->provinsi?>'";
            var textra_kec = "kode_kabupaten='<?=$data->kabupaten?>'";
            var textra_kel = "kode_kecamatan='<?=$data->kecamatan?>'";

            $("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();

            $("#btn_simpan1").click(function () { $("#f1").submit(); });
            $("#btn_simpan2").click(function () { $("#f2").submit(); });

            $("#f1").submit(function (e) {
                e.preventDefault();
                Swal.fire({
                    text: 'Menyimpan data.. Silahkan tunggu..',
                    allowOutsideClick: false
                });
                Swal.showLoading();
                var data_kirim = $("#f1").serialize();
                $(".invalid-feedback").hide();
                $.post("<?=base_url()?>update_agen_pokok/<?=$data->id_agen?>",data_kirim).done(function (data) {
                    if (data.status == 'success')
                    {
                        Swal.fire({
                            text:'Data Pokok berhasil diupdate!',
                            icon : 'success'
                        }).then(function () {
                            window.location.href = '<?=base_url()?>view_agen/<?=$data->id_agen?>';
                        });
                    }
                }).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 penyimpanan!',
                            icon : 'error'
                        });
                    }
                });
            });

            $("#f2").submit(function (e) {
                e.preventDefault();
                Swal.fire({
                    text: 'Menyimpan data.. Silahkan tunggu..',
                    allowOutsideClick: false
                });
                Swal.showLoading();
                var data_kirim = $("#f2").serialize();
                $(".invalid-feedback").hide();
                $.post("<?=base_url()?>update_agen_lokasi/<?=$data->id_agen?>",data_kirim).done(function (data) {
                    if (data.status == 'success'){
                        Swal.fire({
                            text:'Data Lokasi berhasil diupdate!',
                            icon : 'success'
                        }).then(function () {
                            window.location.href = '<?=base_url()?>view_agen/<?=$data->id_agen?>';
                        });
                    }
                }).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 penyimpanan!',
                            icon : 'error'
                        });
                    }
                });
            });
        });
    </script>
    <script>
        var map;
        var map2;
        var markers = [];
        var markers2 = [];
        var iconBase;
        var icons;
        $(document).ready(function () {
            const infoWindow = new google.maps.InfoWindow();
            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($data->lat) && !empty($data->lng))
                {
                ?>
                    var marker = new google.maps.Marker({
                        position: new google.maps.LatLng(<?=$data->lat?>, <?=$data->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];
                });

                map2 = new google.maps.Map(document.getElementById("map2"), {
                    zoom: 7,
                    center: { lat: 1.9067513, lng: 99.7160549 },
                });

                <?php
                foreach ($pangkalan as $row)
                {
                    ?>
                    addMarker2(new google.maps.LatLng(<?=$row->lat?>, <?=$row->lng?>),map2,'<?=$row->nama_pangkalan?>');
                    <?php
                }
                ?>
            }

            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]);
            }

            function addMarker2(position, emap, ttitle) {
                var marker = new google.maps.Marker({
                    position: position,
                    map: emap,
                    icon: icons['info'].icon,
                    title:ttitle
                });
                
                marker.addListener("click", () => {
                    infoWindow.close();
                    infoWindow.setContent(marker.getTitle());
                    infoWindow.open(marker.getMap(), marker);
                });
                markers2.push(marker);
            }
        });
    </script>
    <script>
        $(document).ready(function () {
            var table1 = $("#tbl_data1").DataTable({
                processing: true,
                serverSide: true,
                dom: 'Bfrtip',
                language : {
                    searchBuilder: {
                        button: '<span class="fas fa-search"> Filter</span>',
                    }
                },
                ajax: {
                    url : '<?=base_url()?>get_all_pangkalan',
                    type : 'POST'
                },
                columns: [
                    { data: 'nama_pangkalan' },
                    { data: 'nama_tipe' },
                    { data: 'id_registrasi' },
                    { data: 'nama_provinsi' },
                    { data: 'nama_kabupaten' },
                    { data: 'nama_kecamatan' },
                    { data: 'nama_kelurahan' },
                    { data: 'status' },
                    { data: 'action',
                        searchable : false,
                        orderable : false },
                ],
                columnDefs: [
                    {
                        targets:7,
                        data:'status',
                        "render": function ( data, type, row, meta ) {
                            if (type === 'display')
                            {
                                if (data == 'draft')
                                {
                                    return '<div class="btn bg-gradient-secondary btn-block btn-sm">Draft</div>';
                                }else if (data == 'waiting')
                                {
                                    return '<div class="btn bg-gradient-info btn-block btn-sm">Waiting</div>';
                                }else if (data == 'revision')
                                {
                                    return '<div class="btn bg-gradient-warning btn-block btn-sm">Revision</div>';
                                }else if (data == 'reject')
                                {
                                    return '<div class="btn bg-gradient-danger btn-block btn-sm">Reject</div>';
                                }else if (data == 'approve')
                                {
                                    return '<div class="btn bg-gradient-success btn-block btn-sm">Approved / Aktif</div>';
                                }
                            }else if (type === 'type')
                            {
                                return data;
                            }
                        }
                    },
                    {
                        targets: -1,
                        data: null,
                        defaultContent: '<button class="btn-view btn btn-block bg-gradient-success btn-sm"><span class="fas fa-search"></span> View</button>',
                    },
                ],
                buttons: [
                    'searchBuilder',
                    'copyHtml5',
                    {
                        extend: 'excelHtml5',
                        text: 'Export Excel',
                        orientation: 'landscape',
                        exportOptions: {
                            columns: [ ':not(.not-export)' ]
                        }
                    },
                    {
                        extend: 'csvHtml5',
                        text: 'Export CSV',
                        orientation: 'landscape',
                        exportOptions: {
                            columns: [ ':not(.not-export)' ]
                        }
                    },
                    {
                        extend: 'pdfHtml5',
                        text: 'Export PDF',
                        orientation: 'landscape',
                        exportOptions: {
                            columns: [ ':not(.not-export)' ]
                        }
                    }
                ],
                "initComplete": function(settings, json) {
                }
            });

            $('#tbl_data1 tbody').on('click','.btn-view',function () {
                var data = table1.row($(this).parents('tr')[0]).data();
                document.location.href='<?=base_url()?>view_pangkalan/'+data['id_pangkalan'];
            });
        });
    </script>
</div>