<?php use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Fill;

if(!isset($_COOKIE['ADMIN_KEY']) && !isset($_COOKIE['KEY'])){
  header('location:./login/');
  exit;
}
else{
require_once '../../../sw-library/sw-config.php';
require_once '../../../sw-library/sw-function.php';
require_once '../../login/user.php';
$total_pembayaran = 0;

function hitung_total_bayar($connection, $master_spp) {
    $query_tagihan = mysqli_query($connection, "
        SELECT SUM(nominal) AS total_tagihan, tahun_ajaran
        FROM master_spp
        WHERE master_spp_id='$master_spp' AND status = 'Y'");
    $data_tagihan = mysqli_fetch_assoc($query_tagihan);
    $total_tagihan = (int) ($data_tagihan['total_tagihan'] ?? 0);
    return [
        'total_tagihan' => $total_tagihan,
        'tahun_ajaran'  => $data_tagihan['tahun_ajaran'],
    ];
}


$no = 0;
switch (@$_GET['action']){

case'print':
$nisn               = isset($_GET['nisn']) ? (int)$_GET['nisn'] : 0;
$master_spp         = isset($_GET['id']) ? $_GET['id'] : 0;
$hasil              = hitung_total_bayar($connection, $master_spp);

$query ="SELECT pembayaran.*, user.nama_lengkap,user.nisn,user.kelas FROM pembayaran
LEFT JOIN user ON pembayaran.user_id=user.user_id 
WHERE user.nisn='$nisn' AND pembayaran.master_spp_id='$master_spp'";
$result = $connection->query($query);
if($result->num_rows > 0){

echo'
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="s-widodo.com">
    <meta name="author" content="s-widodo.com">
    <title>Pembayaran SPP</title>
    <style>

    body{font-family:Arial,Helvetica,sans-serif}
    .text-center{
      text-align: center;
    }
    
    .kop {
      position:relative;
      display:contents;
      margin:0px 0px 20px 0px;
    }

    .kop img{
      width:100%;
      height:auto;
    }

    table.datatable{
      width:100%;
      background-color:#fff;
      border-collapse:collapse;
      border-width:1px;
      border-color:#b3b3b3;
      border-style:solid;
      color:#000;
      margin:10px 0px 0px 0px;
  }
    table.datatable td,table.datatable th{
      border-width:1px;
      border-color:#b3b3b3;
      border-style:solid;
      padding:5px;text-align:left;
      
    }
    table.datatable th{
      background-color:#666666;
      color:#ffffff;
    }
    table.datatable td.text-center,
    table.datatable th.text-center{text-align:center}
    

    .badge {
      font-size: 66%;
      font-weight: 600;
      line-height: 1;
      display: inline-block;
      padding: 0.35rem 0.375rem;
      transition: color .15s ease-in-out, background-color .15s ease-in-out, border-color .15s ease-in-out, box-shadow .15s ease-in-out;
      text-align: center;
      vertical-align: baseline;
      white-space: nowrap;
      border-radius: 0.375rem;
    }
    .badge-success {
      color: #1aae6f;
      background-color: #b0eed3;
    }
    
    .badge-danger {
      color: #f80031;
      background-color: #fdd1da;
    }

    .bg-secondary{
      background-color:#f7fafc !important
    }

    .rounded {
      border-radius: 0.375rem !important;
    }

    </style>
      <script>
        window.onafterprint = window.close;
        window.print();
      </script>
</head>
<body>

  <div class="container">
    <div class="row">
      <div class="col-md-12">
        <h3 class="text-center" style="margin:0px">LAPORAN PEMBAYARAN SPP</h3>
        <div class="mt-3 text-center">'.($hasil['tahun_ajaran'] ?? '').'</div>
      </div>

      <div class="col-md-12">
          <table class="datatable mt-3">
              <thead>
                <tr>
                  <th class="text-center" width="5">No</th>
                  <th>Nama Siswa</th>
                  <th>NISN</th>
                  <th>Kelas</th>
                  <th>Tahun Ajaran</th>
                  <th>Bulan</th>
                  <th>Jumlah</th>
                  <th>Tanggal</th>
                  <th>Petugas</th>
                  <th>Status</th>
                </tr>
              </thead>
              <tbody>';
              while ($data= $result->fetch_assoc()) {$no++;

                if ($data['status'] === 'berhasil') {
                  $total_pembayaran += (int)$data['nominal'];
                }

                $query_petugas  = "SELECT fullname FROM admin WHERE admin_id='$data[admin_id]'";
                $result_petugas = $connection->query($query_petugas);
                $data_petugas = $result_petugas->fetch_assoc();
    

                echo '
                  <tr>
                    <td class="text-center">'.$no.'</td>
                    <td>'.(isset($data['nama_lengkap']) ? $data['nama_lengkap'] : '-').'</td>
                    <td>'.(isset($data['nisn']) ? $data['nisn'] : '-').'</td>
                    <td class="text-center">'.($data['kelas'] ?? '-').'</td>
                    <td class="text-center">'.($hasil['tahun_ajaran'] ?? '-').'</td>
                    <td class="text-center">'.ambilbulan($data['bulan'] ?? '-').'</td>
                    <td>Rp '.format_angka($data['nominal'] ?? 0).'</td>
                    <td>'.(isset($data['tanggal']) 
                          ? date('d-m-Y', strtotime($data['tanggal'])) 
                          : '-').' '.$data['time'].'</td>
                    <td>'.($data_petugas['fullname']??'-').'</td>
                    <td>'.(
                          $data['status'] === 'berhasil' ? 'Berhasil' :
                          ($data['status'] === 'batal'   ? 'Batal'   :
                          ($data['status'] === 'pending' ? 'Pending' : 'Cancel'))
                        ).'</td>
                  </tr>';
              }
              echo'
              </tbody>';
              $total_tagihan = (int) $hasil['total_tagihan']?? 0;  
              $sisa_tagihan = $total_tagihan - $total_pembayaran;
              if ($sisa_tagihan <= 0) {
                  $status_pembayaran = 'LUNAS';
              } else {
                  $status_pembayaran = 'BELUM LUNAS';
              }

              echo'
              <tfoot>

              <tr class="bg-secondary font-weight-bold">
                <td colspan="8" class="text-right">Status</td>
                <td colspan="2">
                  <span class="total_pembayaran">'.$status_pembayaran.'</span>
                </td>
              </tr>

              <tr class="bg-secondary font-weight-bold">
                <td colspan="8" class="text-right">Jumlah Pembayaran</td>
                <td colspan="2">
                  <span class="total_pembayaran">Rp '.format_angka($total_pembayaran ?? 0).'</span>
                </td>
              </tr>

              <tr class="bg-secondary font-weight-bold text-info">
                <td colspan="8" class="text-right">Total Tagihan</td>
                <td colspan="2">
                  <span class="total_tagihan">Rp '.format_angka($total_tagihan ?? 0).'</span>
                </td>
              </tr>
              
              <tr class="bg-secondary font-weight-bold text-danger">
                <td colspan="8" class="text-right">Sisa Tagihan</td>
                <td colspan="2">
                  <span class="sisa_tagihan">Rp '.format_angka($sisa_tagihan ?? 0).'</span>
                </td>
              </tr>
            </tfoot>

          </table>

      </div>
    </div>
  </div>
</body>
</html>';

}else{
  echo'<div style="font-size:30px;text-align:center;margin-top:30px;">Data tidak ditemukan</div>
  <center><button onclick="window.close();" style="background:#111111;padding:8px 20px;color:#ffffff;border-radius:10px;">KEMBALI</button></center>';
}



break;
case'pdf':
require_once '../../../sw-library/fpdf/fpdf.php';

$nisn               = isset($_GET['nisn']) ? (int)$_GET['nisn'] : 0;
$master_spp         = isset($_GET['id']) ? $_GET['id'] : 0;
$hasil              = hitung_total_bayar($connection, $master_spp);

$query ="SELECT pembayaran.*, user.nama_lengkap,user.nisn,user.kelas FROM pembayaran
LEFT JOIN user ON pembayaran.user_id=user.user_id 
WHERE user.nisn='$nisn' AND pembayaran.master_spp_id='$master_spp'";
$result = $connection->query($query);
if($result->num_rows > 0){

// PDF Setup
class PDFWithFooter extends FPDF {
    function Footer() {
        $this->SetY(-15);
        $this->SetFont('Arial', 'I', 8);
        $this->Cell(0, 10, 'Halaman ' . $this->PageNo() . ' dari {nb} | Dicetak pada ' . date('d-m-Y H:i'), 0, 0, 'C');
    }
}

/** Buat Footer Dan halaman */
$pdf = new PDFWithFooter();
$pdf->AliasNbPages(); 
$pdf->AddPage('L', 'A4');


$pdf->SetFont('Arial', 'B', 14);
$pdf->Cell(0, 3, 'DATA PEMBAYARAN SPP', 0, 1, 'C');
$pdf->SetFont('Arial', '', 12);
$pdf->Cell(0, 10, ''.($hasil['tahun_ajaran'] ?? '').'', 0, 1, 'C');
$pdf->Ln(5);


// Table Header
$pdf->SetFont('Arial', 'B', 9);
$pdf->SetFillColor(200, 200, 200);
$pdf->Cell(10, 8, 'No', 1, 0, 'C', true);
$pdf->Cell(40, 8, 'Nama Siswa', 1, 0, 'C', true);
$pdf->Cell(30, 8, 'NISN', 1, 0, 'C', true);
$pdf->Cell(20, 8, 'Kelas', 1, 0, 'C', true);
$pdf->Cell(20, 8, 'Bulan', 1, 0, 'C', true);
$pdf->Cell(20, 8, 'Tahun Ajaran', 1, 0, 'C', true);
$pdf->Cell(25, 8, 'Jumlah', 1, 0, 'C', true);
$pdf->Cell(40, 8, 'Tanggal', 1, 0, 'C', true);
$pdf->Cell(40, 8, 'Petugas', 1, 0, 'C', true);
$pdf->Cell(30, 8, 'Status', 1, 1, 'C', true);

// Table Data
$pdf->SetFont('Arial', '', 9);
while ($data= $result->fetch_assoc()) {$no++;

  $query_petugas  = "SELECT fullname FROM admin WHERE admin_id='$data[admin_id]'";
  $result_petugas = $connection->query($query_petugas);
  $data_petugas = $result_petugas->fetch_assoc();

  if ($data['status'] === 'berhasil') {
    $total_pembayaran += (int)$data['nominal'];
  }

    $pdf->Cell(10, 8, $no++, 1, 0, 'C');
    $pdf->Cell(40, 8, (isset($data['nama_lengkap']) ? $data['nama_lengkap'] : '-'), 1);
    $pdf->Cell(30, 8, (isset($data['nisn']) ? $data['nisn'] : '-'), 1);
    $pdf->Cell(20, 8, (isset($data['kelas']) ? $data['kelas'] : '-'), 1);
    $pdf->Cell(20, 8, ambilbulan($data['bulan']??'-'), 1);
    $pdf->Cell(20, 8, ($hasil['tahun_ajaran'] ?? '-'), 1);
    $pdf->Cell(25, 8, 'Rp '.format_angka($data['nominal'] ?? 0), 1, 0, 'C');
    $pdf->Cell(40, 8, ''.(isset($data['tanggal']) 
                          ? date('d-m-Y', strtotime($data['tanggal'])) 
                          : '-').' '.$data['time'], 1).'';
    $pdf->Cell(40, 8, ucfirst($data_petugas['fullname'] ?? '-'), 1);
    $pdf->Cell(30, 8, (
                          $data['status'] === 'berhasil' ? 'Berhasil' :
                          ($data['status'] === 'batal'   ? 'Batal'   :
                          ($data['status'] === 'pending' ? 'Pending' : 'Cancel'))
                        ), 1);
    $pdf->SetTextColor(0, 0, 0);
    $pdf->Ln();
}

  $total_tagihan = (int) $hasil['total_tagihan']?? 0;  
  $sisa_tagihan = $total_tagihan - $total_pembayaran;

  if ($sisa_tagihan <= 0) {
    $status_pembayaran = 'LUNAS';
  } else {
    $status_pembayaran = 'BELUM LUNAS';
  }

  $pdf->SetFont('Arial','B',10);
  /* ===============================
    JUMLAH PEMBAYARAN
  ================================ */
  $pdf->SetFillColor(214,216,219);
  $pdf->Cell(205, 8, 'Status', 1, 0, 'R', true);
  $pdf->Cell(70, 8, $status_pembayaran, 1, 1, 'R', true);


  $pdf->Cell(205, 8, 'Jumlah Pembayaran', 1, 0, 'R', true);
  $pdf->Cell(70, 8, 'Rp '.format_angka($total_pembayaran ?? 0), 1, 1, 'R', true);

  /* ===============================
    TOTAL TAGIHAN
  ================================ */
  $pdf->SetTextColor(13,110,253);
  $pdf->Cell(205, 8, 'Total Tagihan', 1, 0, 'R', true);
  $pdf->Cell(70, 8, 'Rp '.format_angka($total_tagihan ?? 0), 1, 1, 'R', true);

  /* ===============================
    SISA TAGIHAN
  ================================ */
  $pdf->SetTextColor(220,53,69);
  $pdf->Cell(205, 8, 'Sisa Tagihan', 1, 0, 'R', true);
  $pdf->Cell(70, 8, 'Rp '.format_angka($sisa_tagihan ?? 0), 1, 1, 'R', true);

  // Reset warna
  $pdf->SetTextColor(0,0,0);

  $pdf->Ln(20);
  $pdf->Output('I', 'Data_pembayaran_'. $date . '.pdf');

}else{
  echo'<div style="font-size:30px;text-align:center;margin-top:30px;">Data tidak ditemukan</div>
  <center><button onclick="window.close();" style="background:#111111;padding:8px 20px;color:#ffffff;border-radius:10px;">KEMBALI</button></center>';
}



/** Download Excel */
break;
case'excel';
require '../../../sw-library/PhpSpreadsheet/autoload.php';

$nisn               = isset($_GET['nisn']) ? (int)$_GET['nisn'] : 0;
$master_spp         = isset($_GET['id']) ? $_GET['id'] : 0;
$hasil              = hitung_total_bayar($connection, $master_spp);

$query ="SELECT pembayaran.*, user.nama_lengkap,user.nisn,user.kelas FROM pembayaran
LEFT JOIN user ON pembayaran.user_id=user.user_id 
WHERE user.nisn='$nisn' AND pembayaran.master_spp_id='$master_spp'";
$result = $connection->query($query);
if($result->num_rows > 0){


$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
// Set header style
$style_col = [
  'font' => ['bold' => true],
  'alignment' => [
    'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
    'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER
  ],
  'borders' => [
    'top' => ['borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN],
    'right' => ['borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN],
    'bottom' => ['borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN],
    'left' => ['borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN]
  ],
  'fill' => [
    'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
    'startColor' => ['argb' => 'FFC0C0C0']
  ]
];

// Set row style
$style_row = [
  'alignment' => [
    'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER
  ],
  'borders' => [
    'top' => ['borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_NONE],
    'right' => ['borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_NONE],
    'bottom' => ['borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_NONE],
    'left' => ['borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_NONE]
  ]
];


  // Set judul
  $sheet->setCellValue('A1', 'LAPORAN PEMBAYARAN SPP');
  $sheet->mergeCells('A1:J1');
  $sheet->getStyle('A1')->getFont()->setBold(true)->setSize(15);

  // Tanggal
  $sheet->setCellValue('A2', ($hasil['tahun_ajaran'] ?? '-'));
  $sheet->mergeCells('A2:J2');
  

  // Mengatur alignment (ratakan ke tengah)
  $sheet->getStyle('A1')->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
  $sheet->getStyle('A2')->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);

  // Tambahkan style untuk font dan ukuran teks
  $sheet->getStyle('A1')->getFont()->setBold(true)->setSize(15);
  $sheet->getStyle('A2')->getFont()->setSize(12);
  
  // Header tabel
  $headers = ['No', 'Nama Siswa', 'NISN', 'Kelas', 'Bulan', 
  'Tahun Ajaran', 'Jumlah', 'Tanggal', 'Petugas', 'Status'
  ];
  $sheet->fromArray($headers, NULL, 'A4');

  $col = 'A';
  foreach ($headers as $header) {
    $sheet->setCellValue($col . '3', $header);
    $sheet->getStyle($col . '3')->applyFromArray($style_col);
    $sheet->getColumnDimension($col)->setAutoSize(14); // Set auto width for each column
    $col++;
  }

  $row = 4;
  $no = 0;
while ($data= $result->fetch_assoc()) {$no++;

  $query_petugas  = "SELECT fullname FROM admin WHERE admin_id='$data[admin_id]'";
  $result_petugas = $connection->query($query_petugas);
  $data_petugas = $result_petugas->fetch_assoc();

    if ($data['status'] === 'berhasil') {
      $total_pembayaran += (int)$data['nominal'];
    }
   
    $sheet->setCellValue('A' . $row, $no++);
    $sheet->setCellValue(
        'B' . $row,
        isset($data['nama_lengkap']) ? $data['nama_lengkap'] : '-'
    );

    $sheet->setCellValue(
        'C' . $row,
        isset($data['nisn']) ? $data['nisn'] : '-'
    );

    $sheet->setCellValue(
        'D' . $row,
        ($data['kelas'] ?? '-') . ' ' . ($hasil['kelas'] ?? '')
    );

    $sheet->setCellValue(
        'E' . $row,
        ambilbulan($data['bulan'] ?? '-') . ' ' .ambilbulan($hasil['bulan'] ?? '')
    );

    $sheet->setCellValue(
        'F' . $row,
        ($hasil['tahun_ajaran'] ?? '-')
    );

    $sheet->setCellValue(
        'G' . $row,
        'Rp ' . format_angka($data['nominal'] ?? 0)
    );
    $sheet->setCellValue(
        'H' . $row,
        isset($data['tanggal'])
            ? date('d-m-Y H:i:s', strtotime($data['tanggal']))
            : '-'
    );

    $sheet->setCellValue(
        'I' . $row,
        ucfirst($data_petugas['fullname'] ?? '-')
    );

    $sheet->setCellValue(
        'J' . $row,
        $data['status'] === 'berhasil' ? 'Berhasil' :
        ($data['status'] === 'batal'   ? 'Batal'   :
        ($data['status'] === 'pending' ? 'Pending' : 'Cancel'))
    );
    $row++;
  }


  // Helper footer row
  function footerRow($sheet, $row, $label, $value, $color = '000000') {

      // Label dari A sampai I
      $sheet->mergeCells("A{$row}:I{$row}");

      // Isi label
      $sheet->setCellValue("A{$row}", $label);

      // Isi value di kolom J
      $sheet->setCellValue("J{$row}", $value);

      $sheet->getStyle("A{$row}:J{$row}")->applyFromArray([
          'font' => [
              'bold' => true,
              'color' => ['rgb' => $color]
          ],
          'alignment' => [
              'horizontal' => Alignment::HORIZONTAL_RIGHT
          ],
          'fill' => [
              'fillType' => Fill::FILL_SOLID,
              'startColor' => ['rgb' => 'D6D8DB']
          ]
      ]);

      // Format angka
      if (is_numeric($value)) {
          $sheet->getStyle("J{$row}")
              ->getNumberFormat()
              ->setFormatCode('#,##0');
      }
  }

  $total_tagihan = (int) $hasil['total_tagihan']?? 0;  
  $sisa_tagihan = $total_tagihan - $total_pembayaran;


  if ($sisa_tagihan <= 0) {
    $status_pembayaran = 'LUNAS';
  } else {
    $status_pembayaran = 'BELUM LUNAS';
  }

  $footerRow = $row;
  footerRow($sheet, $footerRow++, 'Status', $status_pembayaran ?? 0);
  footerRow($sheet, $footerRow++, 'Jumlah Pembayaran', $total_pembayaran ?? 0);
  footerRow($sheet, $footerRow++, 'Total Tagihan', $total_tagihan ?? 0, '0D6EFD');
  footerRow($sheet, $footerRow++, 'Sisa Tagihan', $sisa_tagihan ?? 0, 'DC3545');

  // Atur lebar kolom
  foreach (range('A', 'J') as $col) {
    $sheet->getColumnDimension($col)->setAutoSize(true);
  }

  $lastColIndex = count($headers);
  $lastColumn = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($lastColIndex);
  $lastRow = $row - 1; // baris terakhir yang berisi data
  $range = 'A3:' . $lastColumn . $lastRow;
  $borderStyle = [
      'borders' => [
          'allBorders' => [
              'borderStyle' => Border::BORDER_THIN,
              'color' => ['argb' => 'FF000000']
          ]
      ]
  ];

  $sheet->getStyle($range)->applyFromArray($borderStyle);
  $sheet->getPageSetup()->setOrientation(\PhpOffice\PhpSpreadsheet\Worksheet\PageSetup::ORIENTATION_LANDSCAPE);

  // Simpan sebagai file Excel
  header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  header('Content-Disposition: attachment;filename="Data_pembayaran' . $date. '.xlsx"');
  header('Cache-Control: max-age=0');

  $writer = new Xlsx($spreadsheet);
  $writer->save('php://output');
  exit;

} else {
    echo '<div style="font-size:30px;text-align:center;margin-top:30px;">Data tidak ditemukan</div>
    <center><button onclick="window.close();" style="background:#111111;padding:8px 20px;color:#ffffff;border-radius:10px;">KEMBALI</button></center>';
}

break;
case'print-struk':
$idId = isset($_GET['id']) ? convert('decrypt', $_GET['id']) : null;
if (!$idId) {
    echo 'Order ID tidak ditemukan!';
    exit;
}

$query ="SELECT pembayaran.*, user.nama_lengkap,user.nisn,user.kelas FROM pembayaran
LEFT JOIN user ON pembayaran.user_id=user.user_id WHERE pembayaran_id='$idId'";
$result = $connection->query($query);
if($result->num_rows > 0){
$data = $result->fetch_assoc();
        
echo'
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Struk Pembayaran SPP</title>
    <style>
       body {
        font-family: Arial, sans-serif;
        font-size: 12px;
        width: 500px;
        margin: 0 auto;
    }
    .struk {
        border: 1px solid #000;
        padding: 10px;
    }
    .struk h2, .struk h3 {
        text-align: center;
        margin: 5px 0;
    }
    .line {
        border-bottom: 1px dashed #000;
        margin: 5px 0;
    }
    .info, .items, .footer {
        width: 100%;
        margin-bottom: 5px;
    }
    .info td, .items td {
        padding: 5px 0;
    }
    .items td:nth-child(2), .items td:nth-child(3) {
        text-align: right;
    }
    .total {
        font-weight: bold;
        border-top: 1px solid #000;
        padding-top: 3px;
    }
    .footer {
        text-align: center;
        font-size: 11px;
    }
    </style>';?>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"
    integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>
<script>
$(document).ready(function($) {
    var ua = navigator.userAgent.toLowerCase();
    var isAndroid = ua.indexOf("android") > -1;

    if (isAndroid) {
        var gadget = new cloudprint.Gadget();
        gadget.setPrintDocument("url", $('title').html(), window.location.href, "utf-8");
        gadget.openPrintDialog();
    } else {
        window.print();
        window.onafterprint = window.close;
    }
    return false;

});
</script>
</head>

<body>
    <?php
echo'
<div class="struk">
    <h2>'.$row_site['site_name'].'</h2>
    <h3>STRUK PEMBAYARAN SPP</h3>
    <div class="line"></div>

    <table class="items">
        <tr>
            <td>NISN</td>
            <td>:</td>
            <td>'.$data['nisn'].'</td>
        </tr>
        <tr>
            <td>Nama</td>
            <td>:</td>
            <td>'.$data['nama_lengkap'].'</td>
        </tr>
        <tr>
            <td>Kelas</td>
            <td>:</td>
            <td>'.$data['kelas'].'</td>
        </tr>
    </table>

    <div class="line"></div>

    <table class="items">
        <tr>
            <td>Jumlah</td>
            <td>:</td>
            <td>Rp '.format_angka($data['nominal']??'-').'</td>
        </tr>

        <tr>
            <td>Bulan</td>
            <td>:</td>
            <td>'.ambilbulan($data['bulan']??'-').'</td>
        </tr>

                <tr>
            <td>Tanggal</td>
            <td>:</td>
            <td>'.tgl_indo($data['tanggal']??'-').'</td>
        </tr>

        <tr class="total">
            <td>Total Bayar</td>
            <td>:</td>
            <td>Rp '.format_angka($data['nominal']??'-').'</td>
        </tr>
    </table>

    <div class="line"></div>

    <div class="footer">
        Terima kasih telah melakukan pembayaran
    </div>
</div>

</body>
</html>';
}else{
  echo 'Tidak ada data data pembayaran!.';
}

  break;
}
}?>