<template>
  <el-dialog v-model="dialogVisible" title="添加留言" width="900px" @closed="closeDialog" @open="openDialog">
    <el-form ref="formRef" :model="formData" :rules="rules">
      <el-row>
        
        
                           <el-col :span="12">
                               <el-form-item :label-width="labelWidth" label="姓名" prop="leave_message_name">
                                  <el-input v-model='formData.leave_message_name' type="text" placeholder='请输入姓名'></el-input>
                               </el-form-item>
                           </el-col>
                           <el-col :span="12">
                               <el-form-item :label-width="labelWidth" label="电话" prop="leave_message_phone">
                                  <el-input v-model='formData.leave_message_phone' type="text" placeholder='请输入电话'></el-input>
                               </el-form-item>
                           </el-col>
                           <el-col :span="12">
                               <el-form-item :label-width="labelWidth" label="邮箱" prop="leave_message_email">
                                  <el-input v-model='formData.leave_message_email' type="text" placeholder='请输入邮箱'></el-input>
                               </el-form-item>
                           </el-col>
                           <el-col :span="24">
                               <el-form-item :label-width="labelWidth" label="信息" prop="leave_message_info">
                                  <el-input v-model='formData.leave_message_info' type="textarea" :rows="5" placeholder='请输入信息'></el-input>
                               </el-form-item>
                           </el-col>

      </el-row>
    </el-form>

    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="handleAddClick(formRef)">添加</el-button>
        <el-button @click="handleResetClick(formRef)">重置</el-button>
        <el-button @click="closeDialog()">取消</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { reactive, ref, watch } from "vue";
import { addLeaveMessage  } from "~/service/leave_message";
import { useLoginStore } from "~/store";

// --业务参数



// --业务方法





// --基础参数
const store = useLoginStore();
const headers = {
  Accept: "application/json",
  ...store.headers,
};

const isBtnLod = ref(false);
const formRef = ref();
const labelWidth = 90;
const props = defineProps({
  modelValue: Boolean,
  done: Function,
});
const emits = defineEmits(["update:modelValue"]);
const dialogVisible = ref(props.modelValue);
const formData = reactive({});

const uoloadData = ref({
  dirName: "LeaveMessage"
})

watch(props, (v) => {
  dialogVisible.value = v.modelValue;
});

const rules = reactive({
  leave_message_name: [
                               {
                               required: true,
                               message: '姓名不能为空'
                               }
                           ],
                           leave_message_phone: [
                               {
                               required: true,
                               message: '电话不能为空'
                               }
                           ],
                           leave_message_email: [
                               {
                               required: true,
                               message: '邮箱不能为空'
                               }
                           ],
                           leave_message_info: [
                               {
                               required: true,
                               message: '信息不能为空'
                               }
                           ],
                           
});


// --基础方法

// 打开弹窗时执行
const openDialog = () => {
  
};

const closeDialog = () => {
  handleResetClick(formRef.value);
  dialogVisible.value = false;
  emits("update:modelValue", false);
};

const handleAddClick = async (formEl) => {
  if (!formEl) return;
  formEl.validate(async (valid) => {
    if (!valid) {
      return;
    }
    isBtnLod.value = true;

    
    const { code } = await addLeaveMessage(formData);
    if (code == 0) {
      closeDialog();
      props.done();
    }
    isBtnLod.value = flase;
  });
};

const handleResetClick = async (formEl) => {
  if (!formEl) return;
  formEl.resetFields();
};
</script>

<style lang="less" scoped>

</style>
