<template>
  <div class="app-container">
    <div class="flex">
      <div class="top-box m-r">
        <div class="line"></div>
        <div class="flex flex1">
          <div class="tit">
            今日收到预警数（条）<span class="num">{{
              warningInfoData.todayWarningNum
            }}</span>
          </div>
          <div class="tit">
            本周收到预警数（条）<span class="num">{{
              warningInfoData.weekWarningNum
            }}</span>
          </div>
        </div>
        <div class="flex flex1 m-t">
          <div class="tit">
            本月收到预警数（条）<span class="num">{{
              warningInfoData.monthWarningNum
            }}</span>
          </div>
          <div class="tit">
            本年度收到预警数（条）<span class="num">{{
              warningInfoData.yearWarningNum
            }}</span>
          </div>
        </div>
      </div>
      <div class="top-box">
        <div class="line"></div>
        <div class="flex flex1">
          <div class="tit">
            时间范围：
            <el-date-picker
              @change="warningRangeInfo"
              v-model="range.startTime"
              type="datetime"
              placeholder="有效期开始时间"
              value-format="yyyy-MM-dd HH:mm:ss"
            >
            </el-date-picker>
            至
            <el-date-picker
              @change="warningRangeInfo"
              v-model="range.endTime"
              type="datetime"
              placeholder="有效期结束时间"
              value-format="yyyy-MM-dd HH:mm:ss"
            >
            </el-date-picker>
          </div>
        </div>
        <div class="flex flex1 m-t">
          <div class="tit">
            收到（条）<span class="num">{{
              warningRangeInfoData.warningNum
            }}</span>
          </div>
          <div class="tit">
            已处理（条）<span class="num">{{
              warningRangeInfoData.handleWarningNum
            }}</span>
          </div>
          <div class="tit">
            待处理（条）<span class="num">{{
              warningRangeInfoData.noHandleWarningNum
            }}</span>
          </div>
        </div>
      </div>
    </div>
    <template>
      <el-select
        v-model="page.data.zoneDeviceRId"
        clearable
        filterable
        placeholder="选择区域"
        class="margin-left input"
      >
        <el-option
          v-for="item in zoneList"
          :key="item.zoneDeviceRId"
          :label="item.name"
          :value="item.zoneDeviceRId"
        >
        </el-option>
      </el-select>
      <el-input
        v-model="page.data.roomNumber"
        placeholder="请输入房间号"
        class="margin-left input"
      ></el-input>
      <el-input
        v-model="page.data.name"
        placeholder="请输入姓名"
        class="margin-left input"
      ></el-input>
      <el-input
        v-model="page.data.cardIdEx"
        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.lastPassSn"
        placeholder="上次过闸设备"
        class="margin-left input"
      ></el-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>
      <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-select
        v-model="page.data.handleStatus"
        clearable
        filterable
        placeholder="是否处理"
        class="margin-left input"
      >
        <el-option
          v-for="item in handleList"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        >
        </el-option>
      </el-select>
      <el-button
        type="primary"
        icon="el-icon-search"
        @click="fetchData"
        class="margin-left"
        >搜索</el-button
      >
    </template>
    <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.zoneDeviceRName | matchNull }}
        </template>
      </el-table-column>
      <el-table-column label="房间号" align="center">
        <template slot-scope="scope">
          {{ scope.row.roomNumber | matchNull }}
        </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">
          {{ scope.row.cardIdEx | matchNull }}
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center">
        <template slot-scope="scope">
          {{ scope.row.phone | desensitization(3, 7) }}
        </template>
      </el-table-column>
      <el-table-column label="身份证号" align="center">
        <template slot-scope="scope">
          {{ scope.row.idNumber | desensitization(6, 14) }}
        </template>
      </el-table-column>
      <el-table-column label="人员照片" align="center">
        <template slot-scope="scope">
          <el-image
            style="width: 70px; height: 70px"
            :src="scope.row.avatar"
            :preview-src-list="[scope.row.avatar]"
          >
            <div slot="error" class="image-slot">
              {{ scope.row.avatar ? "加载失败" : "暂无照片" }}
            </div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="上次过闸设备编号" align="center">
        <template slot-scope="scope">
          {{ scope.row.lastPassSn | matchNull }}
        </template>
      </el-table-column>
      <el-table-column label="上次过闸时间" align="center">
        <template slot-scope="scope">
          {{ scope.row.lastPassTime | matchNull }}
        </template>
      </el-table-column>
      <el-table-column label="是否处理" align="center">
        <template slot-scope="scope">
          <el-tag effect="plain" type="success" v-if="scope.row.handleStatus"
            >已处理</el-tag
          >
          <el-tag effect="plain" type="info" v-else-if="!scope.row.handleStatus"
            >待处理</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column label="处理结果" align="center">
        <template slot-scope="scope">
          <el-tooltip
            class="item"
            effect="dark"
            :content="scope.row.handleResult"
            placement="top-start"
          >
            <div>
              {{ scope.row.handleResult | beyondOmission }}
            </div>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="预警时间" align="center">
        <template slot-scope="scope">
          {{ scope.row.warningTime | matchNull }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="" label="操作区" width="80">
        <template slot-scope="scope">
          <el-button
            type="primary"
            :disabled="scope.row.status == 0"
            size="small"
            @click="handleFun(scope.row)"
            >处理</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!--分页栏-->
    <pagination
      :total="total"
      :page.sync="page.pageNum"
      :limit.sync="page.pageSize"
      @pagination="fetchData"
    />
    <!--对话框-->
    <el-dialog
      title="处理预警"
      :close-on-click-modal="false"
      :visible.sync="dialogVisible"
      width="600px"
    >
      <el-form
        :model="form"
        :rules="rules"
        ref="form"
        label-width="80px"
        label-position="left"
      >
        <el-form-item label="处理结果" prop="handleResult" style="flex: 0.5">
          <el-input
            type="textarea"
            :rows="4"
            v-model="form.handleResult"
            placeholder="请输入处理结果"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false"> 取消 </el-button>
        <el-button type="primary" @click="resultFun"> 确定 </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from "@/components/Pagination";
import { checkRole } from "@/utils/checkRole";
import {
  getZoneList,
  warningInfo,
  warningRangeInfo,
  getPageList,
  warningHandle,
} from "@/api/dormitory_warning";
export default {
  components: { Pagination },
  filters: {
    matchNull(str) {
      if (!str) {
        return "未填写";
      } else {
        return str;
      }
    },
    beyondOmission(str) {
      let newStr = "";
      if (str && str.length > 8) {
        newStr = str.substring(0, 7) + "...";
        console.log(newStr);
        return newStr;
      } else {
        return str;
      }
    },
    // 脱敏
    desensitization(string = "", start, end) {
      if (string) {
        var star = "";
        function makeStar(number) {
          if (number > 0) {
            star += "*";
            return makeStar(--number);
          } else {
            return star;
          }
        }
        return (
          string.substring(0, start) +
          makeStar(end - start) +
          string.substring(end)
        );
      }
    },
  },
  props: {
    markStatus: {
      type: Number,
    },
  },
  computed: {},
  data() {
    return {
      warningInfoData: {},
      warningRangeInfoData: {},
      zoneList: [],
      // 主表格&主表格加载状态
      list: [],
      listLoading: true,
      range: {
        startTime: null,
        endTime: null,
      },
      // 分页参数
      page: {
        data: {
          handleStatus: null,
          name: "",
          cardIdEx: "",
          phone: "",
          idNumber: "",
          startTime: "",
          endTime: "",
          roomNumber: "",
          zoneDeviceRId: null,
        },
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      dialogVisible: false,
      handleList: [
        { id: true, name: "已处理" },
        { id: false, name: "待处理" },
      ],

      form: {
        handleResult: "",
      },
      rules: {
        handleResult: [{ required: true, message: "请输入", trigger: "blur" }],
      },
    };
  },
  created() {
    this.fetchData();
    this.warningInfo();
    this.getZoneList();
  },
  watch: {},
  methods: {
    // 查询权限
    checkRole,
    // 获取列表数据
    fetchData() {
      this.listLoading = true;
      this.page.data.markStatus = this.markStatus;
      getPageList(this.page).then((res) => {
        this.list = res.data.list;
        this.total = res.data.total;
        this.listLoading = false;
      });
    },
    // 区域列表
    getZoneList() {
      getZoneList().then((res) => {
        this.zoneList = res.data;
      });
    },
    // 预警信息
    warningInfo() {
      warningInfo({ markStatus: this.markStatus }).then((res) => {
        this.warningInfoData = res.data;
      });
    },
    // 预警范围信息
    warningRangeInfo() {
      if (this.range.startTime && this.range.endTime) {
        this.range.markStatus = this.markStatus;
        warningRangeInfo(this.range).then((res) => {
          this.warningRangeInfoData = res.data;
        });
      }
    },
    // 处理
    handleFun(row) {
      console.log(row);
      this.form.userWhitelistWarningId = row.userWhitelistWarningId;
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs["form"].clearValidate();
      });
    },
    resultFun() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          warningHandle(this.form).then((res) => {
            this.dialogVisible = false;
            this.fetchData();
            this.$message({
              type: "success",
              message: "操作成功!",
            });
          });
        }
      });
    },
    // 返回列表序号
    indexMethod(index) {
      return (this.page.pageNum - 1) * this.page.pageSize + index + 1;
    },
  },
};
</script>
<style scoped lang="scss">
.app-container {
  min-width: 1400px;
}
.table {
  margin-top: 15px;
}
.input {
  width: 200px;
}
.flex {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.margin-left {
  margin: 10px 0 0 10px;
}
.top-box {
  padding: 15px 3% 15px 3%;
  flex: 0.5;
  height: 120px;
  background-color: #fbfdff;
  margin-bottom: 20px;
  filter: drop-shadow(0px 2px 5px rgba(210, 223, 255, 0.5));
  border: 1px solid #dbe6ff;
  border-radius: 15px;
  position: relative;
  .line {
    height: 111px;
    width: 6px;
    background-color: #80a7ff;
    border-radius: 15px 0 0 15px;
    position: absolute;
    top: 3px;
    left: 0;
  }
}
.flex1 {
  justify-content: space-between;
}
.tit {
  color: #4b5d8a;
  font-size: 17px;
}
.num {
  color: #3a568e;
  font-size: 28px;
  font-weight: 700;
  font-family: Source Han Sans CN;
}
.m-t {
  margin-top: 15px;
}
.m-r {
  margin-right: 1%;
}
.el-date-picker {
  width: 200px;
}
</style>


