<template>
    <div class="main">
        <el-card class="box-card">
            <template #header>
                <div class="card-header">
                    <span>调用记录</span>
                </div>
            </template>
            <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="68px">
                <el-form-item label="Key" prop="secretKey">
                    <el-input
                    v-model="queryParams.secretKey"
                    placeholder="请输入Key"
                    clearable
                    @keyup.enter="handleQuery"
                    />
                </el-form-item>
                <el-form-item label="模型" prop="model">
                    <el-input
                    v-model="queryParams.model"
                    placeholder="请输入模型"
                    clearable
                    @keyup.enter="handleQuery"
                    />
                </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="usageRecordList" @selection-change="handleSelectionChange">
                <el-table-column label="调用时间" align="center" prop="createTime" />
                <el-table-column label="调用KEY" align="center" prop="secretKey" />
                <el-table-column label="KEY备注" align="center" prop="secretKey" >
                  <template #default="scope">
                    <el-tag class="ml-2" type="success">{{ getRemark(scope.row.secretKey) }}</el-tag>
                  </template>
                </el-table-column>
                <el-table-column label="模型" align="center" prop="model" />
                <el-table-column label="prompt" align="center" prop="promptTokens" />
                <el-table-column label="completion" align="center" prop="completionTokens" />
                <el-table-column label="total" align="center" prop="totalTokens" />
                <el-table-column label="费用" align="center" prop="fee" />
            </el-table>
            <div style="margin-top: 20px;">
              <el-pagination
                v-model:current-page="queryParams.pageNum"
                v-model:page-size="queryParams.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                v-show="total>0"
                :background="true"
                :total="total"
                @size-change="getList"
                @current-change="getList"
              />
            </div>
        </el-card>
    </div>
 </template>
 
 <script setup name="usage">
import { currentUserListUsageRecord } from "@/api/cert/usageRecord";
import { getKeyList } from "@/api/cert/apiToken";

const { proxy } = getCurrentInstance();

const usageRecordList = ref([]);
const keyList = ref([]);
const loading = ref(true);
const total = ref(0);

const data = reactive({
  queryParams: {
    pageNum: 1,
    userId: null,
    pageSize: 10,
    secretKey: null,
    model: null,
    msgId: null
  },
});

const { queryParams } = toRefs(data);

function getRemark(key) {
  for(let i=0;i<keyList.value.length;i++) {
    if('AK-'+keyList.value[i].id == key) {
      return keyList.value[i].remark;
    }
  }
}

/** 查询使用记录列表 */
function getList() {
  loading.value = true;
  getKeyList().then(response => {
    keyList.value = response.data
  })
  currentUserListUsageRecord(queryParams.value).then(response => {
    usageRecordList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

getList()
</script>
 
 <style scoped lang="scss">
 .main {
    padding: 20px;
 }

 .el-pagination {
    justify-content: center;
  }
 </style>
 
 