<template>
  <div>
    <div class="field is-horizontal">
      <div class="field-body">
        <div class="field is-narrow">
          <label class="label">Code <span class="has-text-danger"> * </span> </label>
          <p class="control">
            <input
              class="input"
              type="text"
              placeholder=""
              v-model="record.code"
              id="code"
            />
          </p>
        </div>
        <div class="field">
          <label class="label">Name <span class="has-text-danger"> * </span> </label>
          <p class="control">
            <input
              class="input"
              type="text"
              placeholder=""
              v-model="record.name"
            />
          </p>
        </div>
      </div>
    </div>
    <div class="field mt-5">
      <label class="label">Index</label>
      <p class="control">
        <input
          class="input"
          type="text"
          placeholder=""
          v-model="record.index"
        />
      </p>
    </div>
    <div class="mt-5">
      <button
        class="button is-primary has-text-white"
        @click="update()"
      >
        Save
      </button>
    </div>
  </div>
</template>
<script setup>
import { onMounted } from "vue";
const { $copy, $resetNull, $insertrow, $updaterow, $snackbar } = useNuxtApp();
var props = defineProps({
  api: String,
  pagename: String,
  row: Object,
  prefix: String,
});
const emit = defineEmits(["close", "modalevent"]);
var record = $copy(props.row || {});
async function update() {
  let data = $resetNull(record);
  let rs = data.id
    ? await $updaterow(props.api, data, undefined, props.pagename)
    : await $insertrow(props.api, data, undefined, props.pagename);
  if (rs === "error") return $snackbar(rs);
  emit("modalevent", { name: "dataevent", data: rs });
  emit("close");
}
onMounted(() => {
  document.getElementById("code").focus();
});
</script>
