<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item :label="$t('公告标题')" prop="title">
        <el-input v-model="queryParams.title" :placeholder="$t('请输入公告标题')" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item :label="$t('操作人员')" prop="createBy">
        <el-input v-model="queryParams.createBy" :placeholder="$t('请输入操作人员')" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item :label="$t('类型')" prop="type">
        <el-select v-model="queryParams.type" :placeholder="$t('公告类型')" clearable>
          <el-option v-for="dict in noticeTypeDictDatas" :key="parseInt(dict.value)" :label="$l(dict, 'label')" :value="parseInt(dict.value)"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">{{$t('搜索')}}</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">{{$t('重置')}}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['system:notice:create']"s>{{$t('新增')}}</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="noticeList">
      <el-table-column :label="$t('序号')" align="center" prop="id" width="100" />
      <el-table-column :label="$t('公告标题')" align="center" prop="title" :show-overflow-tooltip="true"/>
      <el-table-column :label="$t('公告类型')" align="center" prop="type" width="100">
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.SYSTEM_NOTICE_TYPE" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column :label="$t('状态')" align="center" prop="status" width="100">
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column :label="$t('有效日期')" align="center" prop="validateDate" width="150" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.validateDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column :label="$t('创建者')" align="center" prop="createBy"  width="150"/>-->
      <el-table-column :label="$t('创建时间')" align="center" prop="createTime"  width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope" >
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['system:notice:update']">{{$t('修改')}}</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['system:notice:delete']">{{$t('删除')}}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 添加或修改公告对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item :label="$t('公告标题')" prop="title">
              <el-input v-model="form.title" :placeholder="$t('请输入公告标题')" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('标题英文')" prop="titleEn">
              <el-input v-model="form.titleEn" :placeholder="$t('请输入标题英文')" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('标题法文')" prop="titleFr">
              <el-input v-model="form.titleFr" :placeholder="$t('请输入标题法文')" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('公告类型')" prop="type">
              <el-select v-model="form.type" :placeholder="$t('请选择')">
                <el-option
                    v-for="dict in noticeTypeDictDatas"
                    :key="parseInt(dict.value)"
                    :label="dict.label"
                    :value="parseInt(dict.value)"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('状态')">
              <el-radio-group v-model="form.status">
                <el-radio
                    v-for="dict in statusDictDatas"
                    :key="parseInt(dict.value)"
                    :label="parseInt(dict.value)"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('有效日期')">
              <el-date-picker type="date" value-format="yyyy-MM-dd" format="yyyy-MM-dd" :editable="false"
                              v-model="form.validateDate"
                              style="width: 100%;"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="$t('内容')">
              <editor v-model="form.content" :min-height="192"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="$t('内容英文')">
              <editor v-model="form.contentEn" :min-height="192"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="$t('内容法文')">
              <editor v-model="form.contentFr" :min-height="192"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{$t('确 定')}}</el-button>
        <el-button @click="cancel">{{$t('取 消')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listNotice, getNotice, delNotice, addNotice, updateNotice } from "@/api/system/notice";
import Editor from '@/components/Editor';

import {CommonStatusEnum} from '@/utils/constants'
import { getDictDatas, DICT_TYPE } from '@/utils/dict'

export default {
  name: "Notice",
  components: {
    Editor
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公告表格数据
      noticeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        title: undefined,
        createBy: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: this.$t("公告标题不能为空"), trigger: "blur" }
        ],
        type: [
          { required: true, message: this.$t("公告类型不能为空"), trigger: "change" }
        ]
      },

      // 枚举
      CommonStatusEnum: CommonStatusEnum,
      // 数据字典
      noticeTypeDictDatas: getDictDatas(DICT_TYPE.SYSTEM_NOTICE_TYPE),
      statusDictDatas: getDictDatas(DICT_TYPE.COMMON_STATUS)
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询公告列表 */
    getList() {
      this.loading = true;
      listNotice(this.queryParams).then(response => {
        this.noticeList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        title: undefined,
        titleEn: undefined,
        titleFr: undefined,
        type: undefined,
        content: undefined,
        contentEn: undefined,
        contentFr: undefined,
        status: CommonStatusEnum.ENABLE
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = this.$t("添加公告");
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getNotice(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = this.$t("修改公告");
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id !== undefined) {
            updateNotice(this.form).then(response => {
              this.$modal.msgSuccess(this.$t("修改成功"));
              this.open = false;
              this.getList();
            });
          } else {
            addNotice(this.form).then(response => {
              this.$modal.msgSuccess(this.$t("新增成功"));
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm(this.$t('是否确认删除公告编号为"') + ids + this.$t('"的数据项?')).then(function() {
          return delNotice(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess(this.$t("删除成功"));
      }).catch(() => {});
    }
  }
};
</script>
