<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="业务域" prop="bizCode">
        <el-select v-model="queryParams.bizCode" placeholder="全部" clearable style="width: 200px">
          <el-option v-for="item in bizOptions" :key="item.bizCode" :label="item.bizName" :value="item.bizCode" />
        </el-select>
      </el-form-item>
      <el-form-item label="日志时间">
        <el-date-picker v-model="dateRange" style="width: 240px" value-format="YYYY-MM-DD" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" />
      </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="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['gx:log:remove']">删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="logList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="业务编码" prop="bizCode" width="160" />
      <el-table-column label="业务名称" prop="bizName" width="180" :show-overflow-tooltip="true" />
      <el-table-column label="日志时间" prop="logTime" width="170" align="center" />
      <el-table-column label="数据条数" prop="dnum" width="100" align="center" />
      <el-table-column label="日志内容" prop="content" :show-overflow-tooltip="true" />
    </el-table>

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

<script setup name="GxLog">
import { listLog, delLog } from "@/api/gx/log";
import { listBiz } from "@/api/gx/biz";

const { proxy } = getCurrentInstance();

const logList = ref([]);
const bizOptions = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const multiple = ref(true);
const total = ref(0);
const dateRange = ref([]);

const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  bizCode: undefined,
});

function getList() {
  loading.value = true;
  const params = { ...queryParams.value };
  if (dateRange.value && dateRange.value.length === 2) {
    params.params = { beginTime: dateRange.value[0], endTime: dateRange.value[1] };
  }
  listLog(params).then((response) => {
    logList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

function getBizOptions() {
  listBiz({ pageNum: 1, pageSize: 999 }).then((response) => {
    bizOptions.value = response.rows;
  });
}

function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  multiple.value = !selection.length;
}

function handleDelete(row) {
  const deleteIds = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除日志编号为"' + deleteIds + '"的数据项？').then(() => {
    return delLog(deleteIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

getBizOptions();
getList();
</script>
