<template>
  <div v-loading="loading">
    <div class="white-box">
      <el-table ref="multipleTable" :data="tableData" stripe style="width: 100%;"
                @selection-change="handleSelectionChange" @row-click="handleExpand" :height="tool.getTableHeight()">
        <el-table-column type="expand">
          <template slot-scope="scope">
            {{scope.row.CONTENT}}
          </template>
        </el-table-column>
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="标题" prop="TITLE"></el-table-column>
        <el-table-column label="发送给会员">
          <template slot-scope="scope">
            <span v-if="scope.row.USER_NAME">{{scope.row.USER_NAME}}</span>
            <span v-else>全部</span>
          </template>
        </el-table-column>
        <el-table-column label="管理员" prop="ADMIN_NAME"></el-table-column>
        <el-table-column label="创建时间">
          <template slot-scope="scope">
            {{tool.formatDate(scope.row.CREATED_AT)}}
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="180">
          <template slot-scope="scope">
            <el-dropdown size="small" trigger="click">
              <el-button type="primary" size="small" @click.stop="">
                操作该数据<i class="el-icon-arrow-down el-icon--right"></i>
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="delete" @click.native="handleDelete(scope.row)">删除</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <div class="white-box-footer">
        <el-dropdown size="small" trigger="click">
          <el-button type="primary" size="small">
            所选数据<i class="el-icon-arrow-down el-icon--right"></i>
          </el-button>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="delete" @click.native="handleMuliDel()">删除</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
        <el-button type="primary" size="small" @click="handleAdd">发送站内信</el-button>
        <pagination :total="totalCount" :page_size="pageSize" @size-change="handleSizeChange" @current-change="handleCurrentChange"></pagination>
      </div>
    </div>
  </div>
</template>

<script>
import Vue from 'vue'
import network from '@/utils/network'
import tool from '@/utils/tool'
import store from '@/utils/vuexStore'
import Pagination from '@/components/Pagination'

export default {
  name: 'message_list',
  components: {Pagination},
  mounted () {
    this.getData()
  },
  data () {
    return {
      allData: null,
      tableData: null,
      loading: true,
      multipleSelection: [],
      currentPage: 1,
      totalPages: 1,
      totalCount: 1,
      pageSize: 20,
      tool: tool,
    }
  },
  methods: {
    handleExpand (row, event, column) {
      this.$refs.multipleTable.toggleRowExpansion(row)
    },
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    handleCurrentChange (page) {
      this.getData(page, this.pageSize)
    },
    handleSizeChange (pageSize) {
      this.getData(this.currentPage, pageSize)
    },
    handleAdd () {
      this.$router.push({path: `/message/add`})
    },
    handleDelete (row) {
      this.delData(row.ID)
    },
    handleMuliDel () {
      this.delData()
    },
    getData (page, pageSize) {
      let obj = this
      network.getPageData(this, `message/list`, page, pageSize, null, function (response) {
        obj.allData = response
      })
    },
    delData (id = null) {
      let obj = this
      obj.$confirm('确定删除选定的数据？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let selectedIds = []
        if (id === null) {
          for (let val of obj.multipleSelection) {
            selectedIds.push(val.ID)
          }
        } else {
          selectedIds.push(id)
        }

        return network.postData(`message/delete`, {
          selected: selectedIds
        })
      }).then(response => {
        this.$message({
          message: response,
          type: 'success'
        })
        obj.getData(obj.currentPage, obj.pageSize)
      }).catch(response => {

      })
    },
  }
}

</script>

<style scoped>

</style>