<template>
  <div class="override-panel">
    <div class="panel-header">
      <h3>🚨 Super Admin Override Panel</h3>
      <p class="warning-text">Use these actions with extreme caution. All actions are logged and audited.</p>
    </div>

    <div class="actions-grid">
      <!-- Force Cancel Order -->
      <div class="action-card danger">
        <div class="action-icon">❌</div>
        <div class="action-content">
          <h4>Force Cancel Order</h4>
          <p>Immediately cancel this order regardless of current status</p>
          <button @click="openModal('cancel')" class="action-btn danger">Force Cancel</button>
        </div>
      </div>

      <!-- Reassign Delivery Boy -->
      <div class="action-card warning">
        <div class="action-icon">🔄</div>
        <div class="action-content">
          <h4>Reassign Delivery Boy</h4>
          <p>Change the assigned delivery boy for this order</p>
          <button @click="openModal('reassign')" class="action-btn warning">Reassign</button>
        </div>
      </div>

      <!-- Trigger Refund -->
      <div class="action-card info">
        <div class="action-icon">💸</div>
        <div class="action-content">
          <h4>Process Refund</h4>
          <p>Issue a full or partial refund for this order</p>
          <button @click="openModal('refund')" class="action-btn info">Process Refund</button>
        </div>
      </div>

      <!-- Emergency Status Change -->
      <div class="action-card warning">
        <div class="action-icon">⚡</div>
        <div class="action-content">
          <h4>Emergency Status Change</h4>
          <p>Override order status for emergency situations</p>
          <button @click="openModal('status')" class="action-btn warning">Change Status</button>
        </div>
      </div>

      <!-- Block Restaurant -->
      <div class="action-card danger">
        <div class="action-icon">🚫</div>
        <div class="action-content">
          <h4>Block Restaurant</h4>
          <p>Prevent this restaurant from receiving future orders</p>
          <button @click="openModal('block')" class="action-btn danger">Block Restaurant</button>
        </div>
      </div>
    </div>

    <!-- Confirmation Modal -->
    <div v-if="showModal" class="modal-overlay" @click="closeModal">
      <div class="modal" @click.stop>
        <div class="modal-header">
          <h3>{{ modalConfig.title }}</h3>
          <button @click="closeModal" class="close-btn">×</button>
        </div>
        
        <div class="modal-body">
          <div class="warning-banner" :class="modalConfig.type">
            <span class="warning-icon">{{ modalConfig.icon }}</span>
            <div class="warning-text">
              <strong>{{ modalConfig.warning }}</strong>
              <p>{{ modalConfig.description }}</p>
            </div>
          </div>

          <!-- Action-specific forms -->
          <div class="form-section">
            <!-- Cancel Order Form -->
            <div v-if="activeAction === 'cancel'">
              <label class="form-label">Cancellation Reason *</label>
              <select v-model="formData.cancelReason" class="form-select" required>
                <option value="">Select reason...</option>
                <option value="customer_request">Customer Request</option>
                <option value="restaurant_issue">Restaurant Issue</option>
                <option value="delivery_issue">Delivery Issue</option>
                <option value="payment_issue">Payment Issue</option>
                <option value="system_error">System Error</option>
                <option value="other">Other</option>
              </select>
            </div>

            <!-- Reassign Delivery Boy Form -->
            <div v-if="activeAction === 'reassign'">
              <label class="form-label">Select New Delivery Boy *</label>
              <select v-model="formData.deliveryBoyId" class="form-select" required>
                <option value="">Select delivery boy...</option>
                <option v-for="boy in availableDeliveryBoys" :key="boy.id" :value="boy.id">
                  {{ boy.name }} - {{ boy.rating }}⭐ ({{ boy.distance }}km away)
                </option>
              </select>
            </div>

            <!-- Refund Form -->
            <div v-if="activeAction === 'refund'">
              <label class="form-label">Refund Type *</label>
              <select v-model="formData.refundType" class="form-select" required>
                <option value="">Select refund type...</option>
                <option value="full">Full Refund</option>
                <option value="partial">Partial Refund</option>
              </select>
              
              <div v-if="formData.refundType === 'partial'" class="mt-3">
                <label class="form-label">Refund Amount (Rs.)</label>
                <input v-model="formData.refundAmount" type="number" class="form-input" 
                       :max="order.total" placeholder="Enter amount">
              </div>
            </div>

            <!-- Status Change Form -->
            <div v-if="activeAction === 'status'">
              <label class="form-label">New Status *</label>
              <select v-model="formData.newStatus" class="form-select" required>
                <option value="">Select new status...</option>
                <option value="pending">Pending</option>
                <option value="accepted">Accepted</option>
                <option value="preparing">Preparing</option>
                <option value="ready_for_pickup">Ready for Pickup</option>
                <option value="picked_up">Picked Up</option>
                <option value="delivered">Delivered</option>
                <option value="cancelled">Cancelled</option>
              </select>
            </div>

            <!-- Block Restaurant Form -->
            <div v-if="activeAction === 'block'">
              <label class="form-label">Block Duration *</label>
              <select v-model="formData.blockDuration" class="form-select" required>
                <option value="">Select duration...</option>
                <option value="24h">24 Hours</option>
                <option value="7d">7 Days</option>
                <option value="30d">30 Days</option>
                <option value="permanent">Permanent</option>
              </select>
            </div>

            <!-- Common reason field -->
            <div class="mt-3">
              <label class="form-label">Detailed Reason *</label>
              <textarea v-model="formData.reason" class="form-textarea" 
                        placeholder="Provide detailed explanation for this action..." 
                        rows="3" required></textarea>
            </div>
          </div>
        </div>

        <div class="modal-footer">
          <button @click="closeModal" class="btn-secondary">Cancel</button>
          <button @click="confirmAction" class="btn-primary" :class="modalConfig.type" 
                  :disabled="!isFormValid">
            {{ modalConfig.confirmText }}
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, computed } from 'vue'

