<?php
require_once 'config.inc.php';require_login();
$action=$_GET['action']??'list';$id=(int)($_GET['id']??0);
if($_SERVER['REQUEST_METHOD']==='POST'){check_csrf();$op=$_POST['op']??'';
 if($op==='save'){
  $id=(int)$_POST['id'];$pid=(int)$_POST['product_id'];$bid=(int)$_POST['brand_id'];$model=trim($_POST['model_name']);
  try{if($id){$s=db()->prepare("UPDATE compatible_models SET product_id=?,brand_id=?,model_name=? WHERE id=?");$s->bind_param("iisi",$pid,$bid,$model,$id);}
  else{$s=db()->prepare("INSERT INTO compatible_models(product_id,brand_id,model_name) VALUES(?,?,?)");$s->bind_param("iis",$pid,$bid,$model);} $s->execute();flash('success','Compatible model saved.');}
  catch(Throwable $e){flash('danger','This compatibility entry already exists or is invalid.');}redirect('compatible.php');
 }
 if($op==='delete'){$id=(int)$_POST['id'];$s=db()->prepare("DELETE FROM compatible_models WHERE id=?");$s->bind_param("i",$id);$s->execute();flash('success','Deleted.');redirect('compatible.php');}
}
$edit=null;if($action==='edit'&&$id){$s=db()->prepare("SELECT * FROM compatible_models WHERE id=?");$s->bind_param("i",$id);$s->execute();$edit=$s->get_result()->fetch_assoc();}
$pageTitle='Compatible Models';include 'partials/header.php';
$brands=db()->query("SELECT id,name FROM brands ORDER BY name");$products=db()->query("SELECT p.id,p.display_model,b.name brand FROM products p JOIN brands b ON b.id=p.brand_id ORDER BY b.name,p.display_model");?>
<div class="row g-4"><div class="col-lg-4"><div class="card p-4"><h5><?=$edit?'Edit':'Add'?> Compatibility</h5><form method="post"><input type="hidden" name="csrf" value="<?=e(csrf())?>"><input type="hidden" name="op" value="save"><input type="hidden" name="id" value="<?=e($edit['id']??0)?>">
<label class="form-label mt-3">Display</label><select class="form-select" name="product_id" required><option value="">Select display</option><?php while($p=$products->fetch_assoc()):?><option value="<?=$p['id']?>" <?=$p['id']==($edit['product_id']??0)?'selected':''?>><?=e($p['brand'].' - '.$p['display_model'])?></option><?php endwhile;?></select>
<label class="form-label mt-3">Compatible Brand</label><select class="form-select" name="brand_id" required><option value="">Select brand</option><?php while($b=$brands->fetch_assoc()):?><option value="<?=$b['id']?>" <?=$b['id']==($edit['brand_id']??0)?'selected':''?>><?=e($b['name'])?></option><?php endwhile;?></select>
<label class="form-label mt-3">Compatible Model</label><input class="form-control" name="model_name" value="<?=e($edit['model_name']??'')?>" placeholder="e.g. Realme 8" required>
<button class="btn btn-primary mt-4">Save Compatibility</button></form></div></div>
<div class="col-lg-8"><div class="card p-4"><h5>Compatibility List</h5><table class="table align-middle"><thead><tr><th>Display</th><th>Compatible Brand</th><th>Model</th><th></th></tr></thead><tbody><?php $r=db()->query("SELECT c.*,p.display_model,db.name display_brand,cb.name compat_brand FROM compatible_models c JOIN products p ON p.id=c.product_id JOIN brands db ON db.id=p.brand_id JOIN brands cb ON cb.id=c.brand_id ORDER BY c.id DESC");while($x=$r->fetch_assoc()):?><tr><td><?=e($x['display_brand'].' - '.$x['display_model'])?></td><td><?=e($x['compat_brand'])?></td><td><?=e($x['model_name'])?></td><td><a class="btn btn-sm btn-outline-secondary" href="?action=edit&id=<?=$x['id']?>">Edit</a><form class="d-inline" method="post"><input type="hidden" name="csrf" value="<?=e(csrf())?>"><input type="hidden" name="op" value="delete"><input type="hidden" name="id" value="<?=$x['id']?>"><button class="btn btn-sm btn-outline-danger" data-confirm="Delete?">Delete</button></form></td></tr><?php endwhile;?></tbody></table></div></div></div>
<?php include 'partials/footer.php';?>
