<template>
  <Modal v-model="showModalNoticeUser" :title="modalTitle"
         width="820">
    <i-form label-position="right" :label-width="100" ref="noticeUserForm" :model="bean" :rules="rules" inline>
      <Row>
        <i-col :span="12">
          <Form-item label="通知标题" prop="title" class="ivu-form-item-search">
            <Input v-model="bean.title" style="width: 243px;" :disabled="disabled"></Input>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="提醒时间" prop="noticeTime" class="ivu-form-item-search">
            <Input v-model="bean.noticeTime" style="width: 243px;" :disabled="disabled"></Input>
          </Form-item>
        </i-col>
        <i-col :span="24">
          <Form-item label="通知内容" prop="content" class="ivu-form-item-search">
            <Input v-model="bean.content" style="width: 638px;" :disabled="disabled" type="textarea"></Input>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="备注说明" prop="remark" class="ivu-form-item-search">
            <Input v-model="bean.remark" style="width: 243px;" :disabled="disabled"></Input>
          </Form-item>
        </i-col>
      </Row>
    </i-form>
    <div slot="footer">
      <Button @click="cancel">关闭</Button>
      <Button type="primary" :loading="saveLoading" @click="ok('noticeUserForm')" v-if="showSaveBtn">保存</Button>
    </div>
  </Modal>
</template>

<script>
  import { addNoticeUser, editNoticeUser, makeNoticeReaded } from './noticeUser-api'
  import moment from 'moment';

  let $this
  export default {
    name: 'noticeUserModal',
    components: {},
    props: {
      show: Boolean,
      value: Object
    },
    data () {
      return {
        showModalNoticeUser: false,
        bean: {},
        initBean: {},
        flag: '',
        saveLoading: false,
        modalTitle: '',
        disabled: false,
        showSaveBtn: true,
        rules: {
        },
        options: {
          trigger: '.ivu-modal-header',
          body: '.ivu-modal',
          recover: true
        }
      }
    }, methods: {
      ok (name) {
        this.$refs[name].validate((valid) => {
          if (!valid) {
            return
          }//表单校验失败则返回
          this.saveLoading = true
          //this.showModalNoticeUser = false;
          if (this.flag == 'edit') {
            let parmas = this.bean
            delete parmas['createTime']
            delete parmas['modifyTime']
            //执行修改操作
            editNoticeUser(parmas).then(data => {
              if (!data.status) {
                this.$Message.error(data.error.message)
                this.saveLoading = false
                console.log(data)
                return false
              }
              this.$Message.success('操作成功！')
              this.closeAndRefresh()
            }).catch(err => {
              this.$Message.error('操作失败！')
              this.saveLoading = false
              console.log(err)
            })
          } else if (this.flag == 'add') {
            delete this.bean['createTime']
            delete this.bean['modifyTime']
            //执行添加操作
            addNoticeUser(this.bean).then(data => {
              if (!data.status) {
                this.$Message.error(data.error.message)
                this.saveLoading = false
                console.log(data)
                return false
              }
              this.$Message.success('操作成功！')
              this.closeAndRefresh()
            }).catch(err => {
              this.$Message.error('操作失败！')
              this.saveLoading = false
              console.log(err)
            })
          }
        })

      },
      cancel () {
        this.showModalNoticeUser = false
        this.$emit('update:show', false)
        //重置校验结果
        this.$refs['noticeUserForm'].resetFields()
      },
      closeAndRefresh () {
        this.saveLoading = false
        this.cancel()
        this.$emit('reload-data')
      },
    },
    mounted () {
      $this = this
      this.bean = this.initBean
    },
    watch: {
      show: {
        immediate: true,
        handler: function () {
          this.showModalNoticeUser = this.show
        }
      },
      value: {
        immediate: true,
        handler: function () {
          //赋值
          this.bean = this.value.bean == null ? Object.assign({}, this.initBean) : JSON.parse(JSON.stringify(this.value.bean))
          if(this.bean.noticeTime){
            this.bean.noticeTime = moment(this.bean.noticeTime).format('YYYY.MM.DD')
          }
          this.flag = this.value.flag
          this.modalTitle = this.value.title
          //如果时查看的功能，要将所有input框只读，并且去掉保存按钮
          if (this.flag == 'view') {
            this.disabled = true
            this.showSaveBtn = false
            //将此消息设置为已读
            makeNoticeReaded(this.bean.id).then(data => {
              if (!data.status) {
                this.$Message.error(data.error.message)
                return false
              }
            }).catch(err => {
              this.$Message.error('操作失败！')
              console.log(err)
            })
          } else {
            this.disabled = false
            this.showSaveBtn = true
          }
        }
      }
    }

  }
</script>

<style scoped>
  .ivu-form-item-search {
    margin-bottom: 21px;
  }
</style>
