<script setup lang="ts">
/**
 * 文件职责：堂食基础设置区块。
 * 1. 展示堂食开关、默认时长、超时提醒配置。
 * 2. 抛出保存与重置事件。
 */
import type { DineInBasicSettingsDto } from '#/api/store-dinein';

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

interface Props {
  canOperate: boolean;
  isSaving: boolean;
  onSetDefaultDiningMinutes: (value: number) => void;
  onSetEnabled: (value: boolean) => void;
  onSetOvertimeReminderMinutes: (value: number) => void;
  settings: DineInBasicSettingsDto;
}

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" class="dinein-card">
    <template #title>
      <span class="section-title">堂食设置</span>
    </template>

    <div class="dinein-form-row">
      <label class="dinein-form-label">是否开启堂食</label>
      <div class="dinein-form-control">
        <Switch
          :checked="props.settings.enabled"
          :disabled="!props.canOperate"
          @update:checked="(value) => props.onSetEnabled(Boolean(value))"
        />
      </div>
    </div>

    <div class="dinein-form-row">
      <label class="dinein-form-label">默认用餐时长</label>
      <div class="dinein-form-control">
        <InputNumber
          :value="props.settings.defaultDiningMinutes"
          :min="1"
          :precision="0"
          :controls="false"
          :disabled="!props.canOperate"
          class="dinein-number-input"
          @update:value="
            (value) => props.onSetDefaultDiningMinutes(toNumber(value, 90))
          "
        />
        <span class="dinein-form-unit">分钟</span>
      </div>
    </div>

    <div class="dinein-form-row">
      <label class="dinein-form-label">超时提醒</label>
      <div class="dinein-form-control">
        <InputNumber
          :value="props.settings.overtimeReminderMinutes"
          :min="0"
          :precision="0"
          :controls="false"
          :disabled="!props.canOperate"
          class="dinein-number-input"
          @update:value="
            (value) => props.onSetOvertimeReminderMinutes(toNumber(value, 10))
          "
        />
        <span class="dinein-form-unit">分钟</span>
        <span class="dinein-form-hint">超过默认用餐时长后提醒</span>
      </div>
    </div>

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