<?php
require_once __DIR__.'/includes/functions.php';
require_once __DIR__.'/includes/account-v281-ui.php';
mj_v281_ensure_schema();
$u=require_user(); $userId=(int)$u['id']; mj_require_user_feature('support_center', $userId, 'Help & Support is not enabled for your account.'); $pageTitle='Help & Support';
$tabs=['tickets'=>'Tickets','new'=>'New Ticket','faq'=>'FAQ','guides'=>'Help Guides','status'=>'Request Status','contact'=>'Contact Admin'];
$tab=(string)($_GET['tab']??'tickets'); if(!isset($tabs[$tab])){$tab='tickets';}
try{
  if($_SERVER['REQUEST_METHOD']==='POST'){
    verify_csrf();
    if((string)($_POST['action']??'')==='create_ticket'){
      $id=mj_v281_create_support_ticket($userId,(string)($_POST['subject']??''),(string)($_POST['category']??'technical'),(string)($_POST['priority']??'normal'),(string)($_POST['message']??''));
      flash('success','Support ticket #'.$id.' created. Admin can reply in Support Center.'); redirect('help-support.php');
    }
  }
}catch(Throwable $e){ flash('error',$e->getMessage()); }
$tickets=mj_v281_support_tickets($userId,30);
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-page-hero rounded-4 mb-4"><div class="container-fluid"><span class="mj-badge mb-2"><i class="bi bi-headset"></i> Help & Support</span><h1 class="fw-bold mb-1">Help & Support</h1><p class="text-muted mb-0">Support tickets, FAQ, help guides, attachments, priority and admin replies.</p></div></div>
  <?php mj_v281_tabs($tabs,$tab,'help-support.php'); ?>

  <?php if($tab==='tickets'): ?>
    <div class="d-flex justify-content-between align-items-center mb-3"><h3 class="h5 fw-bold mb-0">My Support Tickets</h3><a class="btn btn-primary rounded-pill" href="<?=base_url('help-support.php?tab=new')?>"><i class="bi bi-plus-circle me-1"></i>New Ticket</a></div>
    <?php if(!$tickets): mj_v281_empty('No support tickets','Create a ticket for payment, listing, verification, technical or abuse support.','bi-headset'); else: ?><div class="card card-body border-0 rounded-4 shadow-sm"><div class="table-responsive"><table class="table align-middle"><thead><tr><th>#</th><th>Subject</th><th>Category</th><th>Priority</th><th>Status</th><th>Date</th></tr></thead><tbody><?php foreach($tickets as $t): ?><tr><td><?=e((int)$t['id'])?></td><td><b><?=e($t['subject']??'Ticket')?></b><br><small class="text-muted"><?=e(function_exists('mb_strimwidth') ? mb_strimwidth((string)($t['message']??''),0,90,'...') : (strlen((string)($t['message']??''))>90 ? substr((string)($t['message']??''),0,87).'...' : (string)($t['message']??'')))?></small></td><td><?=e($t['category']??'')?></td><td><?=mj_v281_badge((string)($t['priority']??'normal'))?></td><td><?=mj_v281_badge((string)($t['status']??'open'))?></td><td><?=e($t['created_at']??'')?></td></tr><?php endforeach; ?></tbody></table></div></div><?php endif; ?>

  <?php elseif($tab==='new'): ?>
    <form method="post" class="card card-body border-0 rounded-4 shadow-sm"><?=csrf_input()?><input type="hidden" name="action" value="create_ticket"><h3 class="h5 fw-bold">Create Support Ticket</h3><div class="row g-3"><div class="col-md-6"><label class="label">Category</label><select class="form-select" name="category"><option value="payment">Payment</option><option value="listing">Listing</option><option value="verification">Verification</option><option value="technical">Technical</option><option value="abuse">Abuse / Safety</option><option value="billing">Billing / Invoice</option></select></div><div class="col-md-6"><label class="label">Priority</label><select class="form-select" name="priority"><option value="low">Low</option><option value="normal" selected>Normal</option><option value="urgent">Urgent</option></select></div><div class="col-12"><label class="label">Subject</label><input class="form-control" name="subject" required></div><div class="col-12"><label class="label">Message</label><textarea class="form-control" rows="6" name="message" required></textarea></div></div><button class="btn btn-primary rounded-pill mt-3">Submit Ticket</button></form>
    <div class="mt-4"><?php mj_v281_feature_grid([['bi-paperclip','Ticket Attachment','Attachment-ready for screenshots and payment proof.'],['bi-stopwatch','SLA Timer','Admin can track pending too long.'],['bi-pencil-square','Admin Internal Notes','Private admin notes are not visible to user.']]); ?></div>

  <?php elseif($tab==='faq'): ?>
    <div class="accordion" id="faqAcc"><?php $faq=[['How to post listing?','Click Post Free, fill title, category, location, price, photos and submit for admin approval if required.'],['How to renew listing?','Open My Listings and choose renew. Renew request goes to admin, payment may be required before extension.'],['How to sponsor or feature?','Open listing upgrade or Billing & Payments. Admin-enabled payment methods and charges are shown only when active.'],['How verification works?','Submit email, mobile, company, address or document verification in Trust & Verification. Admin approves verified badge.'],['How payment proof works?','Upload proof for renewal/upgrade. Admin approves/rejects and the invoice/receipt history updates.']]; foreach($faq as $i=>$f): ?><div class="accordion-item border-0 rounded-4 shadow-sm mb-2"><h2 class="accordion-header"><button class="accordion-button <?=$i?'collapsed':''?>" type="button" data-bs-toggle="collapse" data-bs-target="#faq<?=$i?>"><?=e($f[0])?></button></h2><div id="faq<?=$i?>" class="accordion-collapse collapse <?=$i?'':'show'?>" data-bs-parent="#faqAcc"><div class="accordion-body text-muted"><?=e($f[1])?></div></div></div><?php endforeach; ?></div>

  <?php elseif($tab==='guides'): ?>
    <?php mj_v281_feature_grid([['bi-card-checklist','Posting Guide','Step-by-step guide to create better listings.'],['bi-shield-check','Safety Guide','Safe dealing tips for buyers and sellers.'],['bi-graph-up-arrow','Seller Growth Guide','Improve photos, title, price, SEO and response rate.'],['bi-building','Company Guide','Create company profile, branch, staff and documents.'],['bi-credit-card','Billing Guide','Renew, sponsor, feature, extra slots, invoices and receipts.'],['bi-patch-check','Verification Guide','Improve trust score and verification badge.']]); ?>

  <?php elseif($tab==='status'): ?>
    <?php mj_v281_feature_grid([['bi-hourglass-split','Pending Review','Tickets, payment proof and verification pending status.'],['bi-check-circle','Approved Items','Payment approved, badge approved, listing approved.'],['bi-x-circle','Rejected Items','Admin reason and resubmit flow.']]); ?>

  <?php elseif($tab==='contact'): ?>
    <div class="mj-mini-card"><h3 class="h5 fw-bold">Contact Admin</h3><p class="text-muted">Use support tickets for clean tracking. Admin can reply inside the system and keep history for each user/company.</p><a class="btn btn-primary rounded-pill" href="<?=base_url('help-support.php?tab=new')?>">Open Ticket</a></div>
  <?php endif; ?>
</section></div></div>
<?php include __DIR__.'/includes/footer.php'; ?>
