<?php
$conPelanggan = $koneksi->query("SELECT * FROM tbl_penggunamikrotik");
$checkUser = $conPelanggan->fetch_assoc();
?>

<div class="row">
    <!-- left column -->
    <div class="col-md-6">

        <div class="box box-primary box-solid">
            <div class="box-header with-border">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
                Ubah Data Pelanggan
            </div>
            <div class="modal-body">

                <form role="form" method="POST">

                    <?php

                    $id_pelanggan = $_GET['id'];

                    $sql1 = $koneksi->query("SELECT * FROM tb_pelanggan WHERE id_pelanggan='$id_pelanggan'");

                    while ($data1 = $sql1->fetch_assoc()) {

                        $formattedDate = date('Y-m-d H:i', strtotime($data1['jatuh_tempo']));

                    ?>

                        <input type="hidden" name="id_pelanggan" value="<?php echo $data1['id_pelanggan']; ?>">

                        <?php if ($checkUser['ippelanggan'] == 'statik') { ?>
                            <div class="form-group">
                                <label>IP Address</label>
                                <input type="text" name="ip_address" class="form-control" value="<?php echo $data1['ip_address']; ?>">
                            </div>
                        <?php } ?>

                        <div class="form-group">
                            <label>Jatuh Tempo</label>
                            <input type="text" name="jatuhtempo" class="form-control" value="<?php echo $formattedDate; ?>" required="">
                        </div>

                        <div class="form-group">
                            <label>NIK KTP <small style="color: red;">*Kosongkan Jika Tidak Ada</small></label>
                            <input type="text" name="nik" class="form-control" value="<?php echo $data1['nik']; ?>">
                        </div>

                        <div class="form-group">
                            <label>Nama Pelanggan</label>
                            <input type="text" name="nama" class="form-control" value="<?php echo $data1['nama_pelanggan']; ?>" required="">
                        </div>

                        <div class="form-group">
                            <label>Alamat </label>
                            <textarea class="form-control" rows="3" name="alamat"><?php echo $data1['alamat']; ?></textarea>
                        </div>

                        <div class="form-group">
                            <input type="hidden" name="mapping" class="form-control" value="<?php echo $data1['location'] ?>" id="coordinates">
                        </div>

                        <div class="form-group">
                            <label>No Telp (Contoh 089612345678)</label>
                            <input type="text" name="no_telp" class="form-control" value="<?php echo $data1['no_telp']; ?>" required="">
                        </div>

                        <div class="form-group">

                            <label>Paket :</label> <br>
                            <select class="form-control" name="paket">

                                <?php

                                $query = $koneksi->query("SELECT * FROM tb_paket ORDER by id_paket");

                                while ($tampil_t = $query->fetch_assoc()) {
                                    $pilih_t = ($tampil_t['id_paket'] == $data1['paket'] ? "selected" : "");
                                    echo "<option value='$tampil_t[id_paket]' $pilih_t> $tampil_t[nama_paket]</option>";
                                }

                                ?>

                            </select>
                        </div>

                        <div class="form-group">
                            <label>Perangkat Modem :</label> <br>
                            <select class="form-control" name="perangkat">
                                <?php
                                $queryy = $koneksi->query("SELECT * FROM tb_perangkat ORDER BY id_perangkat");

                                $selectedNull = ($data1['id_perangkat'] === NULL || $data1['id_perangkat'] === '0') ? "selected" : "";
                                echo "<option value='NULL' $selectedNull>== Jangan Sebutkan ==</option>";

                                while ($tampil_tt = $queryy->fetch_assoc()) {
                                    $pilih_tt = ($tampil_tt['id_perangkat'] == $data1['id_perangkat']) ? "selected" : "";
                                    echo "<option value='$tampil_tt[id_perangkat]' $pilih_tt> $tampil_tt[nama_perangkat]</option>";
                                }
                                ?>
                            </select>
                        </div>

                        <div class="form-group">

                            <label>Dari ODP</label> <br>
                            <select class="form-control" name="odp">
                                <option value="NULL">== Tidak Ada ==</option>
                                <?php

                                $queryy = $koneksi->query("SELECT * FROM tbl_odp ORDER BY id_odp");

                                while ($tampil_tt = $queryy->fetch_assoc()) {
                                    $pilih = ($tampil_tt['id_odp'] == $data1['odp'] ? "selected" : "");
                                    echo "<option value='$tampil_tt[id_odp]' $pilih > $tampil_tt[nama_odp]</option>";
                                }

                                ?>

                            </select>

                        </div>

            </div>
            <div class="modal-footer">

                <button type="submit" name="simpan" class="btn btn-block btn-primary btn-lg">Simpan</button>

            </div>

        <?php } ?>

        </form>

        </div>
    </div>

    <div class="col-md-6">
        <div class="box box-primary">

            <div class="box-header with-border">
                <h3 class="box-title">Lokasi Pelanggan</h3>
            </div>

            <div class="box-body">
                <div id="map" style="height: 600px;"></div>
            </div>
        </div>
    </div>

</div>

<?php

if (isset($_POST['simpan'])) {
    $id_pelanggan = $_POST['id_pelanggan'];
    $nama = htmlspecialchars($_POST['nama']);
    $alamat = htmlspecialchars($_POST['alamat']);
    $nik = htmlspecialchars($_POST['nik']);
    $no_telp = htmlspecialchars($_POST['no_telp']);
    $paket = $_POST['paket'];
    $jatuh_tempo = htmlspecialchars($_POST['jatuhtempo']);
    $ip_address = isset($_POST['ip_address']) ? $_POST['ip_address'] : '-';
    $mapping = $_POST['mapping'];
    $odp = $_POST['odp'];
    $perangkat = ($_POST['perangkat'] !== 'NULL') ? $_POST['perangkat'] : NULL;

    // Update tb_pelanggan
    $koneksi->query("UPDATE tb_pelanggan SET
        nik = '$nik',
        nama_pelanggan = '$nama',
        alamat = '$alamat',
        no_telp = '$no_telp',
        ip_address = '$ip_address',
        jatuh_tempo = '$jatuh_tempo',
        paket = '$paket',
        location = '$mapping',
        odp = '$odp',
        id_perangkat = " . ($perangkat ? "'$perangkat'" : "NULL") . "
        WHERE id_pelanggan = '$id_pelanggan'
    ");

    // Update tb_user berdasarkan id_pelanggan
    $koneksi->query("UPDATE tb_user SET
        nama_user = '$nama'
        WHERE id_pelanggan = '$id_pelanggan'
    ");

    echo "<script>
        swal({
            title: 'Berhasil',
            text: 'Data pelanggan berhasil diubah!',
            type: 'success'
        }, function () {
            window.location = '?page=pelanggan';
        });
    </script>";
}



?>