{{>header}}
<title>Manage Booking</title>

<style>

:root {
  --primary-green: #083c30;
  --secondary-green: #015843;
  --light-green: #0a5847;
  --accent-gold: #d4af37;
  --text-light: #e0e0e0;
  --bg-light: #f8f9fa;
}
body { background-color: #f5f7f6; }
.page-header {
  background: white;
  color: #083c30;
  padding: 1.5rem 2rem;
  border-radius: 10px;
  margin-bottom: 2rem;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  border-left: 5px solid var(--primary-green);
}
.page-header h2 {
  margin: 0;
  font-weight: 700;
  font-size: 1.75rem;
  color: var(--primary-green);
}
.page-header p {
  margin: 0.5rem 0 0 0;
  color: #6c757d;
  font-size: 0.95rem;
}
.booking-info-card {
  background: linear-gradient(135deg, white 0%, white 100%);
  color: #083c30;
  border-radius: 10px;
  padding: 2rem;
  margin-bottom: 2rem;
  box-shadow: 0 4px 12px rgba(8, 60, 48, 0.15);
}
.booking-info-card h4 {
  margin: 0 0 1.5rem 0;
  font-size: 1.3rem;
  font-weight: 600;
  padding-bottom: 1rem;
  border-bottom: 2px solid rgba(255, 255, 255, 0.2);
}
.booking-info-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 1.25rem;
}
.info-item {
  background: rgba(255, 255, 255, 0.12);
  padding: 1.25rem;
  border-radius: 8px;
  border-left: 3px solid var(--accent-gold);
  backdrop-filter: blur(10px);
  transition: all 0.3s ease;
}
.info-item:hover {
  background: rgba(255, 255, 255, 0.18);
  transform: translateY(-2px);
}
.info-item label {
  font-size: 0.8rem;
  /*color: rgba(255, 255, 255, 0.8);*/
  text-transform: uppercase;
  letter-spacing: 0.8px;
  margin-bottom: 0.5rem;
  display: block;
  font-weight: 500;
}
.info-item .value {
  font-size: 1.1rem;
  font-weight: 600;
  /*color: white;*/
  line-height: 1.5;
  word-break: break-word;
}
.status-badge {
  display: inline-block;
  padding: 0.4rem 1.2rem;
  border-radius: 25px;
  font-weight: 600;
  font-size: 0.85rem;
  letter-spacing: 0.3px;
}
.status-confirmed { background: #28a745; color: white; }
.status-onhold { background: #fd7e14; color: white; }
.status-cancelled { background: #dc3545; color: white; }
.status-completed { background: #17a2b8; color: white; }
.action-card {
  background: white;
  border-radius: 10px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.06);
  margin-bottom: 1.5rem;
  overflow: hidden;
  border: 1px solid #e8eaed;
  transition: all 0.3s ease;
}
.action-card-header {
  background: var(--primary-green);
  color: white;
  padding: 1.25rem 1.75rem;
  font-weight: 600;
  font-size: 1.05rem;
  display: flex;
  align-items: center;
  gap: 0.75rem;
}
.action-card-header.warning { background: #143d1d; }
.action-card-header.info { background: #17a2b8; }
.action-card-body { padding: 2rem; }
.form-label {
  font-weight: 600;
  color: var(--primary-green);
  margin-bottom: 0.75rem;
  display: block;
  font-size: 0.95rem;
}
.form-control {
  border: 2px solid #d1d5db;
  border-radius: 6px;
  padding: 0.55rem 1rem;
  transition: all 0.3s ease;
  font-size: 0.95rem;
  width: 100%;
  background: white;
  color: #1f2937;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%23083c30' d='M6 9L1 4h10z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 1rem center;
  padding-right: 2.5rem;
}
.form-control:focus {
  border-color: var(--primary-green);
  box-shadow: 0 0 0 0.25rem rgba(8, 60, 48, 0.08);
  outline: none;
}
.btn-custom, .btn-danger-custom {
  border: none;
  padding: 0.85rem 2.5rem;
  border-radius: 6px;
  font-weight: 600;
  transition: all 0.3s ease;
  cursor: pointer;
  font-size: 0.95rem;
}
.btn-custom { background: var(--primary-green); color: white; }
.btn-custom:hover {
  background: var(--secondary-green);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(8, 60, 48, 0.25);
}
.btn-danger-custom { background: #470209; color: white; }
.btn-danger-custom:hover {
  background: #080808;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(220, 53, 69, 0.25);
}
.timeline-container { position: relative; padding-left: 2.5rem; }
.timeline-item {
  position: relative;
  padding: 1.25rem;
  margin-bottom: 1.25rem;
  background: #f8f9fa;
  border-radius: 8px;
  border-left: 4px solid var(--primary-green);
  transition: all 0.2s ease;
}
.timeline-item:hover { background: #e9ecef; }
.timeline-status { font-weight: 700; color: var(--primary-green); }
.timeline-meta {
  display: flex; justify-content: space-between; font-size: 0.85rem; color: #6c757d;
  margin-top: 0.75rem; padding-top: 0.75rem; border-top: 1px solid #dee2e6;
}
.no-partner-badge {
  background: #fff3cd;
  color: #856404;
  padding: 0.6rem 1.2rem;
  border-radius: 6px;
  font-weight: 600;
  border: 1px solid #ffeaa7;
}
.back-button {
  background: #6c757d;
  color: white;
  border: none;
  padding: 0.85rem 2.5rem;
  border-radius: 6px;
  font-weight: 600;
  text-decoration: none;
  transition: all 0.3s ease;
}
.back-button:hover {
  background: #5a6268;
  transform: translateY(-2px);
}
.alert-success { background-color: #d4edda; color: #155724; }
.alert-danger { background-color: #f8d7da; color: #721c24; }
</style>

<div class="container mt-4 mb-5">
  {{#if success}}
  <div class="alert alert-success"><strong>✓</strong> {{success}}</div>
  {{/if}}
  {{#if error}}
  <div class="alert alert-danger"><strong>✗</strong> {{error}}</div>
  {{/if}}

  <div class="page-header">
    <h2>📋 Manage Booking</h2>
    <p>Complete booking management and tracking system</p>
  </div>

  <!-- Booking Information -->
  <div class="booking-info-card">
    <h4>📦 Booking Information</h4>
    <div class="booking-info-grid">
      <div class="info-item">
        <label>Booking ID</label>
        <div class="value">{{booking.bookingId}}</div>
      </div>

      <div class="info-item">
        <label>Current Status</label>
        <div class="value">
          <span class="status-badge status-confirmed">{{booking.status}}</span>
        </div>
      </div>

      <div class="info-item">
        <label>Assigned Partner</label>
        <div class="value">
          {{#if booking.deliveryPartner}}
            {{booking.deliveryPartner.profile.name}}<br>
            <small>📞 {{booking.deliveryPartner.phoneNumber}}</small>
          {{else}}
            <span class="no-partner-badge">⚠ Not Assigned</span>
          {{/if}}
        </div>
      </div>
    </div>
  </div>

  <div class="row">
    <!-- Assign Partner -->
    <div class="col-lg-6 mb-3">
      <div class="action-card">
        <div class="action-card-header">🚗 Assign / Reassign Delivery Partner</div>
        <div class="action-card-body">
          <form action="/admin/assigninstantbookingpartner" method="POST">
            <input type="hidden" name="bookingId" value="{{booking.bookingId}}">
            <div class="form-group">
              <label class="form-label" for="driverId">Select Driver:</label>
              <select class="form-control" id="driverId" name="driverId" required>
                <option value="">-- Choose a driver --</option>
                {{#each drivers}}
                <option value="{{this._id}}" {{#if ../booking.deliveryPartner}}{{#if (eq this._id ../booking.deliveryPartner._id)}}selected{{/if}}{{/if}}>
                  {{this.profile.name}} - {{this.phoneNumber}}
                </option>
                {{/each}}
              </select>
            </div>
            <button type="submit" class="btn-custom mt-3">✓ Assign Partner</button>
          </form>
        </div>
      </div>
    </div>

    <!-- Update Status -->
    <div class="col-lg-6 mb-3">
      <div class="action-card">
        <div class="action-card-header warning">Update Booking Status</div>
        <div class="action-card-body">
          <form action="/admin/updateinstantbookingstatus" method="POST">
            <input type="hidden" name="bookingId" value="{{booking.bookingId}}">
            <div class="form-group">
              <label class="form-label" for="status">Change Status:</label>
              <select class="form-control" id="status" name="status" required>
                <option value="On Hold" {{#if (eq booking.status "On Hold")}}selected{{/if}}>⏸ On Hold</option>
                <option value="Cancelled" {{#if (eq booking.status "Cancelled")}}selected{{/if}}>✗ Cancelled</option>
                <option value="Confirmed" {{#if (eq booking.status "Confirmed")}}selected{{/if}}>✓ Confirmed</option>
              </select>
            </div>
            <button type="submit" class="btn-danger-custom mt-3">🔄 Update Status</button>
          </form>
        </div>
      </div>
    </div>
  </div>

  <!-- Booking Timeline -->
  <div class="action-card">
    <div class="action-card-header info">⏱ Booking Timeline & History</div>
    <div class="action-card-body">
      {{#if booking.timeline}}
      <div class="timeline-container">
        {{#each booking.timeline}}
        <div class="timeline-item">
          <div class="timeline-status">{{this.status}}</div>
          <div class="timeline-message">{{this.message}}</div>
          <div class="timeline-meta">
            <span><span class="timeline-badge">{{this.updatedBy}}</span></span>
            <span>🕒 {{this.createdAt}}</span>
          </div>
        </div>
        {{/each}}
      </div>
      {{else}}
      <p class="text-muted text-center mb-0" style="padding: 2rem;">No timeline events recorded yet.</p>
      {{/if}}
    </div>
  </div>

  <div class="text-center mt-4">
    <a href="/admin/instantbooking" class="back-button">← Back to All Bookings</a>
  </div>
</div>

{{>footer}}
