<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.name" placeholder="直播间名称" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
    </div>
    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%">
      <el-table-column align="center" label="ID" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column width="180px" align="center" label="创建日期">
        <template slot-scope="scope">
          <span>{{ scope.row.create_time }}</span>
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" align="center" label="直播间id">
        <template slot-scope="scope">
          <span>{{ scope.row.room_id }}</span>
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="直播间名称" align="center" >
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="开始时间" width="180px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.start_time }}</span>
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" align="center" label="结束时间" width="180px">
        <template slot-scope="scope">
          <span>{{ scope.row.end_time }}</span>
        </template>
      </el-table-column>
      <!--<el-table-column class-name="status-col" label="主播昵称" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.anchor_name }}</span>
        </template>
      </el-table-column>-->
      <!--<el-table-column class-name="status-col" label="直播类型" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.type | typeFilter }}</span>
        </template>
      </el-table-column>-->
      <el-table-column class-name="status-col" label="状态" align="center">
        <template slot-scope="{row}">
          <el-tag type="success">
            {{ row | statusFilter }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="Actions" width="300" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-row>
            <router-link :to="'/live/edit/'+scope.row.id">
              <el-button type="primary" size="mini" icon="el-icon-edit" style="margin-right: 10px;">
                查看
              </el-button>
            </router-link>
            <router-link :to="'/live/chatList/'+scope.row.id">
              <el-button type="primary" size="mini" icon="el-icon-edit" style="margin-right: 10px;">
                查看聊天记录
              </el-button>
            </router-link>
<!--            <el-button type="primary" size="mini" icon="el-icon-s-check" :disabled="(scope.row.status === 1 || scope.row.status === 3)" @click="handleUpdate(scope)">-->
<!--              {{ scope.row.status === 1 || scope.row.status === 3 ? '已处理' : '处理' }}-->
<!--            </el-button>-->
            <!--<el-button type="danger" size="mini" icon="el-icon-delete" style="margin-left: 10px;" @click="handleDelete(scope)">
              删除
            </el-button>-->
          </el-row>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.pageSize" @pagination="getList" />
  </div>
</template>

<script>
import { fetchList } from '@/api/live'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import waves from '@/directive/waves'

export default {
  name: 'ArticleList',
  components: { Pagination },
  directives: { waves },
  filters: {
    statusFilter(row) {
      const startTimeData = new Date(row.start_time)
      const endTimeData = new Date(row.end_time)
      const now = new Date()
      if (now.getTime() > startTimeData.getTime() && now.getTime() < endTimeData.getTime()) {
        return '直播中'
      }
      if (now.getTime() > endTimeData.getTime()) {
        return '已结束'
      }
      if (now.getTime() < startTimeData.getTime()) {
        return '未开始'
      }
    },
    typeFilter(type) {
      const statusMap = {
        1: '推流',
        0: '手机直播'
      }
      return statusMap[type]
    }
  },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        pageSize: 10
      },
      temp: {
        id: undefined,
        mark: '',
        status: ''
      },
      dialogFormVisible: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.data.count
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.data.count
        this.listLoading = false
      })
    },
    async handleUpdate({ $index, row }) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
.filter-container{
  margin-bottom: 20px;
}
.filter-item {
  margin-right: 10px;
}

</style>
