<template>
  <el-dialog v-model="visible" :title="!dataForm.ptsid ? '新增' : '修改'" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
          <el-form-item label="" prop="zntid">
        <el-input v-model="dataForm.zntid" placeholder=""></el-input>
      </el-form-item>
          <el-form-item label="" prop="name">
        <el-input v-model="dataForm.name" placeholder=""></el-input>
      </el-form-item>
          <el-form-item label="" prop="parentid">
        <el-input v-model="dataForm.parentid" placeholder=""></el-input>
      </el-form-item>
          <el-form-item label="" prop="selectid">
        <el-input v-model="dataForm.selectid" placeholder=""></el-input>
      </el-form-item>
          <el-form-item label="" prop="link">
        <el-input v-model="dataForm.link" placeholder=""></el-input>
      </el-form-item>
          <el-form-item label="" prop="thumbnail">
        <el-input v-model="dataForm.thumbnail" placeholder=""></el-input>
      </el-form-item>
          <el-form-item label="" prop="sort">
        <el-input v-model="dataForm.sort" placeholder=""></el-input>
      </el-form-item>
          <el-form-item label="" prop="admin">
        <el-input v-model="dataForm.admin" placeholder=""></el-input>
      </el-form-item>
          <el-form-item label="" prop="eaddtime">
        <el-input v-model="dataForm.eaddtime" placeholder=""></el-input>
      </el-form-item>
      </el-form>
    <template slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { ElMessage } from "element-plus";
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  ptsid: '',  zntid: '',  name: '',  parentid: '',  selectid: '',  link: '',  thumbnail: '',  sort: '',  admin: '',  eaddtime: ''});

const rules = ref({
          zntid: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          name: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          parentid: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          selectid: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          link: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          thumbnail: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          sort: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          admin: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          eaddtime: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ]
  });

const init = (ptsid?: number) => {
  visible.value = true;
  dataForm.ptsid = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (ptsid) {
    getInfo(ptsid);
  }
};

// 获取信息
const getInfo = (ptsid: number) => {
  baseService.get("/cniyv/cniyvarticletype/" + ptsid).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.ptsid ? baseService.post : baseService.put)("/cniyv/cniyvarticletype", dataForm).then((res) => {
      ElMessage.success({
        message: '成功',
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>
