<template>
  <div class="p-2 h-full flex flex-col">
    <Pageheader title="访客信息"></Pageheader>
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="访客状态" prop="status">
              <el-select v-model="queryParams.status" placeholder="请选择访客状态" clearable>
                <el-option v-for="dict in com_visitor_status" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="访客姓名" prop="visitorName">
              <el-input v-model="queryParams.visitorName" placeholder="请输入访客姓名" clearable @keyup.enter="handleQuery" />
            </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-card>
      </div>
    </transition>

    <el-card class="flex-1" shadow="never">
      <el-table v-loading="loading" border :data="visitorList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="访问房屋" align="center" prop="houseName" />
        <el-table-column label="访客姓名" align="center" prop="visitorName" />
        <el-table-column label="访客状态" align="center" prop="status">
          <template #default="scope">
            <dict-tag :options="com_visitor_status" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column label="访客性别" align="center" prop="visitorGender">
          <template #default="scope">
            <dict-tag :options="sys_user_sex" :value="scope.row.visitorGender" />
          </template>
        </el-table-column>
        <el-table-column label="访客联系方式" align="center" prop="visitorPhone" />
        <el-table-column label="来访时间" align="center" prop="startTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" @click="handleMain(scope.row)" v-hasPermi="['repair:main:visitor']">访客详情</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-card>
  </div>
</template>

<script setup name="Visitor" lang="ts">
import { listVisitor } from '@/api/system/Visitor/index';
import { VisitorVO, VisitorQuery, VisitorForm } from '@/api/system/Visitor/type';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { sys_user_sex } = toRefs<any>(proxy?.useDict('sys_user_sex'));
const { com_visitor_status } = toRefs<any>(proxy?.useDict('com_visitor_status'));
const router = useRouter();

// com_visitor_status
const visitorList = ref<VisitorVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const queryFormRef = ref<ElFormInstance>();
const visitorFormRef = ref<ElFormInstance>();

const initFormData: VisitorForm = {
  id: undefined,
  houseId: undefined,
  visitorName: undefined,
  visitorGender: undefined,
  visitorPhone: undefined,
  carNum: undefined,
  visitorNum: undefined,
  startTime: undefined,
  endTime: undefined,
  type: undefined,
  status: undefined,
  createTime: undefined,
  createId: undefined
};
const data = reactive<PageData<VisitorForm, VisitorQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    houseId: undefined,
    visitorName: undefined,
    visitorGender: undefined,
    visitorPhone: undefined,
    carNum: undefined,
    visitorNum: undefined,
    startTime: undefined,
    endTime: undefined,
    type: undefined,
    status: undefined,
    params: {}
  },
  rules: {
    id: [{ required: true, message: '访客管理表id不能为空', trigger: 'blur' }]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询访客管理列表 */
const getList = async () => {
  loading.value = true;
  const res = await listVisitor(queryParams.value);
  visitorList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: VisitorVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 删除按钮操作 */
const handleMain = async (row?: VisitorVO) => {
  router.push({
    path: '/repair/visitorMain',
    query: {
      id: row.id
    }
  });
};

onMounted(() => {
  getList();
});
</script>
<style lang="scss" scoped>
.el-table {
  height: calc(100% - 80px);
}
</style>
