<?php
//
require_once 'vendor/autoload.php';
include('class/Database.php');
include "vendor/phpqrcode/qrlib.php";
//
use PhpOffice\PhpWord\TemplateProcessor;
//
$object = new Database;
if(!$object->is_login())
{
    header("location:".$object->base_url);
}
?>
<!DOCTYPE html>
<html lang="en">
<?php 
include('header.php'); 
$success='';
$error='';
//

//
$id_satker='';
$object->query = "SELECT id_satker FROM akun_user WHERE id_user=".$_SESSION['id_akun']." ";
$result3 = $object->get_result();
foreach ($result3 as $row) {
  $id_satker = $row["id_satker"];
}
//
if($id_satker==0){
  $main_query = " SELECT * FROM tujuan_detail INNER JOIN akun_user, surat_keluar, satuan_kerja, tanda_tangan WHERE id_tujuan = akun_user.id_user AND tujuan_detail.id_surat = surat_keluar.no_urut AND satuan_kerja.id_satker=surat_keluar.satker AND tanda_tangan.id_pejabat=surat_keluar.ttd AND surat_keluar.status=4 ORDER BY surat_keluar.id_surat DESC";
}
else{
  $main_query = " SELECT * FROM tujuan_detail INNER JOIN akun_user, surat_keluar, satuan_kerja, tanda_tangan WHERE id_tujuan = akun_user.id_user AND tujuan_detail.id_surat = surat_keluar.no_urut AND satuan_kerja.id_satker=surat_keluar.satker AND tanda_tangan.id_pejabat=surat_keluar.ttd AND id_tujuan = ".$_SESSION['id_akun']." AND  surat_keluar.status=4 ORDER BY surat_keluar.id_surat DESC";
} 
//
$status='';
$object->query = $main_query;
$result = $object->get_result();
foreach($result as $row)
{
  $status= $row["status"];
}
?>

<body class="hold-transition sidebar-mini">
<div class="wrapper">
<!-- Navbar -->
<?php include('navbar.php'); ?>
<!-- /.navbar -->
<!-- Main Sidebar Container -->
<?php include('sidebar.php'); ?>
  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <div class="container-fluid">
        <!--div class="row mb-2">
          <div class="col-sm-6">
            <h1>Surat Keluar</h1>
          </div>
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="#">Home</a></li>
              <li class="breadcrumb-item active">Surat Keluar</li>
            </ol>
          </div>
        </div-->
      </div><!-- /.container-fluid -->
    </section>

    <!-- Main content -->
    <section class="content">
      <div class="container-fluid">
        <div class="row">
          <div class="col-12">
            <div class="card">
              <div class="card-header">
                <div class="row mb-2">
                  <div class="col-sm-6">
                    <h1>Surat Masuk </h1>
                  </div>
                  <div class="col-sm-6">
                    <ol class=" float-sm-right">
                      <!--a href="form_surat_keluar.php"><button type="button" class="btn btn-primary" >Tambah Surat Keluar</button></a-->
                    </ol>
                  </div>
              </div>
               
                <!--h3 class="card-title">DataTable with default features</h3-->
              </div>
              <!-- /.card-header -->
              <div class="card-body">
                <table id="example1" class="table table-bordered table-hover">
                  <thead>
                  <tr>
                    <th>jenis naskah</th>
                    <th>nomor surat</th>
                    <th>perihal</th>
                    <th>satuan kerja</th>
                    <th>tanggal</th>
                    <th>penanda tangan</th>
                    <th>file</th>
                  </tr>
                  </thead>
                  <tbody>
                  <?php
                    $object->query = $main_query;
                    $result = $object->get_result();
                    foreach($result as $row)
                    {
                      echo '<tr><td>'.$row["jenis_naskah"].'</td>';
                      echo '<td>'.$row["nomor"].'</td>';
                      echo '<td>'.$row["perihal"].'</td>';
                      echo '<td>'.$row["satuan_kerja"].'</td>';
                      echo '<td>'.$row["tanggal"].'</td>';
                      echo '<td>'.$row["tanda_tangan"].'</td>';
                      echo '<td><center><a href="'.$row["file_pdf"].'"><i class="fas fa-file-pdf" style="font-size:36px;color:red"></i></a></center></td></tr>';
                    
                    }
                  ?>
                  </tbody>
                </table>
              </div>
              <!-- /.card-body -->
            </div>
            <!-- /.card -->
          </div>
          <!-- /.col -->
        </div>
        <!-- /.row -->
      </div>
      <!-- /.container-fluid -->
    </section>
    <!-- /.content -->
  </div>
  <!-- /.content-wrapper -->
  
