<template>
  <div class="form-wrap">
    <el-form
      class="search-box"
      ref="form"
      :model="form"
      :inline="true"
      label-width="140px"
    >
      <div class="block-title">
        查询条件
      </div>
      <el-form-item
        label="名称"
        prop="keyWords1"
      >
        <el-input v-model="form.keyWords1"></el-input>
      </el-form-item>
      <el-form-item
        label="IP地址"
        prop="keyWords2"
      >
        <el-input v-model="form.keyWords2"></el-input>
      </el-form-item>
      <el-form-item
        label="类型"
        prop="keyWords3"
      >
        <el-select v-model="form.keyWords3">
          <el-option
            label="全部"
            value=""
          ></el-option>
          <el-option
            label="人脸"
            value="人脸"
          ></el-option>
          <el-option
            label="指纹"
            value="指纹"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="在线状态"
        prop="keyWords4"
      >
        <el-select v-model="form.keyWords4">
          <el-option
            label="全部"
            value=""
          ></el-option>
          <el-option
            label="在线"
            :value="1"
          ></el-option>
          <el-option
            label="离线"
            :value="0"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="是否启用"
        prop="keyWords5"
      >
        <el-select v-model="form.keyWords5">
          <el-option
            label="全部"
            value=""
          ></el-option>
          <el-option
            label="启用"
            :value="1"
          ></el-option>
          <el-option
            label="停用"
            :value="0"
          ></el-option>
        </el-select>
      </el-form-item>
      <div class="btn-wrap">
        <el-button
          @click="reset"
        >
          重置
        </el-button>
        <el-button
          type="primary"
          @click="getList(true)"
        >
          搜索
        </el-button>
      </div>
    </el-form>

    <div class="table-box">
      <div class="block-title">
        二次入园登记设备
      </div>
      <el-table
        border
        stripe
        :data="tableData">
        <el-table-column
          type="index"
          width="50px"
          label="#"
        >
        </el-table-column>
        <el-table-column
          prop="name"
          label="名称"
        >
        </el-table-column>
        <el-table-column
          prop="scenic_name"
          label="景点"
        >
        </el-table-column>
        <el-table-column
          prop="type_name"
          label="类型"
        >
        </el-table-column>
        <el-table-column
          prop="ip"
          label="IP"
        >
        </el-table-column>
        <el-table-column
          prop="mac"
          label="MAC"
        >
        </el-table-column>
        <el-table-column
          prop="version_name"
          label="版本名"
        >
        </el-table-column>
        <el-table-column
          label="状态"
        >
          <template slot-scope="props">
            <el-tag :type="props.row.online | onOffColor">
              {{ props.row.online | filterOnline }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          width="140"
          label="最后在线时间"
        >
          <template slot-scope="props">
            <span>{{ props.row.lastOnlineTime | formatTime }}</span>
          </template>
        </el-table-column>
        <el-table-column
          width="140"
          label="创建时间"
        >
          <template slot-scope="props">
            <span>{{ props.row.createTime | formatTime }}</span>
          </template>
        </el-table-column>
        <el-table-column
          width="120"
          label="启用状态"
        >
          <template slot-scope="scope">
            <el-tag :type="scope.row.isActive | runStopColor">
              {{ scope.row.isActive ? '已启用' : '已停用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          width="120"
          label="启用"
        >
          <template slot-scope="scope">
            <el-button
              :type="scope.row.isActive?'danger':'success'"
              size="mini"
              @click="scope.row.isActive?offSale(scope.row):onSale(scope.row)"
            >
              {{ scope.row.isActive?'停用':'启用' }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column
          width="100px"
          label="操作"
        >
          <template slot-scope="scope">
            <el-link
              type="primary"
              @click="openDialog(scope.row)"
            >
              编辑
            </el-link>
            <el-link
              type="primary"
              @click="deleteBinder(scope.row)"
            >
              删除
            </el-link>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        layout="total, sizes, prev, pager, next, jumper"
        :current-page.sync="form.pageNum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="form.pageSize"
        @size-change="handleSizeChange"
        @current-change="getList()"
        :total="total"
      >
      </el-pagination>
    </div>
    <!-- 弹框 -->
    <ElDialog
      title="编辑设备"
      width="760px"
      v-model="dialogVisible"
    >
      <DialogEdit
        :current-item="currentItem"
        @updateList="getList"
      ></DialogEdit>
    </ElDialog>
  </div>
</template>

<script>
import { getBinderList, updateBinder, deleteBinder } from '@/api/ticketbinder'
import ElDialog from '@/components/Dialog'
import DialogEdit from './secondDevice/DialogEdit'
export default {
  data () {
    return {
      form: {
        startDate: '',
        endDate: '',
        keyWords1: '', // 名称
        keyWords2: '', // IP
        keyWords3: '', // 类型id
        keyWords4: '', // online 在线状态0和1
        keyWords5: '', // isActive是否启用
        pageNum: 1,
        pageSize: 10
      },
      tableData: [],
      total: 0,
      dialogVisible: false,
      currentItem: {}
    }
  },
  created () {
    this.getList()
  },
  components: {
    ElDialog,
    DialogEdit
  },
  filters: {
    filterOnline (n) {
      return n === 1 ? '在线' : '离线'
    },
    filterInOut (n) {
      return n === 1 ? '入口' : '出口'
    }
  },
  methods: {
    reset () {
      this.$refs.form.resetFields()
    },
    handleSizeChange (size) {
      this.form.pageSize = size
      this.getList()
    },
    getList (goFirst) {
      goFirst && (this.form.pageNum = 1)

      getBinderList(this.form).then(res => {
        this.total = res.total
        this.tableData = res.list
      })
    },
    // 打开弹框
    openDialog (item) {
      this.currentItem = item
      this.dialogVisible = true
    },
    offSale (data) {
      let form = Object.assign({}, data)
      form.isActive = 0
      updateBinder(form).then(res => {
        this.$message.success('操作成功')
        this.getList()
      })
    },
    deleteBinder (data) {
      this.$confirm('确定删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteBinder(data).then(res => {
          this.$message.success('操作成功')
          this.getList()
        })
      }).catch(() => {})
    },
    onSale (data) {
      let form = Object.assign({}, data)
      form.isActive = 1
      updateBinder(form).then(res => {
        this.$message.success('操作成功')
        this.getList()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.offSale-btn{
  color: #F56C6C
}
.onSale-btn{
  color: #67C23A
}
</style>
