<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-button
      type="primary"
      icon="el-icon-search"
      @click="fetchData"
      class="margin-left"
      >搜索</el-button
    >
    <el-tooltip
      class="item"
      effect="dark"
      content="最大支持导出五万条数据！"
      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-table
      class="table"
      ref="table_form"
      v-loading="listLoading"
      :data="list"
      element-loading-text="操作中，请耐心等待！"
      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">
          <el-image
            style="width: 70px; height: 70px"
            :src="scope.row.photo"
            :preview-src-list="[scope.row.photo]"
          >
            <div slot="error" class="image-slot">暂无照片</div>
          </el-image>
        </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.phone | matchNull }}
        </template>
      </el-table-column>
      <el-table-column label="身份证号" align="center">
        <template slot-scope="scope">
          {{ scope.row.idNumber | matchNull }}
        </template>
      </el-table-column>
      <el-table-column label="单位" align="center">
        <template slot-scope="scope">
          {{ scope.row.company | matchNull }}
        </template>
      </el-table-column>
      <el-table-column label="受访人姓名" align="center">
        <template slot-scope="scope">
          {{ scope.row.visiteeXm | matchNull }}
        </template>
      </el-table-column>
      <el-table-column label="受访人电话" align="center">
        <template slot-scope="scope">
          {{ scope.row.visiteePhone | matchNull }}
        </template>
      </el-table-column>
      <el-table-column label="受访人单位" align="center">
        <template slot-scope="scope">
          {{ scope.row.visiteeCompany | matchNull }}
        </template>
      </el-table-column>
      <el-table-column label="拜访事由" align="center">
        <template slot-scope="scope">
          {{ scope.row.reason | matchNull }}
        </template>
      </el-table-column>
      <el-table-column label="开始时间" align="center">
        <template slot-scope="scope">
          {{ scope.row.startTime | matchNull }}
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center">
        <template slot-scope="scope">
          {{ scope.row.endTime | matchNull }}
        </template>
      </el-table-column>
      <el-table-column label="来访记录类型" align="center" width="150">
        <template slot-scope="scope">
          <el-tag
            effect="plain"
            type="primary"
            v-if="scope.row.visitingType === 0"
            >访客申请</el-tag
          >
          <el-tag
            effect="plain"
            type="success"
            v-if="scope.row.visitingType === 1"
            >{{
              `${
                scope.row.invitationWay === 1
                  ? " - 短信"
                  : scope.row.invitationWay == 0
                  ? " - 微信"
                  : ""
              }邀请`
            }}</el-tag
          >
        </template>
      </el-table-column>

      <el-table-column label="审核状态" align="center">
        <template slot-scope="scope">
          <!-- {{scope.row.personnelType | personnelType }} {{scope.row.personnelType | personnelType }} {{scope.row.personnelType | personnelType }}-->
          <el-tag
            effect="plain"
            type="primary"
            v-if="scope.row.reviewStatus === 0"
            >待审核</el-tag
          >
          <el-tag effect="plain" type="info" v-if="scope.row.reviewStatus === 1"
            >已拒绝</el-tag
          >
          <el-tag
            effect="plain"
            type="success"
            v-if="scope.row.reviewStatus === 2"
            >已同意</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center">
        <template slot-scope="scope">
          {{ scope.row.createTime | matchNull }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="" label="操作区" :width="120">
        <template slot-scope="scope">
          <!-- <el-button
            type="danger"
            size="small"
            @click="deleteUser(scope.row.id)"
            >删除</el-button
          > -->
          <el-button
            type="primary"
            size="small"
            @click="searchContent(scope.row)"
            >详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!--分页栏-->
    <pagination
      :total="total"
      :page.sync="page.pageNum"
      :limit.sync="page.pageSize"
      @pagination="fetchData"
    />
    <!-- 详情对话框 -->
    <detailsDialog
      :visitToJetList="visitToJetList"
      :row="row"
      :dialogTableVisible="dialogTableVisible"
      @dialog="dialogTableVisible = false"
    />
  </div>
</template>

<script>
import detailsDialog from "@/views/visitors_record/compoment/dialog.vue";

import { getPageListVisitor } from "@/api/ordinary_user";
import { checkRole } from "@/utils/checkRole";
import Pagination from "@/components/Pagination";
import {
  getContext,
  getVisitorListFrom,
  deleteVisitor,
  userVisitorlistExcelOut,
  getDetailPageList,
  getDetailInfo,
} from "@/api/user_manage";

export default {
  components: { Pagination, detailsDialog },
  filters: {
    matchNull(str) {
      if (!str) {
        return "暂无";
      } else {
        return str;
      }
    },
    personnelType(str) {
      return str === 0 ? "访客" : str === 1 ? "同事" : str === 2 ? "亲属" : "";
    },
  },
  data() {
    return {
      // 主表格&主表格加载状态
      list: [],
      listLoading: true,
      downloadLoading: false, //导出加载
      isOut: this.$store.getters.isOut, // 是否可以导出
      placeType: this.$store.getters.placeType, //0普通1预约2楼宇
      // 分页参数
      page: {
        data: {
          username: null,
          name: "",
          phone: "",
          idNumber: "",
          provinceId: null,
          cityId: null,
          areaId: null,
          contents: null,
        },
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      // 账号列表_搜索
      user_list: [],
      //省市县列表_搜索
      province_list_search: [],
      city_list_search: [],
      area_list_search: [],
      // 是否认证_搜索
      status_list: [
        { id: 1, name: "已认证" },
        { id: 2, name: "未认证" },
      ],
      dialogTableVisible: false,
      visitToJetList: {}, //详情
      row: null,
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    // 查询权限
    checkRole,
    // 获取列表数据
    fetchData() {
      this.listLoading = true;
      getDetailPageList(this.page).then((res) => {
        this.list = res.data.list;
        this.total = res.data.total;
        this.listLoading = false;
      });
    },
    // 查询详细信息
    searchContent(row) {
      this.row = row;
      getDetailInfo(row.id).then((res) => {
        this.visitToJetList = res.data;
        this.dialogTableVisible = true;
      });
    },
    // 返回列表序号
    indexMethod(index) {
      return (this.page.pageNum - 1) * this.page.pageSize + index + 1;
    },
    // 导出记录
    download() {
      this.$prompt("请输入导出密码", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        closeOnClickModal: false,
      })
        .then(({ value }) => {
          if (value == "Nqj##361") {
            this.$message({
              type: "success",
              message: "密码正确,正在下载...",
            });
            this.downloadLoading = true;
            userVisitorlistExcelOut(this.page.data).then((res) => {
              window.location.href = res.data;
              this.downloadLoading = false;
            });
          } else {
            this.$message({
              type: "error",
              message: "您输入的密码不正确",
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消导出",
          });
        });
    },
    // 删除
    deleteUser(id) {
      this.$confirm("确定要删除此人员吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      }).then(() => {
        deleteVisitor(id).then((res) => {
          this.fetchData();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        });
      });
    },
  },
};
</script>

<style scoped>
.table {
  margin-top: 15px;
}
.flex {
  display: flex;
  justify-content: flex-start;
  align-items: center;
}
.margin-left {
  margin: 10px 0 0 10px;
}
.margin-left-s {
  margin-left: 15px;
}
.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>

