<template>
  <div class="app-container" v-loading="downloadLoading">
    <!--搜索区-->
    <el-input
      v-model="page.data.name"
      placeholder="请输入姓名"
      class="margin-left input"
    ></el-input>
    <el-input
      v-model="page.data.phone"
      placeholder="请输入手机号"
      class="margin-left input"
    ></el-input>
    <el-input
      v-model="page.data.idNumber"
      placeholder="请输入身份证号"
      class="margin-left input"
    ></el-input>
    <el-input
      v-model="page.data.cardIdEx"
      placeholder="请输入卡号"
      class="margin-left input"
    ></el-input>
    <el-select
      filterable
      clearable
      v-model="page.data.deviceSn"
      placeholder="请选择设备编号"
      class="margin-left input"
    >
      <el-option
        v-for="item in machine_list"
        :key="item.sn"
        :label="item.sn"
        :value="item.sn"
      >
      </el-option>
    </el-select>
    <el-select
      filterable
      clearable
      v-model="page.data.deviceSn"
      placeholder="请选择设备地点"
      class="margin-left input"
    >
      <el-option
        v-for="item in machine_list"
        :key="item.sn"
        :label="item.name"
        :value="item.sn"
      >
      </el-option>
    </el-select>
    <el-select
      filterable
      clearable
      v-model="page.data.pass"
      placeholder="请选择进出门"
      class="margin-left input"
    >
      <el-option
        v-for="item in into_door_list"
        :key="item.id"
        :label="item.name"
        :value="item.id"
      >
      </el-option>
    </el-select>
    <el-select
      filterable
      clearable
      v-model="page.data.verifyType"
      placeholder="请选择验证类型"
      class="margin-left input"
    >
      <el-option
        v-for="item in authentication_type_list"
        :key="item.id"
        :label="item.name"
        :value="item.id"
      >
      </el-option>
    </el-select>
    <el-select
      filterable
      clearable
      v-model="page.data.rightno"
      placeholder="请选择人员类型"
      class="margin-left input"
    >
      <el-option
        v-for="item in Personnel_type_list"
        :key="item.id"
        :label="item.name"
        :value="item.id"
      >
      </el-option>
    </el-select>
    <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>
    <el-date-picker
      v-model="page.data.endTime"
      type="datetime"
      placeholder="选择通行结束时间"
      value-format="yyyy-MM-dd HH:mm:ss"
      class="margin-left"
    >
    </el-date-picker>
    <el-button
      type="primary"
      icon="el-icon-search"
      class="margin-left"
      @click="fetchData"
      >搜索</el-button
    >
    <el-tooltip
      class="item"
      effect="dark"
      content="最大支持导出50000条数据！"
      placement="top-start"
    >
      <el-button
        type="primary"
        icon="el-icon-download"
        class="margin-left"
        @click="download"
        v-if="isOut === 1"
        >导出记录</el-button
      >
    </el-tooltip>

    <el-tooltip
      class="item"
      effect="dark"
      content="最大支持导出500条数据！"
      placement="top-start"
    >
      <el-button
        type="primary"
        icon="el-icon-download"
        class="margin-left"
        @click="steakPhoto(500)"
        v-if="isOut === 1"
        >导出照片</el-button
      >
    </el-tooltip>
    <el-button
      type="primary"
      :icon="!isUpdate ? 'el-icon-refresh-left' : 'el-icon-loading'"
      class="margin-left"
      @click="isUpdate ? stopUpdate() : autoUpdate()"
      >{{ !isUpdate ? "打开自动刷新" : "关闭自动刷新" }}</el-button
    >
    <!--主表格-->
    <el-table
      class="table"
      ref="table_form"
      v-loading="listLoading"
      :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">
          {{ scope.row.deviceSn | matchNull }}
        </template>
      </el-table-column>
      <el-table-column label="设备地点" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.faceDeviceName | matchNull }}</span>
        </template>
      </el-table-column>
      <el-table-column label="设备类型" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.faceDeviceTypeName | matchNull }}</span>
        </template>
      </el-table-column>

      <el-table-column label="用户姓名" align="center">
        <template slot-scope="scope">
          {{ scope.row.name | matchNull }}
        </template>
      </el-table-column>
      <el-table-column label="现场照片" align="center">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.photo"
            :preview-src-list="[scope.row.photo]"
          >
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="证件号" align="center">
        <template slot-scope="scope">
          {{ scope.row.cardid }}
        </template>
      </el-table-column>
      <el-table-column label="卡号" align="center">
        <template slot-scope="scope">
          {{ scope.row.cardIdEx }}
        </template>
      </el-table-column>
      <el-table-column label="手机号码" align="center">
        <template slot-scope="scope">
          {{ scope.row.phone }}
        </template>
      </el-table-column>
      <el-table-column label="进/出" align="center">
        <template slot-scope="scope">
          <el-tag type="success" effect="light" v-if="scope.row.outType === 0"
            >通用</el-tag
          >
          <el-tag
            type="primary"
            effect="plain"
            v-else-if="scope.row.outType === 1"
            >进门</el-tag
          >
          <el-tag
            type="primary"
            effect="dark"
            v-else-if="scope.row.outType === 2"
            >出门</el-tag
          >
          <el-tag v-else>其它</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="验证类型" align="center">
        <template slot-scope="scope">
          <el-tag type="primary" plain v-if="scope.row.verifyType === 0"
            >人脸</el-tag
          >
          <el-tag type="primary" plain v-else-if="scope.row.verifyType === 1"
            >IC卡</el-tag
          >
          <el-tag type="primary" plain v-else-if="scope.row.verifyType === 2"
            >身份证</el-tag
          >
          <el-tag type="primary" plain v-else-if="scope.row.verifyType === 3"
            >二维码</el-tag
          >
          <el-tag type="primary" plain v-else-if="scope.row.verifyType === 4"
            >远程</el-tag
          >
          <el-tag type="primary" plain v-else-if="scope.row.verifyType === 6"
            >IC卡+人脸</el-tag
          >
          <el-tag v-else>其它</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="人员类型" align="center">
        <template slot-scope="scope">
          <el-tag type="" effect="plain" v-if="scope.row.rightno === 104"
            >访客</el-tag
          >
          <el-tag
            type="success"
            effect="plain"
            v-else-if="scope.row.rightno === 105"
            >员工</el-tag
          >
          <el-tag
            type="info"
            effect="plain"
            v-else-if="scope.row.rightno === 101"
            >黑名单</el-tag
          >
          <el-tag type="info" effect="plain" v-else>未知</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="通行时间" align="center">
        <template slot-scope="scope">
          {{ scope.row.faceTime }}
        </template>
      </el-table-column>
      <el-table-column label="预警原因" align="center">
        <template slot-scope="scope">
          {{ scope.row.warningMsg }}
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center">
        <template slot-scope="scope">
          {{ scope.row.remark }}
        </template>
      </el-table-column>
      <el-table-column
        label="操作区"
        align="center"
        fixed="right"
        width="220px"
      >
        <template slot-scope="scope">
          <el-button
            v-if="!scope.row.isSure"
            type="primary"
            size="small"
            @click="clickSure(scope.row.waringLogId)"
            >确认</el-button
          >
          <el-button
            type="danger"
            size="small"
            @click="deleteItem(scope.row.waringLogId, 1)"
            >删除</el-button
          >
          <el-button
            v-if="!scope.row.isBlack"
            type="danger"
            size="small"
            @click="deleteItem(scope.row.waringLogId, 2)"
            >拉黑</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!--分页栏-->
    <pagination
      :total="total"
      :page.sync="page.pageNum"
      :limit.sync="page.pageSize"
      @pagination="fetchData"
    />
    <el-dialog
      title="确认情况"
      width="400px"
      :visible.sync="sureVisible"
      destroy-on-close
    >
      <el-form
        :model="sure_form"
        ref="sure_form"
        label-width="60px"
        label-position="left"
      >
        <el-form-item label="备注" prop="remark" class="form-item-style">
          <el-input
            v-model="sure_form.remark"
            placeholder="请输入备注信息"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="sureFun"> 确定 </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import detailsDialog from "@/components/DialogDetails/index.vue";

