<?php
session_start();
include "../config.php";

function kirimWA($no, $pesan) {
    $token = "hbXfbV6ZbY4yVW5rsonS";

    $curl = curl_init();

    curl_setopt_array($curl, array(
        CURLOPT_URL => "https://api.fonnte.com/send",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => array(
            'target' => $no,
            'message' => $pesan,
        ),
        CURLOPT_HTTPHEADER => array(
            "Authorization: $token"
        ),
    ));

    curl_exec($curl);
    curl_close($curl);
}

// Hanya dfo yang bisa akses
if(!isset($_SESSION['id_user']) || $_SESSION['role'] != 'dct'){
    header("Location: ../index.php");
    exit;
}

// Pastikan parameter id ada
if(!isset($_GET['id']) || empty($_GET['id'])){
    die("ID Pagamento tidak ditemukan!");
}
$id = intval($_GET['id']); // amankan jadi integer

// Ambil data untuk pre-fill form
$stmt = $koneksi->prepare("SELECT * FROM jv_documents WHERE id_jv=?");
$stmt->bind_param("i", $id);
$stmt->execute();
$row = $stmt->get_result()->fetch_assoc();

if(!$row){
    die("Data Pagamento dengan ID $id tidak ditemukan.");
}

if (isset($_POST['save'])) {
    $tgl_registrasi = $_POST['tgl_registrasi'];
    $line_item      = $_POST['line_item'];
    $division       = $_POST['division'];
    $vendor_name    = $_POST['vendor_name'];
    $nomor_tpo      = $_POST['nomor_tpo'];
    $total_relatorio = $_POST['total_relatorio'];
    $outstanding    = trim($_POST['outstanding']);
    $overdue        = trim($_POST['overdue']);
    $nomor_jv       = trim($_POST['nomor_jv']);
    $jv_date        = $_POST['jv_date'];
    $nomor_its         = $_POST['nomor_its'];
$tanggal_kria_its  = $_POST['tanggal_kria_its'];
$nomor_prt2        = $_POST['nomor_prt2'];
$tanggal_kria_prt  = $_POST['tanggal_kria_prt'];
$jenis_pembayaran  = $_POST['jenis_pembayaran'];
$deskripsi2        = $_POST['deskripsi2'];
$total_relatorio2  = $_POST['total_relatorio2'];

    // Update data jv_documents
   $stmt = $koneksi->prepare("
UPDATE jv_documents SET 
    tgl_registrasi = ?, 
    line_item = ?, 
    division = ?, 
    vendor_name = ?, 
    nomor_tpo = ?, 
    total_relatorio = ?, 
    outstanding = ?, 
    overdue = ?, 
    nomor_jv = ?, 
    jv_date = ?, 

    nomor_its = ?,
    tanggal_kria_its = ?,
    nomor_prt2 = ?,
    tanggal_kria_prt = ?,
    jenis_pembayaran = ?,
    deskripsi2 = ?,
    total_relatorio2 = ?,

    status = 'review'

WHERE id_jv = ?
");

$stmt->bind_param(
    "sssssssssssssssssi",
    $tgl_registrasi,
    $line_item,
    $division,
    $vendor_name,
    $nomor_tpo,
    $total_relatorio,
    $outstanding,
    $overdue,
    $nomor_jv,
    $jv_date,

    $nomor_its,
    $tanggal_kria_its,
    $nomor_prt2,
    $tanggal_kria_prt,
    $jenis_pembayaran,
    $deskripsi2,
    $total_relatorio2,

    $id
);
    $stmt->execute();

    // Simpan history
    $stmt2 = $koneksi->prepare("INSERT INTO jv_history (id_jv, action_by, action_role, action_type) 
                                VALUES (?, ?, 'dct', 'konfirmasi')");
    $stmt2->bind_param("ii", $id, $_SESSION['id_user']);
    $stmt2->execute();

    // =======================
// KIRIM WA OTOMATIS
// =======================
$no_wa = "6283145505756";

$pesan = "📢 KONFIRMASI DATA PAGAMENTO\n\n"
       . "PRT: " . $row['nomor_prt'] . "\n"
       . "TPO: " . $nomor_tpo . "\n"
       . "JV: " . $nomor_jv . "\n"
       . "Vendor: " . $vendor_name . "\n"
       . "Total: " . $total_relatorio . "\n\n"
       . "Status: Sudah dikonfirmasi DFO";

kirimWA($no_wa, $pesan);
    header("Location: verifikasi.php");
    exit;
}
?>


<?php include "../header.php"; ?>
<div class="content-wrapper">
  <div class="content-header"><h1>Konfirmasaun Dadus Pagamento</h1></div>
  <div class="content">
    <div class="container-fluid">
      <div class="card">
        <div class="card-body">
          <form method="POST">
             <div class="form-group">


<div class="form-group">
              <label>Data Simu Pedido</label>
                      <input type="text" class="form-control" value="<?= htmlspecialchars($row['tgl_terima']) ?>" readonly>

            </div>


<div class="form-group">
  <label>Diresaun</label>
  <input type="text" class="form-control" value="<?= htmlspecialchars($row['division']) ?>" readonly>
  <input type="hidden" name="division" value="<?= htmlspecialchars($row['division']) ?>">
</div>



            <div class="form-group">
              <label>Numeru Pedido</label>
                      <input type="text" class="form-control" value="<?= htmlspecialchars($row['nomor_prt']) ?>" readonly>
            </div>

        

            <div class="form-group">
              <label>Assunto</label>
                      <input type="text" class="form-control" value="<?= htmlspecialchars($row['descripsi']) ?>" readonly>
            </div>
            
                   
       <div class="form-group">
              <label>Numeru CPV / PR</label>
             <input type="text" class="form-control" value="<?= htmlspecialchars($row['nomor_tpo']) ?>" readonly>

            </div>


             <div class="form-group">
              <label>Data Kria CPV</label>
             <input type="text" class="form-control" value="<?= htmlspecialchars($row['tgl_registrasi']) ?>" readonly>
            </div>


            

           
           
          
        
          
            
            <div class="form-group">
  <label>Montante</label>
  <input type="text" name="total_relatorio" class="form-control"
         value="<?= htmlspecialchars($row['total_relatorio']) ?>" readonly>
</div>
      
   

  <div class="form-group">
  <label>Numeru Obl / PO</label>
  <input type="text" name="nomor_apt" class="form-control"
         value="<?= htmlspecialchars($row['nomor_apt']) ?>" readonly>
</div>


<div class="form-group">
  <label>Data Kria Obl</label>
  <input type="date" name="jv_date" class="form-control"
         value="<?= htmlspecialchars($row['jv_date']) ?>"readonly>
</div>
      
      

  


<div class="form-group">
  <label>Vendor</label>
  <input type="text" name="vendor_name" class="form-control"
         value="<?= htmlspecialchars($row['vendor_name']) ?>"readonly>
</div>


<div class="form-group">
  <label>Numeru Noumeru ITS / APT</label>
  <input type="text" id="nomor_its" name="nomor_its" class="form-control" 
         value="<?= htmlspecialchars($row['nomor_its'] ?? '') ?>" required>
</div>

<div class="form-group">
        <label>Data Kria ITS / APT</label>
        <input type="date" name="tanggal_kria_its" class="form-control" value="<?= htmlspecialchars($row['tanggal_kria_its'] ?? '') ?>" required>
      </div>

  <div class="form-group">
  <label>Numeru PRT</label>
  <input type="text" id="nomor_prt2" name="nomor_prt2" class="form-control" 
         value="<?= htmlspecialchars($row['nomor_prt2'] ?? '') ?>" required>
</div>    

<div class="form-group">
        <label>Data Kria PRT</label>
        <input type="date" name="tanggal_kria_prt" class="form-control" value="<?= htmlspecialchars($row['tanggal_kria_prt'] ?? '') ?>" required>
      </div>


      <div class="form-group">
  <label>Tipo Pagamento</label>
  <input type="text" id="jenis_pembayaran" name="jenis_pembayaran" class="form-control" 
         value="<?= htmlspecialchars($row['jenis_pembayaran'] ?? '') ?>" required>
</div>    


<div class="form-group">
  <label>Assunto</label>
  <textarea name="deskripsi2" class="form-control" required><?= htmlspecialchars($row['deskripsi2'] ?? '') ?></textarea>
</div>

             <div class="form-group">
  <label>Montante</label>
  <input type="text" id="total_relatorio2" name="total_relatorio2" class="form-control" 
         value="<?= htmlspecialchars($row['total_relatorio2'] ?? '') ?>" required>
</div>
<!-- 
        <div class="form-group">
  <label>Outstanding</label>
  <input type="text" id="outstanding" name="outstanding" class="form-control" 
         value="<?= htmlspecialchars($row['outstanding'] ?? '') ?>" readonly>
</div>
      
      <div class="form-group">
        <label>Overdue</label>
        <input type="text" name="overdue" class="form-control" value="<?= htmlspecialchars($row['overdue'] ?? '') ?>" required>
      </div>
      
          <div class="form-group">
        <label>Numeru Finance</label>
        <input type="text" name="nomor_jv" class="form-control" value="<?= htmlspecialchars($row['nomor_jv'] ?? '') ?>" required>
      </div>
       -->
            
      
            <!-- <div class="form-group"> -->
  <!-- <label>Outstanding</label>
  <input type="text" name="outstanding" class="form-control" 
         value="<?= htmlspecialchars($row['outstanding']) ?>" 
         required 
         oninput="this.value = this.value.replace(/[^0-9.,]/g, '');">
</div> -->

            <button type="submit" name="save" class="btn btn-success"><i class="fas fa-check"></i> Salva & Kontinua</button>
          </form>
        </div>
      </div>
    </div>
  </div>
</div>

<script>
// Fungsi ubah format angka (hilangkan titik, ubah koma jadi titik)
function parseNumber(str) {
  if (!str) return 0;
  return parseFloat(str.replace(/\./g, '').replace(',', '.')) || 0;
}

// Format tampilan angka (ribuan titik, desimal koma)
function formatNumber(num) {
  return num.toLocaleString('id-ID', {minimumFractionDigits: 2, maximumFractionDigits: 2});
}

// Kalkulasi otomatis Outstanding
document.addEventListener('DOMContentLoaded', function() {
  const totalAdiantamento = document.getElementById('total_adiantamento');
  const totalRelatorio = document.getElementById('total_relatorio');
  const outstanding = document.getElementById('outstanding');

  function hitungOutstanding() {
    const adiantamento = parseNumber(totalAdiantamento.value);
    const relatorio = parseNumber(totalRelatorio.value);
    const hasil = adiantamento - relatorio;
    outstanding.value = formatNumber(hasil);
  }

  // Jalankan saat mengetik di kolom relatorio
  totalRelatorio.addEventListener('input', hitungOutstanding);

  // Jalankan pertama kali saat halaman dibuka
  hitungOutstanding();
});
</script>

<?php include "../footer.php"; ?>
