<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="publishDepartName" style="width: 100%;" :rules="[{ required: true, message: '请选择单位', trigger: 'change' }]">
              <ai-picker :instance="instance" multiple @pick="e => onUserChange(e)" dialogTitle="选择部门" action="/app/wxcp/wxdepartment/departList">
                <div class="time-select">
                  <span class="dept-name" style="color:#999;" v-if="!form.publishDepartName">选择部门</span>
                  <span class="dept-name" v-else>{{form.publishDepartName}}</span>
                  <i class="el-icon-arrow-down"></i>
                </div>
              </ai-picker>
            </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-input type="textarea" placeholder="请输入内容" v-model="form.content" rows="8" maxlength="500" :show-word-limit="true"></el-input>
            </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="themeId" style="width: 100%;" label="话题">
              <ai-select v-model="form.themeId" :selectList="talkList" placeholder="请选择话题" />
            </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: {
          themeId: '',
          content: '',
          files: [],
          publishDepartName: '',
          publishDepartIdList: [],
        },
        id: '',
        department: [],
        talkList: []
      }
    },

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

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

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

      confirm () {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.instance.post(`/app/appneighborhoodassistance/addOrUpdate`, {
              ...this.form,
              publishDepartId: this.form.publishDepartIdList.join(',')
            }).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.publishDepartIdList = e.map(v => v.id)
          this.form.publishDepartName = e.map(v => v.name).join(',')
        } else {
          this.form.publishDepartIdList = ''
          this.form.publishDepartName = ''
        }
      },

      onUserChange (e) {
        this.deptList = e
       this.onChange(e)
      },

      getTalkList() {
        this.instance.post(`/app/appneighborhoodassistancetheme/list?size=100`).then(res => {
          if (res.code == 0) {
            this.talkList = []
            res.data.records.map((item) => {
              this.talkList.push({
                dictName: item.title,
                dictValue: item.id
              })
            })
          }
        })
      },
    }
  }
</script>

<style scoped lang="scss">
  .time-select {
    padding: 0 16px;
    height: 36px;
    line-height: 36px;
    border: 1px solid #d0d4dc;
    border-radius: 4px;
    display: flex;
    justify-content: space-between;
    cursor: pointer;
    .el-icon-arrow-down {
      line-height: 36px;
    }
  }
</style>
