<template>
  <ai-detail>
    <template slot="title">
      <ai-title :title="params.id ? '编辑小程序公告' : '添加小程序公告'" isShowBack isShowBottomBorder @onBackClick="cancel(false)">
      </ai-title>
    </template>
    <template slot="content">
      <ai-card title="基本信息">
        <template #content>
          <el-form ref="form" class="ai-form" :model="form" label-width="110px" label-position="right">
            <el-form-item label="标题" style="width: 100%;" prop="title" :rules="[{ required: true, message: '请输入标题', trigger: 'blur' }]">
              <el-input size="small" placeholder="请输入标题" :maxlength="30" v-model="form.title"></el-input>
            </el-form-item>
            <el-form-item label="正文" style="width: 100%;" prop="content" :rules="[{ required: true, message: '请输入正文', trigger: 'blur' }]">
              <el-input size="small" placeholder="请输入正文" :rows="6" :maxlength="1000" type="textarea" v-model="form.content"></el-input>
            </el-form-item>
            <el-form-item label="图片" style="width: 100%;" prop="images">
              <ai-uploader v-model="form.images" :instance="instance" :limit="9" isShowTip>
              </ai-uploader>
            </el-form-item>
          </el-form>
        </template>
      </ai-card>
    </template>
    <template #footer>
      <el-button @click="cancel">取消</el-button>
      <el-button type="primary" @click="confirm">提交</el-button>
    </template>
  </ai-detail>
</template>

<script>
import Template from '../../../wechat/AppAskForm/components/Template.vue'
  export default {
  components: { Template },
    name: 'Add',

    props: {
      instance: Function,
      dict: Object,
      params: Object
    },

    data () {
      return {
        info: {},
        form: {
          title: '',
          content: '',
          images: []
        },
        id: ''
      }
    },

    created () {
      if (this.params && this.params.id) {
        this.id = this.params.id
        this.getInfo(this.params.id)
      }
    },

    methods: {
      getInfo (id) {
        this.instance.post(`/app/appmininotice/queryDetailById?id=${id}`).then(res => {
          if (res.code === 0) {
            this.form = {
              ...res.data,
              images: res.data.images ? JSON.parse(res.data.images) : []
            }
          }
        })
      },

      onClose () {
        this.form.explain = ''
      },

      confirm () {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.instance.post(`/app/appmininotice/addOrUpdate`, {
              ...this.form,
              images: this.form.images ? JSON.stringify(this.form.images) : []
            }).then(res => {
              if (res.code == 0) {
                this.$message.success('提交成功')
                setTimeout(() => {
                  this.cancel(true)
                }, 600)
              }
            })
          }
        })
      },

      cancel (isRefresh) {
        this.$emit('change', {
          type: 'list',
          isRefresh: !!isRefresh
        })
      }
    }
  }
</script>

<style scoped lang="scss">
</style>
