<template>
  <div>
    <!-- <el-dialog
      :title="!id ? '发布': display ? '详情' : '修改'"
      width="70%"
      :close-on-click-modal="false"
      :visible.sync="visible"> -->
      <div class="my-title">{{ !id ? '发布': display ? '详情' : '修改' }}</div>
      <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="auto">
        <el-row class="my-row">
          <el-col :span="12">
            <el-form-item label="公告主题名称" prop="title">
              <el-input v-model="dataForm.title" :disabled="display" placeholder="请输入公告主题名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" style="padding-left: 20px">
            <el-form-item label="级别" prop="level">
              <el-input v-if="display" v-model="dataForm.levelDesc" :disabled="true"/>
              <el-select v-else
                v-model="dataForm.level"
                :disabled="display"
                placeholder="请选择">
                <el-option
                  v-for="item in optionsLevel"
                  :key="item.code"
                  :label="item.value"
                  :value="item.code">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row class="my-row">
          <el-form-item label="公告内容" prop="content">
            <el-input type="textarea" v-model="dataForm.content" :disabled="display" placeholder="公告内容"></el-input>
          </el-form-item>
        </el-row>
        <el-row class="my-row">
          <upload-component :display-star="false" :display="display" :title="'公告附件'" :accept="'*'" :file-obj-list="fileList" @uploadSuccess="uploadSuccess"/>
        </el-row>
        <el-row style="margin-top: 15px">
          <el-form-item prop="receivers">
            <span slot="label">
              <span style="color: red">*</span>&nbsp;接收对象
            </span>
            <el-input v-if="display" :disabled="true" v-model="dataForm.receiverList"/>
            <user-components v-else v-model="dataForm.receivers" :user-ids="dataForm.receivers" @change='receiverChange' :disabled="display"/>
          </el-form-item>
        </el-row>
        <el-row v-if="display">
          <el-form-item label="已读" prop="readReceiverList">
            <el-input :disabled="true" v-model="dataForm.readReceiverList"/>
          </el-form-item>
        </el-row>
        <el-row class="my-row">
          <el-form-item label="备注说明" prop="notes">
            <el-input type="textarea" v-model="dataForm.notes" :disabled="display" placeholder="备注说明"></el-input>
          </el-form-item>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="onChose">取消</el-button>
        <el-button v-if="!display" type="primary" @click="dataFormSubmit()" v-reClick>确定</el-button>
      </span>
    <!-- </el-dialog> -->
  </div>
</template>

<script>
  import { getNoticeDetail, publishNotice } from '@/api/notice'
  import UserComponents from '../common/user-components'
  import { getDictList } from '@/api/dict'
  import UploadComponent from '../common/upload-component'
  export default {
    name: 'notice-add-or-update',
    components: {UploadComponent, UserComponents},
    data () {
      return {
        visible: false,
        optionsLevel: [],
        dataList: [],
        fileList: [],
        id: 0,
        dataForm: {},
        dataRule: {
          title: [{ required: true, message: '公告主题名称不能为空', trigger: 'blur' }],
          level: [{ required: true, message: '请选择级别', trigger: 'change' }],
          content: [{ required: true, message: '公告内容不能为空', trigger: 'blur' }]
        },
        display: false
      }
    },
    methods: {
      async init (id, display) {
        this.dataForm = {}
        this.fileList = []
        this.visible = true
        this.id = id || 0
        this.display = display
        // 获取沟通类别
        await getDictList({type: 'announcement_level'}).then(({data}) => {
          if (data) {
            this.optionsLevel = data
          }
        })
        if (!id) return
        await getNoticeDetail(this.id).then(({data}) => {
          if (data && data.code === '200') {
            this.dataForm = data.data
            // 附件显示
            this.fileList = []
            data.data.attaches.forEach((item) => {
              this.fileList.push({
                name: item.fileName,
                url: item.url,
                id: item.url
              })
            })
          }
        })
      },
      onChose () {
        this.$emit('onChose')
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            // 添加附件
            let fList = this.fileList
            if (fList.length > 0) {
              this.dataForm.attaches = []
              for (let i = 0; i < fList.length; i++) {
                this.dataForm.attaches.push({
                  fileName: fList[i].name,
                  url: fList[i].url
                })
              }
            }
            if (!this.dataForm.receivers || this.dataForm.receivers.length === 0) {
              this.$message.error('清选择接收对象')
              return
            }
            publishNotice(this.dataForm).then(({data}) => {
              if (data && data.code === '200') {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.onChose()
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      validateField (type) {
        this.$refs.dataForm.validateField(type)
      },
      receiverChange (val) {
        this.dataForm.receivers = val
      },
      uploadSuccess (fileList) {
        this.fileList = fileList
      }
    }
  }
</script>

<style scoped>
.my-line{
  border-bottom: 1px solid #c0c4cc;
  margin-bottom: 10px;
}
.title{
  padding: 10px 0 ;
}
</style>
