<?php
// api_master.php
require_once 'db.php';

header('Content-Type: application/json');
$action = $_GET['action'] ?? '';

try {
    if ($action === 'get_products') {
        $stmt = $pdo->query("SELECT * FROM products ORDER BY id DESC");
        echo json_encode(['status' => 'success', 'data' => $stmt->fetchAll()]);
    } 
	elseif ($action === 'save_product') {
        $id = $_POST['id'] ?? '';
        $code = $_POST['product_code'];
        $name = $_POST['product_name'];
        $unit = $_POST['unit'];
        $price = $_POST['selling_price'];

        if (!empty($id)) {
            // Update Produk
            $stmt = $pdo->prepare("UPDATE products SET product_code=?, product_name=?, unit=?, selling_price=? WHERE id=?");
            $stmt->execute([$code, $name, $unit, $price, $id]);
        } else {
            // Insert Produk Baru
            $stmt = $pdo->prepare("INSERT INTO products (product_code, product_name, unit, selling_price) VALUES (?, ?, ?, ?)");
            $stmt->execute([$code, $name, $unit, $price]);
        }
        echo json_encode(['status' => 'success', 'message' => 'Produk berhasil disimpan']);
    }
    elseif ($action === 'delete_product') {
        $id = $_POST['id'];
        $stmt = $pdo->prepare("DELETE FROM products WHERE id = ?");
        $stmt->execute([$id]);
        echo json_encode(['status' => 'success', 'message' => 'Produk berhasil dihapus']);
    }
    elseif ($action === 'get_customers') {
        $stmt = $pdo->query("SELECT * FROM customers ORDER BY id DESC");
        echo json_encode(['status' => 'success', 'data' => $stmt->fetchAll()]);
    }
    elseif ($action === 'save_customer') {
        $id = $_POST['id'] ?? '';
        $code = $_POST['customer_code'];
        $name = $_POST['customer_name'];
        $contact = $_POST['contact_person'];
        $phone = $_POST['phone'];
        $address = $_POST['address'];
        $limit = $_POST['credit_limit'];

        if (!empty($id)) {
            // Update
            $stmt = $pdo->prepare("UPDATE customers SET customer_code=?, customer_name=?, contact_person=?, phone=?, address=?, credit_limit=? WHERE id=?");
            $stmt->execute([$code, $name, $contact, $phone, $address, $limit, $id]);
        } else {
            // Insert
            $stmt = $pdo->prepare("INSERT INTO customers (customer_code, customer_name, contact_person, phone, address, credit_limit) VALUES (?, ?, ?, ?, ?, ?)");
            $stmt->execute([$code, $name, $contact, $phone, $address, $limit]);
        }
        echo json_encode(['status' => 'success', 'message' => 'Pelanggan berhasil disimpan']);
    }
    elseif ($action === 'delete_customer') {
        $id = $_POST['id'];
        $stmt = $pdo->prepare("DELETE FROM customers WHERE id = ?");
        $stmt->execute([$id]);
        echo json_encode(['status' => 'success', 'message' => 'Pelanggan berhasil dihapus']);
    }
} catch (Exception $e) {
    echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
}
?>