<?php $PAGE_TITLE='APIs'; require __DIR__.'/_layout.php';
if($_SERVER['REQUEST_METHOD']==='POST'){csrf_check();
  $id=(int)($_POST['id']??0);
  $data=[$_POST['name'],$_POST['url_template'],$_POST['api_key'],$_POST['api_id'],$_POST['method'],isset($_POST['active'])?1:0];
  if($id) {$data[]=$id; $pdo->prepare("UPDATE apis SET name=?,url_template=?,api_key=?,api_id=?,method=?,active=? WHERE id=?")->execute($data);}
  else $pdo->prepare("INSERT INTO apis(name,url_template,api_key,api_id,method,active) VALUES(?,?,?,?,?,?)")->execute($data);
  redirect('apis.php');
}
if(isset($_GET['del'])){$pdo->prepare("DELETE FROM apis WHERE id=?")->execute([(int)$_GET['del']]); redirect('apis.php');}
$edit=null; if(isset($_GET['edit'])){$s=$pdo->prepare("SELECT * FROM apis WHERE id=?"); $s->execute([(int)$_GET['edit']]); $edit=$s->fetch();}
$rows=$pdo->query("SELECT * FROM apis ORDER BY id DESC")->fetchAll();
?>
<h2 style="margin-bottom:14px">🔌 API সমূহ</h2>
<div class="card" style="margin-bottom:20px">
<h3><?=$edit?'✏️ Edit':'➕ নতুন'?> API</h3>
<div class="alert info" style="margin-top:10px">
<b>URL Template</b>-এ placeholder ব্যবহার করুন: <code>{NID}</code>, <code>{DOB}</code>, <code>{KEY}</code>, <code>{ID}</code><br>
উদাহরণ: <code>https://example.com/api?nid={NID}&dob={DOB}&key={KEY}</code>
</div>
<form method="post" class="grid g2"><?=csrf_field()?><input type="hidden" name="id" value="<?=e($edit['id']??'')?>">
<div class="field"><label>Name</label><input class="input" name="name" value="<?=e($edit['name']??'')?>" required></div>
<div class="field"><label>Method</label>
<select name="method" class="input">
<option <?=($edit['method']??'')==='GET'?'selected':''?>>GET</option>
<option <?=($edit['method']??'')==='POST'?'selected':''?>>POST</option>
</select></div>
<div class="field" style="grid-column:1/-1"><label>URL Template</label><input class="input" name="url_template" value="<?=e($edit['url_template']??'')?>" required></div>
<div class="field"><label>API Key</label><input class="input" name="api_key" value="<?=e($edit['api_key']??'')?>"></div>
<div class="field"><label>API ID (ঐচ্ছিক)</label><input class="input" name="api_id" value="<?=e($edit['api_id']??'')?>"></div>
<div style="grid-column:1/-1"><label style="display:flex;gap:6px;align-items:center"><input type="checkbox" name="active" <?=($edit['active']??1)?'checked':''?>> Active</label>
<button class="btn" style="margin-top:10px"><?=$edit?'Update':'Add'?></button>
<?php if($edit):?><a class="btn ghost" href="apis.php">Cancel</a><?php endif;?>
</div>
</form></div>

<table><thead><tr><th>#</th><th>Name</th><th>URL</th><th>Method</th><th>Active</th><th>Actions</th></tr></thead><tbody>
<?php foreach($rows as $r):?>
<tr><td>#<?=$r['id']?></td><td><b><?=e($r['name'])?></b></td>
<td style="max-width:400px;word-break:break-all;font-family:monospace;font-size:12px"><?=e($r['url_template'])?></td>
<td><?=e($r['method'])?></td>
<td><span class="badge <?=$r['active']?'ok':'fail'?>"><?=$r['active']?'Active':'Off'?></span></td>
<td><a class="btn sm" href="?edit=<?=$r['id']?>">✏️</a>
<a class="btn sm danger" href="?del=<?=$r['id']?>" onclick="return confirm('Delete?')">🗑️</a></td></tr>
<?php endforeach;?>
</tbody></table>
<?php require __DIR__.'/_footer.php';
