<template>
  <div>
    <div class="titleBox">
      <div class="title">登录日志</div>
      <div class="btnBox">
        <el-button style="margin-left: 10px" type="info" @click="exportExcel">
          导出
        </el-button>
      </div>
    </div>
    <div class="searchBox" ref="searchBox">
      <el-form :model="listQuery" :inline="true">
        <el-form-item label="登录账号">
          <el-input
            placeholder="请输入登录账号"
            v-model="listQuery.userName"
            clearable
          ></el-input>
        </el-form-item>
        <!-- <el-form-item label="操作人员">
          <el-input placeholder="请输入操作人员" v-model="listQuery.userName" clearable></el-input>
        </el-form-item> -->

        <el-form-item label="状态">
          <el-select
            clearable
            v-model="listQuery.status"
            placeholder="请选择状态"
          >
            <el-option label="正常" value="0"></el-option>
            <el-option label="异常" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="success" @click="handleFilter">查询</el-button>
          <el-button type="success" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="tabBox">
      <el-table
        :data="tableData"
        border
        style="width: 100%"
        :height="tableHeight"
        :key="keyCount"
      >
        <el-table-column
          sortable
          prop="infoId"
          label="访问编号"
        ></el-table-column>
        <el-table-column
          sortable
          prop="browser"
          label="浏览器"
        ></el-table-column>
        <el-table-column
          sortable
          prop="ipaddr"
          label="访问地址"
        ></el-table-column>
        <el-table-column sortable prop="msg" label="操作信息"></el-table-column>
        <el-table-column sortable prop="os" label="操作系统"></el-table-column>
        <el-table-column
          sortable
          prop="userName"
          label="登录账号"
        ></el-table-column>
        <!-- <el-table-column prop="msg" label="操作信息"></el-table-column> -->
        <el-table-column sortable prop="accessTime" label="操作时间">
          <template #default="{ row }">
            {{ $filters.dateFilter(row.accessTime) }}
          </template>
        </el-table-column>

        <el-table-column sortable label="登录状态" width="110px">
          <template #default="{ row }">
            <div>
              <el-tag size="small" type="success" v-if="row.status == 0"
                >正常</el-tag
              >
              <el-tag size="small" type="danger" v-else-if="row.status == 1"
                >异常</el-tag
              >
              <el-tag size="small" type="info" v-else>未知</el-tag>
            </div>
          </template>
        </el-table-column>

        <!-- <el-table-column label="操作" fixed="right" width="120">
          <template #default="{ row }">
            <el-button type="success" size="small" @click="handleDetail(row)">详情</el-button>
            <el-popconfirm
              confirm-button-text="是"
              cancel-button-text="否"
              icon-color="red"
              title="确定要删除?"
              @confirm="delRow(row)"
            >
              <template #reference>
                <el-button type="danger" size="small">删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column> -->
      </el-table>
      <div class="fenye" :style="{ top: tableHeight + 'px' }">
        <el-pagination
          class="pagination"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="listQuery.page"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="listQuery.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
    </div>

    <el-dialog
      custom-class="way"
      v-model="dialogFormVisible"
      @closed="closeDialog"
      :title="isEdit ? '修改' : '新增'"
    >
      <el-form :model="dialogForm">
        <el-form-item label="部门名称" label-width="140px">
          <el-input v-model="dialogForm.orgName"></el-input>
        </el-form-item>
        <el-form-item label="部门全称" label-width="140px">
          <el-input v-model="dialogForm.orgQc"></el-input>
        </el-form-item>
        <el-form-item label="部门简称" label-width="140px">
          <el-input v-model="dialogForm.orgJc"></el-input>
        </el-form-item>
        <el-form-item label="部门类型" label-width="140px">
          <el-input v-model="dialogForm.orgType"></el-input>
        </el-form-item>
        <el-form-item label="部门代码" label-width="140px">
          <el-input v-model="dialogForm.orgCode"></el-input>
        </el-form-item>
        <el-form-item label="部门等级" label-width="140px">
          <el-input-number
            v-model="dialogForm.orgLevel"
            class="mx-4"
            :min="1"
            :max="100"
            controls-position="right"
          />
        </el-form-item>
        <el-form-item label="部门顺序号" label-width="140px">
          <el-input-number
            v-model="dialogForm.orgNo"
            class="mx-4"
            :min="1"
            :max="100"
            controls-position="right"
          />
        </el-form-item>

        <el-form-item label="部门电话" label-width="140px">
          <el-input v-model="dialogForm.linkTel"></el-input>
        </el-form-item>
        <el-form-item label="部门业务类型" label-width="140px">
          <el-input v-model="dialogForm.orgBizType"></el-input>
        </el-form-item>
        <el-form-item label="部门联系人电话" label-width="140px">
          <el-input v-model="dialogForm.linkManTel"></el-input>
        </el-form-item>
        <el-form-item label="部门联系人" label-width="140px">
          <el-input v-model="dialogForm.linkMan"></el-input>
        </el-form-item>
        <el-form-item label="部门邮箱" label-width="140px">
          <el-input v-model="dialogForm.email"></el-input>
        </el-form-item>
        <el-form-item label="部门名称拼音" label-width="140px">
          <el-input v-model="dialogForm.bmpyszm"></el-input>
        </el-form-item>

        <el-form-item label="部门地址" label-width="140px">
          <el-input v-model="dialogForm.address"></el-input>
        </el-form-item>
        <el-form-item label="部门主页" label-width="140px">
          <el-input v-model="dialogForm.webUrl"></el-input>
        </el-form-item>
        <el-form-item label="部门所属行政区划" label-width="140px">
          <el-input v-model="dialogForm.xzqh"></el-input>
        </el-form-item>

        <!-- <el-form-item label="状态" label-width="140px">
          <el-select v-model="dialogForm.zdywlb" placeholder="请选择业务类别">
            <el-option label="正常" value="0"></el-option>
            <el-option label="注销" value="1"></el-option>
          </el-select>
        </el-form-item>-->
        <el-form-item label="备注" label-width="140px">
          <el-input
            readonly
            v-model="dialogForm.bz"
            :autosize="{ minRows: 2, maxRows: 4 }"
            type="textarea"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" v-if="isEdit" @click="onSave"
            >保存</el-button
          >
          <el-button type="primary" v-else @click="onAdd">新增</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { download } from "@/utils/request";
