<?php
//
require_once 'vendor/autoload.php';
include('class/Database.php');
$object = new Database;
if(!$object->is_login())
{
    header("location:".$object->base_url);
}
?>
<!DOCTYPE html>
<html lang="en">
<?php 
include('header.php');
$success='';
$error='';
//
//
use PhpOffice\PhpWord\TemplateProcessor;
//
//
if(isset($_POST["action"]))
  {
    if($_POST["action"] == 'add_surat'){
      //Tambah Surat
      //
      $main_query="SELECT * FROM tanda_tangan INNER JOIN akun_user, jabatan WHERE id_pejabat=akun_user.id_user AND akun_user.id_jabatan = jabatan.id_jabatan AND id_pejabat='".$_POST["tanda_tangan"]."' ";
      $object->query = $main_query ;
      $result = $object->get_result();
      $kode_ttd='';
      $jabatan='';
      $nama_pejabat='';
      foreach ($result as $row) {
        $kode_ttd = $row["kode_ttd"];
        $jabatan  = $row["nama_jabatan"];
        $nama_pejabat= $row["nama"];
      }
      $template_docx='';
      //
      $bulan = array (
      1 =>   'Januari','Februari','Maret','April','Mei','Juni','Juli','Agustus','September','Oktober','November','Desember' );
      $pecahkan = explode('-', date("Y-m-d"));
      //
      $array_bln  = array(1=>"I","II","III", "IV", "V","VI","VII","VIII","IX","X", "XI","XII");
      $bln    = $array_bln[date('n')];
      //
      $format_nomor='';
      //
      IF($_POST["jenis_naskah"]=='SDinas'){
        $format_nomor = 
        'B-     /'.$bln.'/'.$kode_ttd.'/'.$_POST["kode_klasifikasi"].'/'. date("Y").'/BNN';
        $template_docx='../docx/result_SDinas'.date("ymhis").'.docx';
        } 
      elseif($_POST["jenis_naskah"]=='ND'){
        $format_nomor = 
        'B/ND-     /'.$bln.'/'.$kode_ttd.'/'.$_POST["kode_klasifikasi"].'/'. date("Y").'/BNN';
        $template_docx='../docx/result_ND'.date("ymhis").'.docx';
        }
      elseif($_POST["jenis_naskah"]=='SE'){
        $format_nomor = 
        $_POST["jenis_naskah"].'/     /'.$bln.'/'.$kode_ttd.'/'.$_POST["kode_klasifikasi"].'/'. date("Y").'/BNN';
        $template_docx='../docx/result_SE'.date("ymhis").'.docx';
        }
      elseif($_POST["jenis_naskah"]=='Sprin1'){
        $format_nomor = 
        'Sprin/     /'.$bln.'/'.$kode_ttd.'/'.$_POST["kode_klasifikasi"].'/'. date("Y").'/BNN';
        $template_docx='../docx/result_Sprin'.date("ymhis").'.docx';
        }
      elseif($_POST["jenis_naskah"]=='Sgas1'){
        $format_nomor = 
        'Sgas/     /'.$bln.'/'.$kode_ttd.'/'.$_POST["kode_klasifikasi"].'/'. date("Y").'/BNN';
        $template_docx='../docx/result_Sgas'.date("ymhis").'.docx';
        }
        //
        /*
        $object->query ="INSERT INTO surat_keluar (jenis_naskah, nomor, perihal, tujuan, klasifikasi, satker, tanggal, ttd, id_pejabat, template, template_tte, status) VALUES ('".$_POST["jenis_naskah"]."', '".$format_nomor."', '".$_POST["perihal"]."', '', '".$_POST["kode_klasifikasi"]."', '-', '".$_POST["tanggal"]."', '".$_POST["tanda_tangan"]."', '2', '".$template_docx."', '', '0') ";
        $object->execute();*/
        //echo '<script>alert( "Form successful submitted! '.$format_nomor.'");</script>';
        //
        //ubah nomor
        $main_query="SELECT COUNT(id_surat)+1 as nomor FROM surat_keluar WHERE YEAR(tanggal)=YEAR(CURDATE()) ";
        $object->query = $main_query ;
        $result = $object->get_result();
        $nomor='';
        foreach ($result as $row) {
          $nomor = $row["nomor"].date("Y");
        }
        //input surat
        $object->query ="INSERT INTO surat_keluar (no_urut, jenis_naskah, nomor, perihal, tujuan, klasifikasi, satker, tanggal, ttd, id_pejabat, template, template_tte, status) VALUES ('".$nomor."','".$_POST["jenis_naskah"]."', '".$format_nomor."', '".$_POST["perihal"]."', '', '".$_POST["kode_klasifikasi"]."', '-', '".$_POST["tanggal"]."', '".$_POST["tanda_tangan"]."', '2', '".$template_docx."', '', '0') ";
        $object->execute();
        //input verifikator
         for ($x = 1; $x <= count($_POST["verifikator"]); $x++) {
          //echo '<script>alert( "Form successful submitted! '.$nomor.'");</script>';
          $object->query ="INSERT INTO verifikator_detail (id_verifikator, id_surat,status, tanggal) VALUES ('".$_POST["verifikator"][$x-1]."','".$nomor."',0,'".$_POST["tanggal"]."')";
          $object->execute();
          $object->query ="INSERT INTO histori_surat (id_verifikator, id_surat,status, tanggal) VALUES ('".$_POST["verifikator"][$x-1]."','".$nomor."',0, '".$_POST["tanggal"]."')";
          $object->execute();
        }
        //input tujuan
        for ($x = 1; $x <= count($_POST["tujuan"]); $x++) {
          $object->query ="INSERT INTO tujuan_detail (id_tujuan, id_surat, tanggal) VALUES ('".$_POST["tujuan"][$x-1]."','".$nomor."','".$_POST["tanggal"]."')";
          $object->execute();
          //echo '<script>alert( "Form successful submitted! '.($_POST["tujuan"][$x-1]).'");</script>';
        }
        //
      //echo '<script>alert( "Form successful submitted! '.implode(", ",$_POST["verifikator"]).' '.$_POST["action"].' '.$_POST["jenis_naskah"].' '.$_POST["kode_klasifikasi"].' '.$_POST["tanggal"].' '.$_POST["tanda_tangan"].' '.$_POST["perihal"].'");</script>';
    }
    elseif($_POST["action"] == 'ubah_nomor'){
      //ubah nomor
      $main_query="SELECT COUNT(id_surat)+1 as nomor FROM surat_keluar WHERE YEAR(tanggal)=YEAR(CURDATE()) AND jenis_naskah='".$_POST["kode_naskah"]."' ";
      $object->query = $main_query ;
      $result = $object->get_result();
      $nomor='';
      foreach ($result as $row) {
        $nomor = $row["nomor"];
      }
      //masih salah query
      $main_query2 = "
        SELECT klasifikasi,satuan_kerja,tanggal,kode_ttd FROM surat_keluar INNER JOIN jenis_naskah,satuan_kerja,tanda_tangan WHERE surat_keluar.jenis_naskah=jenis_naskah.kode_naskah AND satuan_kerja.kode_satker=surat_keluar.satker AND ttd=tanda_tangan.kode_ttd AND id_surat = '".$_POST["id"]."' ";
      $object->query = $main_query2 ;
      $object->execute();
      $result2 = $object->get_result();
      $klasifikasi='';
      $ttd='';
      foreach($result2 as $row)
      {
        $klasifikasi = $row["klasifikasi"];
        $ttd= $row["kode_ttd"];
      }
      //
      $bulan = array (
      1 =>   'Januari','Februari','Maret','April','Mei','Juni','Juli','Agustus','September','Oktober','November','Desember' );
      $pecahkan = explode('-', date("Y-m-d"));
      //
      $array_bln  = array(1=>"I","II","III", "IV", "V","VI","VII","VIII","IX","X", "XI","XII");
      $bln    = $array_bln[date('n')];
      //
      $format_nomor='';
      //
      IF($_POST["kode_naskah"]=='SDinas'){
        $format_nomor = 
        'B- '.$nomor.' /'.$bln.'/'.$ttd.'/'.$klasifikasi.'/'. date("Y").'/BNN';
        } 
      elseif($_POST["kode_naskah"]=='ND'){
        $format_nomor = 
        'B/ND- '.$nomor.' /'.$bln.'/'.$ttd.'/'.$klasifikasi.'/'. date("Y").'/BNN';
        }
      elseif($_POST["kode_naskah"]=='SE'){
        $format_nomor = 
        $_POST["kode_naskah"].'/ '.$nomor.' /'.$bln.'/'.$ttd.'/'.$klasifikasi.'/'. date("Y").'/BNN';
        }
      elseif($_POST["kode_naskah"]=='Sprin1'){
        $format_nomor = 
        'Sprin/ '.$nomor.' /'.$bln.'/'.$ttd.'/'.$klasifikasi.'/'. date("Y").'/BNN';
        }
      elseif($_POST["kode_naskah"]=='Sgas1'){
        $format_nomor = 
        'Sgas/ '.$nomor.' /'.$bln.'/'.$ttd.'/'.$klasifikasi.'/'. date("Y").'/BNN';
        }
      //
      echo '<script>alert("Kasih nomor action '.$_POST["action"].'-'.$_POST["kode_naskah"].'-'.$_POST["id"].'-'.$format_nomor.'");</script>';
    }
    elseif($_POST["action"] == 'ubah_verif'){
      //verifikasi
      $object->query ="UPDATE verifikator_detail SET status='".$_POST["radio_setuju"]."' WHERE id_verifikator='".$_POST['id_verifikator']."' AND id_surat='".$_POST['id_surat']."' ";
      $object->execute();
      $object->query ="INSERT INTO histori_surat (id_verifikator,id_surat,status,tanggal,alasan) VALUES('".$_POST['id_verifikator']."','".$_POST['id_surat']."','".$_POST["radio_setuju"]."','".date('Y/m/d')."','".$_POST["alasan"]."' ) ";
      $object->execute();
      if($_POST["radio_setuju"]==0){
          //status surat keluar
          $object->query = "UPDATE surat_keluar SET status=0  WHERE no_urut = ".$_POST['id_surat'];
          $object->execute();
      }
      //
      $sum='';
      $count='';
      $object->query = "SELECT COUNT(verifikator_detail.id_surat) as COUNT,SUM(verifikator_detail.status) as SUM FROM verifikator_detail INNER JOIN surat_keluar WHERE verifikator_detail.id_surat = surat_keluar.no_urut AND no_urut = '".$_POST['id_surat']."' " ;
      $result3 = $object->get_result();
      foreach($result3 as $row)
      {
          $sum = $row["SUM"];
          $count = $row["COUNT"];
      }
      if($sum==$count){
          //status surat keluar
          $object->query = "UPDATE surat_keluar SET status=2  WHERE no_urut = ".$_POST['id_surat'];
          $object->execute();
      }
      //echo '<script>alert("Kasih nomor action '.$_POST["id_surat"].' - '.$_POST["id_verifikator"].'");</script>';
    }
    elseif($_POST["action"] == 'ttd_surat'){
      //
      if($_POST["radio_setuju"]==1){
        $object->query = "UPDATE surat_keluar SET status=3  WHERE no_urut = ".$_POST['id_surat'];
          $object->execute();
        $object->query ="INSERT INTO histori_surat (id_verifikator,id_surat,status,tanggal,alasan) VALUES('".$_POST['id_ttd']."','".$_POST['id_surat']."',3,'".date('Y/m/d')."','".$_POST["alasan"]."' ) ";
          $object->execute();
      } else{
        $object->query = "UPDATE surat_keluar SET status=0  WHERE no_urut = ".$_POST['id_surat'];
          $object->execute();
        $object->query ="INSERT INTO histori_surat (id_verifikator,id_surat,status,tanggal,alasan) VALUES('".$_POST['id_ttd']."','".$_POST['id_surat']."',0,'".date('Y/m/d')."','".$_POST["alasan"]."' ) ";
          $object->execute();
      }
      $success='Naskah berhasil di tanda tangani';
      //echo '<script>alert("Kasih ttd action '.$_POST["action"].' - '.$_POST["id_surat"].' - '.$_POST["id_ttd"].'");</script>';
    }
  }
