<template>
  <view v-if="visible" class="add-note-overlay">
    <view class="add-note-content">
      <!-- 标题 -->
      <text class="dialog-title">添加备注</text>

      <scroll-view class="scroll-area" scroll-y>
        <!-- 业务名称 -->
        <view class="section">
          <text class="section-label">业务名称</text>
          <input
            class="form-input"
            v-model="businessName"
            placeholder="请输入业务名称"
          />
        </view>

        <!-- 设备型号 -->
        <view class="section">
          <text class="section-label">设备型号</text>
          <picker
            mode="selector"
            :range="store.dictUnitTypes"
            range-key="dictLabel"
            :value="selectedUnitType"
            @change="onUnitTypeChange"
          >
            <view class="picker-box">
              <text class="picker-text">{{ unitTypeLabel }}</text>
            </view>
          </picker>
        </view>

        <!-- 业务类型 -->
        <view class="section">
          <text class="section-label">业务类型</text>
          <picker
            mode="selector"
            :range="store.dictBusinessTypes"
            range-key="dictLabel"
            :value="selectedBusinessType"
            @change="onBusinessTypeChange"
          >
            <view class="picker-box">
              <text class="picker-text">{{ businessTypeLabel }}</text>
            </view>
          </picker>
        </view>

        <!-- 端口号输入 -->
        <view class="section">
          <text class="section-label">端口号</text>
          <view class="port-inputs">
            <input
              class="form-input port-input"
              v-model="portNumber1"
              type="number"
              placeholder="1号端口数"
            />
            <input
              class="form-input port-input"
              v-model="portNumber2"
              type="number"
              placeholder="2号端口数"
            />
            <input
              class="form-input port-input"
              v-model="portNumber3"
              type="number"
              placeholder="3号端口数"
            />
          </view>
        </view>
      </scroll-view>

      <!-- 底部按钮 -->
      <view class="btn-row">
        <view class="btn btn-cancel" @click="onCancel">
          <text class="btn-text">取消</text>
        </view>
        <view class="btn btn-submit" @click="onSubmit">
          <text class="btn-text-white">提交</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, computed, watch } from 'vue'
import store from '@/store'

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

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

const businessName = ref('')
const selectedUnitType = ref(0)
const selectedBusinessType = ref(0)
const portNumber1 = ref('')
const portNumber2 = ref('')
const portNumber3 = ref('')

const unitTypeLabel = computed(() => {
  if (store.dictUnitTypes.length > 0 && selectedUnitType.value < store.dictUnitTypes.length) {
    return store.dictUnitTypes[selectedUnitType.value].dictLabel
  }
  return '请选择'
})

const businessTypeLabel = computed(() => {
  if (store.dictBusinessTypes.length > 0 && selectedBusinessType.value < store.dictBusinessTypes.length) {
    return store.dictBusinessTypes[selectedBusinessType.value].dictLabel
  }
  return '请选择'
})

watch(
  () => props.visible,
  (val) => {
    if (val) {
      // Reset form and default select first item
      businessName.value = ''
      selectedUnitType.value = 0
      selectedBusinessType.value = 0
      portNumber1.value = ''
      portNumber2.value = ''
      portNumber3.value = ''
    }
  }
)

function onUnitTypeChange(e) {
  selectedUnitType.value = Number(e.detail.value)
}

function onBusinessTypeChange(e) {
  selectedBusinessType.value = Number(e.detail.value)
}

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

function onSubmit() {
  if (!businessName.value.trim()) {
    uni.showToast({ title: '请输入业务名称', icon: 'none' })
    return
  }
  if (!portNumber1.value || !portNumber2.value || !portNumber3.value) {
    uni.showToast({ title: '请输入端口号', icon: 'none' })
    return
  }

  const unitLabel = store.dictUnitTypes.length > 0
    ? store.dictUnitTypes[selectedUnitType.value].dictLabel
    : ''
  const bizLabel = store.dictBusinessTypes.length > 0
    ? store.dictBusinessTypes[selectedBusinessType.value].dictLabel
    : ''

  const formatted = `${businessName.value} ${unitLabel} ${bizLabel} ${portNumber1.value}/${portNumber2.value}/${portNumber3.value}`
  emit('confirm', formatted)
  emit('close')
}
</script>

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

.add-note-content {
  background-color: #fff;
  border-radius: 16rpx;
  width: 90%;
  max-height: 80vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  padding: 32rpx;
}

.dialog-title {
  font-size: 32rpx;
  font-weight: 700;
  color: #333;
  text-align: center;
  margin-bottom: 24rpx;
}

.scroll-area {
  flex: 1;
  width: 90%;
  max-height: 60vh;
}

.section {
  margin-bottom: 24rpx;
}

.section-label {
  font-size: 28rpx;
  font-weight: 600;
  color: #333;
  margin-bottom: 12rpx;
}

.form-input {
  width: 100%;
  height: 72rpx;
  border: 1rpx solid #ddd;
  border-radius: 8rpx;
  padding: 0 16rpx;
  font-size: 26rpx;
  box-sizing: border-box;
}

.picker-box {
  width: 100%;
  height: 72rpx;
  border: 1rpx solid #ddd;
  border-radius: 8rpx;
  display: flex;
  align-items: center;
  padding: 0 16rpx;
}

.picker-text {
  font-size: 26rpx;
  color: #333;
}

.port-inputs {
  display: flex;
  flex-direction: column;
  gap: 16rpx;
}

.port-input {
  width: 100%;
}

.btn-row {
  display: flex;
  gap: 20rpx;
  margin-top: 24rpx;
}

.btn {
  flex: 1;
  height: 80rpx;
  border-radius: 8rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.btn-cancel {
  background-color: #f5f5f5;
  border: 1rpx solid #ddd;
}

.btn-submit {
  background-color: #1A73EC;
}

.btn-text {
  font-size: 28rpx;
  color: #666;
}

.btn-text-white {
  font-size: 28rpx;
  color: #fff;
}
</style>