<template>
  <ai-detail class="content-add">
    <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 class="ai-form" :model="form" label-width="120px" ref="form">
            <el-form-item label="单位" prop="departmentName" style="width: 100%;" :rules="[{ required: true, message: '请选择单位', trigger: 'change' }]">
              <el-input size="small" placeholder="请选择..." disabled v-model="form.departmentName">
                <ai-wechat-selecter slot="append" isStrictly :instance="instance" @change="onChange" v-model="department" isChooseUnit>
                  <el-button type="info">选择</el-button>
                </ai-wechat-selecter>
              </el-input>
            </el-form-item>
            <el-form-item label="发布内容" prop="content" style="width: 100%;" :rules="[{required: true, message: '请输入发布内容', trigger: 'change'}]">
              <ai-editor v-model="form.content" :instance="instance"/>
            </el-form-item>
            <el-form-item label="图片" prop="files" style="width: 100%;">
              <ai-uploader
                :instance="instance"
                v-model="form.files"
                isShowTip
                :limit="9">
              </ai-uploader>
            </el-form-item>
            <el-form-item prop="title" style="width: 100%;" label="话题">
              <el-input size="small" placeholder="请输入话题" v-model="form.title"></el-input>
            </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 { mapState } from 'vuex'
  export default {
    name: 'Add',

    props: {
      instance: Function,
      dict: Object,
      params: Object
    },
    data () {
      return {
        info: {},
        form: {
          title: '',
          content: '',
          files: [],
          departmentName: '',
          departmentIds: [],
        },
        id: '',
        department: [],
      }
    },

    computed: {
      ...mapState(['user'])
    },

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

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

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

      cancel (isRefresh) {
        this.$emit('change', {
          type: 'List',
          isRefresh: !!isRefresh
        })
      },

      onChange (e) {
        if (e.length) {
          this.form.departmentIds = e.map(v => v.id)
          this.form.departmentName = e.map(v => v.name).join(',')
        } else {
          this.form.departmentIds = ''
          this.form.departmentName = ''
        }
      },
    }
  }
</script>

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