<template>
  <div class="app-container">
    <div v-show="showSearch" class="search-form">
      <el-form ref="searchForm" :model="queryParams" inline :label-width="'100px'">
        <el-form-item label="用户名称" prop="userId">
          <el-input v-model="queryParams.userId" type="text" placeholder="用户名称" />
        </el-form-item>
        <el-form-item label="消息标识" prop="msgId">
          <el-input v-model="queryParams.msgId" type="text" placeholder="消息标识" />
        </el-form-item>
        <el-form-item class="searchItem" :label-width="'50px'">
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button type="default" @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <vxe-grid
      ref="vxe"
      highlight-hover-row
      border
      resizable
      sync-resize
      auto-resize
      class="vxe-table-element"
      :loading="loading"
      :start-index="(page.currentPage - 1) * page.pageSize"
      :pager-config="page"
      :columns="columns"
      :data="tableData"
      :toolbar="tableToolbar"
      @toolbar-button-click="toolbarButtonClickEvent"
      @page-change="handlePageChange"
    >
      <template v-slot:status="{ row }">
        <span>{{ $dictUtils.getDictLabel('sendStatus', row.status, row.status) }}</span>
      </template>
      <template v-slot:operation="{row}">
        <div>
<!--          <el-button size="mini" type="primary" @click="edit(row.id)">编辑</el-button>-->
          <el-button size="mini" type="danger" @click="remove(row.id)">删除</el-button>
        </div>
      </template>
    </vxe-grid>

  </div>
</template>

<script>
import mixin from 'ecip-web/utils/common-mixin'
import { getData, remove } from 'ecip-web/api/vue/push/pushLogApi'

export default {
  name: 'ModulesPushLogView',
  components: {},
  mixins: [...mixin],
  data() {
    return {
      tableToolbar: {
        id: 'modules.push.pushLogView-toolbar',
        export: true, // 导出按钮
        zoom: true, // 最大化按钮
        // 列宽操作记录
        resizable: {
          storage: true
        },
        // 列操作记录
        setting: {
          storage: true
        }
      },
      showSearch: false,
      loading: false,
      page: {
        total: 0,
        currentPage: 1,
        pageSize: 10,
        align: 'left',
        pageSizes: [10, 20, 50, 100, 200, 500],
        layouts: ['Sizes', 'PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'FullJump', 'Total'],
        perfect: true
      },
      tableData: [],
      columns: [
        { type: 'seq', width: 60, fixed: 'left', align: 'center' },
        { title: '用户名称', key: 'userId', field: 'userId', minWidth: 100, showOverflow: true },
        { title: '消息标识', key: 'msgId', field: 'msgId', minWidth: 100, showOverflow: true },
        { title: '消息参数', key: 'params', field: 'params', minWidth: 100, showOverflow: true },
        { title: '发送状态', key: 'status', field: 'status', minWidth: 100, showOverflow: true, slots: { default: 'status' }},
        { title: '发送时间', key: 'createDate', field: 'createDate', minWidth: 100, showOverflow: true },
        { title: '接收时间', key: 'updateDate', field: 'updateDate', minWidth: 100, showOverflow: true }
        /* { title: '操作', width: 150, fixed: 'right', align: 'center', slots: { default: 'operation' }}*/
      ],
      queryParams: {
        userId:'',
        msgId:''
      }
    }
  },
  created() {
    this.getData()
  },
  methods: {
    toolbarButtonClickEvent({ code }, event) {
      switch (code) {
        case 'add':
          this.add()
          break
        case 'remove':
          this.remove()
          break
        case 'search':
          this.showSearch = !this.showSearch
          break
        case 'reset':
          this.getData()
          break
      }
    },
    getData() {
      this.loading = true
      getData(Object.assign({}, { size: this.page.pageSize, current: this.page.currentPage }),
        this.queryParams).then(res => {
        const data = res.data
        this.tableData = data.records
        this.page.total = data.total
        this.loading = false
      }).catch(e => {
        this.loading = false
      })
    },
    handlePageChange({ currentPage, pageSize }) {
      this.page.currentPage = currentPage
      this.page.pageSize = pageSize
      this.getData()
    },
    add() {
      this.$router.push(`/pushTemplateAdd`)
    },
    edit(id) {
      this.$router.push(`/pushTemplateEdit/${id}`)
    },
    remove(id) {
      const deleteIds = []
      if (id) {
        deleteIds.push(id)
      } else {
        this.$refs.vxe.getSelectRecords().forEach(row => {
          const id = row.id
          deleteIds.push(id)
        })
      }
      if (deleteIds.length > 0) {
        this.$confirm(`确定删除所选项吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          remove(deleteIds).then(res => {
            this.getData()
            this.$notify.success('删除成功')
          })
        }
        )
      } else {
        this.$message({
          message: '请至少选择一条数据',
          type: 'warning'
        })
      }
    },
    handleSearch() {
      this.getData()
    },
    handleReset() {
      this.$refs['searchForm'].resetFields()
      this.getData()
    }
  }
}
</script>

<style scoped>

</style>
