<template>
  <div class="app-container">
    <el-form
      ref="convenienceForm"
      style="width:680px"
      :model="form"
      :rules="rules"
      :disabled="pageQuery.type === 'detail'"
      label-width="80px"
      class="el-form"
    >
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="form.title"
          placeholder="请输入"
        />
      </el-form-item>
      <el-form-item label="简介" prop="introduction">
        <el-input
          v-model="form.introduction"
          type="textarea"
          placeholder="请输入"
        />
      </el-form-item>
      <el-form-item label="图片" prop="images">
        <template v-if="pageQuery.type === 'detail'">
          <el-image v-for="item of form.images" :key="item" :src="item" />
        </template>
        <CutUploadImage v-else :disabled="pageQuery.type === 'detail'" :init-url="form.images" multiple @imgupload="imgUpload" />
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <UEditor v-model="form.content" :disabled="pageQuery.type === 'detail'" />
      </el-form-item>
      <el-form-item v-if="pageQuery.type !== 'detail'">
        <el-button type="primary" @click="handleAdd">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { activityUpdate, activityInfo } from '@/api/fingertipIntegral'
import UEditor from '@/components/UEditor'
import CutUploadImage from '@/components/cutUploadImage'

export default {
  components: { UEditor, CutUploadImage },
  data() {
    return {
      pageQuery: {
        type: undefined,
        id: undefined
      },
      form: {
        active: true,
        audioImage: undefined,
        audioUrl: undefined,
        content: undefined,
        images: [],
        introduction: undefined,
        show: true,
        subtitle: undefined,
        title: undefined,
        videoUrl: undefined
      },
      rules: {
        title: [{ required: true, message: '请输入', trigger: 'change' }]
      }
    }
  },
  created() {
    this.pageQuery = this.$route.query
    if (this.pageQuery.id) this.getInfo()
  },
  methods: {
    getInfo() {
      activityInfo({ id: this.pageQuery.id }).then(res => {
        console.log(res)
        this.form = res.d
      })
    },
    imgUpload(e) {
      this.form.images = e
    },
    handleAdd() {
      this.$refs.convenienceForm.validate(valid => {
        if (valid) {
          const form = this.form
          activityUpdate(form).then(res => {
            this.$message({
              type: 'success',
              message: '操作成功!'
            })
            this.$router.back()
          })
        }
      })
    }
  }
}
</script>

<style scope></style>
