<template>
  <div class="app-container">
    <el-tabs v-model="activeTab">
      <!-- 访客登记信息标签页 -->
      <el-tab-pane label="访客登记" name="visitors">
        <el-form :model="visitorQuery" ref="visitorQueryRef" :inline="true" label-width="80px">
          <el-form-item label="访客姓名" prop="visitorName">
            <el-input v-model="visitorQuery.visitorName" placeholder="请输入访客姓名" clearable />
          </el-form-item>
          <el-form-item label="被访人" prop="visitedPerson">
            <el-input v-model="visitorQuery.visitedPerson" placeholder="请输入被访人" clearable />
          </el-form-item>
          <el-form-item label="状态" prop="visitStatus">
            <el-select v-model="visitorQuery.visitStatus" placeholder="请选择状态" clearable  style="width: 180px;">
              <el-option label="预约" :value="0" />
              <el-option label="已到达" :value="1" />
              <el-option label="访问中" :value="2" />
              <el-option label="已离开" :value="3" />
              <el-option label="过期" :value="4" />
            </el-select>
          </el-form-item>
          <el-form-item label="时间范围" prop="timeRange">
            <el-date-picker
                v-model="visitorQuery.timeRange"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="YYYY-MM-DD"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="getVisitorList">搜索</el-button>
            <el-button icon="Refresh" @click="resetVisitorQuery">重置</el-button>
          </el-form-item>
        </el-form>

