<?php
//
require_once 'vendor/autoload.php';
include('class/Database.php');
include "vendor/phpqrcode/qrlib.php";
//
use PhpOffice\PhpWord\TemplateProcessor;
//
$object = new Database;
if(!isset($_SESSION['id_akun'])) $_SESSION['id_akun']=$_GET['id_akun'];
if(!$object->is_login())
{
    header("location:".$object->base_url);
}
?>
<!DOCTYPE html>
<html lang="en">
<?php 
include('header.php'); 
$success='';
$error='';
//
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';
        //
        $templateProcessor = new TemplateProcessor('docx/template_SDinas_kosong.docx');
        //$templateProcessor->setValue('nomor',$format_nomor);
        $templateProcessor->setValue('perihal',$_POST['perihal']);
        $templateProcessor->setValue('tanggal',$pecahkan[2] . ' ' . $bulan[ (int)$pecahkan[1] ] . ' ' . $pecahkan[0]);
        $templateProcessor->setValue('jabatan',$jabatan);
        $templateProcessor->setValue('nama_pejabat',$nama_pejabat);
        
        $templateProcessor->saveAs($template_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';
        //
        $templateProcessor = new TemplateProcessor('docx/template_ND_kosong.docx');
        //$templateProcessor->setValue('nomor',$format_nomor);
        $templateProcessor->setValue('perihal',$_POST['perihal']);
        $templateProcessor->setValue('tanggal',$pecahkan[2] . ' ' . $bulan[ (int)$pecahkan[1] ] . ' ' . $pecahkan[0]);
        $templateProcessor->setValue('jabatan',$jabatan);
        $templateProcessor->setValue('nama_pejabat',$nama_pejabat);

        $templateProcessor->saveAs($template_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';
        //
        $templateProcessor = new TemplateProcessor('docx/template_SE_kosong.docx');
        //$templateProcessor->setValue('nomor',$format_nomor);
        $templateProcessor->setValue('perihal',$_POST['perihal']);
        $templateProcessor->setValue('tanggal',$pecahkan[2] . ' ' . $bulan[ (int)$pecahkan[1] ] . ' ' . $pecahkan[0]);
        $templateProcessor->setValue('jabatan',$jabatan);
        $templateProcessor->setValue('nama_pejabat',$nama_pejabat);
        
        $templateProcessor->saveAs($template_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';
        //
        $templateProcessor = new TemplateProcessor('docx/template_Sprin1_kosong.docx');
        //$templateProcessor->setValue('nomor',$format_nomor);
        $templateProcessor->setValue('perihal',$_POST['perihal']);
        $templateProcessor->setValue('tanggal',$pecahkan[2] . ' ' . $bulan[ (int)$pecahkan[1] ] . ' ' . $pecahkan[0]);
        $templateProcessor->setValue('jabatan',$jabatan);
        $templateProcessor->setValue('nama_pejabat',$nama_pejabat);
        
        $templateProcessor->saveAs($template_docx);
        }
    elseif($_POST["jenis_naskah"]=='Sprin2'){
        $format_nomor = 
        'Sprin/     /'.$bln.'/'.$kode_ttd.'/'.$_POST["kode_klasifikasi"].'/'. date("Y").'/BNN';
        $template_docx='docx/result_Sprin'.date("ymhis").'.docx';
        //
        $templateProcessor = new TemplateProcessor('docx/template_Sprin2_kosong.docx');
        //$templateProcessor->setValue('nomor',$format_nomor);
        $templateProcessor->setValue('perihal',$_POST['perihal']);
        $templateProcessor->setValue('tanggal',$pecahkan[2] . ' ' . $bulan[ (int)$pecahkan[1] ] . ' ' . $pecahkan[0]);
        $templateProcessor->setValue('jabatan',$jabatan);
        $templateProcessor->setValue('nama_pejabat',$nama_pejabat);
        
        $templateProcessor->saveAs($template_docx);
        }
    elseif($_POST["jenis_naskah"]=='Sgas2'){
        $format_nomor = 
        'Sgas/     /'.$bln.'/'.$kode_ttd.'/'.$_POST["kode_klasifikasi"].'/'. date("Y").'/BNN';
        $template_docx='docx/result_Sgas'.date("ymhis").'.docx';
        //
        $templateProcessor = new TemplateProcessor('docx/template_Sgas2_kosong.docx');
        //$templateProcessor->setValue('nomor',$format_nomor);
        $templateProcessor->setValue('perihal',$_POST['perihal']);
        $templateProcessor->setValue('tanggal',$pecahkan[2] . ' ' . $bulan[ (int)$pecahkan[1] ] . ' ' . $pecahkan[0]);
        $templateProcessor->setValue('jabatan',$jabatan);
        $templateProcessor->setValue('nama_pejabat',$nama_pejabat);
        
        $templateProcessor->saveAs($template_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';
        }
        //
        //echo '<script>alert( "Form successful submitted! '.$format_nomor.'");</script>';
        //ambil satker
        $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"];
        }
        //
        $nomor= date("ymhis");

        //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"]."', '".$id_satker."', '".$_POST["tanggal"]."', '".$_POST["tanda_tangan"]."', '2', '".$template_docx."', '', '0') ";
        $object->execute();

        //input verifikator
         for ($x = 1; $x <= count($_POST["verifikator"]); $x++) {
            //
            $object->query ="INSERT INTO verifikator_detail (id_verifikator, id_surat,status, tanggal) VALUES ('".$_POST["verifikator"][$x-1]."','".$nomor."',0,'".$_POST["tanggal"]."')";
            $object->execute();
            //input histori
            $object->query ="INSERT INTO histori_surat (id_verifikator, id_surat,status, tanggal, alasan) 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();
        }
        //
        $success='data berhasil disimpan';
        //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"]."' AND status=4";
      $object->query = $main_query ;
      $result = $object->get_result();
      $nomor='';
      foreach ($result as $row) {
        $nomor = $row["nomor"];
      }
      //masih salah query
      $main_query2 = "
        SELECT * FROM tanda_tangan INNER JOIN akun_user, jabatan,surat_keluar WHERE tanda_tangan.id_pejabat=akun_user.id_user AND akun_user.id_jabatan = jabatan.id_jabatan AND surat_keluar.ttd = akun_user.id_user AND surat_keluar.id_surat = '".$_POST["id"]."' ";
      $object->query = $main_query2 ;
      $result2 = $object->get_result();
      $klasifikasi='';
      $ttd='';
      $docx='';
      foreach($result2 as $row)
      {
        $klasifikasi = $row["klasifikasi"];
        $ttd= $row["kode_ttd"];
        $docx=$row["template"];
      }

      //
      $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';
        }
      //update status surat ke 4 (final)
      //$tte = $object->base_url.'pages/tables/check_tte.php?id='.$nama_file.'.pdf';
      $tte = pathinfo($docx, PATHINFO_FILENAME);
      $tte_fix = 'docx/'.$tte.'.docx';
      $tte_png = 'docx/'.$tte.'.png';
      $tte = $object->base_url.'pages/tables/check_tte.php?id='.$tte.'.pdf';
      $object->query = "UPDATE surat_keluar SET status=4, template_tte='".$tte."', nomor='".$format_nomor."' WHERE id_surat = ".$_POST['id'];
      $object->execute();
      //echo '<script>alert("Kasih nomor action '.$_POST["action"].'--'.$format_nomor.'--'.$tte.'");</script>';
      //generate word dan tte
      QRCode::png($tte,$tte_png,'H',8,0);
      //$qrcode = (new QRCode($options))->render($docx);
      $templateProcessor = new TemplateProcessor($docx);
      $templateProcessor->setValue('nomor',$format_nomor);
      $templateProcessor->setImageValue('ttd_pejabat',$tte_png);
      $templateProcessor->saveAs($docx);
      $success=' Penomoran berhasil ';
    }
    elseif ($_POST["action"] == 'edit_surat') {
      //echo '<script>alert( "Form successful submitted! '.$_POST["hidden_id"].'");</script>';
      //upload file
      $allowed_file_format = array("docx");
      $file_extension = pathinfo($_FILES["customFile"]["name"], PATHINFO_EXTENSION);
      if(!in_array($file_extension, $allowed_file_format))
        {
            $error = "Upload hanya file .docxf ".$_FILES["customFile"]["name"];
        }
        else if (($_FILES["customFile"]["size"] > 2000000))
        {
           $error = "Ukuran File maksimum 2MB";
        }
        else
        {
        //$new_name = rand() . '.' . $file_extension;
        $destination='docx/'.date("ymhis").'.docx';
        //$destination = 'docx/' . $_FILES['customFile']['name'];
        move_uploaded_file($_FILES['customFile']['tmp_name'], $destination);
        $nama_naskah = $destination;
        //
        $object->query = "UPDATE surat_keluar SET template = '".$nama_naskah."',jenis_naskah='".$_POST["jenis_naskah"]."',klasifikasi='".$_POST["kode_klasifikasi"]."',tanggal='".$_POST["tanggal"]."',ttd='".$_POST["tanda_tangan"]."',perihal='".$_POST["perihal"]."',status=1  WHERE id_surat = ".$_POST['hidden_id'];
        $object->execute();
        $success='Data berhasil dikirim';
        //$success = 'Data Updated '.$_FILES["customFile"]["name"].' - '.$_POST["jenis_naskah"].' - '.$_POST["kode_klasifikasi"].' - '.$_POST["tanggal"].' - '.$_POST["tanda_tangan"].' - '.$_POST["perihal"];
        }
    }
    elseif ($_POST["action"] == 'upload_pdf') {
      //upload file
      $allowed_file_format = array("pdf");
      $file_extension = pathinfo($_FILES["customFile"]["name"], PATHINFO_EXTENSION);
      if(!in_array($file_extension, $allowed_file_format))
        {
            $error = "Upload hanya file .docx ".$_FILES["customFile"]["name"];
        }
        else if (($_FILES["customFile"]["size"] > 2000000))
        {
           $error = "Ukuran File maksimum 2MB";
        }
        else
        {
        $tte = pathinfo($_POST["templates"], PATHINFO_FILENAME);
        $destination='docx/'.$tte.'.pdf';
        move_uploaded_file($_FILES['customFile']['tmp_name'], $destination);
        //
        //echo '<script>alert( "Form successful submitted! '.$_POST["templates"].'-'.$_POST["hidden_id"].'-'.$tte.'");</script>';
        $object->query = "UPDATE surat_keluar SET file_pdf = '".$destination."' WHERE id_surat = ".$_POST['hidden_id'];
        $object->execute();
        $success='PDF berhasil di upload';
        //
        }
    }
  }
 //
