<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="image">
            <image-upload v-model="form.image"/>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="视频" prop="video">
            <file-upload
              v-model="form.video"
              :limit="1"
              :file-size="100"
              :file-type="['mp4', 'avi', 'mov', 'wmv', 'flv', 'mkv']"
            />
          </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:overview:edit']">保 存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getOverview, addOverview, updateOverview } from "@/api/homepage/overview"
import FileUpload from "@/components/FileUpload"

export default {
  name: "Overview",
  components: {
    FileUpload
  },
  dicts: ['sys_normal_disable'],
  data() {
    return {
      loading: true,
      form: {
        overviewId: undefined,
        title: undefined,
        summary: undefined,
        image: undefined,
        video: undefined,
        status: "0"
      },
      rules: {
        title: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ]
      }
    }
  },
  created() {
    this.getOverviewData()
  },
  methods: {
    getOverviewData() {
      this.loading = true
      getOverview().then(response => {
        if (response.data) {
          this.form = response.data
        } else {
          this.form = {
            overviewId: undefined,
            title: undefined,
            summary: undefined,
            image: undefined,
            video: undefined,
            status: "0"
          }
        }
        this.loading = false
      }).catch(() => {
        this.form = {
          overviewId: undefined,
          title: undefined,
          summary: undefined,
          image: undefined,
          video: undefined,
          status: "0"
        }
        this.loading = false
      })
    },
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.overviewId != undefined) {
            updateOverview(this.form).then(() => {
              this.$modal.msgSuccess("保存成功")
              this.getOverviewData()
            })
          } else {
            addOverview(this.form).then(() => {
              this.$modal.msgSuccess("保存成功")
              this.getOverviewData()
            })
          }
        }
      })
    }
  }
}
</script>
