<?php
// api-payment-methods.php
require_once __DIR__ . '/config.php';

$method = $_SERVER['REQUEST_METHOD'];
$db = getDB();

switch ($method) {
    case 'GET':
        try {
            $stmt = $db->query('SELECT * FROM payment_methods ORDER BY sort_order, id');
            $rows = $stmt->fetchAll();
        } catch (Exception $e) {
            $rows = [];
        }
        $methods = [];
        foreach ($rows as $row) {
            $methods[] = [
                'id' => (int)$row['id'],
                'name' => $row['name'],
                'type' => $row['type'],
                'bank_name' => $row['bank_name'],
                'account_name' => $row['account_name'],
                'account_number' => $row['account_number'],
                'is_active' => (int)$row['is_active'],
                'sort_order' => (int)$row['sort_order'],
            ];
        }
        jsonResponse(['status' => 'success', 'data' => $methods]);
        break;

    case 'POST':
        requireAdmin();
        $input = json_decode(file_get_contents('php://input'), true);
        $name = trim($input['name'] ?? '');
        if (empty($name)) {
            jsonResponse(['status' => 'error', 'message' => 'Nama metode wajib diisi'], 400);
        }
        $stmt = $db->prepare('INSERT INTO payment_methods (name, type, bank_name, account_name, account_number, is_active, sort_order) VALUES (?,?,?,?,?,?,?)');
        $stmt->execute([
            $name,
            $input['type'] ?? 'cash',
            $input['bank_name'] ?: null,
            $input['account_name'] ?: null,
            $input['account_number'] ?: null,
            (int)($input['is_active'] ?? 1),
            (int)($input['sort_order'] ?? 0),
        ]);
        jsonResponse(['status' => 'success', 'id' => (int)$db->lastInsertId()]);
        break;

    case 'PUT':
        requireAdmin();
        $input = json_decode(file_get_contents('php://input'), true);
        $id = (int)($input['id'] ?? 0);
        if ($id <= 0) {
            jsonResponse(['status' => 'error', 'message' => 'ID tidak valid'], 400);
        }
        $stmt = $db->prepare('UPDATE payment_methods SET name=?, type=?, bank_name=?, account_name=?, account_number=?, is_active=?, sort_order=? WHERE id=?');
        $stmt->execute([
            trim($input['name'] ?? ''),
            $input['type'] ?? 'cash',
            $input['bank_name'] ?: null,
            $input['account_name'] ?: null,
            $input['account_number'] ?: null,
            (int)($input['is_active'] ?? 1),
            (int)($input['sort_order'] ?? 0),
            $id,
        ]);
        jsonResponse(['status' => 'success']);
        break;

    case 'DELETE':
        requireAdmin();
        $input = json_decode(file_get_contents('php://input'), true);
        $id = (int)($input['id'] ?? 0);
        if ($id <= 0) {
            jsonResponse(['status' => 'error', 'message' => 'ID tidak valid'], 400);
        }
        $stmt = $db->prepare('DELETE FROM payment_methods WHERE id = ?');
        $stmt->execute([$id]);
        jsonResponse(['status' => 'success']);
        break;
}