<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-row>
        <el-col :span="24">
          <el-form-item label="标题" prop="title">
            <el-input v-model="form.title" placeholder="请输入标题" maxlength="255" show-word-limit />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="详细介绍" prop="content">
            <editor v-model="form.content" :min-height="300" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <div class="form-footer">
      <el-button type="primary" @click="submitForm" :loading="loading">保存</el-button>
    </div>
  </div>
</template>

<script>
import { getAchievementOverview, updateAchievementOverview } from '@/api/achievement'

export default {
  name: 'AchievementOverview',
  data() {
    return {
      loading: false,
      form: {
        id: 1,
        title: '',
        content: ''
      },
      rules: {
        title: [
          { required: true, message: '标题不能为空', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '详细介绍不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getInfo()
  },
  methods: {
    getInfo() {
      this.loading = true
      getAchievementOverview(1).then(response => {
        if (response.code === 200 && response.data) {
          this.form = response.data
        }
      }).finally(() => {
        this.loading = false
      })
    },
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          updateAchievementOverview(this.form).then(response => {
            if (response.code === 200) {
              this.$modal.msgSuccess('保存成功')
              this.getInfo()
            }
          }).finally(() => {
            this.loading = false
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.form-footer {
  margin-top: 30px;
  text-align: center;
}
</style>
