<?php 
$stmt = $pdo->prepare("SELECT * FROM v_tagihan WHERE $SqlSvData id = '$page[1]'");
$stmt->execute();
$data = $stmt->fetchAll();
if ($data[0]['konpensasi'] == 0) {
  $datakonpensasi = $data[0]['jumlah_konpensasi'];
} else {
  $datakonpensasi = 0;
}
// echo "<pre>";
// print_r($_SERVER);
// echo "</pre>";
?>
<section class="content">
    <div class="container-fluid">
      <div class="row">
          <div class="col-12">
            <div class="card">
                <div class="card-header bg-secondary">
                  <div class="row">
                      <div class="col-sm-6">
                        <h5>Detail Tagihan</h5>
                      </div>
                      <div class="col-sm-6">
                      </div>
                  </div>
                </div>
                <!-- /.card-header -->
                <div class="card-body">
                  <div class="row">
                    <div class="col-md-6">
                      <table class="table table-borderless">
                        <tbody>
                          <tr>
                            <td class="p-1">Status Tagihan</td>
                            <td class="p-1">:</td>
                            <td class="p-1">
                              <?php 
                                if($data[0]['status'] == 'buat') {
                                  echo '<div class="badge badge-warning">Akan datang</div>';
                                } elseif($data[0]['status'] == 'nunggak') {
                                  echo '<div class="badge badge-danger">Tunggakan</div>';
                                } elseif($data[0]['status'] == 'lunas') {
                                  echo '<div class="badge badge-success">Telah dibayar</div>';
                                } else {
                                  echo '<div class="badge badge-primary">Sedang Berjalan</div>';
                                }
                              ?>
                            </td>
                          </tr>
                          <tr>
                            <td class="p-1">Id Pelanggan</td>
                            <td class="p-1">:</td>
                            <td class="p-1"><?php echo $data[0]['akun']; ?></td>
                          </tr>
                          <tr>
                            <td class="p-1">Nama</td>
                            <td class="p-1">:</td>
                            <td class="p-1"><?php echo $data[0]['nama']; ?></td>
                          </tr>
                          <tr>
                            <td class="p-1">Alamat</td>
                            <td class="p-1">:</td>
                            <td class="p-1"><?php echo $data[0]['alamat']; ?></td>
                          </tr>
                          <tr>
                            <td class="p-1">Jenis Layanan</td>
                            <td class="p-1">:</td>
                            <td class="p-1"><?php echo $data[0]['jenis_layanan']; ?></td>
                          </tr>
                          <tr>
                            <td class="p-1">Keterangan Tagihan</td>
                            <td class="p-1">:</td>
                            <td class="p-1"><?php echo $data[0]['keterangan']; ?></td>
                          </tr>
                          <tr>
                            <td class="p-1">Paket Berlangganan</td>
                            <td class="p-1">:</td>
                            <td class="p-1"><?php echo $data[0]['nama_paket']; ?></td>
                          </tr>
                          <tr>
                            <td class="p-1">Harga Layanan</td>
                            <td class="p-1">:</td>
                            <td class="p-1">Rp. <?php echo number_format($data[0]['nominal_tagihan']); ?></td>
                          </tr>
                          <tr>
                            <td class="p-1">Jumlah Potongan</td>
                            <td class="p-1">:</td>
                            <td class="p-1">Rp. <?php echo number_format($data[0]['jumlah_konpensasi']); ?></td>
                          </tr>
                          <tr>
                            <td class="p-1">Jumlah Tagihan</td>
                            <td class="p-1">:</td>
                            <td class="p-1">Rp. <?php echo number_format($data[0]['jumlah_tagihan']); ?></td>
                          </tr>
                          <tr>
                            <td class="p-1">PPN 11%</td>
                            <td class="p-1">:</td>
                            <td class="p-1">Rp. <?php echo number_format($data[0]['ppn']); ?></td>
                          </tr>
                          <?php 
                          if($data[0]['kode_unik'] != '0') { ?>
                          <tr>
                            <td class="p-1">Subsidi</td>
                            <td class="p-1">:</td>
                            <td class="p-1">Rp. <?php echo number_format($data[0]['kode_unik'], 0, ',', '.'); ?></td>
                          </tr>
                          <?php } ?>
                          <tr>
                            <td class="p-1"><b>Total Tagihan</b></td>
                            <td class="p-1">:</td>
                            <td class="p-1"><b>Rp. <?php echo number_format($data[0]['verifikasi_tagihan']); ?></b></td>
                          </tr>
                          <?php 
                          if(isset($data[0]['transaction_link'])) { ?>
                          <tr>
                            <td class="p-1">Status Payment</td>
                            <td class="p-1">:</td>
                            <td class="p-1"><?php echo $data[0]['transaction_status']; ?></td>
                          </tr>
                          <tr>
                            <td class="p-1">Link Pembayaran</td>
                            <td class="p-1">:</td>
                            <td class="p-1" style="max-width: 200px;">
                              <?php 
                              $validStat = ['ACTIVE','INACTIVE'];
                              if (in_array($data[0]['transaction_status'], $validStat)) { ?>
                              <a href="https://<?php echo $data[0]['transaction_link']; ?>" target="_blank">https://<?php echo $data[0]['transaction_link']; ?></a>
                              <?php } else { echo "NO LINK"; }?>
                            </td>
                          </tr>
                          <?php } ?>
                        </tbody>
                      </table>
                    </div>
                    <div class="col-md-6">
                      <form method="post" enctype="mulipart/form-data">
                        <div class="form-group">
                          <label>Potongan Persentase Perhari</label>
                          <input class="form-control" value="<?php echo $data[0]['konpensasi']; ?>" type="text" name="potongan">
                          <i>Masukan jumlah hari dari potongan</i>
                        </div>
                        <div class="form-group">
                          <label>Jumlah Potongan Fix</label>
                          <input type="text" class="form-control rupiah" value="<?php echo $datakonpensasi; ?>" id="rupiah0" name="nominal">
                          <i>Masukan nominal potongan</i>
                        </div>
                        <?php if(in_array("Ubah", $json_array[4]['sub_menu'])){ 
                        $validStatus = ['CANCELLED','CANCEL'];
                        if (in_array($data[0]['transaction_status'], $validStatus)) { ?>

                          <input type="submit" name="simpan" class="btn btn-primary btn-sm m-1" value="Ganerate Link Baru">

                        <?php } else { ?>

                          <input type="submit" name="simpan" class="btn btn-primary btn-sm m-1" value="Edit / Simpan">

                        <?php } if($data[0]['status'] != 'buat'){ ?>

                          <input type="submit" name="kirim" class="btn btn-success btn-sm m-1" value="Kirim Ulang Tagihan">

                        <?php }} if(in_array("Ubah", $json_array[4]['sub_menu'])){ ?>

                          <input type="submit" name="hapus" class="btn btn-danger btn-sm m-1" value="Hapus">

                        <?php } ?>
                      </form>
                      <?php
                      if(isset($_POST['simpan'])) {
                        try {
                          $stmt = $pdo->prepare("SELECT * FROM profile_tagihan WHERE id = :id1 ");
                          $stmt->bindParam(':id1', $data[0]['id_profile_tagihan']);
                          $stmt->execute();
                          $profiletag = $stmt->fetchAll();

                          $ppn = $profiletag[0]['ppn'];

                          if (empty($_POST['potongan'])) {
                            $nominal = preg_replace("/[^0-9]/","",$_POST['nominal']);
                            $konpensasi = 0;
                            $jumlahkon = $nominal;
                            $tagihan = $data[0]['nominal_tagihan'] - $jumlahkon ;
                          } else {
                            $konpensasi = $_POST['potongan'];
                            $jumhari = date('t');
                            $jumlahkon = $data[0]['nominal_tagihan'] / $jumhari * $konpensasi;
                            $tagihan = $data[0]['nominal_tagihan'] - $jumlahkon ;
                          }

                          if ($ppn == 1 ) {
                              $nominal_tagihan = $tagihan;
                              $jumlah_tagihan = $tagihan;
                              $ppn_amount = 0;
                              $total_tagihan = round($jumlah_tagihan + $ppn_amount);
                              $kode_unik = 0;
                              $verifikasi_tagihan = round($total_tagihan + $kode_unik);
                          }
                          elseif($ppn == 2 ) {
                              $nominal_tagihan = $tagihan;
                              $jumlah_tagihan = $tagihan;
                              $ppn_amount = $jumlah_tagihan / 100 * 11;
                              $total_tagihan = round($jumlah_tagihan + $ppn_amount);
                              $kode_unik = 0;
                              $verifikasi_tagihan = round($total_tagihan + $kode_unik);
                          }
                          elseif($ppn == 3 ) {
                              $nominal_tagihan = $tagihan - ($tagihan / 100 * 9.91);
                              $jumlah_tagihan = $tagihan - ($tagihan / 100 * 9.91);
                              $ppn_amount = $jumlah_tagihan / 100 * 11;
                              $total_tagihan = round($jumlah_tagihan + $ppn_amount);
                              $kode_unik = 0;
                              $verifikasi_tagihan = round($total_tagihan + $kode_unik);
                          }
                          elseif($ppn == 4 ) {
                              $nominal_tagihan = $tagihan;
                              $jumlah_tagihan = $tagihan;
                              $ppn_amount = $jumlah_tagihan / 100 * 11;
                              $total_tagihan = round($jumlah_tagihan + $ppn_amount);
                              $kode_unik = substr($total_tagihan, -3);
                              $verifikasi_tagihan = round($total_tagihan - $kode_unik);
                          }


                          // Cari Payment Gateway
                          $stmt = $pdo->prepare("SELECT payment_gateway,apikey_payment_gateway,url_payment_gateway,status_payment_gateway FROM data_server WHERE id = :id_data ");
                          $stmt->bindParam(':id_data', $data[0]['id_data_server'], PDO::PARAM_INT);
                          $stmt->execute();
                          $paygw = $stmt->fetchAll(PDO::FETCH_ASSOC);
                        //   print_r($paygw);
                            //Jika menggunakan payment gateway
                            if (!empty($paygw[0]['apikey_payment_gateway']) && isset($paygw[0]['status_payment_gateway']) && $paygw[0]['status_payment_gateway'] === 'ACTIVE') {
                              require 'payment/flip/Flip/Transaction.php';
                              $apiKey = $paygw[0]['apikey_payment_gateway'];
                              $apiUrl = $paygw[0]['url_payment_gateway'];
                              $transactionId = $data[0]['transaction_id'];
                              $status = $data[0]['transaction_status'];
                              $title = $data[0]['keterangan'];
                              $amount = $verifikasi_tagihan;
                            //   echo $apiUrl;
                              //Buat Fungsion Flip Payment Gateway
                              $flip = new FlipPaymentGateway($apiKey,$apiUrl);
                              if ($status == 'ACTIVE') {
                                $response = $flip->editBill($transactionId,$status,$title, $amount);
                              } else {
                                $response = $flip->createBill($title, $amount, $data[0]['email'], $data[0]['nama'], $data[0]['nomor_whatsapp'], $data[0]['alamat']);
                              }
                            // echo $verifikasi_tagihan;
                            // print_r($response);
                              $transaction_id = $response['link_id'];
                              $transaction_link = $response['link_url'];
                              $transaction_status = $response['status'];
                            } 
                            else { 
                              $transaction_id = "";
                              $transaction_link = "";
                              $transaction_status = "";
                            }
                            
                            $stmt = $pdo->prepare("UPDATE tagihan SET konpensasi = :konpensasi, jumlah_konpensasi = :jumlahkon, jumlah_tagihan = :jumlah_tagihan, ppn = :ppn, total_tagihan = :total_tagihan, verifikasi_tagihan = :verifikasi_tagihan, transaction_id = :transaction_id, transaction_link = :transaction_link, transaction_status = :transaction_status, kode_unik = :kode_unik WHERE id = :idtag");
                            $stmt->bindParam(':jumlahkon', $jumlahkon);
                            $stmt->bindParam(':jumlah_tagihan', $jumlah_tagihan);
                            $stmt->bindParam(':ppn', $ppn_amount);
                            $stmt->bindParam(':total_tagihan', $total_tagihan);
                            $stmt->bindParam(':verifikasi_tagihan', $verifikasi_tagihan);
                            $stmt->bindParam(':konpensasi', $konpensasi);
                            $stmt->bindParam(':transaction_id', $transaction_id);
                            $stmt->bindParam(':transaction_link', $transaction_link);
                            $stmt->bindParam(':transaction_status', $transaction_status);
                            $stmt->bindParam(':kode_unik', $kode_unik);
                            $stmt->bindParam(':idtag', $page[1]);
                            $stmt->execute();

                            sweetAlert("Tagihan telah di edit", "success");
                        } catch (PDOException $e) {
                            sweetAlert("Gagal: " . $e->getMessage(), "error");
                        }
                      }

                      if (isset($_POST['kirim']))
                      {
                        // KIRIM PESAN WHATSAPP
                        $phone = array($data[0]['nomor_whatsapp']);
                        $API_WA = new PesanWA();
                        $API_WA->Create_Pesan($data[0]['id_data_server'], $page[1], 'pesan_kirim_tagihan', 'tag');
                        $Pesan = $API_WA->Get_Pesan();
                        $API_WA->Send_Pesan($data[0]['id_data_server'], 'sendMessage', $phone, $Pesan);
                        if($API_WA){
                            //  echo $data[0]['id_data_server']. 'sendMessage'. $phone. $Pesan;
                            sweetAlert("Tagihan Akan Segera di Kirim", "success");
                        }
                      }

                      if (isset($_POST['hapus']))
                      {
                        $iddata = $data[0]['id'];
                        deleteAlert("tagihan",$iddata, '?'.encrypt_url($page[2]) );
                      }
                      ?>

                    </div>
                  </div>
                </div>
          </div>
        </div>
    </div>
  </div>
</section>