<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd">新增模型</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="modelList">
      <el-table-column label="模型名称" align="center" prop="modelName" />
      <el-table-column label="模型代码" align="center" prop="modelCode" />
      <el-table-column label="厂商" align="center" prop="providerName" />
      <el-table-column label="模型类型" align="center" prop="modelType">
        <template #default="scope">
          <el-tag v-if="scope.row.modelType === 'chat'" type="primary">对话</el-tag>
          <el-tag v-else-if="scope.row.modelType === 'completion'" type="success">补全</el-tag>
          <el-tag v-else>其他</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="上下文窗口" align="center" prop="contextWindow" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <el-tag v-if="scope.row.status === '0'" type="success">启用</el-tag>
          <el-tag v-else type="danger">停用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="title" v-model="open" width="700px">
      <el-form ref="formRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="选择厂商" prop="providerId">
          <el-select v-model="form.providerId" placeholder="请选择厂商" style="width: 100%">
            <el-option v-for="provider in providerList" :key="provider.providerId" :label="provider.providerName" :value="provider.providerId" />
          </el-select>
        </el-form-item>
        <el-form-item label="模型代码" prop="modelCode">
          <el-input v-model="form.modelCode" placeholder="如: gpt-4, glm-4" />
        </el-form-item>
        <el-form-item label="模型名称" prop="modelName">
          <el-input v-model="form.modelName" placeholder="请输入模型名称" />
        </el-form-item>
        <el-form-item label="模型类型" prop="modelType">
          <el-select v-model="form.modelType" placeholder="请选择类型" style="width: 100%">
            <el-option label="对话" value="chat" />
            <el-option label="文本补全" value="completion" />
            <el-option label="嵌入向量" value="embedding" />
          </el-select>
        </el-form-item>
        <el-form-item label="上下文窗口" prop="contextWindow">
          <el-input-number v-model="form.contextWindow" :min="0" style="width: 100%" />
        </el-form-item>
        <el-form-item label="最大Token数" prop="maxTokens">
          <el-input-number v-model="form.maxTokens" :min="0" style="width: 100%" />
        </el-form-item>
        <el-form-item label="API地址" prop="defaultApiUrl">
          <el-input v-model="form.defaultApiUrl" placeholder="API端点地址" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio value="0">启用</el-radio>
            <el-radio value="1">停用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="AiModel">
import { listModel, getModel, delModel, addModel, updateModel } from '@/api/system/ai/model'
import { listProvider } from '@/api/system/ai/provider'

const { proxy } = getCurrentInstance()

const loading = ref(false)
const modelList = ref([])
const providerList = ref([])
const title = ref('')
const open = ref(false)
const formRef = ref(null)
const form = ref({})

const rules = {
  providerId: [{ required: true, message: '厂商不能为空', trigger: 'change' }],
  modelCode: [{ required: true, message: '模型代码不能为空', trigger: 'blur' }],
  modelName: [{ required: true, message: '模型名称不能为空', trigger: 'blur' }]
}

function getList() {
  loading.value = true
  listModel({}).then(response => {
    modelList.value = response.rows
    loading.value = false
  })
}

function loadProviders() {
  listProvider({ status: '0' }).then(response => {
    providerList.value = response.rows
  })
}

function cancel() {
  open.value = false
  reset()
}

function reset() {
  form.value = { 
    modelId: null, 
    providerId: null, 
    modelCode: null, 
    modelName: null, 
    modelType: 'chat', 
    contextWindow: 4096, 
    maxTokens: 2000, 
    defaultApiUrl: null, 
    status: '0' 
  }
  proxy.resetForm('formRef')
}

function handleAdd() {
  reset()
  open.value = true
  title.value = '添加AI模型'
}

function handleUpdate(row) {
  reset()
  getModel(row.modelId).then(response => {
    form.value = response.data
    open.value = true
    title.value = '修改AI模型'
  })
}

function submitForm() {
  formRef.value.validate(valid => {
    if (valid) {
      if (form.value.modelId != null) {
        updateModel(form.value).then(() => {
          proxy.$modal.msgSuccess('修改成功')
          open.value = false
          getList()
        })
      } else {
        addModel(form.value).then(() => {
          proxy.$modal.msgSuccess('新增成功')
          open.value = false
          getList()
        })
      }
    }
  })
}

function handleDelete(row) {
  proxy.$modal.confirm('确认删除该模型？').then(() => {
    return delModel(row.modelId)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess('删除成功')
  }).catch(() => {})
}

getList()
loadProviders()
</script>
