<template>
<div class="app-container calendar-list-container">
  <!-- 筛选条件 -->
  <div class="filter-container">
    <el-input @keyup.enter.native="handleSearch" style="width: 200px;" class="filter-item" clearable placeholder="标签" v-model="params.search_like_tag"></el-input>
    <el-input @keyup.enter.native="handleSearch" style="width: 500px;" class="filter-item" clearable placeholder="标题" v-model="params.search_like_title"></el-input>
  
    <el-button class="filter-item" type="primary" v-waves icon="search" @click="handleSearch">搜索</el-button>
    <el-button class="filter-item pull-right" type="success" icon="edit" @click="handleCreate">添加</el-button>
  </div>

  <!-- 列表数据 -->
  <Pagination uri="/announcements" :request-params="params" ref="pagination">

      <!-- 标签 -->
      <el-table-column align="center" label="标签">
        <template scope="scope">
          <span>{{scope.row.tag}}</span>
        </template>
      </el-table-column>

      <!-- 标题 -->
      <el-table-column align="center" label="标题">
        <template scope="scope">
          <span>{{scope.row.title}}</span>
        </template>
      </el-table-column>

      <!-- 短文介绍 -->
      <el-table-column align="center" label="短文介绍">
        <template scope="scope">
          <span>{{scope.row.content}}</span>
        </template>
      </el-table-column>

      <!-- 图片image -->
      <el-table-column align="center" label="图片">
        <template scope="scope">
          <img :src="scope.row.image" style="width:50px;height:50px;margin-top:8px"/>
        </template>
      </el-table-column>

      <!-- 创建时间 -->
      <el-table-column align="center" label="创建时间">
        <template scope="scope">
          <span>{{scope.row.createdTime | parseTime('{y}-{m}-{d} {h}:{i}')}}</span>
        </template>
      </el-table-column>

    <el-table-column label="操作" align="center" width="150">
      <template scope="scope">
        <el-button size="small" type="text" @click="handleEdit(scope.row.id)">编辑</el-button>
        <el-button size="small" type="text" class="danger" @click="handleDelete(scope.row)">删除</el-button>
      </template>
    </el-table-column>
  </Pagination>
</div>
</template>

<script>
import { remove } from '@/api//announcement'
import waves from '@/directive/waves.js'// 水波纹指令
import Pagination from '@/components/Pagination'
import moment from 'moment'

export default {
  name: 'announcement_list',
  components: { Pagination },
  directives: {
    waves
  },
  data() {
    return {
      params: {
        search_like_tag: '',
        search_like_title: '',
        starTime: '',
        endTime: ''
      }
    }
  },
  created() {
  },
  methods: {
    /**
     * 搜索公告
     */
    handleSearch() {
      if (this.params.date != null) {
        this.params.endTime = moment(this.params.date[1]).format('YYYY-M-D')
        this.params.starTime = moment(this.params.date[0]).format('YYYY-M-D')
      } else {
        this.params.starTime = ''
        this.params.endTime = ''
      }
      this.$refs.pagination.handleSearch()
    },

    /**
     * 编辑公告
     */
    handleEdit(id) {
      this.$router.push({ path: '/announcement/edit', query: { id: id }})
    },

    /**
     * 创建公告
     */
    handleCreate() {
      this.$router.push({ path: '/announcement/edit' })
    },

    /**
     * 删除公告
     */
    handleDelete(row) {
      this.$confirm('确认删除该公告？').then(_ => {
        remove(row.id).then(response => {
          this.$refs.pagination.removeItem(row)
        })
      }).catch(_ => {})
    }
  }
}
</script>

