<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px" v-loading="loading">
      <el-row>
        <el-col :span="24">
          <el-form-item label="标题" prop="title">
            <el-input v-model="form.title" placeholder="请输入标题" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="简介" prop="summary">
            <el-input v-model="form.summary" type="textarea" placeholder="请输入简介" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="图片" prop="images">
            <image-upload v-model="form.images" :multiple="true"/>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="状态">
            <el-radio-group v-model="form.status">
              <el-radio
                v-for="dict in dict.type.sys_normal_disable"
                :key="dict.value"
                :label="dict.value"
              >{{dict.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item>
        <el-button type="primary" @click="submitForm" v-hasPermi="['homepage:introduction:edit']">保 存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getIntroduction, addIntroduction, updateIntroduction } from "@/api/homepage/introduction"

export default {
  name: "Introduction",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      loading: true,
      form: {
        introId: undefined,
        title: undefined,
        summary: undefined,
        images: undefined,
        status: "0"
      },
      rules: {
        title: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
        summary: [
          { required: true, message: "简介不能为空", trigger: "blur" }
        ]
      }
    }
  },
  created() {
    this.getIntroductionData()
  },
  methods: {
    getIntroductionData() {
      this.loading = true
      getIntroduction().then(response => {
        if (response.data) {
          this.form = response.data
        } else {
          this.form = {
            introId: undefined,
            title: undefined,
            summary: undefined,
            images: undefined,
            status: "0"
          }
        }
        this.loading = false
      }).catch(() => {
        this.form = {
          introId: undefined,
          title: undefined,
          summary: undefined,
          images: undefined,
          status: "0"
        }
        this.loading = false
      })
    },
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.introId != undefined) {
            updateIntroduction(this.form).then(() => {
              this.$modal.msgSuccess("保存成功")
              this.getIntroductionData()
            })
          } else {
            addIntroduction(this.form).then(() => {
              this.$modal.msgSuccess("保存成功")
              this.getIntroductionData()
            })
          }
        }
      })
    }
  }
}
</script>
