<template>
  <div style="padding: 10px;background-color:#F2F3F5">
    <div class="history">
      <div style="height:58px;line-height:58px;">
        <div style="color:#0006"> <span>消息中心</span>   <span style="padding:0 5px;">></span>  <span style="color:#000000e6">历史消息</span></div>
      </div>
      <div class="content">
        <!-- 顶部搜索 -->
        <div class="toolbar">
          <el-form ref="formParams" :inline="true" :model="formParams">
            <el-form-item label="标题">
              <el-input v-model="formParams.noticeTitle" placeholder="请输入标题"  />
            </el-form-item>
            <el-form-item label="消息类型">
              <el-select v-model="formParams.noticeType" placeholder="请选择消息类型"  size="mini">
                <el-option
                  v-for="(item, index) in tipsList"
                  :key="index"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="发送时间">
              <el-date-picker
                v-model="formParams.dates"
                type="daterange"

                range-separator="至"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
                value-format="yyyy-MM-dd"
              />
            </el-form-item>

            <el-form-item label-width="0">
              <el-button   @click="search('formParams')"  style="background-color: #3F9B6A;color: #fff">查询</el-button>
              <el-button  @click="clear('formParams')"  class="buttonHover"
            style="color: #606266;border: 1px solid #dddfe5;background-color: #fff;" >重置</el-button>
            </el-form-item>
          </el-form>
        </div>
        <!-- 表格 -->
        <div class="content_table">
          <div class="table">
            <el-table
              :data="list"
              :header-cell-style="{fontSize: '14px',color:'#0009',fontWeight: 'normal',backgroundColor:'#F2F3F5'}"
            >
              <el-table-column prop="noticeTitle" label="标题" />
              <el-table-column label="内容" :show-overflow-tooltip="true">
                <template slot-scope="scope">
                  <span v-html="scope.row.noticeContent" />
                </template>
              </el-table-column>
              <el-table-column prop="noticeType" label="消息类型">
                <template slot-scope="scope">
                  <span>{{ scope.row.noticeType===1?'系统通知':scope.row.noticeType===2?'公告':scope.row.noticeType===3?'站内信':'' }}</span>
                </template>
              </el-table-column>
              <el-table-column prop="createTime" label="发送时间" />
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <div  @click="details(scope.row.noticeId)"  class="tableBtn greens">详情</div>
                  <div  @click="delMsg(scope.row.noticeId)"  class="tableBtn greens">删除</div>
                </template>
              </el-table-column>
            </el-table>
          </div>
          <!-- 分页 -->
          <div class="fenye">
            <el-pagination
              :current-page="formParams.page"
              :page-sizes="[10, 20, 50, 100]"
              :page-size="10"
              background
              small
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </div>
        </div>
      </div>
      <!-- 消息详情弹窗 -->
      <el-dialog
        title="消息详情"
        :visible.sync="dialogVisible"
        width="60%"
        :before-close="handleClose"
        modal
      >
        <div>
          <h2 style="text-align:center;font-weight:bold;">{{ noticeDetails.noticeTitle }}</h2>
          <span style="float: right;">{{ noticeDetails.createTime }}</span>
          <el-divider />
          <div v-html="noticeDetails.noticeContent" />
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { noticeGetAll, noticeGetById, noticeDelete } from '@/api/notice'
export default {
  data() {
    return {
      list: [],
      total: 1,
      formParams: {
        noticeTitle: null,
        noticeType: null,
        dates: [],
        page: 1,
        pageSize: 10
      },
      tipsList: [{
        id: 1,
        name: '系统消息'
      },
      {
        id: 2,
        name: '公告'
      },
      {
        id: 3,
        name: '站内信'
      }],
      rules: {
        noticeTitle: [
          { required: true, message: '请输入标题', trigger: 'blur' },
          { min: 1, message: '请输入标题', trigger: 'blur' }
        ],
        noticeType: [
          { required: true, message: '请选择消息类型', trigger: 'change' }
        ]
      },
      dialogVisible: false,
      noticeDetails: {
        noticeTitle: '啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊',
        createTime: '2021-03-10 10:10',
        noticeContent: `<h1 style="text-align: center;">Welcome to the TinyMCE demo!</h1><p style="text-align: center; font-size: 15px;"><img title="TinyMCE Logo" src="//www.tinymce.com/images/glyph-tinymce@2x.png" alt="TinyMCE Logo" width="110" height="97" /><ul>
        <li>Our <a href="//www.tinymce.com/docs/">documentation</a> is a great resource for learning how to configure TinyMCE.</li><li>Have a specific question? Visit the <a href="https://community.tinymce.com/forum/">Community Forum</a>.</li><li>We also offer enterprise grade support as part of <a href="https://tinymce.com/pricing">TinyMCE premium subscriptions</a>.</li>
        </ul>`
      }
    }
  },
  mounted() {
    this.getAll()
  },
  methods: {
    // 历史消息列表
    async getAll() {
      const res = await noticeGetAll(this.formParams)
      this.list = res.data.list
      this.total = res.data.total
    },
    // 消息详情
    async details(id) {
      const res = await noticeGetById({ noticeId: id })
      this.noticeDetails = res.data
      this.dialogVisible = true
    },
    // 删除消息提示
    async delMsg(id) {
      this.$confirm('此操作将永久删除该消息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.del(id)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 删除消息
    async del(id) {
      const res = await noticeDelete({ noticeId: id })
      if (res.code === '') {
        this.$message({
          message: '删除成功',
          type: 'success'
        })
        this.getAll()
      }
    },
    // 查询
    async search(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.total = 1
          this.formParams.page = 1
          this.getAll()
        } else {
          return false
        }
      })
    },
    // 重置表单
     clear(formName) {
      this.formParams= {
        noticeTitle: null,
        noticeType: null,
        dates: [],
        page: 1,
        pageSize: 10
      }
      this.getAll()
    },
    // 更改页数
    async handleSizeChange(val) {
      this.formParams.pageSize = val
      this.getAll()
    },
    // 翻页
    async handleCurrentChange(val) {
      this.formParams.page = val
      this.getAll()
    },
    // 关闭消息详情
    async handleClose() {
      this.dialogVisible = false
    }
  }
}
</script>

<style lang='scss' scoped>
@import url("../../styles/elDialog.scss");
.history{
  padding: 0  20px 20px 20px;
  min-height: calc(100vh - 50px - 20px);
  background-color: #Fff;
}
 ::v-deep .el-form-item__label{
        font-weight: normal;
         font-size: 14px;

       }

          ::v-deep .el-pagination.is-background .el-pager li:not(.disabled).active {
           background-color: #3F9B6A;
         }
         .tableBtn {
             display: inline-block;
             margin-right: 10px;
           }

         .greens {
             color: #3F9B6A ;
           }
           ::v-deep .buttonHover:hover{
             color:#3f9b6a !important;
             border-color: #c5e1d2 !important;
             background-color: #ecf5f0 !important;
             outline: none;
           }
           ::v-deep .el-pagination__total {
               position: absolute;
               left: 10px;
             }
</style>
<style scoped>
.history /deep/ .el-table .cell.el-tooltip img {
  max-height: 100px;
}

</style>
<style>
.el-tooltip__popper {
  max-width: 50%;
}
</style>
