<template>
  <div class="app-container">
    <el-row :gutter="20">
        <el-col >
            <!-- 查询 -->
            <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
               <el-form-item label="创建时间" style="width: 308px;">
                  <el-date-picker
                     v-model="dateRange"
                     value-format="YYYY-MM-DD"
                     type="daterange"
                     range-separator="-"
                     start-placeholder="开始日期"
                     end-placeholder="结束日期"
                  ></el-date-picker>
               </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-table v-loading="loading" :data="userList" >
               <!-- 序号 -->
               <el-table-column
                type="index"
                label="序号"
                width="60"
                align="center"
                :index="(index) => (queryParams.pageNum - 1) * queryParams.pageSize + index + 1"
                />
               <el-table-column label="用户名" align="center" key="yonghuming" prop="yonghuming" />
               <el-table-column label="学校名称" align="center" key="xuexiaomingcheng" prop="xuexiaomingcheng" />
               <el-table-column label="课件名称" align="center" key="kejianmingcheng" prop="kejianmingcheng" />
               <el-table-column label="异常行为" align="center" key="yichangxingwei" prop="yichangxingwei" />
               <el-table-column
                  label="报警时间"
                  align="center"
                  key="createdAt"
                  prop="createdAt"
                >
                  <template #default="scope">
                    <span>{{ moment(scope.row.createdAt).format("YYYY-MM-DD HH:mm:ss") }}</span>
                  </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-col>`
    </el-row>
  </div>
</template>

<script setup>
import { getToken } from "@/utils/auth";
import { callCloudFunction } from "@/api/cloud";
import moment from "moment";

const { proxy } = getCurrentInstance();
const { sys_normal_disable, sys_user_sex } = proxy.useDict("sys_normal_disable", "sys_user_sex");

const userList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const single = ref(true);
const total = ref(0);
const dateRange = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    startTime: null,
    endTime: null,
    // deptId: undefined
  },
  rules: {
    userName: [{ required: true, message: "用户名称不能为空", trigger: "blur" }, { min: 2, max: 20, message: "用户名称长度必须介于 2 和 20 之间", trigger: "blur" }],
    nickName: [{ required: true, message: "用户昵称不能为空", trigger: "blur" }],
    password: [{ required: true, message: "用户密码不能为空", trigger: "blur" }, { min: 5, max: 20, message: "用户密码长度必须介于 5 和 20 之间", trigger: "blur" }, { pattern: /^[^<>"'|\\]+$/, message: "不能包含非法字符：< > \" ' \\\ |", trigger: "blur" }],
    email: [{ type: "email", message: "请输入正确的邮箱地址", trigger: ["blur", "change"] }],
    phonenumber: [{ required: true, message: "手机号不能为空", trigger: "blur" },{ pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/, message: "请输入正确的手机号码", trigger: "blur" }]
  }
});

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

/** 查询用户列表 */
async function getList() {
  loading.value = true;
  const res = await callCloudFunction("database", {
    page: "yichangxingweiyujing",
    action: "findBehaviorAlarmPage",
    data: { ...queryParams.value },
  });
  loading.value = false;
  userList.value = res.records;
  total.value = res.total;
};

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  if (dateRange.value && dateRange.value.length === 2) {
    // 转换成时间戳（毫秒）
    queryParams.value.startTime = new Date(dateRange.value[0]).getTime();
    queryParams.value.endTime = new Date(dateRange.value[1]).getTime();
  } else {
    queryParams.value.startTime = null;
    queryParams.value.endTime = null;
  }
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  queryParams.value.startTime = null;
  queryParams.value.endTime = null;
  handleQuery();
}

getList();
</script>

<style lang='scss' scoped>
</style>