<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-input clearable class="filter-item" style="width: 200px;" placeholder="请输入用户姓名"
                v-model="listQuery.userName"></el-input>
      <el-input clearable class="filter-item" style="width: 200px;" placeholder="请输入评论内容"
                v-model="listQuery.content"></el-input>
      <el-date-picker clearable class="filter-item"
                      value-format="yyyy-MM-dd"
                      type="date"
                      placeholder="选择评论时间"
                      v-model="listQuery.createDate">
      </el-date-picker>
      <el-button class="filter-item" type="primary" v-waves icon="el-icon-search" @click="handleFilter">查找</el-button>
      <el-button class="filter-item" type="warning" icon="el-icon-delete" @click="delAll">批量删除</el-button>
    </div>

    <el-table size="small" :data="list" v-loading="listLoading" @selection-change="handleSelectionChange" element-loading-text="正在查询中。。。"  border fit
              highlight-current-row style="width: 100%">
      <el-table-column align="center" type="selection" width="55"> </el-table-column>
      <!--<el-table-column align="center" width="60" label="ID" prop="id" sortable>
      </el-table-column>-->
      <el-table-column type="index" label="序号" header-align="center" align="center">
      </el-table-column>
      <el-table-column align="center" width="80" label="评论人" prop="userName">
      </el-table-column>
      <el-table-column align="center" width="100" label="头像" prop="photo">
        <template slot-scope="scope">
          <img :src= "scope.row.photo" style="width: 50px;height: 50px;border-radius: 50%"/>
        </template>
      </el-table-column>
      <el-table-column align="center" width="200" label="评论来源" prop="newsTitle">
      </el-table-column>

      <el-table-column align="center" width="300" label="评论内容" prop="content">
      </el-table-column>
      <el-table-column align="center" width="150" label="评论时间" prop="createDate">
      </el-table-column>
      <el-table-column align="center" label="操作" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <router-link ref='tag' :to="{path:'/user/userDetail',query: {'id': scope.row.userId}}">
            <el-button type="primary" size="small ">查看用户</el-button>
          </router-link>
          <el-button type="danger" size="small"  @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.page"
                     :page-sizes="[10,20,30,50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import waves from '@/directive/waves' // 水波纹指令
import {listCommon,deleteCommon,deleteAllConent} from '@/api/news'
export default {
  name: 'comment',
  directives: {
    waves
  },
  data() {
    return {
      list: undefined,
      total: undefined,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        comment:undefined,
        createDate:undefined,
        sort: '+id'
      },
      dataForm: {
        id: undefined,
        userName:undefined,
        content: undefined,
        createDate:undefined,
        position: 1,
        enabled: true,
      },
      delarr:[],
      multipleSelection: [],
      downloadLoading: false,

    }

  },
  created(){
    this.getList();
  },
  methods:{
    getList(){
      listCommon(this.listQuery).then(response =>{
        this.list = response.data.data.items
        this.total = response.data.data.total
        this.listLoading = false
      }).catch(()=>{
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },
    handleDelete(row) {

      this.$confirm('确认删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteCommon(row).then(response => {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
          const index = this.list.indexOf(row)
          this.list.splice(index, 1)
        })
      }).catch(() => {

      })

    },

    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },

    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },

    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    delAll(){
      this.$confirm('确认删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const length = this.multipleSelection.length;

        if (length > 0) {
          for (let i = 0; i < length; i++){
            this.delarr.push(this.multipleSelection[i].id)
          }
          const newsIds = this.delarr.join(",");
          deleteAllConent({id:newsIds}).then(()=>{
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
            this.getList();
          }).catch(() => {

          })
        } else {
          this.$notify({
            title: '警告提示',
            message: '请选择要删除的信息！',
            type: 'warning'
          });
        }
      }).catch(() => {

      })
    },
    handleSelectionChange(val){
      this.multipleSelection = val;
    },
  }
}
</script>
