<script setup lang="ts">
import { ref, unref, computed } from "vue";
import { ElForm } from "element-plus";
import { goodTypeOptions } from "../config/_options";
import { ladderFormRules } from "../config/_rules";
import { ImageUpload } from "/@/components/Upload";

import { AmountInput, NumberInput } from "/@/components/Input";
import GoodClass from "/@/components/GoodClass";

const emit = defineEmits(["push", "update"]);

const defaultData = {
  budget_price: "0.00",
  good_type: "1",
  good_name: "",
  cat_info: [],
  num: "0"
};

const isUpdate = ref(false);
const visible = ref(false);
const formRef = ref<InstanceType<typeof ElForm> | null>(null);
const updateIndex = ref("0");

const formData = ref<Record<string, any>>({ ...defaultData });

const title = computed(() => {
  return isUpdate.value ? "修改商品要求" : "添加商品要求";
});

function handleConfirm() {
  formRef.value.validate(isValid => {
    if (!isValid) return;

    emit(
      isUpdate.value ? "update" : "push",
      unref(formData),
      unref(updateIndex)
    );

    visible.value = false;
  });
}

defineExpose({
  onDisplay: (data?: Record<string, any>, index = "0") => {
    if (data) {
      data.cat_info = [];
      formData.value = data;
      updateIndex.value = index;
    }

    visible.value = true;
    isUpdate.value = !!data;
  }
});
</script>

<template>
  <ElDialog
    center
    :title="title"
    v-model="visible"
    @close="() => (formData = { ...defaultData })"
  >
    <PageContent
    ref="pageContentRef"
    :content-config="contentConfig"
    @create-btn-click="handleCreateData"
  />

    
     
        <div class="w-full flex justify-end">
          <ElButton type="primary" @click="handleConfirm">保存</ElButton>
          <ElButton @click="() => (visible = false)">取消</ElButton>
        </div>
    
    
  </ElDialog>
</template>
