<?php
$active = 'jobs';
$pageTitle = 'Job Details';
require_once __DIR__ . '/partials/top.php';
$pdo = db();

$id = (int)($_GET['id'] ?? 0);
if (!$id) { echo "<div class='alert alert-danger'>Job ID required</div>"; require_once __DIR__.'/partials/bottom.php'; exit; }

$stmt = $pdo->prepare("
  SELECT j.*, v.vendor_code, v.name vendor_name, v.phone vendor_phone,
         l.title lead_title, l.location, l.time_slot, l.lead_price, l.customer_name, l.customer_phone, l.service_details
  FROM jobs j
  JOIN vendors v ON v.id=j.vendor_id
  JOIN leads l ON l.id=j.lead_id
  WHERE j.id=?
");
$stmt->execute([$id]);
$job = $stmt->fetch();
if (!$job) { echo "<div class='alert alert-danger'>Job not found</div>"; require_once __DIR__.'/partials/bottom.php'; exit; }

$msg = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_status'])) {
  $newStatus = $_POST['status'] ?? '';
  $earning = (int)($_POST['earning_amount'] ?? $job['earning_amount']);
  if (in_array($newStatus, ['accepted','in_progress','completed','cancelled'])) {
    $stmt = $pdo->prepare("UPDATE jobs SET status=?, earning_amount=? WHERE id=?");
    $stmt->execute([$newStatus, $earning, $id]);
    $job['status'] = $newStatus;
    $job['earning_amount'] = $earning;
    $msg = "<div class='alert alert-success'>Job status updated.</div>";
  }
}

function statusBadge($s) {
  $map = ['accepted'=>'warning','in_progress'=>'primary','completed'=>'success','cancelled'=>'secondary'];
  return "<span class='badge bg-" . ($map[$s] ?? 'secondary') . "'>" . ucfirst(str_replace('_',' ',$s)) . "</span>";
}
?>
<div class="d-flex justify-content-between align-items-center mb-3">
  <div>
    <h4 class="fw-bold mb-0">Job #<?php echo $id; ?></h4>
    <div class="text-muted small"><?php echo htmlspecialchars($job['lead_title']); ?></div>
  </div>
  <a class="btn btn-outline-dark" href="jobs.php">← Back to Jobs</a>
</div>

<?php echo $msg; ?>

<div class="row g-3">
  <div class="col-lg-6">
    <div class="card p-3">
      <div class="fw-semibold mb-2">Job Info</div>
      <table class="table table-sm mb-0">
        <tr><td class="text-muted">Status</td><td><?php echo statusBadge($job['status']); ?></td></tr>
        <tr><td class="text-muted">Payment Mode</td><td><?php echo ucfirst($job['payment_mode'] ?? 'online'); ?></td></tr>
        <tr><td class="text-muted">Lead Price</td><td>₹<?php echo number_format($job['lead_price']); ?></td></tr>
        <tr><td class="text-muted">Vendor Earning</td><td class="text-success">₹<?php echo number_format($job['earning_amount']); ?></td></tr>
        <tr><td class="text-muted">Time Slot</td><td><?php echo htmlspecialchars($job['time_slot']); ?></td></tr>
        <tr><td class="text-muted">Location</td><td><?php echo htmlspecialchars($job['location']); ?></td></tr>
      </table>
    </div>
  </div>
  <div class="col-lg-6">
    <div class="card p-3">
      <div class="fw-semibold mb-2">Vendor</div>
      <div><a href="vendor_view.php?id=<?php echo (int)$job['vendor_id']; ?>"><?php echo htmlspecialchars($job['vendor_code']); ?></a> — <?php echo htmlspecialchars($job['vendor_name']); ?></div>
      <div class="small text-muted"><?php echo htmlspecialchars($job['vendor_phone']); ?></div>
      <hr>
      <div class="fw-semibold mb-2">Customer</div>
      <div><?php echo htmlspecialchars($job['customer_name']); ?></div>
      <div class="small text-muted"><?php echo htmlspecialchars($job['customer_phone'] ?? '-'); ?></div>
      <?php if (!empty($job['service_details'])): ?><div class="small mt-1"><?php echo nl2br(htmlspecialchars($job['service_details'])); ?></div><?php endif; ?>
    </div>
  </div>
</div>

<div class="row g-3 mt-1">
  <div class="col-12">
    <div class="card p-3">
      <div class="fw-semibold mb-2">Photos</div>
      <div class="d-flex gap-3">
        <?php if ($job['before_photo_path']): ?>
        <div><div class="small text-muted">Before</div><a href="../<?php echo htmlspecialchars($job['before_photo_path']); ?>" target="_blank"><img src="../<?php echo htmlspecialchars($job['before_photo_path']); ?>" style="max-height:120px;border-radius:8px;"></a></div>
        <?php endif; ?>
        <?php if ($job['after_photo_path']): ?>
        <div><div class="small text-muted">After</div><a href="../<?php echo htmlspecialchars($job['after_photo_path']); ?>" target="_blank"><img src="../<?php echo htmlspecialchars($job['after_photo_path']); ?>" style="max-height:120px;border-radius:8px;"></a></div>
        <?php endif; ?>
        <?php if (!$job['before_photo_path'] && !$job['after_photo_path']): ?><span class="text-muted">No photos</span><?php endif; ?>
      </div>
    </div>
  </div>
</div>

<div class="card p-3 mt-3">
  <div class="fw-semibold mb-2">Admin Override</div>
  <form method="post" class="row g-3">
    <input type="hidden" name="update_status" value="1">
    <div class="col-md-3">
      <label class="form-label">Status</label>
      <select class="form-select" name="status">
        <?php foreach (['accepted','in_progress','completed','cancelled'] as $s): ?>
        <option value="<?php echo $s; ?>" <?php if ($job['status']===$s) echo 'selected'; ?>><?php echo ucfirst(str_replace('_',' ',$s)); ?></option>
        <?php endforeach; ?>
      </select>
    </div>
    <div class="col-md-3">
      <label class="form-label">Earning Amount (₹)</label>
      <input type="number" name="earning_amount" class="form-control" value="<?php echo (int)$job['earning_amount']; ?>" min="0">
    </div>
    <div class="col-md-4 d-flex align-items-end">
      <button type="submit" class="btn btn-dark">Update Job</button>
    </div>
  </form>
</div>
<?php require_once __DIR__ . '/partials/bottom.php'; ?>
