<script setup>
import { computed } from "vue";
import { Icon } from "@iconify/vue";
import { Input } from "../../../../ui/input";
import { Separator } from "../../../../ui/separator";
import { Field, FieldLabel } from "../../../../ui/field";
import { Locale } from "../../../../ui/locale";
import {
  InputGroup,
  InputGroupAddon,
  InputGroupButton,
  InputGroupInput,
  InputGroupNumberField
} from "../../../../ui/input-group";
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue
} from "../../../../ui/select";
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuTrigger
} from "../../../../ui/dropdown-menu";
import { getStructFieldDescription, getStructFieldTitle } from "../../../utils/schema-meta";
import { Markdown } from "../../../../ui/markdown";
import { schema } from "./schema";
defineOptions({ name: "ShwfedTableNumberRendererConfig" });
const value = defineModel({ type: Object, ...{ required: true } });
const fieldSchema = schema(() => {
});
const fieldTitle = (field) => getStructFieldTitle(fieldSchema, field) ?? field;
const fieldDescription = (field) => getStructFieldDescription(fieldSchema, field);
const ALIGN_OPTIONS = [
  { value: "left", label: "\u5DE6\u5BF9\u9F50", icon: "fluent:text-align-left-20-regular" },
  { value: "center", label: "\u5C45\u4E2D", icon: "fluent:text-align-center-20-regular" },
  { value: "right", label: "\u53F3\u5BF9\u9F50", icon: "fluent:text-align-right-20-regular" }
];
const currentAlignIcon = computed(
  () => (ALIGN_OPTIONS.find((o) => o.value === (value.value.align ?? "right")) ?? ALIGN_OPTIONS[2]).icon
);
const PRESERVE_MODE_OPTIONS = [
  { value: "round", label: "\u56DB\u820D\u4E94\u5165" },
  { value: "floor", label: "\u5411\u4E0B\u53D6\u6574" },
  { value: "ceil", label: "\u5411\u4E0A\u53D6\u6574" }
];
const DISPLAY_MODE_OPTIONS = [
  { value: "plain", label: "\u6570\u503C" },
  { value: "uppercaseChinese", label: "\u4E2D\u6587\u5927\u5199" },
  { value: "monoThousand", label: "\u7B49\u5BBD\u5B57\u4F53\u52A0\u5343\u5206\u4F4D" }
];
const currentPreserveModeLabel = computed(
  () => PRESERVE_MODE_OPTIONS.find((o) => o.value === (value.value.preserveMode ?? "round"))?.label
);
function onPreserveDigitsChange(v) {
  const next = { ...value.value, preserveDigits: v };
  if (v === void 0) {
    next.preserveMode = void 0;
  } else if (next.preserveMode === void 0) {
    next.preserveMode = "round";
  }
  value.value = next;
}
</script>

