<template>
<div class="app-container calendar-list-container">
  <!-- 筛选条件 -->
  <div class="filter-container">
    <el-select class="filter-item" clearable v-model="params.search_eq_d$venueId" filterable placeholder="请选择篮球馆">
      <el-option
        v-for="item in venues"
        :key="item.id"
        :label="item.name"
        :value="item.id">
      </el-option>
    </el-select>
    <el-button class="filter-item" type="primary" v-waves icon="search" @click="handleSearch">搜索</el-button>
    <el-button class="filter-item pull-right" type="success" icon="edit" @click="handleCreate">添加</el-button>
  </div>

  <!-- 列表数据 -->
  <Pagination uri="/devices" :request-params="params" ref="pagination">

      <!-- 设备名称 -->
      <el-table-column align="center" label="门禁序列号">
        <template scope="scope">
          <span>{{scope.row.name}}</span>
        </template>
      </el-table-column>

      <!-- 视频流地址 -->
      <el-table-column align="center" label="设备IP地址">
        <template scope="scope">
          <span>{{scope.row.stream}}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label=状态>
        <template scope="scope">
           <DeviceStatus :status="scope.row.status"></DeviceStatus>
          <!-- <span v-text="getStatus(scope.row.status)"></span> -->
        </template>
      </el-table-column>

       <el-table-column align="center" label=篮球馆>
        <template scope="scope">
          <span v-text="scope.row.venueName"></span>
        </template>
      </el-table-column>

    <el-table-column label="操作" align="center" width="150">
      <template scope="scope">
        <el-button size="small" type="text" @click="uploadLog(scope.row.id)">上传日志(bug)</el-button>
<!--        <el-button size="small" type="text" @click="handleEnter(scope.row.id)">管理员进场</el-button>-->
        <el-button size="small" class="danger" type="text" @click="handleOutPassword(scope.row.id)">管理员开门</el-button>
<!--        <el-button size="small" type="text" @click="printCard(scope.row.id)">打印临时卡凭证</el-button>-->
<!--        <el-button size="small" v-if="scope.row.status == 0" type="text" @click="handleReconnect(scope.row.id)">重新连接</el-button>-->
      </template>
    </el-table-column>
  </Pagination>
  <el-dialog title="管理员开门" :visible.sync="dialogFormByDay">
    <span>请输入开门密码：</span>
    <el-input v-model="form.password" placeholder="请输入密码"></el-input>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleOut">开门</el-button>
      <el-button @click="dialogFormByDay = false">关  闭</el-button>
    </div>
  </el-dialog>
</div>
</template>

<script>
import { remove, reconnect, checkAlive, adminEnter, adminOut, uploadLog } from '@/api//device'
import waves from '@/directive/waves.js'// 水波纹指令
import Pagination from '@/components/Pagination'
import { findVenueByType } from '@/api//venue'
import DeviceStatus from '@/components/DeviceStatus'
export default {
  name: 'device_list',
  components: { Pagination, DeviceStatus },
  directives: {
    waves
  },
  data() {
    return {
      dialogFormByDay: false,
      deviceId: undefined,
      params: {
        search_eq_d$venueId: undefined
      },
      form: {
        password: ''
      },
      venues: [],
      intervalid1: undefined
    }
  },
  created() {
    findVenueByType(1).then(response => {
      this.venues = response.venues
    })
    this.intervalid1 = setInterval(() => {
      this.handleSearch(false)
    }, 3000)
  },
  beforeDestroy () {
    clearInterval(this.intervalid1)
  },
  methods: {
    // getStatus(status) {
    //   if (status === 0) {
    //     return '未连接'
    //   } else if (status === 1) {
    //     return '正在重连'
    //   } else if (status === 2) {
    //     return '已连接'
    //   }
    // },
    handleReconnect(id) {
      reconnect(id).then(response => {
        this.handleSearch()
      })
    },

    handleOutPassword(id) {
      this.deviceId = id
      this.dialogFormByDay = true
    },

    handleEnter() {
      adminEnter(this.deviceId).then(response => {
        this.handleSearch()
      })
    },

    handleOut(id) {
      adminOut(this.deviceId, this.form.password).then(response => {
        this.handleSearch()
      })
    },

    printCard(id) {
      checkAlive(id).then(response => {
        this.handleSearch()
        // const fileUrl = 'http://127.0.0.1:8093/qrcode/print/' + id
        const fileUrl = 'https://api.hongyutiyu.top/qrcode/print/' + id
        window.open(fileUrl)
      })
    },

    uploadLog(id) {
      uploadLog(id).then(response => {
        this.handleSearch()
      })
    },

    /**
     * 搜索门禁设备
     */
    handleSearch(flag) {
      this.$refs.pagination.handleSearch(flag)
    },

    /**
     * 编辑门禁设备
     */
    handleEdit(id) {
      this.$router.push({ path: '/device/edit', query: { id: id }})
    },

    /**
     * 创建门禁设备
     */
    handleCreate() {
      this.$router.push({ path: '/device/edit' })
    },

    /**
     * 删除门禁设备
     */
    handleDelete(row) {
      this.$confirm('确认删除该门禁设备？').then(_ => {
        remove(row.id).then(response => {
          this.$refs.pagination.removeItem(row)
        })
      }).catch(_ => {})
    }
  }
}
</script>

