<template>
  <el-dialog v-model="props.modelValue" 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="love_story_title">
            <el-input v-model='formData.love_story_title' type="text" placeholder='请输入标题'></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label-width="labelWidth" label="作者" prop="love_story_author">
            <el-input v-model='formData.love_story_author' type="text" placeholder='请输入作者'></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label-width="labelWidth" label="地点" prop="love_story_place">
            <el-input v-model='formData.love_story_place' type="text" placeholder='请输入地点'></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label-width="labelWidth" label="日期" prop="love_story_date">
            <el-date-picker v-model="formData.love_story_date" type="date" value-format="YYYY-MM-DD" placeholder="日期" />
          </el-form-item>
        </el-col>
        <el-col :span='12'>
          <el-form-item :label-width='labelWidth' label='封面' prop='love_story_cover'>
            <UploadImage ref='uploadRef' v-model='formData.love_story_cover' :data=uoloadData :limit='1' :fileSize='5'
              :drag='true' :isShowTip='false' />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item :label-width="labelWidth" label="排序" prop="love_story_sort">
            <el-input-number v-model='formData.love_story_sort' controls-position='right' :min='1'></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span='12'>
          <el-form-item :label-width='labelWidth' label='音乐' prop='love_story_music'>
            <UploadFile v-model='formData.love_story_music' :fileType="['mp3', 'wma', 'wav', 'ape', 'flac', 'ogg', 'aac']"
              :data=uoloadData :limit="1"/>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item :label-width="labelWidth" label="内容" prop="love_story_content">
            <RichText v-model='formData.love_story_content' :min-height='196'></RichText>
          </el-form-item>
        </el-col>

      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="handleEditClick(formRef)" :loading="isBtnLod">编辑</el-button>
        <el-button @click="handleResetClick(formRef)">重置</el-button>
      </span>
    </template>
  </el-dialog>
</template>

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

// --业务参数



// --业务方法





// --基础参数
const store = useLoginStore();
const headers = {
  Accept: "application/json",
  ...store.headers,
};
const isBtnLod = ref(false);
const formRef = ref();
const labelWidth = 100;
const props = defineProps({
  modelValue: Boolean,
  data: Object,
  done: Function,
});
const emits = defineEmits(["update:modelValue"]);
const formData = ref({
  ...props.data,
});
const uoloadData = ref({
  dirName: "LoveStory"
})


// --基础方法
watch(props, (v) => {
  formData.value = v.data;


});

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

};

const closeDialog = () => {
  props.done();
  emits("update:modelValue", false);
};

const rules = reactive({
  love_story_title: [
    {
      required: true,
      message: '标题不能为空'
    }
  ],
  love_story_place: [
    {
      required: true,
      message: '地点不能为空'
    }
  ],
  love_story_date: [
    {
      required: true,
      message: '日期不能为空'
    }
  ],
  love_story_cover: [
    {
      required: true,
      message: '封面不能为空'
    }
  ],
  // love_story_sort: [
  //   {
  //     required: true,
  //     message: '排序不能为空'
  //   }
  // ],

});

const handleEditClick = async (formEl) => {
  console.log(formData.value);
  if (!formEl) return;
  formEl.validate(async (valid) => {
    if (!valid) {
      return;
    }
    isBtnLod.value = true;


    const { code } = await editLoveStory(formData.value);
    if (code == 0) {
      closeDialog();
      props.done();
    }
    isBtnLod.value = false;
  });
};
const handleResetClick = async (formEl) => {
  if (!formEl) return;
  formEl.resetFields();
};
</script>

<style lang="less" scoped></style>
