<?php
//
include('class/Database.php');
$object = new Database;
//
if(!$object->is_login())
{
    header("location:".$object->base_url);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<?php include('header.php'); ?>
</head>
<body class="hold-transition sidebar-mini">
<div class="wrapper">
<!-- Navbar -->
<?php include('navbar.php'); ?>
<!-- /.navbar -->

<!-- Main Sidebar Container -->
<?php include('sidebar.php'); 
// 

$object->query = "SELECT * FROM jenis_naskah WHERE status='enable'";
$result = $object->get_result();
$object->query = "SELECT * FROM kode_klasifikasi ";
$result2 = $object->get_result();
$object->query = "SELECT * FROM tanda_tangan INNER JOIN akun_user,jabatan WHERE tanda_tangan.id_pejabat = akun_user.id_user AND jabatan.id_jabatan=akun_user.id_jabatan";
$result3 = $object->get_result();
$object->query = "SELECT * FROM verifikator INNER JOIN akun_user, jabatan WHERE akun_user.id_user = verifikator.id_pejabat AND jabatan.id_jabatan=akun_user.id_jabatan";
$result4 = $object->get_result();
$object->query = "SELECT * FROM tujuan INNER JOIN akun_user, jabatan WHERE tujuan.id_user_tujuan=akun_user.id_user AND akun_user.id_jabatan = jabatan.id_jabatan";
$result5 = $object->get_result();

//
 if(isset($_POST["myInput"]))
  {
    if($_SESSION['id_akun']==9){
    //upload pdf
    //edit
    $template='';
    $file_pdf='';
    $object->query = "SELECT * FROM surat_keluar WHERE id_surat=".$_POST["myInput"]." ";
    $result = $object->get_result();
    foreach($result as $row)
    {
        $template=$row["template"];
        $file_pdf=$row["file_pdf"];
    }
   ?>
    <!-- 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>Upload PDF</h1>
            </div>
            <div class="col-sm-6">
              <ol class="breadcrumb float-sm-right">
                <li class="breadcrumb-item"><a href="tables_surat_keluar.php">Home</a></li>
                <li class="breadcrumb-item active">Upload PDF</li>
              </ol>
            </div>
          </div>
        </div><!-- /.container-fluid -->
      </section>

      <!-- Main content -->
      <section class="content">
        <div class="container-fluid">
            <div class="card card-primary">
                <!--div class="card-header">
                  <h3 class="card-title">Upload PDF</h3>
                </div-->
                <!-- /.card-header -->
                <!-- form start -->
                <form id="quickForm" action="tables_surat_keluar.php" method="post"  enctype="multipart/form-data">
                  <div class="card-body">
                    <div class="form-group" >
                      <iframe src='https://view.officeapps.live.com/op/embed.aspx?src=https://arsiparisbnn.my.id/latihan/<?=$template?>' width='100%' height='423px' frameborder='0'></iframe>
                    </div>      
                    <?php if($file_pdf==''){ ?>              
                    <div class="form-group" >
                          <label for="exampleInputFile">Masukkan Dokumen (PDF)</label>
                          <div class="input-group">
                            <div class="custom-file">
                            <input type="file" class="custom-file-input" id="customFile" name="customFile">
                            <label class="custom-file-label" for="customFile">Choose file</label>
                            </div>
                          </div>
                      </div>
                  </div>
                  <!-- /.card-body -->
                  
                  <div class="card-footer">
                    <input type="hidden" name="templates" id="templates" value="<?php echo $template;?>" />
                    <input type="hidden" name="hidden_id" id="hidden_id" value="<?=$_POST["myInput"]?>" />
                    <input type="hidden" name="action" id="action" value="upload_pdf" />
                    <button type="submit" class="btn btn-primary">Update</button>
                  </div>
                  <?php 
                        
                    }else echo '<div class="form-group" ><a href="https://arsiparisbnn.my.id/latihan/'.$file_pdf.'" class="nav-link">Download PDF</a></div>';
                  
                  ?>
                </form>
              </div>
          <!-- /.row -->
        </div>
        <!-- /.container-fluid -->
      </section>
      <!-- /.content -->
    </div>
    <?php
    } else {
    //
    $object->query = "SELECT * FROM surat_keluar WHERE id_surat=".$_POST["myInput"];
    $result8 = $object->get_result();
    foreach($result8 as $row)
    {
        $status=$row["status"];
    }
    if($status==4){
    //
    $template='';
    $file_pdf='';
    $object->query = "SELECT * FROM surat_keluar WHERE id_surat=".$_POST["myInput"]." ";
    $result = $object->get_result();
    foreach($result as $row)
    {
        $template=$row["template"];
        $file_pdf=$row["file_pdf"];
    }
        ?>
    <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>Upload PDF</h1>
            </div>
            <div class="col-sm-6">
              <ol class="breadcrumb float-sm-right">
                <li class="breadcrumb-item"><a href="tables_surat_keluar.php">Home</a></li>
                <li class="breadcrumb-item active">Upload PDF</li>
              </ol>
            </div>
          </div>
        </div><!-- /.container-fluid -->
      </section>

      <!-- Main content -->
      <section class="content">
        <div class="container-fluid">
            <div class="card card-primary">
                <!--div class="card-header">
                  <h3 class="card-title">Upload PDF</h3>
                </div-->
                <!-- /.card-header -->
                <!-- form start -->
                <form id="quickForm" action="tables_surat_keluar.php" method="post"  enctype="multipart/form-data">
                  <div class="card-body">
                    <div class="form-group" >
                      <iframe src='https://view.officeapps.live.com/op/embed.aspx?src=https://arsiparisbnn.my.id/latihan/<?=$template?>' width='100%' height='423px' frameborder='0'></iframe>
                    </div>               
                  </div>
                  <!-- /.card-body -->
                  <div class="form-group" ><a href="https://arsiparisbnn.my.id/latihan/<?=$file_pdf?>" class="nav-link">Download PDF</a></div>
                </form>
              </div>
          <!-- /.row -->
        </div>
        <!-- /.container-fluid -->
      </section>
      <!-- /.content -->
    </div>
    <?php
    }
    else {
    //edit
    $kode_klasifikasi='';
    $kode_ttd='';
    $jenis_naskah='';
    $perihal='';
    $tanggal='';
    $status='';
    $verifikator = array();
    $object->query = "SELECT *,verifikator_detail.id_verifikator as verifikator, surat_keluar.status as status_ FROM verifikator_detail INNER JOIN akun_user,surat_keluar,tanda_tangan WHERE akun_user.id_user = verifikator_detail.id_verifikator AND verifikator_detail.id_surat = surat_keluar.no_urut AND surat_keluar.ttd = tanda_tangan.id_pejabat AND surat_keluar.id_surat=".$_POST["myInput"]." ";
    $result6 = $object->get_result();
    foreach($result6 as $row)
    {
        $jenis_naskah=$row["jenis_naskah"];
        $kode_klasifikasi=$row["klasifikasi"];
        $kode_ttd=$row["kode_ttd"];
        $perihal=$row["perihal"];
        $tanggal=$row["tanggal"];
        $verifikator[] = $row["verifikator"];
        $status=$row["status_"];
    }
    //
    $tujuan = array();
    //
    $object->query ="SELECT * FROM tujuan_detail INNER JOIN tujuan, akun_user, surat_keluar WHERE tujuan_detail.id_tujuan = tujuan.id_user_tujuan AND akun_user.id_user = tujuan_detail.id_tujuan AND tujuan_detail.id_surat = surat_keluar.no_urut AND surat_keluar.id_surat=".$_POST["myInput"]." ";
    $result7 = $object->get_result();
    foreach($result7 as $row)
    {
        $tujuan[]=$row["id_user_tujuan"];
    }
   ?>
  <!-- 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>Edit Surat Keluar</h1>
            </div>
            <div class="col-sm-6">
              <ol class="breadcrumb float-sm-right">
                <li class="breadcrumb-item"><a href="tables_surat_keluar.php">Home</a></li>
                <li class="breadcrumb-item active">Edit Surat Keluar</li>
              </ol>
            </div>
          </div>
        </div><!-- /.container-fluid -->
      </section>

      <!-- Main content -->
      <section class="content">
        <div class="container-fluid">

            <div class="card card-primary">
                <!--div class="card-header">
                  <h3 class="card-title">Surat Keluar</h3>
                </div-->
                <!-- /.card-header -->
                <!-- form start -->
                <form id="quickForm" action="tables_surat_keluar.php" method="post"  enctype="multipart/form-data">
                  <div class="card-body">
                  <div class="row">
                  <div class="col-md-6">
                    <div class="form-group">
                      <label>Jenis Naskah</label>
                      <select class="form-control select2" name="jenis_naskah" style="width: 100%;" required>
                        <option value="">Pilih Jenis naskah</option>
                        <?php foreach($result as $row)
                          {
                            if ($row["kode_naskah"]==$jenis_naskah) {
                              //$kode_ttd = $row["kode_ttd"];
                              echo '<option value="'.$row["kode_naskah"].'" selected="selected">'.$row["jenis_naskah"].'</option>';
                            }else  echo '<option value="'.$row["kode_naskah"].'">'.$row["jenis_naskah"].'</option>';
                          }
                        ?>
                      </select>
                    </div>
                  </div>
                  <div class="col-md-6">
                  <div class="form-group">
                    <label>Kode Klasifikasi</label>
                    <select class="form-control select2" name="kode_klasifikasi" style="width: 100%;" required>
                      <option value="">Pilih Klasifikasi</option>
                      <?php foreach($result2 as $row)
                        {
                          if ($row["kode_klasifikasi"]==$kode_klasifikasi) {
                              //$kode_ttd = $row["kode_ttd"];
                              echo '<option value="'.$row["kode_klasifikasi"].'" selected="selected">'.$row["kode_klasifikasi"].' '.$row["nama_klasifikasi"].'</option>';
                            }else  echo '<option value="'.$row["kode_klasifikasi"].'">'.$row["kode_klasifikasi"].' '.$row["nama_klasifikasi"].'</option>';
                        }
                      ?>
                    </select>
                  </div>
                </div>
              </div>

              <div class="row">
                <div class="col-md-6">
                  <div class="form-group">
                    <label>Tanggal</label>
                      <input type="text" name="tanggal" id="tanggal" class="form-control datepicker" value="<?=$tanggal?>" data-parsley-trigger="keyup" readonly required />
                  </div>
                </div>
                <div class="col-md-6">
                  <div class="form-group">
                    <label>Penanda Tangan</label>
                    <select class="form-control select2" name="tanda_tangan" style="width: 100%;" required>
                      <option value="">Pilih Penanda Tangan</option>
                        <?php foreach($result3 as $row)
                        {
                          if ($row["kode_ttd"]==$kode_ttd) {
                              //$kode_ttd = $row["kode_ttd"];
                              echo '<option value="'.$row["id_pejabat"].'" selected="selected">'.$row["nama"].' - '.$row["nama_jabatan"].'</option>';
                            }
                          else  echo '<option value="'.$row["id_pejabat"].'">'.$row["nama"].' - '.$row["nama_jabatan"].'</option>';
                        }
                        ?>
                    </select>
                  </div>
                </div>
              </div>
              <div class="form-group" >
                  <label>Perihal</label>
                  <textarea id="inputDescription" name="perihal" class="form-control" rows="4" ><?=$perihal?>
                  </textarea>
              </div>
                    
              <div class="row">
                <div class="col-md-6">
                  <div class="form-group">
                  <table id="example1" class="table table-bordered table-hover">
                    <thead>
                    <tr>
                      <th>Tujuan</th>
                      <th>Jabatan</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php
                      $object->query = "SELECT * FROM tujuan_detail INNER JOIN surat_keluar, akun_user, jabatan WHERE tujuan_detail.id_surat = surat_keluar.no_urut AND akun_user.id_user = tujuan_detail.id_tujuan AND jabatan.id_jabatan=akun_user.id_jabatan  AND surat_keluar.id_surat= ".$_POST["myInput"]." ORDER BY id_detail ASC";
                      $result = $object->get_result();
                      foreach($result as $row)
                      {
                        echo '<tr><td>'.$row["nama"].'</td>';
                        echo '<td>'.$row["nama_jabatan"].'</td>';
                      }
                    ?>
                    </tbody>
                  </table>
                </div>
                </div>
                <div class="col-md-6">
                  <div class="form-group">
                    <table id="example1" class="table table-bordered table-hover">
                      <thead>
                      <tr>
                        <th>Verifikator</th>
                        <th>Jabatan</th>
                      </tr>
                      </thead>
                      <tbody>
                      <?php
                        $object->query = "SELECT * FROM verifikator_detail INNER JOIN surat_keluar, akun_user, jabatan WHERE verifikator_detail.id_surat = surat_keluar.no_urut AND akun_user.id_user = verifikator_detail.id_verifikator AND jabatan.id_jabatan=akun_user.id_jabatan  AND surat_keluar.id_surat= ".$_POST["myInput"]." ORDER BY id_detail ASC";
                        $result = $object->get_result();
                        foreach($result as $row)
                        {
                          echo '<tr><td>'.$row["nama"].'</td>';
                          echo '<td>'.$row["nama_jabatan"].'</td>';
                        }
                      ?>
                      </tbody>
                  </table>
                  </div>
                </div>
              </div>
                <div class="form-group">
                  <label for="exampleInputFile">Masukkan Dokumen (docx)</label>
                  <div class="input-group">
                    <div class="custom-file">
                    <input type="file" class="custom-file-input" id="customFile" name="customFile">
                    <label class="custom-file-label" for="customFile">Choose file</label>
                  </div>
                  </div>
                </div>
                <?php
                    if ($status==1 || $status==2){
                        echo '<div class="form-group"><label> Status Naskah : </label></div>';
                        $object->query = "SELECT *,histori_surat.status as status_,DATE_FORMAT(histori_surat.tanggal,'%d-%m-%Y %H:%i:%s') as tanggal FROM histori_surat INNER JOIN surat_keluar, akun_user WHERE histori_surat.id_surat = surat_keluar.no_urut AND id_verifikator = akun_user.id_user AND surat_keluar.id_surat= ".$_POST["myInput"]." ORDER BY id_histori ASC";
                        $result = $object->get_result();
                        $x=1;
                        foreach($result as $row)
                        {
                            if($row['status_']==1){
                                echo '<div class="form-group"><label>'.$x.'. Naskah sudah diverifikasi oleh '.$row["nama"].' tanggal '.$row["tanggal"].'</label></div>';
                                $x++;
                            }else{
                                echo '<div class="form-group"><label>'.$x.'. oleh '.$row["nama"].' tanggal '.$row["tanggal"].'</label><br>
                                <label>- '.$row["alasan"].'</label></div>';
                                $x++;
                            }
                          
                        }
                  }
                ?>
                  <?php 
                    if ($status==0){
                        echo '<div class="form-group"><label> Status Naskah : </label></div>';
                        $object->query = "SELECT *,histori_surat.status as status_,DATE_FORMAT(histori_surat.tanggal,'%d-%m-%Y %H:%i:%s') as tanggal FROM histori_surat INNER JOIN surat_keluar, akun_user WHERE histori_surat.id_surat = surat_keluar.no_urut AND id_verifikator = akun_user.id_user AND surat_keluar.id_surat= ".$_POST["myInput"]." ORDER BY id_histori ASC";
                        $result = $object->get_result();
                        $x=1;
                        foreach($result as $row)
                        {
                             if($row['status_']==1){
                                echo '<div class="form-group"><label>'.$x.'. Naskah sudah diverifikasi oleh '.$row["nama"].' tanggal '.$row["tanggal"].'</label></div>';
                                $x++;
                            }elseif($row["alasan"]!=''){
                                echo '<div class="form-group"><label>'.$x.'. oleh '.$row["nama"].' tanggal '.$row["tanggal"].'</label><br>
                                <label>- '.$row["alasan"].'</label></div>';
                                $x++;
                            }
                          
                        }
                  ?>
                  </div>
                  <!-- /.card-body -->
                  <div class="card-footer">
                    <input type="hidden" name="hidden_id" id="hidden_id" value="<?=$_POST["myInput"]?>" />
                    <input type="hidden" name="action" id="action" value="edit_surat" />
                    <button type="submit" class="btn btn-primary">Update</button>
                  </div>
                  <?php
                  
                  } 
                  ?>
                </form>
              </div>
          <!-- /.row -->
        </div>
        <!-- /.container-fluid -->
      </section>
      <!-- /.content -->
    </div>
<?php
    }
  }
  } else{
?>
  <!-- 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>Tambah Surat Keluar</h1>
          </div>
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="tables_surat_keluar.php">Home</a></li>
              <li class="breadcrumb-item active">Tambah Surat Keluar</li>
            </ol>
          </div>
        </div>
      </div><!-- /.container-fluid -->
    </section>

    <!-- Main content -->
    <section class="content">
      <div class="container-fluid">

          <div class="card card-primary">
              <!--div class="card-header">
                <h3 class="card-title">Surat Keluar</h3>
              </div-->
              <!-- /.card-header -->
              <!-- form start -->
              <form id="quickForm" action="tables_surat_keluar.php" method="post">
                <div class="card-body">
                <div class="row">
                <div class="col-md-6">
                  <div class="form-group">
                  <label>Jenis Naskah</label>
                  <select class="form-control select2" name="jenis_naskah" style="width: 100%;" required>
                    <option value="">Pilih Jenis naskah</option>
                    <?php foreach($result as $row)
                      {
                        echo '<option value="'.$row["kode_naskah"].'">'.$row["jenis_naskah"].'</option>';
                      }
                    ?>
                  </select>
                </div>
              </div>
              <div class="col-md-6">
                <div class="form-group">
                  <label>Kode Klasifikasi</label>
                  <select class="form-control select2" name="kode_klasifikasi" style="width: 100%;" required>
                    <option value="">Pilih Klasifikasi</option>
                    <?php foreach($result2 as $row)
                      {
                        echo '<option value="'.$row["kode_klasifikasi"].'">'.$row["kode_klasifikasi"].' '.$row["nama_klasifikasi"].'</option>';
                      }
                    ?>
                  </select>
                </div>
              </div>
            </div>
              <div class="row">
              <div class="col-md-6">
                <div class="form-group">
                  <label>Tanggal</label>
                    <input type="text" name="tanggal" id="tanggal" class="form-control datepicker" data-parsley-trigger="keyup" readonly required />
                </div>
              </div>
              <div class="col-md-6">
                <div class="form-group">
                  <label>Penanda Tangan</label>
                  <select class="form-control select2" name="tanda_tangan" style="width: 100%;" required>
                    <option value="">Pilih Penanda Tangan</option>
                      <?php foreach($result3 as $row)
                      {
                        echo '<option value="'.$row["id_pejabat"].'">'.$row["nama"].' - '.$row["nama_jabatan"].'</option>';
                      }
                      ?>
                  </select>
                </div>
              </div>
            </div>
                <div class="form-group" >
                  <label>Perihal</label>
                  <textarea id="inputDescription" name="perihal" class="form-control" rows="4">
                  </textarea>
                </div>
            <div class="row">
              <div class="col-md-6">
                <div class="form-group">
                  <label>Tujuan</label>
                  <select class="select2" multiple="multiple" name="tujuan[]" required style="width: 100%;">
                    
                    <?php foreach($result5 as $row)
                      {
                        echo '<option value="'.$row["id_user"].'">'.$row["nama"].' '.$row["nama_jabatan"].'</option>';
                      }
                    ?>
                  </select>
                </div>
              </div>
              <div class="col-md-6">
                <div class="form-group">
                  <label>Verifikator</label>
                  <select class="select2" multiple="multiple" name="verifikator[]" required style="width: 100%;">
                    
                    <?php foreach($result4 as $row)
                      {
                        echo '<option value="'.$row["id_user"].'">'.$row["nama"].' '.$row["nama_jabatan"].'</option>';
                      }
                    ?>
                  </select>
                </div>
              </div>
                </div>
                <!-- /.card-body -->
                <div class="card-footer">
                  <input type="hidden" name="action" id="action" value="add_surat" />
                  <button type="submit" class="btn btn-primary">Submit</button>
                </div>
              </form>
            </div>
        <!-- /.row -->
      </div>
      <!-- /.container-fluid -->
    </section>
    <!-- /.content -->
  </div>
<?php 
}

include('footer.php'); ?>
<script>
  $(function () {
  $('#tanggal').datepicker({
        format: "yyyy-mm-dd",
        autoclose: true
    });
})
</script>
</body>
</html>
