<?php
require_once __DIR__ . '/includes/admin_bootstrap.php';
$msg='';
$action=$_GET['action']??'list';
$editId=(int)($_GET['id']??0);

if($action==='delete'&&$editId){$pdo->prepare("DELETE FROM payment_methods WHERE id=?")->execute([$editId]);redirect(BASE_URL.'/admin/payments.php?deleted=1');}
if($action==='toggle'&&$editId){$pdo->prepare("UPDATE payment_methods SET is_active=1-is_active WHERE id=?")->execute([$editId]);redirect(BASE_URL.'/admin/payments.php');}

if($_SERVER['REQUEST_METHOD']==='POST'&&isset($_POST['save_payment'])){
    $name=trim($_POST['name']??'');$number=trim($_POST['number']??'');$instr=trim($_POST['instructions']??'');
    $icon=trim($_POST['icon']??'fa-mobile-alt');$sort=(int)($_POST['sort_order']??0);$active=isset($_POST['is_active'])?1:0;
    $mid=(int)($_POST['method_id']??0);
    if(!$name||!$number){$msg='<div class="alert alert-error">নাম ও নম্বর আবশ্যক।</div>';}
    else{
        if($mid)$pdo->prepare("UPDATE payment_methods SET name=?,number=?,instructions=?,icon=?,sort_order=?,is_active=? WHERE id=?")->execute([$name,$number,$instr,$icon,$sort,$active,$mid]);
        else $pdo->prepare("INSERT INTO payment_methods(name,number,instructions,icon,sort_order,is_active)VALUES(?,?,?,?,?,?)")->execute([$name,$number,$instr,$icon,$sort,$active]);
        $msg='<div class="alert alert-success"><i class="fas fa-check-circle"></i> পেমেন্ট পদ্ধতি সংরক্ষণ সফল।</div>';
        $action='list';$editId=0;
    }
}

$methods=$pdo->query("SELECT * FROM payment_methods ORDER BY sort_order,id")->fetchAll();
$editing=null;
if($editId&&$action==='edit'){$es=$pdo->prepare("SELECT * FROM payment_methods WHERE id=?");$es->execute([$editId]);$editing=$es->fetch();}
include __DIR__.'/includes/admin_header.php';
?>
<h2>পেমেন্ট পদ্ধতি ব্যবস্থাপনা</h2>
<?=$msg?>
<?php if(isset($_GET['deleted'])):?><div class="alert alert-success">মুছে ফেলা হয়েছে।</div><?php endif;?>