<template>
  <div class="space-y-5">
    <div class="grid grid-cols-2 gap-x-6 gap-y-4">
      <Field orientation="vertical">
        <FieldLabel class="text-xs text-zinc-500">
          <template
            v-if="fieldDescription('title')"
            #tooltip
          >
            <Markdown
              :source="fieldDescription('title')"
              block
              class="prose prose-sm prose-zinc"
            />
          </template>
          {{ fieldTitle("title") }}
        </FieldLabel>
        <Locale
          :model-value="value.title"
          @update:model-value="(v) => value = { ...value, title: v }"
        />
      </Field>
      <Field orientation="vertical">
        <FieldLabel class="text-xs text-zinc-500">
          <template
            v-if="fieldDescription('tooltip')"
            #tooltip
          >
            <Markdown
              :source="fieldDescription('tooltip')"
              block
              class="prose prose-sm prose-zinc"
            />
          </template>
          {{ fieldTitle("tooltip") }}
        </FieldLabel>
        <Locale
          :model-value="value.tooltip"
          markdown
          @update:model-value="(v) => value = { ...value, tooltip: v }"
        />
      </Field>
    </div>
    <div class="grid grid-cols-2 gap-x-6 gap-y-4">
      <Field orientation="vertical">
        <FieldLabel class="text-xs text-zinc-500">
          <template
            v-if="fieldDescription('accessor')"
            #tooltip
          >
            <Markdown
              :source="fieldDescription('accessor')"
              block
              class="prose prose-sm prose-zinc"
            />
          </template>
          {{ fieldTitle("accessor") }}
        </FieldLabel>
        <Input
          :model-value="value.accessor"
          placeholder="如 row.salary"
          class="font-mono text-xs"
          @update:model-value="(v) => value = { ...value, accessor: v }"
        />
      </Field>
      <Field orientation="vertical">
        <FieldLabel class="text-xs text-zinc-500">
          <template
            v-if="fieldDescription('size')"
            #tooltip
          >
            <Markdown
              :source="fieldDescription('size')"
              block
              class="prose prose-sm prose-zinc"
            />
          </template>
          {{ fieldTitle("size") }}
        </FieldLabel>
        <InputGroup>
          <InputGroupAddon align="inline-start">
            <DropdownMenu>
              <DropdownMenuTrigger as-child>
                <InputGroupButton
                  variant="ghost"
                  size="xs"
                  :title="fieldTitle('align')"
                >
                  <Icon :icon="currentAlignIcon" />
                </InputGroupButton>
              </DropdownMenuTrigger>
              <DropdownMenuContent align="start">
                <DropdownMenuItem
                  v-for="opt in ALIGN_OPTIONS"
                  :key="opt.value"
                  @select="value = { ...value, align: opt.value }"
                >
                  <Icon :icon="opt.icon" />
                  {{ opt.label }}
                </DropdownMenuItem>
              </DropdownMenuContent>
            </DropdownMenu>
          </InputGroupAddon>
          <InputGroupNumberField
            :model-value="value.size"
            :disabled="value.grow"
            :min="0"
            @update:model-value="(v) => value = { ...value, size: v }"
          />
          <InputGroupAddon align="inline-end">
            <InputGroupButton
              :variant="value.grow ? 'primary' : 'ghost'"
              size="xs"
              @click="value = { ...value, grow: !value.grow }"
            >
              <Icon :icon="value.grow ? 'fluent:lock-closed-20-regular' : 'fluent:arrow-autofit-width-20-regular'" />
              {{ fieldTitle("grow") }}
            </InputGroupButton>
          </InputGroupAddon>
        </InputGroup>
      </Field>
      <Field orientation="vertical">
        <FieldLabel class="text-xs text-zinc-500">
          <template
            v-if="fieldDescription('displayMode')"
            #tooltip
          >
            <Markdown
              :source="fieldDescription('displayMode')"
              block
              class="prose prose-sm prose-zinc"
            />
          </template>
          {{ fieldTitle("displayMode") }}
        </FieldLabel>
        <Select
          :model-value="value.displayMode ?? 'plain'"
          @update:model-value="(v) => value = { ...value, displayMode: v }"
        >
          <SelectTrigger class="w-full">
            <SelectValue />
          </SelectTrigger>
          <SelectContent>
            <SelectItem
              v-for="opt in DISPLAY_MODE_OPTIONS"
              :key="opt.value"
              :value="opt.value"
            >
              {{ opt.label }}
            </SelectItem>
          </SelectContent>
        </Select>
      </Field>
      <Field orientation="vertical">
        <FieldLabel class="text-xs text-zinc-500">
          <template
            v-if="fieldDescription('preserveDigits')"
            #tooltip
          >
            <Markdown
              :source="fieldDescription('preserveDigits')"
              block
              class="prose prose-sm prose-zinc"
            />
          </template>
          {{ fieldTitle("preserveDigits") }}
        </FieldLabel>
        <InputGroup>
          <InputGroupNumberField
            :model-value="value.preserveDigits"
            :min="0"
            @update:model-value="onPreserveDigitsChange"
          />
          <InputGroupAddon
            v-if="value.preserveDigits !== void 0"
            align="inline-end"
          >
            <DropdownMenu>
              <DropdownMenuTrigger as-child>
                <InputGroupButton
                  variant="ghost"
                  class="!pr-1.5 text-xs"
                >
                  {{ currentPreserveModeLabel }}
                  <Icon
                    icon="lucide:chevron-down"
                    class="size-3"
                  />
                </InputGroupButton>
              </DropdownMenuTrigger>
              <DropdownMenuContent align="end">
                <DropdownMenuItem
                  v-for="opt in PRESERVE_MODE_OPTIONS"
                  :key="opt.value"
                  @select="value = { ...value, preserveMode: opt.value }"
                >
                  {{ opt.label }}
                </DropdownMenuItem>
              </DropdownMenuContent>
            </DropdownMenu>
          </InputGroupAddon>
        </InputGroup>
      </Field>
    </div>
    <Separator />
    <div class="flex flex-wrap gap-x-8 gap-y-3">
      <Field
        orientation="vertical"
        class="w-full max-w-xs"
      >
        <FieldLabel class="text-xs text-zinc-500">
          <template
            v-if="fieldDescription('enableSorting')"
            #tooltip
          >
            <Markdown
              :source="fieldDescription('enableSorting')"
              block
              class="prose prose-sm prose-zinc"
            />
          </template>
          {{ fieldTitle("enableSorting") }}
        </FieldLabel>
        <InputGroup>
          <InputGroupInput
            :model-value="value.sortKey"
            placeholder="例：created_at"
            @update:model-value="(v) => value = { ...value, sortKey: v || void 0 }"
          />
          <InputGroupAddon align="inline-end">
            <InputGroupButton
              :variant="value.enableSorting ? 'primary' : 'ghost'"
              size="xs"
              @click="value = { ...value, enableSorting: !value.enableSorting }"
            >
              <Icon :icon="value.enableSorting ? 'fluent:arrow-sort-up-16-regular' : 'fluent:arrow-sort-16-regular'" />
              {{ fieldTitle("enableSorting") }}
            </InputGroupButton>
          </InputGroupAddon>
        </InputGroup>
      </Field>
    </div>
  </div>
</template>
