<script setup>
import { computed } from "vue";
import { Icon } from "@iconify/vue";
import { ExpressionEditor } from "../../../../ui/expression-editor";
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 {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuTrigger
} from "../../../../ui/dropdown-menu";
import { getStructFieldDescription, getStructFieldTitle } from "../../../utils/schema-meta";
import { Markdown } from "../../../../ui/markdown";
import TriggersField from "../../../../actions/components/triggers-field.vue";
import { schema } from "./schema";
defineOptions({ name: "ShwfedTableSwitchRendererConfig" });
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 ?? "center")) ?? ALIGN_OPTIONS[1]).icon
);
const ROW_VARS = {
  row: { type: "dyn", label: "\u5F53\u524D\u884C\u6570\u636E" },
  index: { type: "number", label: "\u884C\u7D22\u5F15" }
};
const ROW_VALUE_VARS = {
  ...ROW_VARS,
  value: { type: "bool", label: "\u5207\u6362\u540E\u7684\u65B0\u503C" }
};
const JSON_VARS = {
  json: { type: "dyn", label: "HTTP \u54CD\u5E94\u4F53" }
};
const disabledModel = computed({
  get: () => value.value.disabled ?? "",
  set: (v) => {
    if (v === "") {
      const { disabled: _drop, ...rest } = value.value;
      value.value = rest;
    } else {
      value.value = { ...value.value, disabled: v };
    }
  }
});
const onChangeModel = computed({
  get: () => value.value.onChange ?? "",
  set: (v) => {
    if (v === "") {
      const { onChange: _drop, ...rest } = value.value;
      value.value = rest;
    } else {
      value.value = { ...value.value, onChange: v };
    }
  }
});
const successMessageModel = computed({
  get: () => value.value.successMessage ?? "",
  set: (v) => {
    if (v === "") {
      const { successMessage: _drop, ...rest } = value.value;
      value.value = rest;
    } else {
      value.value = { ...value.value, successMessage: v };
    }
  }
});
const triggers = computed(() => value.value.triggers ?? []);
function updateTriggers(next) {
  if (next.length === 0) {
    const { triggers: _drop, ...rest } = value.value;
    value.value = rest;
  } else {
    value.value = { ...value.value, triggers: 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>
        <ExpressionEditor
          :model-value="value.accessor"
          placeholder="如 row.enabled"
          result-type="bool"
          :extra-vars="ROW_VARS"
          @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('disabled')"
            #tooltip
          >
            <Markdown
              :source="fieldDescription('disabled')"
              block
              class="prose prose-sm prose-zinc"
            />
          </template>
          {{ fieldTitle("disabled") }}
        </FieldLabel>
        <ExpressionEditor
          v-model="disabledModel"
          placeholder="如 row.locked"
          result-type="bool"
          :extra-vars="ROW_VARS"
        />
      </Field>
      <Field orientation="vertical">
        <FieldLabel class="text-xs text-zinc-500">
          <template
            v-if="fieldDescription('onChange')"
            #tooltip
          >
            <Markdown
              :source="fieldDescription('onChange')"
              block
              class="prose prose-sm prose-zinc"
            />
          </template>
          {{ fieldTitle("onChange") }}
        </FieldLabel>
        <ExpressionEditor
          v-model="onChangeModel"
          placeholder="如 http.post('/api/items/' + row.id).body({ enabled: value })"
          result-type="HttpRequest"
          :extra-vars="ROW_VALUE_VARS"
          multiline
        />
      </Field>
      <Field orientation="vertical">
        <FieldLabel class="text-xs text-zinc-500">
          <template
            v-if="fieldDescription('successMessage')"
            #tooltip
          >
            <Markdown
              :source="fieldDescription('successMessage')"
              block
              class="prose prose-sm prose-zinc"
            />
          </template>
          {{ fieldTitle("successMessage") }}
        </FieldLabel>
        <ExpressionEditor
          v-model="successMessageModel"
          placeholder="如 '已更新'"
          result-type="string"
          :extra-vars="JSON_VARS"
        />
      </Field>
      <Field orientation="vertical">
        <FieldLabel class="text-xs text-zinc-500">
          <template
            v-if="fieldDescription('triggers')"
            #tooltip
          >
            <Markdown
              :source="fieldDescription('triggers')"
              block
              class="prose prose-sm prose-zinc"
            />
          </template>
          {{ fieldTitle("triggers") }}
        </FieldLabel>
        <TriggersField
          :triggers="triggers"
          @update:triggers="updateTriggers"
        />
      </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>
