<head>
  <script src="https://cdn.jsdelivr.net/npm/html2canvas@1.4.1/dist/html2canvas.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/jspdf@2.5.1/dist/jspdf.umd.min.js"></script>
</head>


<div id="layoutSidenav_content">
  <main>
    <div class="container-fluid px-4">
      <h1 class="mt-4" id="judulPasien">Dashboard Layanan</h1>
      <ol class="breadcrumb mb-4">
        <li class="breadcrumb-item"><a href="index.html">Dashboard</a></li>
        <li class="breadcrumb-item active">Tampil Info Dashboard Layanan</li>
      </ol>
      <!-- Filter Tanggal -->
      <div class="card mb-4">
        <div class="card-body">
          <form action="<?= base_url('dashboardlayanan/filter_data') ?>" method="POST" name="form-input-data">
            <div class="row">
              <div class="col-md-4">
                <div class="form-group">
                  <label for="tgl_awal">Tanggal Awal</label>
                  <input type="date" id="tanggal_awal" name="tanggal_awal" class="form-control" value="<?= $tglawal ?>" required />
                </div>
              </div>
              <!-- <div class="col-md-4">
                <div class="form-group">
                  <label for="tgl_akhir">Tanggal AKhir</label>
                  <input type="date" id="tanggal_akhir" name="tanggal_akhir" class="form-control" value="<?= $tglakhir ?>" required />
                </div>
              </div> -->
            </div>
            <div class="col-12">
              <button style="margin-top: 20px;" type="submit" class="btn btn-primary btn-block">Filter</button>
            </div>
            <!-- <button style="margin-top: 20px;" id="export-btn">Export to JPG</button> -->
          </form>
        </div>
      </div>

      <!-- TAMPIL TABEL RAWAT JALAN  -->
      <h2 style="margin-top: 10px;">Rawat Jalan</h2>
      <div class="row">
        <div class="col-md-6">
          <div class="card">
            <div class="card-header">
              <h3 style="text-align: center;" class="card-title">Data Pasien Poli Lama-Baru</h3>
            </div>
            <div class="card-body">
              <div class="table-responsive">
                <table id="example2" class="table table-striped table-bordered table-hover">
                  <thead>
                    <tr>
                      <th>Nama Pendaftaran</th>
                      <th>Dilayani</th>
                      <th>Batal</th>
                      <th>Total</th>
                    </tr>
                  </thead>
                  <tbody>
                    <?php
                    $no = 1;
                    if (isset($pasienpolilamabaru->response)) {
                      foreach ($pasienpolilamabaru->response as $a) {
                    ?>
                        <tr>
                          <td><?= $a->nm_pendaftaran ?></td>
                          <td><?= $a->dilayani ?></td>
                          <td><?= $a->batal ?></td>
                          <td><?= $a->total ?></td>
                        </tr>
                      <?php
                      }
                    } else {
                      ?>
                      <tr>
                        <td colspan="2" style="text-align: center;">Data tidak ditemukan</td>
                      <tr>
                      <?php
                    }
                      ?>
                  </tbody>
                </table>
              </div>
            </div>
          </div>
        </div>

        <div class="col-md-6">
          <div class="card">
            <div class="card-header">
              <h3 style="text-align: center;" class="card-title">Pasien Rajal Total</h3>
            </div>
            <div class="card-body">
              <div class="table-responsive">
                <table id="example2" class="table table-striped table-bordered table-hover">
                  <thead>
                    <tr>
                      <th>Daftar</th>
                      <th>Dilayani</th>
                      <th>Batal</th>
                    </tr>
                  </thead>
                  <tbody>
                    <?php
                    $no = 1;
                    if (isset($pasienrajaltotal->response)) {
                      foreach ($pasienrajaltotal->response as $a) {
                    ?>
                        <tr>
                          <td><?= $a->daftar ?></td>
                          <td><?= $a->terlayani ?></td>
                          <td><?= $a->batal ?></td>
                        </tr>
                      <?php
                      }
                    } else {
                      ?>
                      <tr>
                        <td colspan="2" style="text-align: center;">Data tidak ditemukan</td>
                      <tr>
                      <?php
                    }
                      ?>
                  </tbody>
                </table>
              </div>
            </div>
          </div>
        </div>
      </div>

      <div style="margin-top: 15px;" class="card">
        <div class="card-header">
          <h3 style="text-align: center;" class="card-title">Pasien Rajal</h3>
        </div>
        <div class="card-body">
          <div class="table-responsive">
            <table id="example2" class="table table-bordered table-hover">
              <thead>
                <tr style="text-align: center;">
                  <th>Nama</th>
                  <th>Daftar</th>
                  <th>Terlayani</th>
                  <th>Batal</th>
                </tr>
              </thead>
              <tbody>
                <?php
                $no = 1;
                if (isset($pasienrajal->response)) {
                  foreach ($pasienrajal->response as $a) {
                ?>
                    <tr>
                      <td><?= $a->nm_ruang ?></td>
                      <td><?= $a->daftar ?></td>
                      <td><?= $a->terlayani ?></td>
                      <td><?= $a->batal ?></td>
                    </tr>
                  <?php
                  }
                } else {
                  ?>
                  <tr>
                    <td colspan="2" style="text-align: center;">Data tidak ditemukan</td>
                  <tr>
                  <?php
                }
                  ?>
              </tbody>
            </table>
          </div>
        </div>
      </div>


      <!-- END OF TABLE RAWAT JALAN -->

      <h2 style="margin-top: 30px;">Rawat Inap</h2>
      <div class="row">
        <div class="col-md-6">
          <div class="card">
            <div class="card-header">
              <h3 style="text-align: center;" class="card-title">Data Pasien Poli Lama-Baru</h3>
            </div>
            <div class="card-body">
              <div class="table-responsive">
                <table id="example2" class="table table-striped table-bordered table-hover">
                  <thead>
                    <tr>
                      <th>Nama Kelas</th>
                      <th>Jumlah</th>
                    </tr>
                  </thead>
                  <tbody>
                    <?php
                    $no = 1;
                    if (isset($pasienranap->response)) {
                      foreach ($pasienranap->response as $a) {
                    ?>
                        <tr>
                          <td><?= $a->nm_kelas ?></td>
                          <td><?= $a->qty ?></td>
                        </tr>
                      <?php
                      }
                    } else {
                      ?>
                      <tr>
                        <td colspan="2" style="text-align: center;">Data tidak ditemukan</td>
                      <tr>
                      <?php
                    }
                      ?>
                  </tbody>
                </table>
              </div>
            </div>
          </div>
        </div>

        <div class="col-md-6">
          <div class="card">
            <div class="card-header">
              <h3 style="text-align: center;" class="card-title">Pasien Rajal Total</h3>
            </div>
            <div class="card-body">
              <div class="table-responsive">
                <table id="example2" class="table table-striped table-bordered table-hover">
                  <thead>
                    <tr>
                      <td style="font-weight: bold; text-align: center; ">Total</td>
                    </tr>
                  </thead>
                  <tbody>
                    <?php
                    $no = 1;
                    if (isset($pasienranaptotal->response)) {
                      foreach ($pasienranaptotal->response as $a) {
                    ?>
                        <tr style="text-align: center;">
                          <th><?= $a->jumlah ?></th>
                        </tr>
                      <?php
                      }
                    } else {
                      ?>
                      <tr>
                        <td colspan="2" style="text-align: center;">Data tidak ditemukan</td>
                      <tr>
                      <?php
                    }
                      ?>
                  </tbody>
                </table>
              </div>
            </div>
          </div>
        </div>
      </div>

      <div style="margin-top: 10px;" class="card">
        <div class="card-header">
          <h3 style="text-align: center;" class="card-title">Ketersedian Tempat Tidur</h3>
        </div>
        <div class="card-body">
          <div class="table-responsive">
            <table id="example2" class="table table-bordered table-hover">
              <thead>
                <tr style="text-align: center;">
                  <th>Nama Ruang</th>
                  <th>Kelas</th>
                  <th>Jumlah Bed</th>
                  <th>Tersedia</th>
                  <th>Terisi</th>
                </tr>
              </thead>
              <tbody>
                <?php
                $no = 1;
                if (isset($ketersediantt->response)) {
                  foreach ($ketersediantt->response as $a) {
                ?>
                    <tr style="text-align: center;">
                      <td><?= $a->ruang ?></td>
                      <td><?= $a->nm_kelas ?></td>
                      <td><?= $a->jumlah_bed ?></td>
                      <td><?= $a->tersedia ?></td>
                      <td><?= $a->terisi ?></td>
                    </tr>
                  <?php
                  }
                } else {
                  ?>
                  <tr>
                    <td colspan="2" style="text-align: center;">Data tidak ditemukan</td>
                  <tr>
                  <?php
                }
                  ?>
              </tbody>
            </table>
          </div>
        </div>
      </div>

      <!-- END OF TAMPIL TABEL RANAP  -->


      <!-- TAMPIL TABEL IGD  -->

      <h2 style="margin-top: 30px;">Instalasi Gawat Darurat-IGD</h2>
      <div class="card">
        <div class="card-header">
          <h3 style="text-align: center;" class="card-title">Pasien IGD Total</h3>
        </div>
        <div class="card-body">
          <table id="example2" class="table table-bordered table-hover">
            <thead>
              <tr style="text-align: center;">
                <th>Dilayani</th>
                <th>Batal</th>
                <th>Total</th>
              </tr>
            </thead>
            <tbody>
              <?php
              $no = 1;
              if (isset($pasienigdtotal->response)) {
                foreach ($pasienigdtotal->response as $a) {
              ?>
                  <tr style="text-align: center;">
                    <td><?= $a->dilayani ?></td>
                    <td><?= $a->batal ?></td>
                    <td><?= $a->total_igd ?></td>
                  </tr>
                <?php
                }
              } else {
                ?>
                <tr>
                  <td colspan="2" style="text-align: center;">Data tidak ditemukan</td>
                <tr>
                <?php
              }
                ?>
            </tbody>
          </table>
        </div>
      </div>

      <div style="margin-top: 10px;" class="row">
        <div class="col-md-6">
          <div class="card">
            <div class="card-header">
              <h3 style="text-align: center;" class="card-title">Data Jenis Pasien</h3>
            </div>
            <div class="card-body">
              <div class="table-responsive">
                <table id="example2" class="table table-striped table-bordered table-hover">
                  <thead>
                    <tr>
                      <th>Jenis Pasien</th>
                      <th>Dilayani</th>
                      <th>Batal</th>
                      <th>Total</th>
                    </tr>
                  </thead>
                  <tbody>
                    <?php
                    $no = 1;
                    if (isset($pasienigd->response)) {
                      foreach ($pasienigd->response as $a) {
                    ?>
                        <tr>
                          <td><?= $a->nm_jenis_pasien ?></td>
                          <td><?= $a->dilayani ?></td>
                          <td><?= $a->batal ?></td>
                          <td><?= $a->total ?></td>
                        </tr>
                      <?php
                      }
                    } else {
                      ?>
                      <tr>
                        <td colspan="2" style="text-align: center;">Data tidak ditemukan</td>
                      <tr>
                      <?php
                    }
                      ?>
                  </tbody>
                </table>
              </div>
            </div>
          </div>
        </div>
        <div class="col-md-6">
          <div class="card">
            <div class="card-header">
              <h3 style="text-align: center;" class="card-title">Pasien IGD Lama Baru</h3>
            </div>
            <div class="card-body">
              <div class="table-responsive">
                <table id="example2" class="table table-striped table-bordered table-hover">
                  <thead>
                    <tr>
                      <th>Nama Pendaftarn</th>
                      <th>Dilyani</th>
                      <th>Batal</th>
                      <th>Total</th>
                    </tr>
                  </thead>
                  <tbody>
                    <?php
                    $no = 1;
                    if (isset($pasienigdlamabaru->response)) {
                      foreach ($pasienigdlamabaru->response as $a) {
                    ?>
                        <tr>
                          <td><?= $a->nm_pendaftaran ?></td>
                          <td><?= $a->dilayani ?></td>
                          <td><?= $a->batal ?></td>
                          <td><?= $a->total ?></td>
                        </tr>
                      <?php
                      }
                    } else {
                      ?>
                      <tr>
                        <td colspan="2" style="text-align: center;">Data tidak ditemukan</td>
                      <tr>
                      <?php
                    }
                      ?>
                  </tbody>
                </table>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    </footer>