//
if($_SESSION['id_akun']==1)
{
$main_query = " SELECT surat_keluar.id_surat,kode_naskah,jenis_naskah.jenis_naskah,nomor,perihal,tujuan,klasifikasi,satuan_kerja,surat_keluar.tanggal,tanda_tangan,template,surat_keluar.status FROM surat_keluar INNER JOIN jenis_naskah,satuan_kerja,tanda_tangan WHERE surat_keluar.jenis_naskah=jenis_naskah.kode_naskah AND satuan_kerja.id_satker=surat_keluar.satker AND ttd=tanda_tangan.id_pejabat AND surat_keluar.status=2 GROUP BY id_surat";
}else $main_query = " SELECT surat_keluar.id_surat,kode_naskah,jenis_naskah.jenis_naskah,nomor,perihal,tujuan,klasifikasi,satuan_kerja,surat_keluar.tanggal,tanda_tangan,template,surat_keluar.status FROM surat_keluar INNER JOIN jenis_naskah,satuan_kerja,tanda_tangan WHERE surat_keluar.jenis_naskah=jenis_naskah.kode_naskah AND satuan_kerja.id_satker=surat_keluar.satker AND ttd=tanda_tangan.id_pejabat AND surat_keluar.status=2 AND ttd=".$_SESSION['id_akun']." ";
?>
<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>Tanda Tangan Surat</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">Tanda Tangan Surat</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">
                <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>
                    <?php if($_SESSION['id_akun']!=1) echo '<th></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>';
                      if($_SESSION['id_akun']!=1){
                      echo '<td><div align="center"> <button type="button" name="edit_button" class="btn btn-warning btn-circle btn-sm edit_button" data-id="'.$row["id_surat"].'" data-status="'.$row["kode_naskah"].'"><i class="fas fa-edit"></i></button>
                      </div></td></tr>';
                      } else echo '</tr>';
                    }
                  ?>
                  </tbody>
                </table>
              </div>
              <!-- /.card-body -->
            </div>
            <!-- /.card -->
          </div>
          <!-- /.col -->
        </div>
        <!-- /.row -->
      </div>
      <!-- /.container-fluid -->
    </section>
    <!-- /.content -->
  </div>
  <!-- /.content-wrapper -->
  
