<template>
  <el-dialog class="dialogWerapper" width="30%" v-model="modelValue" :title="title" @close="handleClose">
    <form-message ref="FormRef" :disabled="disabled" v-model="formData" :rules="rules" :formList="formList" />

    <template #footer>
      <el-button type="primary" v-if="!disabled" :loading="loading" @click="handleSubmit">确定</el-button>
      <el-button @click="handleClose">取消</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { computed, ref, reactive, getCurrentInstance } from 'vue';
import FormMessage from '@/components/aboutTable/FormMessage.vue'
import { qcckGet, qcckPost, qcckDelete } from "@/api/qcckApi.js";

const { proxy } = getCurrentInstance();
const { D_BAXX_DWLX } = proxy.$dict("D_BAXX_DWLX")
const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  },
})

const emits = defineEmits(['update:modelValue', 'refresh'])

const visible = computed({
  get() {
    return props.modelValue
  },
  set(val) {
    emits('update:modelValue', val)
  }
})

const title = ref('选择培训公司')
const loading = ref(false)
const disabled = ref(false)
const FormRef = ref(null)
const formData = ref({})

const formList = reactive([
  [
    { label: "培训公司", prop: "pxgs", type: "select", options: D_BAXX_DWLX },
  ],
])

const rules = {
  pxgs: [{ required: true, message: "请输入考试成绩", trigger: "change" }],
}

const open = (row = {}, type = 'updata') => {
  visible.value = true
  disabled.value = false
  formData.value = { ...row }
}

const handleClose = () => {
  FormRef.value?.reset()
  visible.value = false
}

const handleSubmit = async () => {
  try {
    loading.value = true
    await FormRef.value.submit(async () => {
      await qcckPost({ ...formData.value  }, `/mosty-base/baxx/basq/edit`)
      proxy.$message.success('培训公司保存成功')
      emits('refresh')
      visible.value = false
    })
  } catch (error) {
    console.log(error)
    proxy.$message.error('培训公司保存失败')
  } finally {
    loading.value = false
  }
}

defineExpose({ open })
</script>
