<script setup lang="ts">
import dayjs from "dayjs";
import { ref, reactive, computed } from "vue";
import { useOperation } from "./utils/hook";
import { getPickerShortcuts } from "../utils";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import { useI18n } from "vue-i18n";
import VueJsonPretty from "vue-json-pretty";
import "vue-json-pretty/lib/styles.css";
import View from "@iconify-icons/ep/view";
import Delete from "@iconify-icons/ep/delete";
import Refresh from "@iconify-icons/ep/refresh";
import { OperationLogInfo } from "types/monitor";
import { hasAuth } from "@/utils/auth";
defineOptions({
  name: "OperationLog"
});

const { t } = useI18n();
const formRef = ref();
const tableRef = ref();

/**
 * 抽屉状态
 */
const drawerStatus = ref<boolean>(false);

// 折叠面板的激活项
const activeCollapse = ref(["requestinfo"]);

const logInfo = reactive<OperationLogInfo>({
  id: "",
  operation_name: "",
  operation_type: 0,
  request_path: "",
  request_method: "",
  request_params: "{}",
  response_result: "{}",
  host: "",
  location: "",
  browser: "",
  os: "",
  user_agent: "",
  operator_id: "",
  operator_name: "",
  operator_nickname: "",
  department_id: "",
  department_name: "",
  status: 1,
  operation_time: "",
  cost_time: 0
});

/**
 * 操作日志详情展开
 */
const onDetailHandle = (row: OperationLogInfo) => {
  activeCollapse.value = ["requestinfo"];
  drawerStatus.value = true;
  Object.assign(logInfo, row);
};

// 解析请求参数
const parsedRequestParams = computed(() => {
  try {
    return JSON.parse(logInfo.request_params);
  } catch (e) {
    return {};
  }
});

// 解析响应结果
const parsedResponseResult = computed(() => {
  try {
    return JSON.parse(logInfo.response_result);
  } catch (e) {
    return {};
  }
});

const {
  form,
  loading,
  columns,
  dataList,
  pagination,
  selectedNum,
  departments,
  onSearch,
  resetForm,
  onbatchDel,
  getOperationName,
  handleDelete,
  handleSizeChange,
  onSelectionCancel,
  handleCurrentChange,
  handleSelectionChange
} = useOperation(tableRef);
</script>

