<?php
session_start();
if (!isset($_SESSION['user_id'])) { http_response_code(403); exit('Unauthorized'); }
require_once '../connect.php';
$account_id=(int)$_SESSION['account_id'];
$type=preg_replace('/[^a-z_]/','',$_GET['type'] ?? 'customers');
$map=[
 'customers'=>[
  'file'=>'customers','sql'=>"SELECT c.id,c.customer_name,c.business_name,c.phone,c.email,co.name country,c.address,c.notes,c.priority,cs.name source,u.name added_by,c.date_created FROM customers c LEFT JOIN countries co ON co.id=c.country_id LEFT JOIN customer_sources cs ON cs.id=c.source_id LEFT JOIN users u ON u.id=c.created_by_user_id WHERE c.account_id=$account_id ORDER BY c.id DESC"],
 'leads'=>[
  'file'=>'leads','sql'=>"SELECT l.id,l.title,c.customer_name,i.name item,s.name stage,l.opportunity_value,l.priority,u.name assigned_to,cu.name added_by,l.datetime_created FROM leads l LEFT JOIN customers c ON c.id=l.customer_id LEFT JOIN items i ON i.id=l.item_id LEFT JOIN stage s ON s.id=l.stage_id LEFT JOIN users u ON u.id=l.assigned_user_id LEFT JOIN users cu ON cu.id=l.created_by_user_id WHERE l.account_id=$account_id AND l.datetime_deleted IS NULL ORDER BY l.id DESC"],
 'items'=>[
  'file'=>'items','sql'=>"SELECT i.id,i.name,it.name type,c.name category,i.description,i.price,i.currency,u.name added_by,i.date_created FROM items i LEFT JOIN item_type it ON it.id=i.items_type_id LEFT JOIN categories c ON c.id=i.category_id LEFT JOIN users u ON u.id=i.added_by WHERE i.account_id=$account_id ORDER BY i.id DESC"],
 'quotations'=>[
  'file'=>'quotations','sql'=>"SELECT q.id,c.customer_name,i.name item,q.quantity,q.unit_price,q.discount,q.tva_percent,q.total,q.status,u.name added_by,q.date_created,q.date_updated FROM quotations q LEFT JOIN customers c ON c.id=q.customer_id LEFT JOIN items i ON i.id=q.item_id LEFT JOIN users u ON u.id=q.created_by WHERE q.account_id=$account_id ORDER BY q.id DESC"],
 'audience'=>[
  'file'=>'audience','sql'=>"SELECT a.id,a.name,a.description,a.customer_count,u.name added_by,a.date_created,a.date_updated FROM audience a LEFT JOIN users u ON u.id=a.added_by WHERE a.account_id=$account_id ORDER BY a.id DESC"],
 'campaigns'=>[
  'file'=>'campaigns','sql'=>"SELECT c.id,c.name,c.description,c.target_type,a.name audience,c.customer_count,c.is_sent,c.sent_count,u.name added_by,c.date_created FROM campaign c LEFT JOIN audience a ON a.id=c.audience_id LEFT JOIN users u ON u.id=c.added_by WHERE c.account_id=$account_id ORDER BY c.id DESC"],
 'occasions'=>[
  'file'=>'occasions','sql'=>"SELECT o.id,cu.customer_name,cu.phone,o.name,o.occasion_date,o.notes,u.name added_by,o.date_created FROM occasions o LEFT JOIN customers cu ON cu.id=o.customer_id LEFT JOIN users u ON u.id=o.added_by WHERE o.account_id=$account_id ORDER BY o.id DESC"]
];
if(!isset($map[$type])) $type='customers';
$res=$conn->query($map[$type]['sql']);
$filename=$map[$type]['file'].'_'.date('Ymd_His').'.xls';
header('Content-Type: application/vnd.ms-excel; charset=utf-8');
header('Content-Disposition: attachment; filename="'.$filename.'"');
echo "<html><head><meta charset='utf-8'></head><body><table border='1'>";
if($res && $res->num_rows){
  $first=$res->fetch_assoc(); echo '<tr>'; foreach(array_keys($first) as $h) echo '<th>'.htmlspecialchars($h).'</th>'; echo '</tr><tr>'; foreach($first as $v) echo '<td>'.htmlspecialchars((string)$v).'</td>'; echo '</tr>';
  while($r=$res->fetch_assoc()){ echo '<tr>'; foreach($r as $v) echo '<td>'.htmlspecialchars((string)$v).'</td>'; echo '</tr>'; }
} else { echo '<tr><td>No data</td></tr>'; }
echo '</table></body></html>';