<div style="display:grid;grid-template-columns:1fr 380px;gap:20px;align-items:start;">
    <div class="card">
        <div class="card-header"><h3><i class="fas fa-money-bill-wave"></i> সকল পেমেন্ট পদ্ধতি</h3><a href="?action=add" class="topbar-btn btn-gold"><i class="fas fa-plus"></i> নতুন</a></div>
        <div class="table-wrap">
            <table>
                <thead><tr><th>আইকন</th><th>নাম</th><th>নম্বর</th><th>ক্রম</th><th>অবস্থা</th><th></th></tr></thead>
                <tbody>
                <?php foreach($methods as $m):?>
                <tr>
                    <td><i class="fas <?=e($m['icon'])?>" style="font-size:1.5rem;color:var(--navy);"></i></td>
                    <td><strong><?=e($m['name'])?></strong></td>
                    <td style="font-family:monospace;font-weight:600;"><?=e($m['number'])?></td>
                    <td><?=$m['sort_order']?></td>
                    <td><span class="badge <?=$m['is_active']?'badge-active':'badge-inactive'?>"><?=$m['is_active']?'সক্রিয়':'বন্ধ'?></span></td>
                    <td><div class="actions">
                        <a href="?action=edit&id=<?=$m['id']?>" class="topbar-btn btn-navy"><i class="fas fa-edit"></i></a>
                        <a href="?action=toggle&id=<?=$m['id']?>" class="topbar-btn btn-gray" title="<?=$m['is_active']?'বন্ধ':'চালু'?> করুন"><i class="fas fa-power-off"></i></a>
                        <a href="?action=delete&id=<?=$m['id']?>" class="topbar-btn btn-red" data-confirm="মুছে ফেলবেন?"><i class="fas fa-trash"></i></a>
                    </div></td>
                </tr>
                <?php endforeach;?>
                </tbody>
            </table>
        </div>
        <div style="padding:14px 18px;background:rgba(212,175,55,.08);border-top:1px solid var(--g100);">
            <strong style="font-size:.88rem;color:var(--navy);">চেকআউটে প্রদর্শন:</strong>
            <span style="font-size:.82rem;color:var(--g600);"> সক্রিয় পদ্ধতিগুলো চেকআউট পেজে গ্রাহকদের দেখাবে।</span>
        </div>
    </div>

    <div class="card">
        <div class="card-header"><h3><?=$editing?'সম্পাদনা':'নতুন পেমেন্ট পদ্ধতি'?></h3><?php if($editing):?><a href="?" class="topbar-btn btn-gray">বাতিল</a><?php endif;?></div>
        <div class="card-body">
            <form method="POST">
                <input type="hidden" name="method_id" value="<?=$editing['id']??0?>">
                <div class="form-group">
                    <label class="form-label">নাম * <span style="color:var(--g600);font-size:.78rem;">(যেমন: বিকাশ)</span></label>
                    <input type="text" class="form-control" name="name" value="<?=e($editing['name']??'')?>" required>
                </div>
                <div class="form-group">
                    <label class="form-label">নম্বর * <span style="color:var(--g600);font-size:.78rem;">(Send Money নম্বর)</span></label>
                    <input type="text" class="form-control" name="number" value="<?=e($editing['number']??'')?>" placeholder="01XXXXXXXXX" required>
                </div>
                <div class="form-group">
                    <label class="form-label">আইকন <span style="color:var(--g600);font-size:.78rem;">(FontAwesome class)</span></label>
                    <select class="form-control" name="icon">
                        <option value="fa-mobile-alt" <?=($editing['icon']??'')==='fa-mobile-alt'?'selected':''?>>📱 বিকাশ (fa-mobile-alt)</option>
                        <option value="fa-wallet"     <?=($editing['icon']??'')==='fa-wallet'    ?'selected':''?>>👛 নগদ (fa-wallet)</option>
                        <option value="fa-rocket"     <?=($editing['icon']??'')==='fa-rocket'    ?'selected':''?>>🚀 রকেট (fa-rocket)</option>
                        <option value="fa-university" <?=($editing['icon']??'')==='fa-university'?'selected':''?>>🏦 ব্যাংক (fa-university)</option>
                        <option value="fa-credit-card"<?=($editing['icon']??'')==='fa-credit-card'?'selected':''?>>💳 কার্ড</option>
                        <option value="fa-money-bill" <?=($editing['icon']??'')==='fa-money-bill'?'selected':''?>>💵 ক্যাশ</option>
                    </select>
                </div>
                <div class="form-group">
                    <label class="form-label">নির্দেশনা <span style="color:var(--g600);font-size:.78rem;">(গ্রাহককে যা দেখাবে)</span></label>
                    <textarea class="form-control" name="instructions" rows="3" placeholder="Send Money করুন। Reference এ অর্ডার নম্বর লিখুন।"><?=e($editing['instructions']??'')?></textarea>
                </div>
                <div style="display:grid;grid-template-columns:1fr 1fr;gap:12px;">
                    <div class="form-group">
                        <label class="form-label">ক্রম (sort)</label>
                        <input type="number" class="form-control" name="sort_order" value="<?=$editing['sort_order']??count($methods)+1?>" min="0">
                    </div>
                    <div class="form-group" style="padding-top:24px;">
                        <label class="form-check"><input type="checkbox" name="is_active" value="1" <?=($editing['is_active']??1)?'checked':''?>><span> সক্রিয়</span></label>
                    </div>
                </div>
                <button type="submit" name="save_payment" class="topbar-btn btn-gold" style="width:100%;justify-content:center;padding:11px;">
                    <i class="fas fa-save"></i> সংরক্ষণ করুন
                </button>
            </form>
        </div>
    </div>
</div>
<?php include __DIR__.'/includes/admin_footer.php';?>