export default {
  name: 'SuperAdminOverridePanel',
  props: {
    order: {
      type: Object,
      required: true
    }
  },
  emits: ['actionExecuted'],
  setup(props, { emit }) {
    const showModal = ref(false)
    const activeAction = ref('')
    const formData = ref({})

    const availableDeliveryBoys = ref([
      { id: 1, name: 'John Doe', rating: 4.8, distance: 2.3 },
      { id: 2, name: 'Jane Smith', rating: 4.6, distance: 1.8 },
      { id: 3, name: 'Mike Johnson', rating: 4.9, distance: 3.1 }
    ])

    const modalConfigs = {
      cancel: {
        title: 'Force Cancel Order',
        icon: '❌',
        type: 'danger',
        warning: 'This will immediately cancel the order',
        description: 'The customer will be notified and refund will be processed automatically.',
        confirmText: 'Force Cancel'
      },
      reassign: {
        title: 'Reassign Delivery Boy',
        icon: '🔄',
        type: 'warning',
        warning: 'This will change the assigned delivery boy',
        description: 'Both delivery boys will be notified of the change.',
        confirmText: 'Reassign'
      },
      refund: {
        title: 'Process Refund',
        icon: '💸',
        type: 'info',
        warning: 'This will process a refund for the customer',
        description: 'Refund will be processed to the original payment method.',
        confirmText: 'Process Refund'
      },
      status: {
        title: 'Emergency Status Change',
        icon: '⚡',
        type: 'warning',
        warning: 'This will override the current order status',
        description: 'Use only in emergency situations. All parties will be notified.',
        confirmText: 'Change Status'
      },
      block: {
        title: 'Block Restaurant',
        icon: '🚫',
        type: 'danger',
        warning: 'This will block the restaurant from future orders',
        description: 'Restaurant will not be able to receive new orders during the block period.',
        confirmText: 'Block Restaurant'
      }
    }

    const modalConfig = computed(() => modalConfigs[activeAction.value] || {})

    const isFormValid = computed(() => {
      if (!formData.value.reason?.trim()) return false
      
      switch (activeAction.value) {
        case 'cancel':
          return formData.value.cancelReason
        case 'reassign':
          return formData.value.deliveryBoyId
        case 'refund':
          return formData.value.refundType && 
                 (formData.value.refundType === 'full' || formData.value.refundAmount > 0)
        case 'status':
          return formData.value.newStatus
        case 'block':
          return formData.value.blockDuration
        default:
          return false
      }
    })

    const openModal = (action) => {
      activeAction.value = action
      formData.value = {}
      showModal.value = true
    }

    const closeModal = () => {
      showModal.value = false
      activeAction.value = ''
      formData.value = {}
    }

    const confirmAction = async () => {
      if (!isFormValid.value) return

      try {
        // Simulate API call
        console.log('Executing action:', activeAction.value, formData.value)
        
        emit('actionExecuted', {
          action: activeAction.value,
          data: formData.value,
          orderId: props.order.id
        })

        closeModal()
      } catch (error) {
        console.error('Action failed:', error)
      }
    }

    return {
      showModal,
      activeAction,
      formData,
      availableDeliveryBoys,
      modalConfig,
      isFormValid,
      openModal,
      closeModal,
      confirmAction
    }
  }
}
</script>

<style scoped>
.override-panel {
  background: rgba(15, 23, 42, 0.8);
  border-radius: 20px;
  border: 1px solid rgba(239, 68, 68, 0.3);
  backdrop-filter: blur(20px);
  padding: 2rem;
}

.panel-header {
  margin-bottom: 2rem;
}

.panel-header h3 {
  margin: 0 0 0.5rem 0;
  color: #f8fafc;
  font-size: 1.5rem;
  font-weight: 700;
}

.warning-text {
  color: #fbbf24;
  font-size: 0.875rem;
  margin: 0;
}

.actions-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 1.5rem;
}

