<template>
  <el-dialog
      model-value="CdialogVisible"
      :title="dialogTitle"

      width="30%"
      @close="handleClose"
  >
    <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="120px"
    >
      <el-form-item label="商品分类名称：" >
        <el-input v-model="form.groupName" />
      </el-form-item>
      <el-form-item label="关联商家ID：" >
        <el-input v-model="form.businessId" />
      </el-form-item>
      <el-form-item label="是否置顶：" >
        <el-input v-model="form.isTopping" />
      </el-form-item>
    </el-form>
    <template #footer>
<span class="dialog-footer">
<el-button @click="handleClose">取消</el-button>
<el-button type="primary" @click="handleConfirm">确认</el-button>
</span>
    </template>
  </el-dialog>
</template>
<script setup>
import {defineEmits, defineProps, ref, watch} from "vue";
import axios from "@/util/axios";
import { ElMessage } from "element-plus";
const props=defineProps({
  dialogTitle:{
    type:String,
    default:'',
    required:true
  }
})
const form=ref({
  businessId: null,
  groupName: "",
  isTopping: null
})
const rules=ref({
  businessId: [
    {
      required: true,
      message: '请输入关联商家ID'
    }
  ],
  groupName: [
    {
      required: true,
      message: '请输入商品大类名称！'
    }
  ],
  isTopping: [
    {
      required: true,
      message: '是否置顶！'
    }
  ]
})

const formRef=ref(null);

// 定义父组件事件
const emits=defineEmits(['update:modelValue','initRatingList'])
const handleClose=()=>{
  console.log("xxx")
// 调用执行
  emits('update:modelValue',false)
}
const handleConfirm=()=>{
  formRef.value.validate(async(valid)=>{
    if(valid){
      let result=await axios.post("group/add",form.value)
      let data=result.data;
      if(data.code==0){
        ElMessage.success("执行成功！");
        formRef.value.resetFields();
        emits("initRatingList");
        handleClose();
      }else{
        ElMessage.error(data.msg);
      }
    }else{
      console.log("fail")
      return false
    }
  })
}
</script>
<style scoped>
</style>