<?php
session_start();
require '../../config.php';

if (isset($_POST['produk'])) {
    
    $produk = addslashes(trim(htmlspecialchars(mysqli_real_escape_string($db, $_POST['produk']))));
    
    // echo $produk; die;
    
    if (!empty($produk)) {
        if (is_numeric($produk)) {
            
            if (isset($_POST['produk'])) {
                $query_produk = mysqli_query($db, "SELECT * FROM layanan_pulsa WHERE id = '$produk'");
            }
            
            if (mysqli_num_rows($query_produk) !== 0) {
                $fetch_produk = mysqli_fetch_assoc($query_produk);
                
                $harga = [];
                
                $query_metode = mysqli_query($db, "SELECT * FROM metode_guest");
                while($fetch_metode = mysqli_fetch_assoc($query_metode)) {
                    
                    $tipe = isset($_POST['produk']);
                    
                    $query_harga = mysqli_query($db, "SELECT * FROM layanan_pulsa WHERE id = '$produk' AND status='Normal'");
                    $fetch_hargaa = mysqli_fetch_assoc($query_harga);
                    if($_SESSION['user']['level'] == "Reseller"){
                            $real_harga = $fetch_hargaa['harga_reseller'];
                        }else if($_SESSION['user']['level'] == "Vip" || $_SESSION['user']['level'] == "Admin" || $_SESSION['user']['level'] == "Lock"){
                            $real_harga = $fetch_hargaa['harga_premium'];
                        }else if($_SESSION['user']['level'] !== "Reseller" || $_SESSION['user']['level'] !== "Admin" || $_SESSION['user']['level'] !== "Vip" || $_SESSION['user']['level'] !== "Lock"){
                            $real_harga = $fetch_hargaa['harga'];
                        }
                        
                    if (mysqli_num_rows($query_harga) == 1) {
                        $fetch_harga = mysqli_fetch_assoc($query_harga);
                        $real_hargaa = $real_harga;
                    }
                    
                    $harga[] = [
                        'id' => $fetch_metode['id'],
                        'harga' => 'Rp. '. number_format($real_harga,0,',','.'),
                        'produk' => $fetch_produk['layanan'],
                        'operator' => $fetch_produk['operator'],
                    ];
                }
                
                echo json_encode($harga);
            }
        }
    }
}