.action-card {
  background: rgba(30, 41, 59, 0.6);
  border-radius: 16px;
  padding: 1.5rem;
  border: 1px solid;
  transition: all 0.3s ease;
}

.action-card.danger {
  border-color: rgba(239, 68, 68, 0.3);
}

.action-card.warning {
  border-color: rgba(245, 158, 11, 0.3);
}

.action-card.info {
  border-color: rgba(59, 130, 246, 0.3);
}

.action-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.2);
}

.action-icon {
  font-size: 2rem;
  margin-bottom: 1rem;
}

.action-content h4 {
  margin: 0 0 0.5rem 0;
  color: #f8fafc;
  font-size: 1.125rem;
  font-weight: 600;
}

.action-content p {
  margin: 0 0 1rem 0;
  color: #94a3b8;
  font-size: 0.875rem;
  line-height: 1.4;
}

.action-btn {
  width: 100%;
  padding: 0.75rem 1.5rem;
  border: none;
  border-radius: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
}

.action-btn.danger {
  background: rgba(239, 68, 68, 0.2);
  color: #ef4444;
  border: 1px solid rgba(239, 68, 68, 0.3);
}

.action-btn.warning {
  background: rgba(245, 158, 11, 0.2);
  color: #f59e0b;
  border: 1px solid rgba(245, 158, 11, 0.3);
}

.action-btn.info {
  background: rgba(59, 130, 246, 0.2);
  color: #3b82f6;
  border: 1px solid rgba(59, 130, 246, 0.3);
}

.action-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.2);
}

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

.modal {
  background: rgba(15, 23, 42, 0.95);
  border-radius: 20px;
  border: 1px solid rgba(59, 130, 246, 0.2);
  backdrop-filter: blur(20px);
  max-width: 500px;
  width: 90%;
  max-height: 80vh;
  overflow-y: auto;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 2rem 2rem 0;
}

.modal-header h3 {
  margin: 0;
  color: #f8fafc;
  font-size: 1.25rem;
  font-weight: 700;
}

.close-btn {
  background: none;
  border: none;
  font-size: 1.5rem;
  cursor: pointer;
  color: #94a3b8;
  padding: 0.25rem;
}

.modal-body {
  padding: 2rem;
}

.warning-banner {
  display: flex;
  align-items: flex-start;
  gap: 1rem;
  padding: 1.5rem;
  border-radius: 12px;
  margin-bottom: 2rem;
  border-left: 4px solid;
}

.warning-banner.danger {
  background: rgba(239, 68, 68, 0.1);
  border-color: #ef4444;
}

.warning-banner.warning {
  background: rgba(245, 158, 11, 0.1);
  border-color: #f59e0b;
}

.warning-banner.info {
  background: rgba(59, 130, 246, 0.1);
  border-color: #3b82f6;
}

.warning-icon {
  font-size: 1.5rem;
  flex-shrink: 0;
}

.warning-text strong {
  color: #f8fafc;
  display: block;
  margin-bottom: 0.25rem;
}

.warning-text p {
  color: #94a3b8;
  margin: 0;
  font-size: 0.875rem;
}

.form-section {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.form-label {
  color: #f8fafc;
  font-weight: 600;
  margin-bottom: 0.5rem;
  display: block;
}

.form-select, .form-input, .form-textarea {
  width: 100%;
  padding: 0.75rem;
  background: rgba(30, 41, 59, 0.8);
  border: 1px solid rgba(59, 130, 246, 0.3);
  border-radius: 8px;
  color: #f8fafc;
  font-size: 0.875rem;
}

.form-select:focus, .form-input:focus, .form-textarea:focus {
  outline: none;
  border-color: #3b82f6;
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.form-textarea {
  resize: vertical;
  min-height: 80px;
}

.mt-3 {
  margin-top: 1rem;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 1rem;
  padding: 0 2rem 2rem;
}

.btn-secondary, .btn-primary {
  padding: 0.75rem 1.5rem;
  border: none;
  border-radius: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
}

.btn-secondary {
  background: rgba(107, 114, 128, 0.2);
  color: #9ca3af;
  border: 1px solid rgba(107, 114, 128, 0.3);
}

.btn-primary {
  background: linear-gradient(135deg, #3b82f6, #1d4ed8);
  color: white;
}

.btn-primary.danger {
  background: linear-gradient(135deg, #ef4444, #dc2626);
}

.btn-primary.warning {
  background: linear-gradient(135deg, #f59e0b, #d97706);
}

.btn-primary:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-secondary:hover, .btn-primary:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.2);
}

@media (max-width: 768px) {
  .override-panel {
    padding: 1.5rem;
  }
  
  .actions-grid {
    grid-template-columns: 1fr;
  }
  
  .modal {
    width: 95%;
    margin: 1rem;
  }
  
  .modal-header, .modal-body, .modal-footer {
    padding: 1.5rem;
  }
  
  .modal-footer {
    flex-direction: column;
  }
}
</style>