<template>
  <ConfigPageBase
    section="routing"
    title="路径规划策略"
    description="算法选择、权重、避障规则、区域限速（RoutingPolicy）"
    :defaults="DEFAULT_ROUTING">
    <template #default="{ payload, update }">
      <el-form label-width="140px" :model="payload">
        <el-form-item label="算法">
          <el-select :model-value="payload.algorithm" @update:model-value="(v: any) => update({ ...payload, algorithm: v })">
            <el-option label="Dijkstra" value="dijkstra" />
            <el-option label="A*" value="astar" />
            <el-option label="自定义" value="custom" />
          </el-select>
        </el-form-item>
        <el-form-item label="权重">
          <el-table :data="weightRows(payload.weights)" size="small">
            <el-table-column label="维度" prop="k" width="140" />
            <el-table-column label="权重">
              <template #default="s">
                <el-input-number :model-value="s.row.v" :min="0" :max="10" :step="0.1" @update:model-value="(v: number | undefined) => update({ ...payload, weights: { ...payload.weights, [s.row.k]: v ?? 0 } })" />
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
        <el-form-item label="避障规则">
          <el-table :data="payload.avoidance" size="small">
            <el-table-column label="ID" prop="id" width="100" />
            <el-table-column label="区域 ID" prop="zoneId" width="140" />
            <el-table-column label="规则" prop="rule" />
          </el-table>
        </el-form-item>
        <el-form-item label="区域限速">
          <el-table :data="payload.zoneSpeedLimits" size="small">
            <el-table-column label="区域" prop="zoneId" width="140" />
            <el-table-column label="最大速度 (m/s)" prop="maxSpeedMps" />
          </el-table>
        </el-form-item>
      </el-form>
    </template>
  </ConfigPageBase>
</template>

<script setup lang="ts">
import ConfigPageBase from '@/components/ConfigPageBase.vue'
import { DEFAULT_ROUTING } from '@/mock/data/configs'

function weightRows(weights: Record<string, number>) {
  return Object.entries(weights).map(([k, v]) => ({ k, v }))
}
</script>
