<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="访客姓名" prop="visitorName">
        <el-input
          v-model="queryParams.visitorName"
          placeholder="请输入访客姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="idCardNo">
        <el-input
          v-model="queryParams.idCardNo"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="到访类型" prop="visitType">
        <el-select v-model="queryParams.visitType" placeholder="请选择到访类型" clearable style="width: 180px">
          <el-option
            v-for="dict in visitor_visit_type"
            :key="dict.value"
            :label="dict.label"
            :value="String(dict.value)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="登记方式" prop="registerType">
        <el-select v-model="queryParams.registerType" placeholder="请选择登记方式" clearable style="width: 180px">
          <el-option
            v-for="dict in visitor_register_type"
            :key="dict.value"
            :label="dict.label"
            :value="String(dict.value)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="被访对象" prop="visitedPerson">
        <el-input
          v-model="queryParams.visitedPerson"
          placeholder="请输入被访对象"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="来访时间" prop="visitTimeRange">
        <el-date-picker
          v-model="visitTimeRange"
          type="datetimerange"
          value-format="YYYY-MM-DD HH:mm:ss"
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          clearable
          style="width: 360px"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</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="handleExport"
          v-hasPermi="['base:visitorRecord:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="visitorRecordList">
      <el-table-column label="访客姓名" align="center" prop="visitorName" min-width="120" show-overflow-tooltip />
      <el-table-column label="手机号" align="center" prop="mobile" width="140" />
      <el-table-column label="身份证号" align="center" prop="idCardNo" min-width="180" show-overflow-tooltip />
      <el-table-column label="访客照片" align="center" prop="visitorPhoto" width="100">
        <template #default="scope">
          <image-preview
            v-if="scope.row.visitorPhoto"
            :src="scope.row.visitorPhoto"
            :width="44"
            :height="44"
          />
          <el-tag v-else type="info">无照片</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="到访类型" align="center" prop="visitType" width="120">
        <template #default="scope">
          <dict-tag :options="visitor_visit_type" :value="String(scope.row.visitType)" />
        </template>
      </el-table-column>
      <el-table-column label="登记方式" align="center" prop="registerType" width="140">
        <template #default="scope">
          <dict-tag :options="visitor_register_type" :value="String(scope.row.registerType)" />
        </template>
      </el-table-column>
      <el-table-column label="访客来源" align="center" prop="visitorSource" min-width="140" show-overflow-tooltip />
      <el-table-column label="来访事由" align="center" prop="visitReason" min-width="160" show-overflow-tooltip />
      <el-table-column label="预约单号" align="center" prop="appointmentNo" min-width="150" show-overflow-tooltip />
      <el-table-column label="被访对象" align="center" prop="visitedPerson" min-width="120" show-overflow-tooltip />
      <el-table-column label="来访时间" align="center" prop="visitTime" width="170">
        <template #default="scope">
          <span>{{ parseTime(scope.row.visitTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="100" fixed="right" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="handleDetail(scope.row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 访客记录详情 -->
    <el-dialog :title="title" v-model="open" width="760px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="访客姓名">{{ form.visitorName || "-" }}</el-descriptions-item>
        <el-descriptions-item label="手机号">{{ form.mobile || "-" }}</el-descriptions-item>
        <el-descriptions-item label="身份证号">{{ form.idCardNo || "-" }}</el-descriptions-item>
        <el-descriptions-item label="到访类型">
          <dict-tag :options="visitor_visit_type" :value="String(form.visitType)" />
        </el-descriptions-item>
        <el-descriptions-item label="登记方式">
          <dict-tag :options="visitor_register_type" :value="String(form.registerType)" />
        </el-descriptions-item>
        <el-descriptions-item label="访客来源">{{ form.visitorSource || "-" }}</el-descriptions-item>
        <el-descriptions-item label="来访事由">{{ form.visitReason || "-" }}</el-descriptions-item>
        <el-descriptions-item label="预约单号">{{ form.appointmentNo || "-" }}</el-descriptions-item>
        <el-descriptions-item label="被访对象">{{ form.visitedPerson || "-" }}</el-descriptions-item>
        <el-descriptions-item label="来访时间">{{ parseTime(form.visitTime, "{y}-{m}-{d} {h}:{i}:{s}") || "-" }}</el-descriptions-item>
        <el-descriptions-item label="备注" :span="2">{{ form.remark || "-" }}</el-descriptions-item>
        <el-descriptions-item label="访客照片" :span="2">
          <image-preview
            v-if="form.visitorPhoto"
            :src="form.visitorPhoto"
            :width="120"
            :height="120"
          />
          <span v-else>-</span>
        </el-descriptions-item>
      </el-descriptions>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="open = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="VisitorRecord">
import { listVisitorRecord, getVisitorRecord } from "@/api/base/visitorRecord"

const { proxy } = getCurrentInstance()
const { visitor_register_type, visitor_visit_type } = useDict('visitor_register_type', 'visitor_visit_type')

const visitorRecordList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const total = ref(0)
const title = ref("")
const visitTimeRange = ref([])

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    visitorName: undefined,
    mobile: undefined,
    idCardNo: undefined,
    visitType: undefined,
    registerType: undefined,
    visitedPerson: undefined,
    visitTimeStart: undefined,
    visitTimeEnd: undefined
  }
})

const { queryParams, form } = toRefs(data)

/** 查询访客登记记录列表 */
function getList() {
  loading.value = true
  listVisitorRecord(queryParams.value).then(response => {
    visitorRecordList.value = response.rows || []
    total.value = response.total || 0
    loading.value = false
  })
}

/** 表单重置 */
function reset() {
  form.value = {
    id: null,
    visitorName: null,
    mobile: null,
    idCardNo: null,
    visitType: null,
    registerType: null,
    visitorSource: null,
    visitReason: null,
    visitorPhoto: null,
    appointmentNo: null,
    visitedPerson: null,
    visitTime: null,
    remark: null,
    createTime: null,
    updateTime: null
  }
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  if (visitTimeRange.value && visitTimeRange.value.length === 2) {
    queryParams.value.visitTimeStart = visitTimeRange.value[0]
    queryParams.value.visitTimeEnd = visitTimeRange.value[1]
  } else {
    queryParams.value.visitTimeStart = undefined
    queryParams.value.visitTimeEnd = undefined
  }
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  visitTimeRange.value = []
  queryParams.value.visitTimeStart = undefined
  queryParams.value.visitTimeEnd = undefined
  proxy.resetForm("queryRef")
  handleQuery()
}

/** 查看详情 */
function handleDetail(row) {
  reset()
  const _id = row.id
  getVisitorRecord(_id).then(response => {
    const _data = response.data || {}
    form.value = {
      ..._data,
      visitType: _data.visitType != null ? String(_data.visitType) : null,
      registerType: _data.registerType != null ? String(_data.registerType) : null
    }
    open.value = true
    title.value = "访客记录详情"
  })
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('base/visitorRecord/export', {
    ...queryParams.value
  }, `访客记录_${new Date().getTime()}.xlsx`)
}

getList()
</script>

<style scoped>
.app-container {
  padding: 20px;
}
:deep(.el-table .cell) {
  line-height: 22px;
}
</style>