<!-- jQuery -->
<?php include('footer.php'); ?>
<!-- Page specific script -->
<script type="text/javascript" language="javascript" >
 $(document).ready(function(){
    var dataTable = $("#example1").DataTable({
      "fixedColumns": true,
      "scrollX": true,
      "responsive": true, 
      "lengthChange": false, 
      "autoWidth": false,
      "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_ttd_surat.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='kode_naskah';
            my_tb2.value= kode_naskah;
            my_form.appendChild(my_tb2);
            document.body.appendChild(my_form);
            my_form.submit();
            },
            error: function(e){
                alert('Error: '+e);
            }  
        });
    });
});
<?php
if(isset($_POST["action"]))
{
//echo '<script>alert( "Form successful submitted! '.implode(", ",$_POST["select23"]).'");</script>';
/*echo '<script>alert( "Form successful submitted! ';
foreach($_POST["select23"] as $value) {
  echo $qtyOut = $value." ";
}
echo '")</script>;';*/
//echo '<script>alert( "'.$_POST["penandatangan"].'-'.$_POST["kodettd"].'");</script>';
//$object->query = "
//  INSERT INTO tanda_tangan (tanda_tangan, kode_ttd) 
//  VALUES ('".$_POST["penandatangan"]."', '".$_POST["kodettd"]."')";
//$object->execute();
if($_POST["action"] = 'add_surat')  echo "toastr.success('Data berhasil disimpan.'); ";
elseif($_POST["action"] = 'edit_surat')  echo "toastr.success('Data berhasil diubah.'); ";
elseif($_POST["action"] = 'ttd_surat')  echo "toastr.success('".$success."'); ";
}
?>
</script>
</body>
</html>