$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 id_surat,kode_naskah,jenis_naskah.jenis_naskah,nomor,perihal,tujuan,klasifikasi,satuan_kerja,tanggal,tanda_tangan,template, satker,surat_keluar.status,akun_user.id_satker FROM surat_keluar INNER JOIN jenis_naskah,satuan_kerja,tanda_tangan, akun_user WHERE surat_keluar.jenis_naskah=jenis_naskah.kode_naskah AND satuan_kerja.id_satker=surat_keluar.satker AND akun_user.id_user = tanda_tangan.id_pejabat AND ttd=tanda_tangan.id_pejabat ORDER BY id_surat DESC";
}elseif($id_satker==500){
  $main_query = " select id_surat,kode_naskah,jenis_naskah.jenis_naskah,nomor,perihal,tujuan,klasifikasi,satuan_kerja,tanggal,tanda_tangan,template, satker,surat_keluar.status,akun_user.id_satker FROM surat_keluar INNER JOIN jenis_naskah,satuan_kerja,tanda_tangan, akun_user WHERE surat_keluar.jenis_naskah=jenis_naskah.kode_naskah AND satuan_kerja.id_satker=surat_keluar.satker AND akun_user.id_user = tanda_tangan.id_pejabat AND ttd=tanda_tangan.id_pejabat AND (surat_keluar.status=3 OR surat_keluar.status=4) ORDER BY id_surat DESC";
} 
else $main_query = " select id_surat,kode_naskah,jenis_naskah.jenis_naskah,nomor,perihal,tujuan,klasifikasi,satuan_kerja,tanggal,tanda_tangan,template, satker,surat_keluar.status,akun_user.id_satker FROM surat_keluar INNER JOIN jenis_naskah,satuan_kerja,tanda_tangan, akun_user WHERE surat_keluar.jenis_naskah=jenis_naskah.kode_naskah AND satuan_kerja.id_satker=surat_keluar.satker AND akun_user.id_user = tanda_tangan.id_pejabat AND ttd=tanda_tangan.id_pejabat AND (satker=".$id_satker." OR satker=0) ORDER BY 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 Keluar </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>template</th>
                    <?php if($status == 3 && ($id_satker==500 || $id_satker==0)) echo '<th>beri nomor</th>';
                    elseif ($id_satker==500 || $id_satker==0) echo '<th>beri nomor</th>';
                    ?>
                    <th></th>
                  </tr>
                  </thead>
                  <tbody>
                  <?php
                    $object->query = $main_query;
                    $result2 = $object->get_result();
                    foreach($result2 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>';
                      $file_extension = pathinfo($row["template"], PATHINFO_EXTENSION);
                      if($file_extension=='docx')
                      {
                        echo '<td><center><a href="'.$row["template"].'"><i class="fas fa-file-word" style="font-size:36px;color:blue"></i></a></center></td>';
                      }
                      else {
                        echo '<td><center><a href="'.$row["template"].'"><i class="fas fa-file-pdf" style="font-size:36px;color:red"></i></a></center></td>';
                      }
                      if($row["status"] == 3 && ($id_satker==500 || $id_satker==0)) 
                      {
                        echo '<td><button type="button" name="status_button" class="btn btn-primary btn-sm status_button" data-id="'.$row["id_surat"].'" data-status="'.$row["kode_naskah"].'"> beri nomor </button></td>';
                      } elseif ($id_satker==500 || $id_satker==0) echo '<td></td>';
                      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>';
                    }
                  ?>
                  </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>