import {
  getFaceDeviceList,
} from "@/api/old_api_pengwenbing";
import Pagination from "@/components/Pagination";
import { checkRole } from "@/utils/checkRole";
import {
  getUserListSearch,
} from "@/api/user_manage";
import { parseTime } from "@/utils";
import { downloadPhotosWarningPassRecords } from "@/utils/steakPhoto"; // secondary package based on el-pagination
import {
  doGetWaringServerLog,
  doGetWaringServerLogExcel,
  warningSure,
  warningDelete,
  warningBlack,
} from "@/api/user_manage";

export default {
  components: { Pagination, detailsDialog },
  filters: {
    matchNull(str) {
      if (!str) {
        return "未记录";
      } else {
        return str;
      }
    },
    aAecimal(str) {
      return (str = str.slice(0, 4));
    },
  },
  data() {
    return {
      // 主表格&主表格加载状态
      list: [],
      listLoading: true,
      downloadLoading: false, //导出加载
      // 分页参数
      page: {
        data: {
          name: null,
          idNumber: null,
          phone: null,
          cardIdEx: null,
          deviceSn: null,
          startTime: "",
          endTime: "",
          pass: null,
          verifyType: null,
          rightno: null,
          contents: null, //自定义字段
        },
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      // 账号列表_搜索
      user_list: [],
      // 设备列表_搜索
      machine_list: [],
      // 进出门_搜索
      into_door_list: [
        { id: 0, name: "不限" },
        { id: 1, name: "进门" },
        { id: 2, name: "出门" },
      ],
      // 验证类型
      authentication_type_list: [
        { id: 0, name: "人脸" },
        // { id: "1", name: "IC卡" },
        { id: 2, name: "身份证" },
        { id: "3", name: "二维码" },
      ],
      // 人员类型
      Personnel_type_list: [
        { id: 104, name: "访客" },
        { id: 105, name: "员工" },
        { id: 301, name: "预约人员" },
      ],
      // 是否可以导出
      isOut: this.$store.getters.isOut,
      // 是否打开自动更新
      isUpdate: "",
      row: {},
      // 确认情况
      sureVisible: false,
      sure_form: {
        remark: null,
      },
    };
  },
  created() {
    this.page_copy = JSON.parse(JSON.stringify(this.page));
    this.fetchData();
    this.matchAutoUpdate();
    this.getUserListSearch();
    this.getFaceDeviceList();
  },
  beforeDestroy() {
    var isUpdate = localStorage.getItem("isUpdateWarning");
    if (isUpdate) {
      clearInterval(isUpdate);
    }
  },
  computed: {},
  methods: {
    // 查询权限
    checkRole,
    // 获取列表数据
    fetchData(isUpdate = false) {
      this.listLoading = true;
      doGetWaringServerLog(this.page).then((res) => {
        this.list = res.data.list;
        this.total = res.data.total;
        this.listLoading = false;
      });
    },
    clickSure(id) {
      this.sure_form.waringLogId = id;
      this.sure_form.remark = null;
      this.sureVisible = true;
    },
    sureFun() {
      warningSure(this.sure_form).then((res) => {
        this.sureVisible = false;
        this.fetchData();
        this.$message({
          type: "success",
          message: "确认成功!",
        });
      });
    },
    // 删除/拉黑
    deleteItem(id, type) {
      this.$confirm(`确定要${type == 1 ? "删除" : "拉黑"}此人员吗?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      }).then(async () => {
        let res;
        if (type == 1) {
          res = await warningDelete(id);
        } else {
          res = await warningBlack(id);
        }
        if (res.code == 200) {
          this.fetchData();
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        }
      });
    },
    // 获取设备列表-搜索
    getFaceDeviceList() {
      getFaceDeviceList().then((res) => {
        this.machine_list = res.data;
      });
    },
    // 获取账号列表数据_搜索
    getUserListSearch() {
      getUserListSearch().then((res) => {
        this.user_list = res.data;
      });
    },
    // 导出记录
    download() {
      this.$prompt("请输入导出密码", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        closeOnClickModal: false,
      })
        .then(({ value }) => {
          if (value == "Nqj##361") {
            this.$message({
              type: "success",
              message: "密码正确,正在下载...",
            });
            this.downloadLoading = true;
            doGetWaringServerLogExcel(this.page.data).then((res) => {
              window.location.href = res;
              this.downloadLoading = false;
            });
          } else {
            this.$message({
              type: "error",
              message: "您输入的密码不正确",
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消导出",
          });
        });
    },
    //批量导出图片-带参数导出入口-BETA $1 查询参数  $2多少条
    steakPhoto(number) {
      downloadPhotosWarningPassRecords(this.page, number);
    },
    // 打开自动更新
    autoUpdate() {
      this.isUpdate = setInterval(() => {
        this.fetchData(true);
      }, 1000*60*5);
      localStorage.setItem("isUpdateWarning", this.isUpdate);
    },
    // 关闭自动更新
    stopUpdate() {
      clearInterval(this.isUpdate);
      localStorage.setItem("isUpdateWarning", "");
      this.isUpdate = "";
    },
    // 检测上次自动更新状态
    matchAutoUpdate() {
      var isUpdate = localStorage.getItem("isUpdateWarning");
      if (isUpdate) {
        clearInterval(isUpdate);
        this.autoUpdate();
      }
    },
    // 返回列表序号
    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: 150px;
}
</style>
