<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="publishUnitName" :rules="[{ required: true, message: '请输入发布组织', trigger: 'blur' }]">
              <el-input size="small" placeholder="请输入发布组织" :maxlength="50" v-model="form.publishUnitName"></el-input>
            </el-form-item>
            <el-form-item label="正文" style="width: 100%;" prop="content">
              <ai-editor v-model="form.content" :instance="instance" />
            </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>
  export default {
    name: 'Add',

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

    data () {
      return {
        info: {},
        form: {
          title: '',
          content: '',
          publishUnitName: '',
          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) {
            if (!this.form.content && !this.form.images.length) {
              return this.$message.error('请输入正文或上传图片')
            }

            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>