import { ElMessage } from "element-plus";
import { ref, reactive, onMounted,onUnmounted,getCurrentInstance } from "vue";
import {
  getSysLoginLogList,
  sysLogRemove,
  LoginlogClean,
  sysLoginogDetail
} from "@/api/user-manage";
const { proxy } = getCurrentInstance();
const searchBox = ref(null); // 搜索盒子
const tableHeight = ref(); // 表格高度
const keyCount = ref(0); //tabel组件刷新值
//查询参数
const total = ref(0);
const page = ref(1);
const size = ref(20);
const currentRow = ref({});
const listQuery = ref({
  page: 1,
  size: 20
});
const isEdit = ref(true);
const dialogForm = ref({});

// 数据相关
const tableData = ref([]);
const dialogFormVisible = ref(false);
const formLabelWidth = "140px";
// 获取数据的方法
const getListData = async () => {
  const params = listQuery.value;
  params.current = params.page;
  const res = await getSysLoginLogList(params);
  tableData.value = res?.records;
  total.value = Number(res.total);
};

const handleFilter = () => {
  listQuery.value.page = 1;
  getListData();
};
getListData();

// 分页相关
/**
 * size 改变触发
 */
const handleSizeChange = (currentSize) => {
  listQuery.value.size = currentSize;
  getListData();
};

/**
 * 页码改变触发
 */
const handleCurrentChange = (currentPage) => {
  listQuery.value.page = currentPage;
  getListData();
};

/**修改字典 */
const updateDict = (row) => {
  isEdit.value = true;
  dialogForm.value = { ...row };
  dialogFormVisible.value = true;
};
const handleDetail = (row) => {
  dialogForm.value = { ...row };
  dialogFormVisible.value = true;
};
const addRole = (row) => {
  isEdit.value = false;
  dialogForm.value = {};
  dialogFormVisible.value = true;
};
const onSave = () => {
  updateSysRole({
    ...dialogForm.value
  }).then((res) => {
    dialogFormVisible.value = false;
    ElMessage.success("修改成功");
    handleFilter();
  });
};

// 导出
function exportExcel() {
  proxy.$confirm(`当前数据${total.value}条,确定是否导出全部数据?`, "警告", {type: "warning"}).then(() => {
     download('/mosty-base/other/exportLoginLog',listQuery.value , `人员核查日志_${new Date().getTime()}.xlsx`)
  }).catch(() => {
    proxy.$message.info("已取消");
  });
}

const onAdd = () => {
  addSysRole({
    ...dialogForm.value
  }).then((res) => {
    dialogFormVisible.value = false;
    ElMessage.success("新增成功");
    handleFilter();
  });
};
const clearAll = async () => {
  const res = await LoginlogClean();
  ElMessage.success("删除成功");
  handleFilter();
};
const delRow = (row) => {
  sysLogRemove({
    ids: [row.infoId]
  }).then((res) => {
    ElMessage.success("删除成功");
    handleFilter();
  });
};
function reset() {
  listQuery.value = {};
  listQuery.value.page = 1;
  listQuery.value.size = 20;
  getListData();
}
const closeDialog = () => {
  dialogForm.value = {};
  dialogFormVisible.value = false;
};
// 表格高度计算
const tabHeightFn = () => {
  tableHeight.value = window.innerHeight - searchBox.value.offsetHeight - 240;
};
onMounted(() => {
  tabHeightFn();
  window.onresize = function () {
    tabHeightFn();
  };
  proxy.mittBus.on("mittFn", (data) => {
    keyCount.value = data;
  });
});
onUnmounted(() => {
  proxy.mittBus.off("mittFn");
});
</script>

<style lang="scss" scoped>
@import "@/assets/css/element-plus.scss";
.user-manage-container {
  .table-header-wrap {
    margin-bottom: 22px;
  }

  ::v-deep .avatar {
    width: 60px;
    height: 60px;
    border-radius: 50%;
  }

  ::v-deep .el-tag {
    margin-right: 6px;
  }

  .pagination {
    margin-top: 20px;
  }
}
</style>
