<?php
session_start();

// Konfigurasi API
$clientId = "f0c07625-c819-4365-95ac-1cefd419cb21";
$clientSecret = "fPvWlLX0QvIPepZSDWuZEINbSw7oxUI6gdtvxzTOdEotyFnFnAa0TS6BmogYvS3R";
$apiBaseUrl = "https://xyz.accurate.id/accurate/api/vendor";

// Ambil token akses dari sesi
$accessToken = isset($_SESSION['access_token']) ? $_SESSION['access_token'] : null;

if (!$accessToken) {
    die("Error: Access token tidak ditemukan. Harap login terlebih dahulu.");
}

// Fungsi untuk menarik data vendor
function getVendorData($apiBaseUrl, $accessToken) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $apiBaseUrl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        "Authorization: Bearer $accessToken",
        "Content-Type: application/json"
    ));

    $response = curl_exec($ch);
    curl_close($ch);

    return json_decode($response, true);
}

// Fungsi untuk menambahkan data vendor
function addVendor($apiBaseUrl, $accessToken, $vendorData) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $apiBaseUrl);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($vendorData));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        "Authorization: Bearer $accessToken",
        "Content-Type: application/json"
    ));

    $response = curl_exec($ch);
    curl_close($ch);

    return json_decode($response, true);
}

// Fungsi untuk memperbarui data vendor
function updateVendor($apiBaseUrl, $accessToken, $vendorId, $vendorData) {
    $url = "$apiBaseUrl/$vendorId";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($vendorData));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        "Authorization: Bearer $accessToken",
        "Content-Type: application/json"
    ));

    $response = curl_exec($ch);
    curl_close($ch);

    return json_decode($response, true);
}

// Fungsi untuk menghapus data vendor
function deleteVendor($apiBaseUrl, $accessToken, $vendorId) {
    $url = "$apiBaseUrl/$vendorId";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        "Authorization: Bearer $accessToken",
        "Content-Type: application/json"
    ));

    $response = curl_exec($ch);
    curl_close($ch);

    return json_decode($response, true);
}

// Menggunakan fungsi API
$action = isset($_GET['action']) ? $_GET['action'] : '';

switch ($action) {
    case 'get':
        $vendorData = getVendorData($apiBaseUrl, $accessToken);
        echo "<pre>";
        print_r($vendorData);
        echo "</pre>";
        break;

    case 'add':
        $vendorData = array(
            'vendor_code' => 'V001',
            'vendor_name' => 'Supplier ABC',
            'contact' => 'John Doe',
            'phone' => '123456789',
            'address' => '123 Main St'
        );
        $result = addVendor($apiBaseUrl, $accessToken, $vendorData);
        echo "<pre>";
        print_r($result);
        echo "</pre>";
        break;

    case 'update':
        $vendorId = '123'; // Ganti dengan ID vendor yang sesuai
        $vendorData = array(
            'vendor_name' => 'Supplier XYZ',
            'contact' => 'Jane Doe',
            'phone' => '987654321',
            'address' => '456 Another St'
        );
        $result = updateVendor($apiBaseUrl, $accessToken, $vendorId, $vendorData);
        echo "<pre>";
        print_r($result);
        echo "</pre>";
        break;

    case 'delete':
        $vendorId = '123'; // Ganti dengan ID vendor yang sesuai
        $result = deleteVendor($apiBaseUrl, $accessToken, $vendorId);
        echo "<pre>";
        print_r($result);
        echo "</pre>";
        break;

    default:
        echo "Silakan pilih aksi: get, add, update, atau delete.";
}
?>
