<template>
  <el-form
    ref="form"
    :model="form"
    label-width="80px"
    @submit.stop.native="submit"
  >
    <el-form-item
      label="相册名称"
      prop="name"
      :rules="[{required: true, message: '请输入相册名称'}]"
    >
      <el-input v-model="form.name" placeholder="请输入相册名称"/>
    </el-form-item>
    <el-form-item
      label="相册时间"
      prop="timeLine"
      :rules="[{required: true, message: '请选择相册时间'}]"
    >
      <el-date-picker
        v-model="form.timeLine"
        type="month"
        value-format="yyyy-MM-DD"
        style="width: 100%"
        placeholder="请选择相册时间">
      </el-date-picker>
    </el-form-item>
    <div class="dialog-footer">
      <el-button @click="$emit('close')">取 消</el-button>
      <el-button
        type="primary"
        native-type="submit"
      >确 定</el-button>
    </div>
  </el-form>
</template>
<script>
import dayjs from 'dayjs'
import { photoAlbumAdd, photoAlbumUpdate } from '../api'
export default {
  props: {
    detail: {
      type: Object,
    }
  },
  data() {
    return {
      form: {
        name: '',
        timeLine: ''
      }
    }
  },
  mounted() {
    if (this.detail) {
      this.form.name = this.detail.name
      this.form.timeLine = this.detail.timeLine
    }
  },
  methods: {
    async submit(evt) {
      evt.stopPropagation()
      evt.stopImmediatePropagation()
      evt.preventDefault()
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          try {
            if (this.detail) {
              await photoAlbumUpdate([{
                ...this.form,
                musicGroupId: this.$route.query.id,
                year: dayjs(this.form.timeLine).year(),
                id: this.detail.id
              }])
              this.$message.success('修改成功')
            } else {
              await photoAlbumAdd({
                ...this.form,
                musicGroupId: this.$route.query.id
              })
              this.$message.success('添加成功')
            }
            this.$emit('close')
            this.$emit('submited')
          } catch (error) {}
        }
      })
    },
  }
}
</script>
<style lang="less" scoped>
.dialog-footer{
  text-align: right;
}
</style>
