<template>
  <div class="detail-wrap">
    <el-form ref="editForm" :model="system_notice">
      <div class="d-header clearfix">
        <div class="fl">
          <i class="el-icon-info"/>
          {{ dataId ? $t1('查看公告') : $t1('新增公告') }}
        </div>
        <div class="fr">
          <el-button type="primary" plain class="button-sty" @click="$baseReload()" icon="el-icon-refresh-right">
            {{ $t1('重置') }}
          </el-button>
          <el-button type="primary" class="button-sty" icon="el-icon-check" @click="saveData">{{ $t1('保存') }}
          </el-button>
        </div>
      </div>
      <baseTabs>
        <baseTabPane :label="$t1('基本信息')">
          <template #default>
            <table class="table-detail">
              <tbody>
              <tr>
                <th>
                  <em class="f-red">*</em>
                  {{ $t1('标题') }}
                </th>
                <td>
                  <el-form-item prop="title" :rules="[{ required: true, trigger: 'blur' }]">
                    <el-input type="text" autocomplete="off" v-model="system_notice.title" clearable/>
                  </el-form-item>
                </td>
                <th>{{ $t1('副标题') }}</th>
                <td>
                  <el-form-item prop="secondTitle" :rules="[{ required: false, trigger: 'blur' }]">
                    <el-input type="text" autocomplete="off" v-model="system_notice.secondTitle" clearable/>
                  </el-form-item>
                </td>
                <th>{{ $t1('设置') }}</th>
                <td colspan="3">
                  <el-checkbox :label="$t1('是否发布')" v-model="system_notice.publish"></el-checkbox>
                  <el-checkbox :label="$t1('是否置顶')" v-model="system_notice.top"></el-checkbox>
                  <el-checkbox :label="$t1('重要')" v-model="system_notice.important"></el-checkbox>
                </td>
              </tr>
              <tr>
                <th>{{ $t1('附件') }}</th>
                <td colspan="7">
                  <baseUpload accept="file" multi="true" :file.sync="sysNoticeAttachmentDTOs"></baseUpload>
                </td>
              </tr>
              <tr>
                <th>{{ $t1('创建人') }}</th>
                <td>{{ system_notice.createBy }}</td>
                <th>{{ $t1('创建时间') }}</th>
                <td>{{ system_notice.createDate }}</td>
                <th>{{ $t1('更新人') }}</th>
                <td>{{ system_notice.modifyBy }}</td>
                <th>{{ $t1('更新时间') }}</th>
                <td>{{ system_notice.modifyDate }}</td>
              </tr>
              </tbody>
            </table>
          </template>
        </baseTabPane>
        <baseTabPane :label="$t1('内容')">
          <template #default>
            <table class="table-detail" style="width: 100%;">
              <tbody>
              <tr>
                <td colspan="=6">
                  <tinymce v-model="system_notice.content" :language="lan" :height="300"/>
                </td>
              </tr>
              </tbody>
            </table>
          </template>
        </baseTabPane>
      </baseTabs>
    </el-form>
  </div>
</template>

<script>
import Tinymce from '@base/components/Tinymce';

export default {
  name: 'system_noticeEdit',
  props: ['_dataId'],
  components: {
    Tinymce
  },
  data() {
    return {
      lan: 'zh_CN',
      tabIndex: 0,
      isEdit: false,
      dataId: '',
      system_notice: {
        publish: false,
        top: false,
        important: false
      },
      sysNoticeAttachmentDTOs: []
    };
  },
  created() {
    if (this._dataId && !isNaN(this._dataId)) this.dataId = this._dataId;
  },
  mounted() {
    this.initAttachfo();
    this.getData();
  },
  methods: {
    getData() {
      if (this.dataId && !isNaN(this.dataId)) {
        this.isEdit = true;
        this.$commonHttp({
          url: USER_PREFIX + `/system_notice/get`,
          method: `post`,
          data: {
            id: this.dataId
          },
          isLoading: true,
          modalStrictly: true,
          success: res => {
            this.system_notice = res.objx || {};
          }
        });
      }
    },
    saveData() {
      this.$refs.editForm.$baseValidate(valid => {
        if (valid) {
          this.$baseConfirm(this.$t1('您确定要保存吗？')).then(() => {
            var url = USER_PREFIX + (this.isEdit ? `/system_notice/update` : `/system_notice/save`);
            let formData = this.system_notice;
            formData.sysNoticeAttachmentDTOs = this.sysNoticeAttachmentDTOs || [];

            this.$http({
              url: url,
              method: `post`,
              data: formData,
              isLoading: true,
              success: res => {
                this.$message({
                  message: res.content,
                  type: 'success',
                  duration: 500,
                  onClose: t => {
                    if (this.isEdit) {
                      this.$baseReload();
                    } else {
                      this.$baseReload({
                        updateParam: {
                          _dataId: res.objx
                        }
                      });
                    }
                  }
                });
              }
            });
          });
        }
      });
    },
    initAttachfo() {
      //初始化附件信息
      if (this.dataId) {
        this.$http({
          url: USER_PREFIX + '/system_notice/getAttachments',
          method: `post`,
          data: {id: this.dataId},
          isLoading: true,
          modalStrictly: true,
          success: res => {
            this.sysNoticeAttachmentDTOs = res.objx || [];
          }
        });
      }
    }
  }
};
</script>

<style></style>
