<?php
require_once 'db.php';

header('Content-Type: application/json');
$pdo = db();
$method = $_SERVER['REQUEST_METHOD'];
$action = $_GET['action'] ?? 'list';

function json($data, $code = 200) {
  http_response_code($code);
  echo json_encode($data);
  exit;
}

try {
  if ($method === 'GET' && $action === 'list') {
    $stmt = $pdo->query("SELECT * FROM suppliers ORDER BY id DESC");
    $data = $stmt->fetchAll();
    json(['ok' => true, 'data' => $data]);
  }

  if ($method === 'GET' && $action === 'get') {
    $id = $_GET['id'] ?? 0;
    $stmt = $pdo->prepare("SELECT * FROM suppliers WHERE id = ?");
    $stmt->execute([$id]);
    $item = $stmt->fetch();
    if ($item) {
      json(['ok' => true, 'data' => $item]);
    } else {
      json(['ok' => false, 'error' => 'Not found'], 404);
    }
  }

  if ($method === 'POST' && $action === 'create') {
    $supplier_name = trim($_POST['supplier_name'] ?? '');
    $last_name = trim($_POST['last_name'] ?? '');
    $email = trim($_POST['email'] ?? '');
    $address = trim($_POST['address'] ?? '');
    $country = trim($_POST['country'] ?? '');
    $contact_number = trim($_POST['contact_number'] ?? '');
    $wechat_id = trim($_POST['wechat_id'] ?? '');
    $business_name = trim($_POST['business_name'] ?? '');

    if (!$supplier_name) {
      json(['ok' => false, 'error' => 'Supplier Name is required'], 400);
    }
    if (!$business_name) {
        json(['ok' => false, 'error' => 'Business Name is required'], 400);
    }

    $stmt = $pdo->prepare("INSERT INTO suppliers (supplier_name, last_name, email, address, country, contact_number, wechat_id, business_name) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
    $stmt->execute([$supplier_name, $last_name, $email, $address, $country, $contact_number, $wechat_id, $business_name]);
    
    json(['ok' => true, 'id' => (int)$pdo->lastInsertId()]);
  }
  
  if ($method === 'POST' && $action === 'update') {
      $id = $_POST['id'] ?? 0;
      if (!$id) json(['ok' => false, 'error' => 'ID required'], 400);

      $supplier_name = trim($_POST['supplier_name'] ?? '');
      $last_name = trim($_POST['last_name'] ?? '');
      $email = trim($_POST['email'] ?? '');
      $address = trim($_POST['address'] ?? '');
      $country = trim($_POST['country'] ?? '');
      $contact_number = trim($_POST['contact_number'] ?? '');
      $wechat_id = trim($_POST['wechat_id'] ?? '');
      $business_name = trim($_POST['business_name'] ?? '');

      if (!$supplier_name) json(['ok' => false, 'error' => 'Supplier Name is required'], 400);
      if (!$business_name) json(['ok' => false, 'error' => 'Business Name is required'], 400);

      $stmt = $pdo->prepare("UPDATE suppliers SET supplier_name=?, last_name=?, email=?, address=?, country=?, contact_number=?, wechat_id=?, business_name=?, updated_at=NOW() WHERE id=?");
      $stmt->execute([$supplier_name, $last_name, $email, $address, $country, $contact_number, $wechat_id, $business_name, $id]);
      
      json(['ok' => true]);
  }

  if ($method === 'POST' && $action === 'delete') {
    $id = $_POST['id'] ?? 0;
    if (!$id) json(['ok' => false, 'error' => 'ID required'], 400);
    
    $stmt = $pdo->prepare("DELETE FROM suppliers WHERE id = ?");
    $stmt->execute([$id]);
    json(['ok' => true]);
  }

  json(['ok' => false, 'error' => 'Invalid action'], 400);

} catch (Exception $e) {
  json(['ok' => false, 'error' => $e->getMessage()], 500);
}
