<template>
  <div class="notice-page">
    <Card>
      <div class="search-con">
        <Input placeholder="请输入标题或内容" class="search-input" clearable v-model="searchContent.content" style="width: 200px"/>
        <ButtonGroup class="search-btngroup">
          <Button type="primary" style="margin-left: 20px" @click="handleSearch">查找</Button>
          <Button type="primary" @click="handleRefresh">
            <Icon type="md-refresh" />
          </Button>
        </ButtonGroup>
        <Button type="info" style="margin-left: 20px" @click="handleAdd">+ 发布公告</Button>
      </div>
      <tables
        ref="tables"
        editable
        pagable
        v-model="tableData"
        :columns="columns"
        :currentPage="searchContent.pageNum"
        :pageSize="searchContent.pageSize"
        :totalSize="totalSize"
        :loading="loading"
        @on-change="handleCurrentChange"
      />
    </Card>
  </div>
</template>
<script>
import Tables from '_c/tables'
import { formatTime } from '@/libs/tools'
export default {
  inject: ['reload'],
  components: {
    Tables
  },
  data () {
    return {
      searchContent: {
        content: '',
        pageNum: 1,
        pageSize: 10
      },
      tableData: [],
      columns: [
        { title: '编号', key: 'noticeId', align: 'left', minWidth: 180 },
        { title: '标题', key: 'title', align: 'center', minWidth: 100 },
        { title: '内容', key: 'content', align: 'left', minWidth: 150 },
        { title: '开始时间',
          key: 'index',
          align: 'center',
          minWidth: 100,
          render: (h, params) => {
            return h('span', formatTime(params.row.startTime, 'yyyy-MM-dd'))
          },
          renderHeader: (h, params) => {
            return h('div',
              [
                h('span', {
                  style: {
                    marginLeft: '8px'
                  }
                }, '开始时间'),
                [
                  h('Tooltip', {
                    props: {
                      trigger: 'hover',
                      content: `活动从当天的00:00:00开始`,
                      wordWrap: true,
                      width: 200,
                      transfer: true
                    }
                  }, [ h('Icon', {
                    props: {
                      type: 'ios-help-circle-outline',
                      size: 14
                    },
                    style: {
                      marginRight: '2px',
                      transform: 'translateY(-1px)'
                    }
                  })])
                ]
              ])
          }
        },
        { title: '结束时间',
          key: 'index',
          align: 'center',
          minWidth: 100,
          render: (h, params) => {
            return h('span', formatTime(params.row.endTime, 'yyyy-MM-dd'))
          },
          renderHeader: (h, params) => {
            return h('span',
              [
                h('span', {
                  style: {
                    marginLeft: '8px'

                  }
                }, '结束时间'),
                [
                  h('Tooltip', {
                    props: {
                      trigger: 'hover',
                      content: `活动到当天的23:59:59结束`,
                      wordWrap: true,
                      width: 200,
                      transfer: true
                    }
                  }, [ h('Icon', {
                    props: {
                      type: 'ios-help-circle-outline',
                      size: 14
                    },
                    style: {
                      marginLeft: '2px',
                      transform: 'translateY(-1px)'
                    }
                  })])
                ]
              ])
          }
        },
        { title: '状态',
          key: 'index',
          align: 'center',
          width: 100,
          render: (h, params) => {
            return h('span', params.row.validStatus ? '有效' : '过期')
          }
        },
        { title: '操作',
          key: 'operate',
          align: 'left',
          minWidth: 100,
          render: (h, params) => {
            let editBtn = h(
              'Button',
              {
                props: {
                  type: 'primary',
                  size: 'small'
                },
                on: {
                  click: this.handleUpdate(params.row)
                },
                style: {
                  marginRight: '5px'
                  // display: params.row.validStatus ? 'inline-block' : 'none'
                }
              },
              '编 辑'
            )
            let delBtn = h(
              'Button',
              {
                props: {
                  type: 'error',
                  size: 'small'
                },
                on: {
                  click: this.handleDelete(params.row)
                },
                style: {
                  marginRight: '5px'
                }
              },
              '删 除'
            )
            return h('div', [editBtn, delBtn])
          }
        }
      ],
      loading: false,
      totalSize: 0
    }
  },
  methods: {
    handleRefresh () {
      this.reload()
    },
    requestData () {
      this.loading = true
      this.api({
        apiName: 'getNoticeList',
        postdata: this.searchContent,
        complete: () => {
          this.loading = false
        },
        success: (res) => {
          this.tableData = res.data.records
          this.totalSize = res.totalSize
        }
      })
    },
    handleCurrentChange (pageNum) {
      this.searchContent.pageNum = pageNum
      this.requestData()
    },
    handleSearch () {
      this.requestData()
    },
    handleAdd () {
      this.$router.push({ name: 'add_announcement' })
    },
    handleUpdate (row) {
      return () => {
        let query = { noticeId: row.noticeId }
        this.$router.push({ name: 'update_announcement', query })
      }
    },
    handleDelete (row) {
      return () => {
        this.$Modal.confirm({
          title: '删除',
          content: `您确认要删除标题为${row.title}的公告吗？`,
          onOk: () => {
            this.api({
              apiName: 'deleteNotice',
              postdata: row.noticeId,
              success: () => {
                this.requestData()
                this.$Notice.success({ title: `删除成功！` })
              }
            })
          }
        })
      }
    }
  },
  created () {
    this.requestData()
  }
}
</script>
<style scoped lang="less">
.notice-page .search-con {
  display: flex;
  padding: 10px 0;
  .search-input {
    width: 300px;
  }
}
</style>
