<template>
  <div class="app-container">
    <el-form ref="queryRef" :inline="true" :model="queryParams">
      <el-form-item label="用户名称" prop="username">
        <el-input
          v-model="queryParams.username"
          clearable
          placeholder="请输入用户名称"
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户姓名" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          clearable
          placeholder="请输入用户姓名"
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="IP地址" prop="ip">
        <el-input
          v-model="queryParams.ip"
          clearable
          placeholder="请输入IP"
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" style="width: 308px">
        <el-date-picker
          v-model="dateRange"
          end-placeholder="结束日期"
          range-separator="-"
          start-placeholder="开始日期"
          type="daterange"
          value-format="YYYY-MM-DD"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button icon="Search" type="primary" @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
          v-hasPermi="['monitor:login-log:remove']"
          :disabled="multiple"
          icon="Delete"
          plain
          type="danger"
          @click="handleClean"
          >清空
        </el-button>
        <el-button
          v-hasPermi="['monitor:login-log:export']"
          icon="Download"
          plain
          type="warning"
          @click="handleExport"
          >导出
        </el-button>
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        :columns="columns"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="logList" style="width: 100%">
      <el-table-column align="center" label="序号" type="index" width="100">
        <template #default="scope">
          <span>{{
            (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :show-overflow-tooltip="true"
        align="center"
        label="会话编号"
        prop="deviceSn"
      />
      <el-table-column
        :show-overflow-tooltip="true"
        align="center"
        label="登录名称"
        prop="username"
      />
      <el-table-column
        :show-overflow-tooltip="true"
        align="center"
        label="用户姓名"
        prop="nickname"
      />
      <el-table-column
        :show-overflow-tooltip="true"
        align="center"
        label="IP"
        prop="ip"
      />
      <el-table-column
        :show-overflow-tooltip="true"
        align="center"
        label="登录地点"
        prop="address"
      />
      <el-table-column
        :show-overflow-tooltip="true"
        align="center"
        label="操作系统"
        prop="os"
      />
      <el-table-column
        :show-overflow-tooltip="true"
        align="center"
        label="浏览器"
        prop="browser"
      />
      <el-table-column
        align="center"
        label="登录时间"
        prop="loginTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.loginTime) }}</span>
        </template>
      </el-table-column>
    </el-table>

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

<script name="LoginLog" setup>
import { cleanLoginLog, list as initData } from "@/api/monitor/login_log";

const { proxy } = getCurrentInstance();

const logList = ref([]);
const loading = ref(true);
const total = ref(0);
const pageNum = ref(1);
const pageSize = ref(10);
const dateRange = ref([]);
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  username: undefined,
  nickname: undefined,
  ip: undefined,
});

/** 清空按钮操作 */
function handleClean() {
  proxy.$modal
    .confirm("是否确认清空所有登录日志数据项?")
    .then(function () {
      return cleanLoginLog();
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("清空成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "/monitor/login-log/export",
    {
      ...queryParams.value,
    },
    `login_log_${new Date().getTime()}.xlsx`
  );
}

/** 查询登录日志列表 */
function getList() {
  loading.value = true;
  initData(proxy.addDateRange(queryParams.value, dateRange.value)).then(
    (response) => {
      logList.value = response.rows;
      total.value = response.total;
      loading.value = false;
    }
  );
}

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

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}
getList();
</script>
