<template>
  <div v-if="isOpen && deliveryBoy" class="modal-overlay" @click="close">
    <div class="modal-container" @click.stop>
      <div class="modal-header">
        <h2>👁️ Delivery Boy Details</h2>
        <button @click="close" class="close-btn">×</button>
      </div>

      <div class="modal-body">
        <div class="info-grid">
          <div class="info-section">
            <h3>Personal Information</h3>
            <div class="info-item">
              <span class="label">Name:</span>
              <span class="value">{{ deliveryBoy.full_name }}</span>
            </div>
            <div class="info-item">
              <span class="label">Phone:</span>
              <span class="value">{{ deliveryBoy.phone }}</span>
            </div>
            <div class="info-item">
              <span class="label">Email:</span>
              <span class="value">{{ deliveryBoy.email }}</span>
            </div>
            <div class="info-item">
              <span class="label">City:</span>
              <span class="value">{{ deliveryBoy.city || 'N/A' }}</span>
            </div>
            <div class="info-item">
              <span class="label">Address:</span>
              <span class="value">{{ deliveryBoy.address || 'N/A' }}</span>
            </div>
          </div>

          <div class="info-section">
            <h3>Status & Performance</h3>
            <div class="info-item">
              <span class="label">Status:</span>
              <span class="status-badge" :class="getStatusClass(deliveryBoy.status)">
                {{ formatStatus(deliveryBoy.status) }}
              </span>
            </div>
            <div class="info-item">
              <span class="label">Verification:</span>
              <span class="status-badge" :class="getVerificationClass(deliveryBoy.verification_status)">
                {{ formatStatus(deliveryBoy.verification_status) }}
              </span>
            </div>
            <div class="info-item">
              <span class="label">Rating:</span>
              <span class="value">⭐ {{ parseFloat(deliveryBoy.rating || 0).toFixed(1) }}</span>
            </div>
            <div class="info-item">
              <span class="label">Total Deliveries:</span>
              <span class="value">{{ deliveryBoy.total_deliveries || 0 }}</span>
            </div>
            <div class="info-item">
              <span class="label">Successful:</span>
              <span class="value">{{ deliveryBoy.successful_deliveries || 0 }}</span>
            </div>
          </div>

          <div class="info-section">
            <h3>Financial</h3>
            <div class="info-item">
              <span class="label">Commission Rate:</span>
              <span class="value">{{ deliveryBoy.commission_rate || 0 }}%</span>
            </div>
            <div class="info-item">
              <span class="label">Wallet Balance:</span>
              <span class="value">Rs {{ parseFloat(deliveryBoy.wallet_balance || 0).toLocaleString('en-US', {minimumFractionDigits: 2}) }}</span>
            </div>
            <div class="info-item">
              <span class="label">Joined Date:</span>
              <span class="value">{{ formatDate(deliveryBoy.joined_date) }}</span>
            </div>
          </div>

          <div class="info-section">
            <h3>Vehicle Information</h3>
            <div v-if="deliveryBoy.vehicles && deliveryBoy.vehicles.length > 0">
              <div v-for="vehicle in deliveryBoy.vehicles" :key="vehicle.id" class="vehicle-card">
                <div class="info-item">
                  <span class="label">Type:</span>
                  <span class="value">{{ vehicle.vehicle_type }}</span>
                </div>
                <div class="info-item">
                  <span class="label">Number:</span>
                  <span class="value">{{ vehicle.vehicle_number || 'N/A' }}</span>
                </div>
                <div class="info-item">
                  <span class="label">Model:</span>
                  <span class="value">{{ vehicle.vehicle_model || 'N/A' }}</span>
                </div>
              </div>
            </div>
            <div v-else class="empty-state">
              No vehicle registered
            </div>
          </div>
        </div>
      </div>

      <div class="modal-footer">
        <button @click="close" class="btn-close">Close</button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ViewDeliveryBoyModal',
  props: {
    isOpen: Boolean,
    deliveryBoy: Object
  },
  emits: ['close'],
  setup(props, { emit }) {
    const close = () => {
      emit('close')
    }

    const getStatusClass = (status) => {
      const classes = {
        online: 'status-online',
        offline: 'status-offline',
        on_delivery: 'status-on-delivery',
        blocked: 'status-blocked'
      }
      return classes[status] || ''
    }

    const getVerificationClass = (status) => {
      const classes = {
        pending: 'verification-pending',
        verified: 'verification-verified',
        rejected: 'verification-rejected'
      }
      return classes[status] || ''
    }

    const formatStatus = (status) => {
      return status?.replace('_', ' ').toUpperCase() || 'N/A'
    }

    const formatDate = (date) => {
      if (!date) return 'N/A'
      return new Date(date).toLocaleDateString()
    }

    return {
      close,
      getStatusClass,
      getVerificationClass,
      formatStatus,
      formatDate,
      parseFloat
    }
  }
}
</script>

<style scoped>
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-container {
  background: white;
  border-radius: 16px;
  max-width: 900px;
  width: 90%;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 25px 60px rgba(0, 0, 0, 0.3);
}

.modal-header {
  background: linear-gradient(135deg, #ff6b35 0%, #f7931e 100%);
  color: white;
  padding: 1.5rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.modal-header h2 {
  margin: 0;
  font-size: 1.5rem;
}

.close-btn {
  background: rgba(255, 255, 255, 0.2);
  border: none;
  color: white;
  font-size: 2rem;
  cursor: pointer;
  padding: 0 0.5rem;
  border-radius: 8px;
  line-height: 1;
}

.modal-body {
  padding: 2rem;
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 2rem;
}

.info-section {
  background: #f9fafb;
  padding: 1.5rem;
  border-radius: 12px;
}

.info-section h3 {
  margin: 0 0 1rem 0;
  color: #1f2937;
  font-size: 1.1rem;
  border-bottom: 2px solid #ff6b35;
  padding-bottom: 0.5rem;
}

.info-item {
  display: flex;
  justify-content: space-between;
  padding: 0.75rem 0;
  border-bottom: 1px solid #e5e7eb;
}

.info-item:last-child {
  border-bottom: none;
}

.label {
  font-weight: 600;
  color: #6b7280;
}

.value {
  color: #1f2937;
  font-weight: 500;
}

.status-badge {
  display: inline-block;
  padding: 0.25rem 0.75rem;
  border-radius: 12px;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
}

.status-online { background: #d1fae5; color: #065f46; }
.status-offline { background: #f3f4f6; color: #4b5563; }
.status-on-delivery { background: #dbeafe; color: #1e40af; }
.status-blocked { background: #fee2e2; color: #991b1b; }

.verification-pending { background: #fef3c7; color: #92400e; }
.verification-verified { background: #d1fae5; color: #065f46; }
.verification-rejected { background: #fee2e2; color: #991b1b; }

.vehicle-card {
  background: white;
  padding: 1rem;
  border-radius: 8px;
  margin-bottom: 1rem;
}

.empty-state {
  text-align: center;
  color: #9ca3af;
  padding: 2rem;
}

.modal-footer {
  padding: 1.5rem;
  border-top: 1px solid #e5e7eb;
  display: flex;
  justify-content: flex-end;
}

.btn-close {
  padding: 0.75rem 1.5rem;
  background: #f3f4f6;
  color: #6b7280;
  border: none;
  border-radius: 8px;
  font-weight: 600;
  cursor: pointer;
}

@media (max-width: 768px) {
  .info-grid {
    grid-template-columns: 1fr;
  }
}
</style>
