<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="title">
        <el-input v-model="dataForm.title" placeholder=""></el-input>
      </el-form-item>
          <el-form-item label="" prop="content">
        <el-input v-model="dataForm.content" placeholder=""></el-input>
      </el-form-item>
          <el-form-item label="产品优势" prop="content1">
        <el-input v-model="dataForm.content1" placeholder="产品优势"></el-input>
      </el-form-item>
          <el-form-item label="产品特点" prop="content2">
        <el-input v-model="dataForm.content2" placeholder="产品特点"></el-input>
      </el-form-item>
          <el-form-item label="产品参数" prop="content3">
        <el-input v-model="dataForm.content3" placeholder="产品参数"></el-input>
      </el-form-item>
          <el-form-item label="价格" prop="price">
        <el-input v-model="dataForm.price" placeholder="价格"></el-input>
      </el-form-item>
          <el-form-item label="市场价" prop="nprice">
        <el-input v-model="dataForm.nprice" placeholder="市场价"></el-input>
      </el-form-item>
          <el-form-item label="数量" prop="quantity">
        <el-input v-model="dataForm.quantity" placeholder="数量"></el-input>
      </el-form-item>
          <el-form-item label="" prop="typeid">
        <el-input v-model="dataForm.typeid" 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="taix">
        <el-input v-model="dataForm.taix" 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="count">
        <el-input v-model="dataForm.count" placeholder="统计"></el-input>
      </el-form-item>
          <el-form-item label="" prop="addtime">
        <el-input v-model="dataForm.addtime" placeholder=""></el-input>
      </el-form-item>
      </el-form>
    <template v-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: '',  title: '',  content: '',  content1: '',  content2: '',  content3: '',  price: '',  nprice: '',  quantity: '',  typeid: '',  admin: '',  taix: '',  thumbnail: '',  count: '',  addtime: ''});

const rules = ref({
          title: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          content: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          content1: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          content2: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          content3: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          price: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          nprice: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          quantity: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          typeid: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          admin: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          taix: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          thumbnail: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          count: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          addtime: [
      { 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/cniyvproduct/" + 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/cniyvproduct", dataForm).then((res) => {
      ElMessage.success({
        message: '成功',
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>
