<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :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="cardSubtitle">
        <el-input v-model="dataForm.cardSubtitle" placeholder="电影副标题"></el-input>
      </el-form-item>
          <el-form-item label="评分" prop="ratingValue">
        <el-input v-model="dataForm.ratingValue" placeholder="评分"></el-input>
      </el-form-item>
          <el-form-item label="评分人数" prop="ratingCount">
        <el-input v-model="dataForm.ratingCount" placeholder="评分人数"></el-input>
      </el-form-item>
          <el-form-item label="星级评分" prop="ratingStarCount">
        <el-input v-model="dataForm.ratingStarCount" placeholder="星级评分"></el-input>
      </el-form-item>
          <el-form-item label="上映时间" prop="year">
        <el-input v-model="dataForm.year" placeholder="上映时间"></el-input>
      </el-form-item>
          <el-form-item label="大图文件名" prop="largePic">
        <el-input v-model="dataForm.largePic" placeholder="大图文件名"></el-input>
      </el-form-item>
          <el-form-item label="小图文件名" prop="normalPic">
        <el-input v-model="dataForm.normalPic" placeholder="小图文件名"></el-input>
      </el-form-item>
          <el-form-item label="国家" prop="country">
        <el-input v-model="dataForm.country" placeholder="国家"></el-input>
      </el-form-item>
          <el-form-item label="电影类型" prop="movieType">
        <el-input v-model="dataForm.movieType" placeholder="电影类型"></el-input>
      </el-form-item>
          <el-form-item label="导演" prop="director">
        <el-input v-model="dataForm.director" placeholder="导演"></el-input>
      </el-form-item>
          <el-form-item label="主演" prop="star">
        <el-input v-model="dataForm.star" placeholder="主演"></el-input>
      </el-form-item>
      </el-form>
    <template #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({
  id: '',  title: '',  cardSubtitle: '',  ratingValue: '',  ratingCount: '',  ratingStarCount: '',  year: '',  largePic: '',  normalPic: '',  country: '',  movieType: '',  director: '',  star: ''});

const rules = ref({
          title: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          cardSubtitle: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          ratingValue: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          ratingCount: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          ratingStarCount: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          year: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          largePic: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          normalPic: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          country: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          movieType: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          director: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          star: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ]
  });

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

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

  if (id) {
    getInfo(id);
  }
};

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

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

defineExpose({
  init
});
</script>
