<?php
session_start(); // Start the session
include('../koneksi.php'); // Include the database connection file

//$user = $_SESSION['username'];
if (isset($_GET['id'])) {
  $produk = $_GET['id'];

    // Execute the query to fetch data
  $query = "
        SELECT
  k_dusun,
  k_rtrw,
  k_desa,
  k_kec,
  k_kab,
  dusun,
  rtrw,
  desa,
  kec,
  kab
FROM
  alamat
WHERE kode = '$produk'
    ";

    // Fetch the row from the query
    $row = mysqli_fetch_array(mysqli_query($koneksi, $query));

    // Calculate projections if $row is not null
    if ($row) {
        
        $dusun = $row['dusun'] ?? 0;
        $rtrw = $row['rtrw'] ?? 0;
        $desa = $row['desa'] ?? 0;
        $kec = $row['kec'] ?? 0;
        $kab = $row['kab'] ?? 0;

        $k_rtrw = $row['k_rtrw'] ?? 0;
        $k_desa = $row['k_desa'] ?? 0;
        $k_kec = $row['k_kec'] ?? 0;
        $k_kab = $row['k_kab'] ?? 0;

        // Calculate projections for each month
        $data_proyeksi = array(
            '1' => ($dusun),
            '2' => ($rtrw),
            '3' => ($desa),  
            '4' => ($kec),
            '5' => ($kab),
            '6' => ($k_rtrw),
            '7' => ($k_desa),
            '8' => ($k_kec),
            '9' => ($k_kab)
        );

        // Prepare the data for output
        $data_pegawai = array(
            'tampil1'  => $data_proyeksi[1],
            'tampil2'  => $data_proyeksi[2],
            'tampil3'  => $data_proyeksi[3],
            'tampil4'  => $data_proyeksi[4],
            'tampil5'  => $data_proyeksi[5],
            'tampil6'  => $data_proyeksi[6],
            'tampil7'  => $data_proyeksi[7],
            'tampil8'  => $data_proyeksi[8],
            'tampil9'  => $data_proyeksi[9],
            
        );

        // Output the data as JSON
        echo json_encode($data_pegawai);
    } else {
        echo json_encode(['error' => 'Data tidak ditemukan']);
    }
} else {
    echo json_encode(['error' => 'Parameter id tidak ditemukan']);
}

// Close database connection if needed
mysqli_close($koneksi);

/*cara kedua sukses*/
/*if (isset($_GET['id'])) {
    $produk = $_GET['id'];
    $query = "SELECT * FROM realita  WHERE kode = '$user' AND produk = '$produk' AND thn = '$tgl'
  "; 
    $result = mysqli_query($koneksi, $query); 
    if ($result) {
        $hasildata = mysqli_fetch_assoc($result); 
        echo json_encode($hasildata); // Kembalikan data sebagai JSON
    } else {
        echo json_encode(['error' => 'Data tidak ditemukan']);
    }
}*/


?>