<!--        <el-row :gutter="10" class="mb8">-->
<!--          <el-col :span="1.5">-->
<!--            <el-button-->
<!--                type="warning"-->
<!--                plain-->
<!--                icon="Download"-->
<!--                @click="handleExportVisitor"-->
<!--                v-hasPermi="['system:visitor:export']"-->
<!--            >导出</el-button>-->
<!--          </el-col>-->
<!--        </el-row>-->

        <el-table v-loading="visitorLoading" :data="visitorList" stripe border>
          <el-table-column label="访客ID" prop="visitor_id"  />
          <el-table-column label="访客姓名" prop="visitor_name"  />
          <el-table-column label="电话" prop="visitor_phone"  />
          <el-table-column label="单位" prop="company" />
          <el-table-column label="访问目的" prop="visit_purpose"  />
          <el-table-column label="被访人" prop="visited_person"  />
          <el-table-column label="访问时间" >
            <template #default="scope">
              <div>预约: {{ parseTime(scope.row.visit_start_time) }} ~ {{ parseTime(scope.row.visit_end_time) }}</div>
              <div v-if="scope.row.actual_start_time">实际: {{ parseTime(scope.row.actual_start_time) }} ~ {{ parseTime(scope.row.actual_end_time) }}</div>
            </template>
          </el-table-column>
          <el-table-column label="状态" prop="visit_status" >
            <template #default="scope">
              <el-tag :type="statusTagType(scope.row.visit_status)">
                {{ statusText(scope.row.visit_status) }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作"  fixed="right">
            <template #default="scope">
              <el-button link type="primary" icon="View" @click="handleVisitorDetail(scope.row)">详情</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
            v-show="visitorTotal > 0"
            :total="visitorTotal"
            v-model:page="visitorQuery.pageNum"
            v-model:limit="visitorQuery.pageSize"
            @pagination="getVisitorList"
        />
      </el-tab-pane>

      <!-- 刷卡记录标签页 -->
      <el-tab-pane label="刷卡记录" name="access">
        <el-form :model="accessQuery" ref="accessQueryRef" :inline="true" label-width="80px">
          <el-form-item label="访客ID" prop="visitorId">
            <el-input v-model="accessQuery.visitorId" placeholder="请输入访客ID" clearable />
          </el-form-item>
          <el-form-item label="访客姓名" prop="visitorName">
            <el-input v-model="accessQuery.visitorName" placeholder="请输入访客姓名" clearable />
          </el-form-item>
          <el-form-item label="门禁位置" prop="gateLocation">
            <el-input v-model="accessQuery.gateLocation" placeholder="请输入门禁位置" clearable />
          </el-form-item>
          <el-form-item label="刷卡类型" prop="swipeType">
            <el-select v-model="accessQuery.swipeType" placeholder="请选择类型" clearable style="width: 180px;">
              <el-option label="进入" :value="1" />
              <el-option label="离开" :value="2" />
            </el-select>
          </el-form-item>
          <el-form-item label="时间范围" prop="timeRange">
            <el-date-picker
                v-model="accessQuery.timeRange"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="YYYY-MM-DD"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="getAccessList">搜索</el-button>
            <el-button icon="Refresh" @click="resetAccessQuery">重置</el-button>
          </el-form-item>
        </el-form>

<!--        <el-row :gutter="10" class="mb8">-->
<!--          <el-col :span="1.5">-->
<!--            <el-button-->
<!--                type="warning"-->
<!--                plain-->
<!--                icon="Download"-->
<!--                @click="handleExportAccess"-->
<!--                v-hasPermi="['system:access:export']"-->
<!--            >导出</el-button>-->
<!--          </el-col>-->
<!--        </el-row>-->

        <el-table v-loading="accessLoading" :data="accessList" stripe border>
          <el-table-column label="记录ID" prop="record_id" />
          <el-table-column label="访客姓名" prop="visitor_name"  />
          <el-table-column label="卡号" prop="card_no"  />
          <el-table-column label="门禁名称" prop="gate_name"  />
          <el-table-column label="门禁位置" prop="gate_location"  />
          <el-table-column label="刷卡时间" prop="swipe_time" >
            <template #default="scope">
              <span>{{ parseTime(scope.row.swipe_time) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="类型" prop="swipe_type" >
            <template #default="scope">
              <el-tag :type="scope.row.swipe_type === 1 ? 'success' : 'danger'">
                {{ scope.row.swipe_type === 1 ? '进入' : '离开' }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="结果" prop="swipe_result" >
            <template #default="scope">
              <el-tag :type="scope.row.swipe_result === 1 ? 'success' : 'danger'">
                {{ scope.row.swipe_result === 1 ? '成功' : '失败' }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="体温" prop="temperature"  />
          <el-table-column label="口罩" prop="mask_detection" >
            <template #default="scope">
              <el-tag :type="scope.row.mask_detection === 1 ? 'success' : 'danger'">
                {{ scope.row.mask_detection === 1 ? '已佩戴' : '未佩戴' }}
              </el-tag>
            </template>
          </el-table-column>
        </el-table>

        <pagination
            v-show="accessTotal > 0"
            :total="accessTotal"
            v-model:page="accessQuery.pageNum"
            v-model:limit="accessQuery.pageSize"
            @pagination="getAccessList"
        />
      </el-tab-pane>
    </el-tabs>

    <!-- 访客详情抽屉 -->
    <el-drawer
        v-model="detailVisible"
        title="访客详情"
        direction="rtl"
        size="40%"
    >
      <el-descriptions :column="2" border>
        <el-descriptions-item label="访客ID">{{ currentVisitor.visitor_id }}</el-descriptions-item>
        <el-descriptions-item label="姓名">{{ currentVisitor.visitor_name }}</el-descriptions-item>
        <el-descriptions-item label="电话">{{ currentVisitor.visitor_phone }}</el-descriptions-item>
        <el-descriptions-item label="身份证">{{ currentVisitor.id_card }}</el-descriptions-item>
        <el-descriptions-item label="单位">{{ currentVisitor.company }}</el-descriptions-item>
        <el-descriptions-item label="访问目的">{{ currentVisitor.visit_purpose }}</el-descriptions-item>
        <el-descriptions-item label="被访人">{{ currentVisitor.visited_person }}</el-descriptions-item>
        <el-descriptions-item label="部门">{{ currentVisitor.visited_dept }}</el-descriptions-item>
        <el-descriptions-item label="被访电话">{{ currentVisitor.visited_phone }}</el-descriptions-item>
        <el-descriptions-item label="预约时间">
          {{ parseTime(currentVisitor.appointment_time) }}
        </el-descriptions-item>
        <el-descriptions-item label="访问时间">
          {{ parseTime(currentVisitor.visit_start_time) }} ~ {{ parseTime(currentVisitor.visit_end_time) }}
        </el-descriptions-item>
        <el-descriptions-item label="实际时间" v-if="currentVisitor.actual_start_time">
          {{ parseTime(currentVisitor.actual_start_time) }} ~ {{ parseTime(currentVisitor.actual_end_time) }}
        </el-descriptions-item>
        <el-descriptions-item label="状态">
          <el-tag :type="statusTagType(currentVisitor.visit_status)">
            {{ statusText(currentVisitor.visit_status) }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="卡号">{{ currentVisitor.card_no }}</el-descriptions-item>
        <el-descriptions-item label="入场门禁">{{ currentVisitor.entry_gate }}</el-descriptions-item>
        <el-descriptions-item label="出场门禁">{{ currentVisitor.exit_gate }}</el-descriptions-item>
        <el-descriptions-item label="审批状态">
          <el-tag :type="currentVisitor.approval_status === 1 ? 'success' : currentVisitor.approval_status === 2 ? 'danger' : 'info'">
            {{ approvalText(currentVisitor.approval_status) }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="审批人">{{ currentVisitor.approver }}</el-descriptions-item>
        <el-descriptions-item label="审批时间">
          {{ parseTime(currentVisitor.approval_time) }}
        </el-descriptions-item>
      </el-descriptions>
    </el-drawer>
  </div>
</template>

<script setup>
import { ref, reactive, watch } from 'vue'
import { listVisitorInfo, exportVisitorInfo } from '@/api/subsystem/visitor'
import { listAccessRecord, exportAccessRecord } from '@/api/subsystem/visitor'

const { proxy } = getCurrentInstance()
const activeTab = ref('visitors')

// 监听activeTab的变化
watch(activeTab, (newVal) => {
  if (newVal === 'access') {
    // 当切换到刷卡记录标签页时，初始化并加载数据
    resetAccessQuery()
  }
})

// 访客登记查询相关
const visitorQuery = reactive({
  pageNum: 1,
  pageSize: 10,
  visitorName: null,
  visitedPerson: null,
  visitStatus: null,
  timeRange: []
})
const visitorList = ref([])
const visitorTotal = ref(0)
const visitorLoading = ref(false)

// 刷卡记录查询相关
const accessQuery = reactive({
  pageNum: 1,
  pageSize: 10,
  visitorId: null,
  visitorName: null,
  gateLocation: null,
  swipeType: null,
  timeRange: []
})
const accessList = ref([])
const accessTotal = ref(0)
const accessLoading = ref(false)

// 访客详情相关
const detailVisible = ref(false)
const currentVisitor = ref({})

// 状态标签类型
const statusTagType = (status) => {
  const types = ['info', 'success', 'warning', 'danger', 'danger']
  return types[status] || 'info'
}

// 状态文本
const statusText = (status) => {
  const texts = ['预约', '已到达', '访问中', '已离开', '过期']
  return texts[status] || '未知'
}

// 审批状态文本
const approvalText = (status) => {
  const texts = ['待审批', '已通过', '已拒绝']
  return texts[status] || '未知'
}

// 查询访客登记列表
function getVisitorList() {
  visitorLoading.value = true
  const params = {
    ...visitorQuery,
    pageNum: visitorQuery.pageNum,
    pageSize: visitorQuery.pageSize
  }
  if (visitorQuery.timeRange && visitorQuery.timeRange.length === 2) {
    params.beginTime = visitorQuery.timeRange[0]
    params.endTime = visitorQuery.timeRange[1]
  }
  listVisitorInfo(params).then(response => {
    visitorList.value = response.rows
    visitorTotal.value = response.total
    visitorLoading.value = false
  })
}

// 重置访客登记查询
function resetVisitorQuery() {
  proxy.resetForm('visitorQueryRef')
  visitorQuery.pageNum = 1
  getVisitorList()
}

// 导出访客登记
function handleExportVisitor() {
  const params = {
    ...visitorQuery,
    pageNum: undefined,
    pageSize: undefined
  }
  if (visitorQuery.timeRange && visitorQuery.timeRange.length === 2) {
    params.beginTime = visitorQuery.timeRange[0]
    params.endTime = visitorQuery.timeRange[1]
  }
  proxy.$modal.confirm('确认导出访客登记信息吗？').then(() => {
    exportVisitorInfo(params).then(response => {
      proxy.download(response.msg)
    })
  })
}

// 查询刷卡记录列表
function getAccessList() {
  accessLoading.value = true
  const params = {
    ...accessQuery,
    pageNum: accessQuery.pageNum,
    pageSize: accessQuery.pageSize
  }
  if (accessQuery.timeRange && accessQuery.timeRange.length === 2) {
    params.beginTime = accessQuery.timeRange[0]
    params.endTime = accessQuery.timeRange[1]
  }
  listAccessRecord(params).then(response => {
    accessList.value = response.rows
    accessTotal.value = response.total
    accessLoading.value = false
  })
}

// 重置刷卡记录查询
function resetAccessQuery() {
  proxy.resetForm('accessQueryRef')
  accessQuery.pageNum = 1
  getAccessList()
}

// 导出门禁刷卡记录
function handleExportAccess() {
  const params = {
    ...accessQuery,
    pageNum: undefined,
    pageSize: undefined
  }
  if (accessQuery.timeRange && accessQuery.timeRange.length === 2) {
    params.beginTime = accessQuery.timeRange[0]
    params.endTime = accessQuery.timeRange[1]
  }
  proxy.$modal.confirm('确认导出门禁刷卡记录吗？').then(() => {
    exportAccessRecord(params).then(response => {
      proxy.download(response.msg)
    })
  })
}

// 查看访客详情
function handleVisitorDetail(row) {
  currentVisitor.value = row
  detailVisible.value = true
}

// 初始化访客列表
getVisitorList()
</script>

<style scoped>
.el-descriptions {
  margin: 20px;
}
</style>
