<?php
require_once __DIR__.'/includes/functions.php';
require_once __DIR__.'/includes/mj_deal_helpers.php';
$user=require_user(); mj_deal_ensure_schema(); mj_review_ensure_schema(); verify_csrf();
$pageTitle='My Deals';
if($_SERVER['REQUEST_METHOD']==='POST'){
    $id=(int)($_POST['id']??0); $action=(string)($_POST['action']??'');
    try{ $d=one('SELECT * FROM mj_deals WHERE id=? AND user_id=? LIMIT 1',[$id,(int)$user['id']]); if(!$d) throw new RuntimeException('Deal not found.');
        if($action==='pause'){ q('UPDATE mj_deals SET status="paused", updated_at=NOW() WHERE id=? AND user_id=?',[$id,(int)$user['id']]); flash('success','Deal paused.'); }
        elseif($action==='submit'){ $st=setting('deal_auto_approve','0')==='1'?'active':'pending'; q('UPDATE mj_deals SET status=?, updated_at=NOW() WHERE id=? AND user_id=?',[$st,$id,(int)$user['id']]); flash('success',$st==='active'?'Deal is active.':'Deal submitted for admin approval.'); }
        elseif($action==='hide'){ q('UPDATE mj_deals SET status="hidden", updated_at=NOW() WHERE id=? AND user_id=?',[$id,(int)$user['id']]); flash('success','Deal hidden.'); }
    }catch(Throwable $e){ flash('error',$e->getMessage()); }
    redirect('my-deals.php');
}
$rows=[]; try{ $rows=all_rows("SELECT d.*, co.name company_name, c.name country_name, (SELECT COUNT(*) FROM mj_deal_revisions dr WHERE dr.deal_id=d.id AND dr.status='pending') pending_update_count FROM mj_deals d LEFT JOIN mj_companies co ON co.id=d.company_id LEFT JOIN mj_countries c ON c.id=d.country_id WHERE d.user_id=? ORDER BY d.id DESC",[(int)$user['id']]); }catch(Throwable $e){}
$summary=['total'=>count($rows),'active'=>0,'pending'=>0,'enq'=>0]; foreach($rows as $r){ if(in_array($r['status'],['active','approved'],true))$summary['active']++; if($r['status']==='pending')$summary['pending']++; $summary['enq']+=(int)($r['enquiry_count']??0); }
include __DIR__.'/includes/header.php';
?>
<section class="mj-section"><div class="container"><div class="mj-center-shell"><?php include __DIR__.'/includes/account-sidebar.php'; ?><div class="mj-center-main">
<div class="d-flex flex-wrap justify-content-between align-items-center mb-4"><div><span class="mj-badge mb-2"><i class="bi bi-fire"></i> Display deals</span><h1 class="h2 fw-bold mb-1">My Deals</h1><p class="text-muted mb-0">Create deals for enquiry, WhatsApp/call clicks and seller leads. No customer payment is collected by the website.</p></div><a class="btn btn-primary rounded-pill" href="<?=base_url('deal-create.php')?>"><i class="bi bi-plus-circle me-1"></i>Add Deal</a></div>
<div class="row g-3 mb-4"><div class="col-md-3"><div class="mj-stat-tile"><small>Total Deals</small><b><?=e($summary['total'])?></b></div></div><div class="col-md-3"><div class="mj-stat-tile"><small>Active</small><b><?=e($summary['active'])?></b></div></div><div class="col-md-3"><div class="mj-stat-tile"><small>Pending</small><b><?=e($summary['pending'])?></b></div></div><div class="col-md-3"><a class="mj-stat-tile d-block text-decoration-none" href="<?=base_url('my-leads.php?source=deal')?>"><small>Enquiries</small><b><?=e($summary['enq'])?></b><span class="small text-primary fw-bold">View details</span></a></div></div>
<div class="mj-center-card"><div class="card-body p-0"><div class="table-responsive"><table class="table mb-0 align-middle"><thead><tr><th>Deal</th><th>Status</th><th>Performance</th><th>Expiry</th><th>Action</th></tr></thead><tbody><?php if(!$rows): ?><tr><td colspan="5" class="text-center text-muted py-5">No deals yet. Create your first display deal.</td></tr><?php endif; ?><?php foreach($rows as $d): ?><tr><td><b><?=e($d['title'])?></b><br><small class="text-muted"><?=e($d['company_name']?:'Direct Seller')?> · <?=e($d['country_name']?:'')?></small></td><td><span class="badge bg-<?=in_array($d['status'],['active','approved'],true)?'success':($d['status']==='pending'?'warning':'secondary')?>"><?=e(ucwords($d['status']))?></span><?php if((int)($d['pending_update_count']??0)>0): ?><br><span class="badge bg-info text-dark mt-1">Pending Update Review</span><?php endif; ?></td><td><small>Views <?=e($d['view_count']??0)?> · <a href="<?=base_url('my-leads.php?source=deal&q='.urlencode((string)$d['title']))?>">Enquiries <?=e($d['enquiry_count']??0)?></a><br>WA <?=e($d['whatsapp_click_count']??0)?> · Calls <?=e($d['call_click_count']??0)?></small></td><td><small><?=e($d['end_at']?date('d M Y',strtotime($d['end_at'])):'No expiry')?></small></td><td><a class="btn btn-sm btn-outline-primary" href="<?=base_url('deal-edit.php?id='.(int)$d['id'])?>">Edit</a> <a class="btn btn-sm btn-outline-info" target="_blank" href="<?=base_url('deal.php?id='.(int)$d['id'])?>">View</a> <a class="btn btn-sm btn-outline-dark" href="<?=base_url('my-leads.php?source=deal&q='.urlencode((string)$d['title']))?>">Leads</a> <a class="btn btn-sm btn-outline-success" href="<?=base_url('billing-payments.php')?>">Promote</a><form method="post" class="d-inline"><?=csrf_input()?><input type="hidden" name="id" value="<?=(int)$d['id']?>"><?php if(in_array($d['status'],['draft','paused','hidden','rejected'],true)): ?><button class="btn btn-sm btn-success" name="action" value="submit">Submit</button><?php else: ?><button class="btn btn-sm btn-warning" name="action" value="pause">Pause</button><?php endif; ?></form></td></tr><?php endforeach; ?></tbody></table></div></div></div>
</div></div></div></section>
<?php include __DIR__.'/includes/footer.php'; ?>
