<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
		<el-form-item :label="$t('table.messageType')">
			<el-select v-model="queryParams.messageType" clearable :placeholder="$t('placeholder.messageType')">
				<el-option
					:label="$t('table.messageType1')"
					value="1">
				</el-option>
				<el-option
					:label="$t('table.messageType2')"
					value="2">
				</el-option>
			</el-select>
      </el-form-item>
	  <el-form-item :label="$t('table.beginDate')" >
        <el-date-picker clearable
          v-model="queryParams['params[beginDate]']"
          type="date"
          value-format="yyyy-MM-dd"
          :placeholder="$t('placeholder.beginDate')">
        </el-date-picker>
      </el-form-item>
      <el-form-item :label="$t('table.endDate')">
        <el-date-picker clearable
          v-model="queryParams['params[endDate]']"
          type="date"
          value-format="yyyy-MM-dd"
          :placeholder="$t('placeholder.endDate')">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{$t('btn.search')}}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{$t('btn.Reset')}}</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="['noticeMessage:noticeMessage:add']"
        >{{$t('btn.add')}}</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['noticeMessage:noticeMessage:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['noticeMessage:noticeMessage:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['noticeMessage:noticeMessage:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="noticeMessageList" @selection-change="handleSelectionChange">
	  <el-table-column :label="$t('table.SerialNumber')" width="50px" align="center">
		<template slot-scope="scope">
			{{ scope.$index + (queryParams.pageNum - 1) * queryParams.pageSize + 1 }}
		</template>
	  </el-table-column>
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="id" align="center" prop="id" /> -->
      <el-table-column :label="$t('table.title')" align="center" prop="title" />
      <el-table-column :label="$t('table.messageType')" align="center" prop="messageType" >
	    <template slot-scope="scope">
			<span v-if='scope.row.messageType=="1"'>{{$t('table.messageType1')}}</span>
			<span v-if='scope.row.messageType=="2"'>{{$t('table.messageType2')}}</span>
			<!-- <span v-if='scope.row.messageType=="3"'>个人消息</span> -->
		</template>
	  </el-table-column>
      <el-table-column :label="$t('table.content2')" align="center" prop="content">
	  	<template slot-scope="scope">
			<!-- <span v-html="scope.row.content"></span> -->
			<span>{{scope.row.content.slice(3,10) + "..."}}</span>
		</template>
	  </el-table-column>
	  <el-table-column :label="$t('table.createTime1')" align="center" prop="createTime" />
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column :label="$t('table.operation')" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['noticeMessage:noticeMessage:edit']"
          >{{$t('table.edit')}}</el-button>
		  <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate2(scope.row)"
            v-hasPermi="['noticeMessage:noticeMessage:edit']"
          >{{$t('table.detail')}}</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['noticeMessage:noticeMessage:remove']"
          >{{$t('table.delete')}}</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改通知消息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item :label="$t('table.title')" prop="title">
          <el-input :disabled='isDetails' v-model="form.title" type="textarea" :placeholder="$t('placeholder.title')" />
        </el-form-item>
		<el-form-item :label="$t('table.messageType')" prop='messageType'>
			<el-select :disabled='isDetails' v-model="form.messageType" :placeholder="$t('placeholder.messageType')">
				<el-option
					:label="$t('table.messageType1')"
					value="1">
				</el-option>
				<el-option
					:label="$t('table.messageType2')"
					value="2">
				</el-option>
				<!-- <el-option
					label="个人信息"
					value="3">
				</el-option> -->
			</el-select>
        </el-form-item>
        <el-form-item :label="$t('table.content2')" prop='content'>
		  <!-- <span v-if='isDetails' v-html='form.content'></span> -->
          <!-- <editor v-else v-model="form.content" :min-height="192"/> -->
		  <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <!-- <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="!isDetails" type="primary" @click="submitForm">{{$t('btn.submit')}}</el-button>
        <el-button @click="cancel">{{$t('btn.cancel')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listNoticeMessage, getNoticeMessage, delNoticeMessage, addNoticeMessage, updateNoticeMessage } from "@/api/noticeMessage/noticeMessage";

export default {
  name: "NoticeMessage",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 通知消息表格数据
      noticeMessageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
	  // 是否打开详情
	  isDetails :false,   
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        // title: null,
        messageType: '',
		'params[beginDate]':null,
		'params[endDate]':null,
        // content: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
		title: [
          { required: true, message: this.$t('rules.title'), trigger: "blur" }
        ],
		messageType: [
          { required: true, message: this.$t('rules.messageType'), trigger: "change" }
        ],
		content: [
          { required: true, message: this.$t('rules.content'), trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询通知消息列表 */
    getList() {
      this.loading = true;
      listNoticeMessage(this.queryParams).then(response => {
        this.noticeMessageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        messageType: null,
        content: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
	  this.isDetails = false
      this.reset();
      this.open = true;
      this.title = this.$t('dialog.noticeMessageTitel1');
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
	  this.isDetails = false
      this.reset();
      const id = row.id || this.ids
      getNoticeMessage(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = this.$t('dialog.noticeMessageTitel2');
      });
    },
	handleUpdate2(row) {
	  this.isDetails = true
      this.reset();
      const id = row.id || this.ids
      getNoticeMessage(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = this.$t('dialog.noticeMessageTitel3');
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateNoticeMessage(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('message.success1'));
              this.open = false;
              this.getList();
            });
          } else {
            addNoticeMessage(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('message.success2'));
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
    //   this.$modal.confirm('是否确认删除通知消息编号为"' + ids + '"的数据项？').then(function() {
	  this.$modal.confirm(this.$t('message.isDelete3')).then(function() {
        return delNoticeMessage(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess(this.$t('message.success3'));
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('noticeMessage/noticeMessage/export', {
        ...this.queryParams
      }, `noticeMessage_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
