<?php
header('Content-Type: application/json');
require '../config.php';
if(isset($_POST['key']) && isset($_POST['sign'])) {
    $key = $_POST['key'];
    $sign = $_POST['sign'];

    $s_api = $db->query("SELECT * FROM users WHERE api_key = '$key'");
    if($s_api->num_rows == 0) die(json_encode(['result' => false,'message' => 'API Key not registered.'],true));
    $r_api = $s_api->fetch_assoc();
    $username = $r_api['username'];
    $data_user = $username;
	if($sign <> md5($r_api['api_id'].$r_api['api_key'])) die(json_encode(['result' => false,'message' => 'Signature Not Valid.'],true));
    if($data_user == false) die(json_encode(['result' => false,'message' => 'User not found.'],true));
    if(check_ip($client_ip, $r_api['ip_static']) == false) die(json_encode(['result' => false,'message' => 'IP Anda '.$client_ip.' Tidak Dikenali'],true));
    $check_service = $db->query("SELECT * FROM layanan_pulsa WHERE tipe !='SOSMED' ORDER BY id ASC");
    if($check_service->num_rows == 0){
         die(json_encode(['result' => true,'message' => 'Tidak Ada Layanan'],true));
    }else{
        while($row = $check_service->fetch_assoc()) {
            if($r_api['level'] == "Member"){
                $price = $row['harga'];
            }else if($r_api['level'] == 'Reseller'){
                $price = $row['harga_reseller'];
            }else if($r_api['level'] == 'Premium' || $r_api['level'] == 'Admin'){
                $price = $row['harga_premium'];
            }
            $data[] = [
                'sku' => $row['service_id'],
                'name' => $row['operator'],
                'category' => $row['tipe'],
                'layanan' => $row['layanan'],
                'price' => (int)$price,
                'status' => $row['status'],
                'note' => $row['note'],
            ];
        }
        die(json_encode(['result' => true,'data' => $data,'message' => 'Daftar layanan berhasil didapatkan.'],true));
    }
}