<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-demo__sysnotice">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getData()">
        <el-form-item>
          <el-input v-model="dataForm.content" dict-type="notice_type" :placeholder="$t('notice.type')"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getData()">{{ $t('query') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="dialogVisible = true">添加公告</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="dialogVisible2 = true">公告周期设置</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="dataListLoading" :data="dataList" border
        style="width: 100%;">
        <el-table-column prop="content" label="内容" header-align="center" align="center" width="950">
        </el-table-column>
        <el-table-column prop="publishTime" label="发布时间" header-align="center" align="center" width="300">
        </el-table-column>
        <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="150">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="viewHandle(scope.row)">
               详情
            </el-button>
              <el-button type="text" size="small" @click="deleteHandle(scope.row)">{{ $t('delete') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination :current-page="dataForm.page" :page-size="dataForm.limit" :total="total"
        layout="total, prev, pager, next"
        @current-change="currentChange">
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    </div>
    <el-dialog
      title="添加公告"
      :visible.sync="dialogVisible"
      width="30%"
      :close-on-click-modal="false">
      <el-input
        type="textarea"
        :rows="5"
        :maxlength="200"
        show-word-limit
        placeholder="请输入公告内容（6-200字）"
        v-model="submitData.content">
      </el-input>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="设置公告周期"
      :visible.sync="dialogVisible2"
      width="30%"
      :close-on-click-modal="false">
      <el-input
        placeholder="请输入公告周期（天）"
        v-model="cycle">
      </el-input>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="submitCycle">确 定</el-button>
      </span>
    </el-dialog>
  </el-card>
</template>

<script>
import qs from 'qs'
import AddOrUpdate from './notice-add-or-update'
import { addDynamicRoute } from '@/router'
export default {
  data () {
    return {
      cycle: 0,
      total: 0,
      dataListLoading: false,
      dialogVisible: false,
      dialogVisible2: false,
      dataList: [],
      dataForm: {
        page: 1,
        limit: 5,
        content: ''
      },
      submitData: {
        content: ''
      }
    }
  },
  components: {
    AddOrUpdate
  },
  methods: {
    currentChange (current) {
      this.dataForm.page = current
      this.getData()
    },
    getData () {
      this.$http.get('/sysnoticemanagement/page?' + qs.stringify(this.dataForm)).then(res => {
        console.log('获取通知数据', res.data.data)
        this.dataList = res.data.data.list
        this.total = res.data.data.total
        this.dataListLoading = false
      })
    },
    getCycle () {
      this.$http.get('/sys/dict/data/page?dictTypeId=1598237460358852610&page=1&limit=9999').then(res => {
        console.log('字典', res.data.data)
        this.cycle = res.data.data.list[0].dictLabel
      })
    },
    handleClose () {
      this.dialogVisible = false
      this.dialogVisible2 = false
      this.submitData.content = ''
      this.getCycle()
    },
    submit () {
      console.log(this.submitData)
      if (this.submitData.content.length < 6) {
        this.$message({
          message: '最少发布6个字的公告！',
          type: 'warning'
        })
        return
      }
      this.$http.post('/sysnoticemanagement', this.submitData).then(res => {
        console.log('获取通知数据', res.data.data)
        this.clear()
      })
    },
    submitCycle () {
      this.$http.put('/sys/dict/data', {
        dictLabel: this.cycle,
        dictTypeId: '1598237460358852610',
        id: '1598237627208265730'
      }).then(res => {
        this.clear()
      })
    },
    clear () {
      this.dialogVisible = false
      this.dialogVisible2 = false
      this.submitData.content = ''
      this.dataForm.content = ''
      this.dataForm.page = 1
      this.getData()
      this.getCycle()
    },
    deleteHandle (item) {
      console.log(item)
      this.$http.post('/sysnoticemanagement/delete', [item.id]).then(res => {
        this.clear()
      })
    },
    viewHandle (row) {
      // 路由参数
      const routeParams = {
        routeName: `${this.$route.name}__${row.id}`,
        title: this.$t('notice.view1'),
        path: 'notice/notice-view',
        params: {
          id: row.id
        }
      }
      // 动态路由
      addDynamicRoute(routeParams, this.$router)
    }
  },
  created () {
    this.getData()
    this.getCycle()
  }
}
</script>
<style scoped>
.text-ellipsis {
  /*1. 先强制一行内显示文本*/
  white-space: nowrap;
  /*2. 超出的部分隐藏*/
  overflow: hidden;
  /*3. 文字用省略号替代超出的部分*/
  text-overflow: ellipsis;
}

::v-deep .el-textarea__inner {
  resize: none;
}
</style>