<?php include('footer.php'); ?>
<!-- Page specific script -->
<script type="text/javascript" language="javascript" >
 $(document).ready(function(){
    var dataTable = $("#example1").DataTable({
      "buttons": ["excel", "pdf", "print", "colvis"],
    }).buttons().container().appendTo('#example1_wrapper .col-md-6:eq(0)');

    //
    $('.edit_button').click(function() {
      var no_urut = $(this).data('id');
      var kode_naskah = $(this).data('status');
      $.ajax({        
        success: function (response) {
          //
            my_form=document.createElement('FORM');
            my_form.name='myForm';
            my_form.method='POST';
            my_form.action='form_surat_keluar.php';

            my_tb=document.createElement('INPUT');
            my_tb.type='TEXT';
            my_tb.name='myInput';
            my_tb.value= no_urut;
            my_form.appendChild(my_tb);
            my_tb2=document.createElement('INPUT');
            my_tb2.type='TEXT';
            my_tb2.name='action';
            my_tb2.value= 'upload_pdf';
            my_form.appendChild(my_tb2);
            document.body.appendChild(my_form);
            my_form.submit();
            },
            error: function(e){
                alert('Error: '+e);
            }  
        });
    });
    $('.status_button').click(function() {
      var no_urut = $(this).data('id');
      var kode_naskah = $(this).data('status');
      if(confirm("Apa Anda Yakin ingin memberi nomor?"))
      {
        $.ajax({        
        success: function (response) {
          //
            my_form=document.createElement('FORM');
            my_form.name='myForm';
            my_form.method='POST';
            my_form.action='tables_surat_keluar.php';

            my_tb=document.createElement('INPUT');
            my_tb.type='TEXT';
            my_tb.name='action';
            my_tb.value= 'ubah_nomor';
            my_form.appendChild(my_tb);
            my_tb2=document.createElement('INPUT');
            my_tb2.type='TEXT';
            my_tb2.name='id';
            my_tb2.value= no_urut;
            my_form.appendChild(my_tb2);
            my_tb3=document.createElement('INPUT');
            my_tb3.type='TEXT';
            my_tb3.name='kode_naskah';
            my_tb3.value= kode_naskah;
            my_form.appendChild(my_tb3);
            document.body.appendChild(my_form);
            my_form.submit();
            },
            error: function(e){
                alert('Error: '+e);
            }  
        });
        //alert("Kasih nomor "+no_urut);
      }
    });

});
 <?php
  if(isset($_POST["action"]))
  {
  if($_POST["action"] == 'add_surat')  echo "toastr.success('Data berhasil disimpan.'); ";
  elseif($_POST["action"] == 'edit_surat')  
    {
      if($error!=''){
        echo "toastr.error('".$error."'); ";
      } else  echo "toastr.success('".$success."'); ";
    }
  elseif($_POST["action"] == 'upload_pdf')  
    {
      if($error!=''){
        echo "toastr.error('".$error."'); ";
      } else  echo "toastr.success('".$success."'); ";
    }
  elseif($_POST["action"] == 'ubah_nomor')  
    {
      if($error!=''){
        echo "toastr.error('".$error."'); ";
      } else  echo "toastr.success('".$success."'); ";
    }
  }
 ?>
</script>
</body>
</html>