<template>
  <div class="main">
    <el-form
      ref="formRef"
      :inline="true"
      :model="form"
      class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px] overflow-auto"
    >
      <el-form-item label="操作名称" prop="name">
        <el-input
          v-model="form.name"
          placeholder="请输入操作名称"
          clearable
          class="!w-[200px]"
        />
      </el-form-item>
      <el-form-item label="操作类型" prop="type">
        <el-select
          v-model="form.type"
          placeholder="请选择操作类型~"
          clearable
          class="!w-[200px]"
        >
          <el-option label="查询" :value="1" />
          <el-option label="新增" :value="2" />
          <el-option label="修改" :value="3" />
          <el-option label="删除" :value="4" />
          <el-option label="授权" :value="5" />
          <el-option label="导出" :value="6" />
          <el-option label="导入" :value="7" />
          <el-option label="强退" :value="8" />
        </el-select>
      </el-form-item>
      <el-form-item label="用户账号" prop="username">
        <el-input
          v-model="form.username"
          placeholder="请输入用户账号~"
          clearable
          class="!w-[200px]"
        />
      </el-form-item>
      <el-form-item label="用户名称" prop="nickname">
        <el-input
          v-model="form.nickname"
          placeholder="请输入用户名称~"
          clearable
          class="!w-[200px]"
        />
      </el-form-item>
      <el-form-item label="操作状态" prop="status">
        <el-select
          v-model="form.status"
          placeholder="请选择操作状态~"
          clearable
          class="!w-[200px]"
        >
          <el-option label="成功" :value="1" />
          <el-option label="失败" :value="0" />
        </el-select>
      </el-form-item>
      <el-form-item label="所属部门：" prop="department_id">
        <el-cascader
          v-model="form.department_id"
          class="!w-[200px]"
          :options="departments"
          :props="{
            value: 'id',
            label: 'name',
            emitPath: false,
            checkStrictly: true
          }"
          clearable
          filterable
          placeholder="请选择所属部门~"
        >
          <template #default="{ node, data }">
            <span>{{ data.name }}</span>
            <span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
          </template>
        </el-cascader>
      </el-form-item>
      <el-form-item label="操作时间" prop="operatingTime">
        <el-date-picker
          v-model="form.operationTime"
          :shortcuts="getPickerShortcuts()"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期时间"
          end-placeholder="结束日期时间"
          value-format="x"
          unlink-panels
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon('ri:search-line')"
          :loading="loading"
          @click="onSearch"
        >
          {{ t("buttons:Search") }}
        </el-button>
        <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
          {{ t("buttons:Reset") }}
        </el-button>
      </el-form-item>
    </el-form>

    <PureTableBar title="操作日志" :columns="columns" @refresh="onSearch">
      <template v-slot="{ size, dynamicColumns }">
        <div
          v-if="selectedNum > 0"
          v-motion-fade
          class="bg-[var(--el-fill-color-light)] w-full h-[46px] mb-2 pl-4 flex items-center"
        >
          <div class="flex-auto">
            <span
              style="font-size: var(--el-font-size-base)"
              class="text-[rgba(42,46,54,0.5)] dark:text-[rgba(220,220,242,0.5)]"
            >
              已选 {{ selectedNum }} 项
            </span>
            <el-button type="primary" text @click="onSelectionCancel">
              {{ t("buttons:Deselect") }}
            </el-button>
          </div>
          <el-popconfirm
            v-if="hasAuth('operation:btn:delete')"
            title="是否确认删除?"
            @confirm="onbatchDel"
          >
            <template #reference>
              <el-button
                type="danger"
                text
                class="mr-1"
                :disabled="selectedNum < 0 || !hasAuth('operation:btn:delete')"
              >
                {{ t("buttons:DeleteInBatches") }}
              </el-button>
            </template>
          </el-popconfirm>
        </div>
        <pure-table
          ref="tableRef"
          row-key="id"
          align-whole="center"
          table-layout="auto"
          :loading="loading"
          :size="size"
          adaptive
          :adaptiveConfig="{ offsetBottom: 108 }"
          :data="dataList"
          :columns="dynamicColumns"
          :pagination="{ ...pagination, size }"
          :header-cell-style="{
            background: 'var(--el-fill-color-light)',
            color: 'var(--el-text-color-primary)'
          }"
          @selection-change="handleSelectionChange"
          @page-size-change="handleSizeChange"
          @page-current-change="handleCurrentChange"
        >
          <template #operation="{ row }">
            <el-button
              class="reset-margin"
              link
              type="primary"
              :size="size"
              :disabled="!hasAuth('operation:btn:info')"
              :icon="useRenderIcon(View)"
              @click="onDetailHandle(row)"
            >
              {{ t("buttons:Details") }}
            </el-button>
            <el-popconfirm
              :title="`是否删除这条操作记录?`"
              @confirm="handleDelete(row)"
            >
              <template #reference>
                <el-button
                  class="reset-margin"
                  link
                  type="danger"
                  :disabled="!hasAuth('operation:btn:delete')"
                  :size="size"
                  :icon="useRenderIcon(Delete)"
                >
                  {{ t("buttons:Delete") }}
                </el-button>
              </template>
            </el-popconfirm>
          </template>
        </pure-table>
      </template>
    </PureTableBar>
    <el-drawer
      v-model="drawerStatus"
      title="操作日志详情"
      :size="`50%`"
      show-close
    >
      <el-collapse v-model="activeCollapse" :accordion="false">
        <!-- 请求基本信息 -->
        <el-collapse-item title="基本信息" name="requestinfo">
          <el-descriptions title="日志基本信息" border>
            <el-descriptions-item align="center" label="操作名称">{{
              logInfo.operation_name
            }}</el-descriptions-item>
            <el-descriptions-item align="center" label="操作类型">{{
              getOperationName(logInfo.operation_type)
            }}</el-descriptions-item>
            <el-descriptions-item align="center" label="请求路径">{{
              logInfo.request_path
            }}</el-descriptions-item>
            <el-descriptions-item align="center" label="请求方法">{{
              logInfo.request_method
            }}</el-descriptions-item>
            <el-descriptions-item align="center" label="操作人账号">{{
              logInfo.operator_name
            }}</el-descriptions-item>
            <el-descriptions-item align="center" label="操作人昵称">{{
              logInfo.operator_nickname
            }}</el-descriptions-item>
            <el-descriptions-item align="center" label="所属部门">{{
              logInfo.department_name
            }}</el-descriptions-item>
            <el-descriptions-item align="center" label="操作时间">{{
              dayjs(logInfo.operation_time).format("YYYY-MM-DD HH:mm:ss")
            }}</el-descriptions-item>
            <el-descriptions-item align="center" label="耗时"
              >{{ logInfo.cost_time.toFixed(2) }} ms</el-descriptions-item
            >
            <el-descriptions-item align="center" label="请求IP">{{
              logInfo.host
            }}</el-descriptions-item>
            <el-descriptions-item align="center" label="请求位置">{{
              logInfo.location
            }}</el-descriptions-item>
            <el-descriptions-item align="center" label="请求浏览器">{{
              logInfo.browser
            }}</el-descriptions-item>
            <el-descriptions-item align="center" label="请求系统">{{
              logInfo.os
            }}</el-descriptions-item>
            <el-descriptions-item align="center" label="请求头">{{
              logInfo.user_agent
            }}</el-descriptions-item>
          </el-descriptions>
        </el-collapse-item>
        <!-- 请求参数 -->
        <el-collapse-item title="请求参数" name="requestParams">
          <vue-json-pretty
            :data="parsedRequestParams"
            showLineNumber
            collapsedOnClickBrackets
            showSelectController
            showIcon
            virtual
            showLength
          />
        </el-collapse-item>
        <!-- 响应结果 -->
        <el-collapse-item title="响应结果" name="responseResult">
          <vue-json-pretty
            :data="parsedResponseResult"
            showLineNumber
            collapsedOnClickBrackets
            showSelectController
            showIcon
            virtual
            showLength
          />
        </el-collapse-item>
      </el-collapse>
    </el-drawer>
  </div>
</template>

<style lang="scss" scoped>
:deep(.el-dropdown-menu__item i) {
  margin: 0;
}

.main-content {
  margin: 24px 24px 0 !important;
}

.search-form {
  :deep(.el-form-item) {
    margin-bottom: 12px;
  }
}
</style>
