<script setup lang="ts">
/**
 * 文件职责：通用配送设置区块。
 * 1. 展示通用字段输入框。
 * 2. 通过回调将字段改动上抛给父级。
 */
import type { DeliveryGeneralSettingsDto } from '#/api/store-delivery';

import { Button, Card, InputNumber } from 'ant-design-vue';

interface Props {
  isSaving: boolean;
  onSetEtaAdjustmentMinutes: (value: number) => void;
  onSetFreeDeliveryThreshold: (value: null | number) => void;
  onSetHourlyCapacityLimit: (value: number) => void;
  onSetMaxDeliveryDistance: (value: number) => void;
  settings: DeliveryGeneralSettingsDto;
}

const props = defineProps<Props>();

const emit = defineEmits<{
  (event: 'reset'): void;
  (event: 'save'): void;
}>();

function toNumber(value: null | number | string, fallback = 0) {
  const parsed = Number(value);
  return Number.isFinite(parsed) ? parsed : fallback;
}
</script>

<template>
  <Card :bordered="false">
    <template #title>
      <span class="section-title">通用设置</span>
    </template>

    <div class="general-grid">
      <div class="general-field">
        <label>免配送费门槛</label>
        <div class="field-input-row">
          <InputNumber
            :value="props.settings.freeDeliveryThreshold ?? undefined"
            :min="0"
            :precision="0"
            :controls="false"
            placeholder="如：50"
            class="field-input"
            @update:value="
              (value) =>
                props.onSetFreeDeliveryThreshold(
                  value === null || value === undefined
                    ? null
                    : toNumber(value, 0),
                )
            "
          />
          <span>元</span>
        </div>
        <div class="field-hint">订单满此金额免配送费，留空则不启用</div>
      </div>

      <div class="general-field">
        <label>最大配送距离</label>
        <div class="field-input-row">
          <InputNumber
            :value="props.settings.maxDeliveryDistance"
            :min="0"
            :precision="1"
            :step="0.5"
            :controls="false"
            placeholder="如：5"
            class="field-input"
            @update:value="
              (value) => props.onSetMaxDeliveryDistance(toNumber(value, 0))
            "
          />
          <span>公里</span>
        </div>
        <div class="field-hint">仅半径模式生效</div>
      </div>

      <div class="general-field">
        <label>每小时配送上限</label>
        <div class="field-input-row">
          <InputNumber
            :value="props.settings.hourlyCapacityLimit"
            :min="1"
            :precision="0"
            :controls="false"
            placeholder="如：50"
            class="field-input"
            @update:value="
              (value) => props.onSetHourlyCapacityLimit(toNumber(value, 1))
            "
          />
          <span>单</span>
        </div>
        <div class="field-hint">达到上限后暂停接单</div>
      </div>

      <div class="general-field">
        <label>配送时间预估加成</label>
        <div class="field-input-row">
          <InputNumber
            :value="props.settings.etaAdjustmentMinutes"
            :min="0"
            :precision="0"
            :controls="false"
            placeholder="如：10"
            class="field-input"
            @update:value="
              (value) => props.onSetEtaAdjustmentMinutes(toNumber(value, 0))
            "
          />
          <span>分钟</span>
        </div>
        <div class="field-hint">高峰期可适当上调预估送达时间</div>
      </div>
    </div>

    <div class="general-actions">
      <Button :disabled="props.isSaving" @click="emit('reset')">重置</Button>
      <Button type="primary" :loading="props.isSaving" @click="emit('save')">
        保存设置
      </Button>
    </div>
  </Card>
</template>
