<?php require_once __DIR__.'/includes/functions.php'; $u=require_user(); $pageTitle='My Account';
$stats=['listings'=>safe_count('SELECT COUNT(*) c FROM mj_listings WHERE user_id=?',[$u['id']]),'companies'=>safe_count('SELECT COUNT(*) c FROM mj_companies WHERE user_id=?',[$u['id']]),'offers'=>safe_count('SELECT COUNT(*) c FROM mj_offers WHERE buyer_id=? OR seller_id=?',[$u['id'],$u['id']]),'chats'=>safe_count('SELECT COUNT(*) c FROM mj_chats WHERE buyer_id=? OR seller_id=?',[$u['id'],$u['id']]),'orders'=>safe_count('SELECT COUNT(*) c FROM mj_orders WHERE buyer_id=? OR seller_id=?',[$u['id'],$u['id']]),'notifications'=>safe_count('SELECT COUNT(*) c FROM mj_notifications WHERE user_id=? AND is_read=0',[$u['id']])];
$limits=function_exists('mj_v300_user_limits')?mj_v300_user_limits((int)$u['id']):['max_listings'=>0,'max_companies'=>0,'max_deals'=>0];
$dealLimit=function_exists('mj_v300_deal_limit_status')?mj_v300_deal_limit_status((int)$u['id']):['used'=>0,'max'=>0,'remaining'=>0];
$perfData=function_exists('mj_v300_user_performance_data')?mj_v300_user_performance_data((int)$u['id']):[];
$recentListings=all_rows('SELECT l.*, c.name country, ci.name city, (SELECT image_path FROM mj_listing_images i WHERE i.listing_id=l.id ORDER BY sort_order,id LIMIT 1) image_path FROM mj_listings l LEFT JOIN mj_countries c ON c.id=l.country_id LEFT JOIN mj_cities ci ON ci.id=l.city_id WHERE l.user_id=? ORDER BY l.id DESC LIMIT 4',[$u['id']]);
include __DIR__.'/includes/header.php'; ?>
<div class="container py-5"><div class="sidebar-layout"><?php include __DIR__.'/includes/account-sidebar.php'; ?><section>
  <div class="mj-user-dashboard-hero mb-4"><div class="row align-items-center"><div class="col-lg-8"><span class="badge text-bg-light text-primary rounded-pill mb-2"><i class="bi bi-person-check me-1"></i>User Dashboard</span><h1 class="fw-bold mb-1">Welcome, <?=e($u['name'])?></h1><p class="text-white-50 mb-3">Manage listings, companies, messages, CRM leads, billing, trust, verification, privacy and support from one place.</p><div class="d-flex gap-2 flex-wrap"><a class="btn btn-light" href="<?=base_url('post-listing.php')?>"><i class="bi bi-plus-circle me-1"></i>Post Listing</a><a class="btn btn-outline-light" href="<?=base_url('add-company.php')?>"><i class="bi bi-building-add me-1"></i>Add Company</a></div></div><div class="col-lg-4"><div class="bg-white bg-opacity-10 rounded-4 p-3"><div class="d-flex justify-content-between"><span>Trust Score</span><b><?=e($u['trust_score'])?>%</b></div><div class="mj-trust-meter mt-2"><span style="width:<?=min(100,max(0,(int)$u['trust_score']))?>%"></span></div><small class="text-white-50">Improve by verifying email, mobile and completing successful deals.</small></div></div></div></div>
  <div class="grid grid-4 mb-4"><?php foreach($stats as $k=>$v): ?><div class="stat"><b><?=number_format($v)?></b><br><span class="muted"><?=e(ucwords($k))?></span></div><?php endforeach; ?></div>
  <div class="row g-3 mb-4"><div class="col-md-4"><?php mj_v300_render_limit_card('Listings', (int)$stats['listings'], (int)$limits['max_listings'], base_url('billing-payments.php?tab=cart'), 'bi-tags'); ?></div><div class="col-md-4"><?php mj_v300_render_limit_card('Companies', (int)$stats['companies'], (int)$limits['max_companies'], base_url('billing-payments.php?tab=cart'), 'bi-building'); ?></div><div class="col-md-4"><?php mj_v300_render_limit_card('Deals', (int)$dealLimit['used'], (int)$dealLimit['max'], base_url('billing-payments.php?tab=cart'), 'bi-fire'); ?></div></div>
  <div class="mj-v3-chart-card mb-4" data-chart-type="bar" data-mj-v3-chart="<?=mj_v300_chart_data_attr($perfData)?>"><h3>Seller performance</h3><p>Views, enquiries and clicks from your listings/deals.</p></div>
  <div class="row g-4 mb-4">
    <?php $tiles=[['post-listing.php','bi-plus-circle','Post Listing','Create products, services, vehicles, jobs, rentals or courses.'],['add-company.php','bi-building-add','Add Company','Add shops, restaurants, services and business branches.'],['near-me.php','bi-geo-alt','Near Me','See area-priority listings and companies from your connection area.'],['crm-leads.php','bi-people','CRM / Leads','Lead inbox, follow-up reminders and customer timeline.'],['billing-payments.php','bi-credit-card-2-front','Billing & Payments','Wallet, invoices, payment proof and receipts.'],['account-center.php','bi-person-gear','Account Center','Profile, company, privacy, security and automation.']]; foreach($tiles as $t): ?><div class="col-md-6 col-xl-4"><a class="mj-account-tile" href="<?=base_url($t[0])?>"><i class="bi <?=e($t[1])?>"></i><h5 class="fw-bold mt-3"><?=e($t[2])?></h5><p class="text-muted mb-0"><?=e($t[3])?></p></a></div><?php endforeach; ?>
  </div>
  <div class="card card-body border-0 rounded-4 shadow-sm"><div class="d-flex justify-content-between align-items-center mb-3"><h2 class="h4 fw-bold mb-0">Recent Listings</h2><a href="<?=base_url('my-listings.php')?>">View all</a></div><?php if(!$recentListings): ?><div class="text-muted">No listings yet.</div><?php else: ?><div class="row g-3"><?php foreach($recentListings as $r): ?><div class="col-md-6 col-xl-3"><?php include __DIR__.'/partials/listing-card.php'; ?></div><?php endforeach; ?></div><?php endif; ?></div>
</section></div></div>
<?php include __DIR__.'/includes/footer.php'; ?>
