<template>
  <div class="app-container">
    <el-button
      v-if="checkRole([1, 2, 5]) && isOut === 1"
      type="primary"
      icon="el-icon-download"
      class="margin-left"
      @click="download"
    >导出记录
    </el-button>
    <el-input
      v-model="page.data.name"
      placeholder="请输入预警人员姓名"
      class="margin-left input"
    />
    <el-input
      v-model="page.data.idNumber"
      placeholder="请输入预警人员证件号"
      class="margin-left input"
    />
    <el-select
      v-model="page.data.status"
      filterable
      clearable
      placeholder="请选择预警是否确认"
      class="margin-left input"
    >
      <el-option
        v-for="item in warning_list"
        :key="item.id"
        :label="item.name"
        :value="item.id"
      />
    </el-select>
    <el-input
      v-model="page.data.controlName"
      placeholder="请输入布控人姓名"
      class="margin-left input"
    />
    <el-input
      v-model="page.data.controlPhone"
      placeholder="请输入布控人手机号"
      class="margin-left input"
    />

    <el-input
      v-model="page.data.controlUnit"
      placeholder="请输入布控人单位"
      class="margin-left input"
    />
    <el-date-picker
      v-model="page.data.startTime"
      type="datetime"
      placeholder="选择开始时间"
      value-format="yyyy-MM-dd HH:mm:ss"
      class="margin-left"
    />
    <el-date-picker
      v-model="page.data.endTime"
      type="datetime"
      placeholder="选择结束时间"
      value-format="yyyy-MM-dd HH:mm:ss"
      class="margin-left"
    />
    <el-button
      type="primary"
      icon="el-icon-search"
      class="margin-left"
      @click="fetchData"
    >搜索</el-button>
    <!--主表格-->
    <el-table
      ref="table_form"
      v-loading="listLoading"
      class="table"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column label="序号" align="center" width="80px">
        <template slot-scope="scope">
          {{ indexMethod(scope.$index) }}
        </template>
      </el-table-column>
      <el-table-column label="设备名称" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.snName | matchNull }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预警人现场照片" align="center">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            fit="cover"
            :src="scope.row.photo"
            :preview-src-list="[scope.row.photo]"
          />
        </template>
      </el-table-column>
      <el-table-column label="预警人姓名" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.name | matchNull }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预警人证件号" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.idNumber | matchNull }}</span>
        </template>
      </el-table-column>
      <el-table-column label="布控人姓名" align="center">
        <template slot-scope="scope">
          {{ scope.row.controlName | matchNull }}
        </template>
      </el-table-column>
      <el-table-column label="布控人手机号" align="center">
        <template slot-scope="scope">
          {{ scope.row.controlPhone | matchNull }}
        </template>
      </el-table-column>
      <el-table-column label="布控人单位" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.controlUnit | matchNull }}</span>
        </template>
      </el-table-column>
      <el-table-column label="过闸时间" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.faceTime | matchNull }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预警是否确认" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 2 " type="success">已确认</el-tag>
          <el-tag v-else-if="scope.row.status === 1 " type="danger">未确认</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="" label="操作区">
        <template slot-scope="scope">
          <el-button
            :disabled="scope.row.status !== 1"
            type="danger"
            size="small"
            @click="warningSure(scope.row.id)"
          >确认</el-button>
          <!-- <el-button v-else disabled type="primary" size="small"
            >已确认</el-button
          > -->
        </template>
      </el-table-column>
    </el-table>
    <!--分页栏-->
    <pagination
      :total="total"
      :page.sync="page.pageNum"
      :limit.sync="page.pageSize"
      @pagination="fetchData"
    />
  </div>
</template>

<script>
import { checkRole } from '@/utils/checkRole'
import Pagination from '@/components/Pagination'
import {
  earlyWarningConfirm,
  getListLogPageInfo,
  getListLogExcel
} from '@/api/monitor_management'

export default {
  name: 'WarningRecord',
  components: { Pagination },
  filters: {
    matchNull(str) {
      if (!str) {
        return '-'
      } else {
        return str
      }
    }
  },
  data() {
    return {
      // 主表格&主表格加载状态
      list: [],
      listLoading: true,
      // 分页参数
      page: {
        data: {
          controlName: null,
          controlPhone: null,
          controlUnit: null,
          idNumber: null,
          name: null,
          status: null,
          endTime: null,
          startTime: null
        },
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      // 设备列表_搜索
      warning_list: [
        {
          id: 2,
          name: '是'
        },
        {
          id: 1,
          name: '否'
        }
      ],
      // 是否可以导出
      isOut: this.$store.getters.isOut
    }
  },
  computed: {},
  watch: {},
  created() {
    this.fetchData()
  },
  mounted() {},
  methods: {
    // 查询权限
    checkRole,
    // 获取列表数据
    fetchData() {
      this.listLoading = true
      getListLogPageInfo(this.page).then((res) => {
        this.list = res.data.list
        this.total = res.data.total
        this.listLoading = false
      })
    },
    // 导出记录
    download() {
      this.$prompt('请输入导出密码', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        closeOnClickModal: false
      })
        .then(({ value }) => {
          if (value == 'Nqj##361') {
            this.$message({
              type: 'success',
              message: '密码正确,正在下载...'
            })
            this.loading = true
            getListLogExcel(this.page.data).then((res) => {
              var eleLink = document.createElement('a')
              eleLink.style.display = 'none'
              // 字符内容转变成blob地址
              var blob = new Blob([res])
              eleLink.href = URL.createObjectURL(blob)
              eleLink.setAttribute('download', '预警记录.xlsx')
              // 自动触发点击
              document.body.appendChild(eleLink)
              eleLink.click()
              // 然后移除
              document.body.removeChild(eleLink)

              // window.location.href = res.data;
              this.loading = false
            })
          } else {
            this.$message({
              type: 'error',
              message: '您输入的密码不正确'
            })
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '取消导出'
          })
        })
    },

    // 预警确认
    warningSure(id) {
      this.$confirm('确认预警吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        earlyWarningConfirm(id).then((res) => {
          this.fetchData()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        })
      })
    },

    // 返回列表序号
    indexMethod(index) {
      return (this.page.pageNum - 1) * this.page.pageSize + index + 1
    }
  }
}
</script>

<style scoped>
.table {
  margin-top: 15px;
}

.flex {
  display: flex;
  justify-content: flex-start;
  align-items: center;
}

.margin-left {
  margin: 10px 0 0 10px;
}

.input {
  width: 200px;
}
/*批量导入按钮*/
.file {
  margin: 10px 0 0 10px;
  position: relative;
  background: #d0eeff;
  border: 1px solid #99d3f5;
  border-radius: 4px;
  padding: 10px 20px;
  box-sizing: border-box;
  overflow: hidden;
  color: #1e88c7;
  text-decoration: none;
  text-indent: 0;
}

.file input {
  position: absolute;
  font-size: 14px;
  width: 106px;
  height: 40px;
  left: 0;
  top: 0;
  opacity: 0;
  cursor: pointer;
}

.file:hover {
  background: #aadffd;
  border-color: #78c3f3;
  color: #004974;
  text-decoration: none;
}
</style>