</div>
</div>



<!-- END OF TAMPIL TABEL IGD  -->

<!-- EXPORT TO JPG  -->
<script>
  document.getElementById('export-btn').addEventListener('click', function() {
    exportToJPG();
  });

  function exportToJPG() {
    var tables = document.querySelectorAll('table');
    var doc = new jsPDF();
    var width = 595; // Ukuran kertas A4 (595 x 842)
    var height = 842;
    var x = 0;
    var y = 0;

    tables.forEach(function(table) {
      var tableWidth = table.offsetWidth;
      var tableHeight = table.offsetHeight;

      // Menggunakan html2canvas untuk menangkap tabel
      html2canvas(table).then(function(canvas) {
        var imgData = canvas.toDataURL('image/jpeg');
        doc.addImage(imgData, 'JPEG', x, y, tableWidth, tableHeight);
        y += tableHeight + 20; // Tambahkan jarak antara tabel
        if (y > height) {
          x += tableWidth + 20; // Tambahkan jarak antara tabel
          y = 0;
        }
      });
    });

    doc.save('tabel.pdf');

    // Konversi PDF ke JPG
    var pdf = new jsPDF();
    pdf.addImage(imgData, 'JPEG', 0, 0, width, height);
    var dataURL = pdf.output('blob');
    var link = document.createElement('a');
    link.href = URL.createObjectURL(dataURL);
    link.download = 'tabel.jpg';
    link.click();
  }
</script>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.2/dist/js/bootstrap.bundle.min.js"></script>

<!-- Jquery -->
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<!-- DataTables JS -->
<script src="https://cdn.datatables.net/1.13.4/js/jquery.dataTables.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
<script src="<?php echo base_url('assets_/js/scripts.js'); ?>"></script>
<script src="https://cdn.jsdelivr.net/npm/simple-datatables@7.1.2/dist/umd/simple-datatables.min.js" crossorigin="anonymous"></script>
<script src="<?php echo base_url('assets_/js/datatables-simple-demo.js'); ?>"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

</body>

</html>