<template>
  <div v-if="visible" class="dialog-mask" @click.self="handleClose">
    <div class="dialog">
      <div class="dialog-header">
        <span class="dialog-title">航空器滑行路由查询</span>
        <img class="dialog-close" :src="closeIcon" alt="close" @click="handleClose" />
      </div>

      <div class="dialog-tabs">
        <div class="tab" :class="{ active: activeTab === 'IN' }" @click="activeTab = 'IN'">进港路由</div>
        <div class="tab" :class="{ active: activeTab === 'OUT' }" @click="activeTab = 'OUT'">出港路由</div>
      </div>

      <div class="dialog-body">
        <div class="form-grid">
          <div class="form-item">
            <span class="form-label">flightNo（航班号）：<span class="required">*</span></span>
            <input v-model="form.flightNo" class="form-input" placeholder="请输入..." />
          </div>
          <div class="form-item">
            <span class="form-label">inRunway（进港跑道）：</span>
            <input v-model="form.inRunway" class="form-input" placeholder="请输入..." />
          </div>
          <div class="form-item">
            <span class="form-label">outRunway（出港跑道）：</span>
            <input v-model="form.outRunway" class="form-input" placeholder="请输入..." />
          </div>

          <div v-if="activeTab === 'IN'" class="form-item">
            <span class="form-label">contactCross（联络道口）：<span class="required">*</span></span>
            <input v-model="form.contactCross" class="form-input" placeholder="请输入..." />
          </div>

          <div class="form-item">
            <span class="form-label">seat（机位号）：<span v-if="activeTab === 'IN'" class="required">*</span></span>
            <input v-model="form.seat" class="form-input" placeholder="请输入..." />
          </div>
          
          
        </div>

        <div class="form-hint">
          <div v-if="activeTab === 'IN'">进港必填：flightNo、inRunway、contactCross、seat；建议填写：outRunway</div>
          <div v-else>出港必填：flightNo、outRunway、seat；建议填写：inRunway</div>
        </div>
      </div>

      <div class="dialog-footer">
        <button class="footer-btn cancel" @click="handleClose">取消</button>
        <button class="footer-btn confirm" @click="handleConfirm">查询</button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, watch } from 'vue'
import { ElMessage } from 'element-plus'
import closeIcon from '@/assets/images/close_icon.png'

const props = defineProps({
  visible: { type: Boolean, default: false }
})

const emit = defineEmits(['close', 'confirm'])

const activeTab = ref('IN')
const form = ref({
  flightNo: '',
  inRunway: '',
  outRunway: '',
  contactCross: '',
  seat: ''
})

function handleClose() {
  emit('close')
}

function handleConfirm() {
  const flightNo = String(form.value.flightNo || '').trim()
  if (!flightNo) {
    ElMessage.warning('请输入航班号 flightNo')
    return
  }

  const payload = {
    routeType: activeTab.value,
    flightNo,
    objectId: flightNo,
    vehicleID: flightNo,
    objectName: flightNo
  }
  
  if (activeTab.value === 'IN') {
    if (!form.value.inRunway || !form.value.contactCross || !form.value.seat) {
      ElMessage.warning('进港查询必须填写：进港跑道、联络道口、机位号')
      return
    }
    payload.inRunway = form.value.inRunway
    payload.contactCross = form.value.contactCross
    payload.seat = form.value.seat
    if (form.value.outRunway) payload.outRunway = form.value.outRunway
  } else {
    if (!form.value.outRunway) {
      ElMessage.warning('出港查询必须填写：出港跑道')
      return
    }
    const seat = String(form.value.seat || '').trim()
    if (!seat) {
      ElMessage.warning('出港查询必须填写：机位号 seat')
      return
    }
    payload.outRunway = form.value.outRunway
    payload.seat = seat
    if (form.value.inRunway) payload.inRunway = form.value.inRunway
  }
  
  emit('confirm', payload)
  emit('close')
}

watch(
  () => props.visible,
  (v) => {
    if (!v) return
    activeTab.value = 'IN'
    form.value = { flightNo: '', inRunway: '', outRunway: '', contactCross: '', seat: '' }
  }
)
</script>

<style scoped>
.dialog-mask {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  z-index: 4000;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
}

.dialog {
  width: 760px;
  max-width: calc(100vw - 40px);
  background-color: #424851;
  border-radius: 10px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
  overflow: hidden;
  color: #ffffff;
}

.dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid #303850;
}

.dialog-title {
  font-size: 18px;
  font-weight: 600;
}

.dialog-close {
  width: 22px;
  height: 22px;
  cursor: pointer;
}

.dialog-tabs {
  display: flex;
  border-bottom: 1px solid #303850;
}

.tab {
  flex: 0 0 120px;
  height: 40px;
  line-height: 40px;
  text-align: center;
  cursor: pointer;
  font-size: 14px;
  color: #f0f0f0;
}

.tab.active {
  color: #ffffff;
  background: rgba(0, 150, 255, 0.15);
  border-bottom: 2px solid #0096ff;
}

.dialog-body {
  padding: 18px 22px 8px;
}

.form-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 14px;
}

.form-item {
  display: flex;
  align-items: center;
  gap: 12px;
}

.form-label {
  width: 220px;
  text-align: right;
  font-size: 14px;
  color: #dcdcdc;
  white-space: nowrap;
}

.form-input {
  flex: 1;
  height: 34px;
  border-radius: 4px;
  border: 1px solid rgba(47, 122, 185, 0.9);
  outline: none;
  background: rgba(0, 150, 255, 0.08);
  color: #ffffff;
  padding: 0 12px;
}

.form-input::placeholder {
  color: rgba(255, 255, 255, 0.6);
}

.form-hint {
  margin-top: 14px;
  padding: 10px 12px;
  border-radius: 6px;
  border: 1px solid rgba(48, 56, 80, 0.9);
  background: rgba(0, 0, 0, 0.12);
  color: rgba(255, 255, 255, 0.8);
  font-size: 12px;
}

.dialog-footer {
  padding: 12px 18px 16px;
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  border-top: 1px solid #303850;
}

.footer-btn {
  width: 88px;
  height: 34px;
  border-radius: 4px;
  cursor: pointer;
  border: 1px solid rgba(47, 122, 185, 0.8);
  background: rgba(0, 150, 255, 0.08);
  color: #ffffff;
}

.footer-btn.confirm {
  background: #0096ff;
  border-color: #0078cc;
}
.required {
  color: #f56c6c;
  margin-left: 4px;
}
</style>
