<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="providerList">
      <el-table-column label="厂商名称" align="center" prop="providerName" />
      <el-table-column label="厂商代码" align="center" prop="providerCode" />
      <el-table-column label="Logo" align="center" prop="logoUrl">
        <template #default="scope">
          <img v-if="scope.row.logoUrl" :src="scope.row.logoUrl" style="width: 32px; height: 32px;" />
        </template>
      </el-table-column>
      <el-table-column label="官网" align="center" prop="officialUrl" show-overflow-tooltip />
      <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="600px">
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="厂商代码" prop="providerCode">
          <el-input v-model="form.providerCode" placeholder="如: openai, zhipu" />
        </el-form-item>
        <el-form-item label="厂商名称" prop="providerName">
          <el-input v-model="form.providerName" placeholder="请输入厂商名称" />
        </el-form-item>
        <el-form-item label="Logo地址" prop="logoUrl">
          <el-input v-model="form.logoUrl" placeholder="请输入Logo地址" />
        </el-form-item>
        <el-form-item label="官网地址" prop="officialUrl">
          <el-input v-model="form.officialUrl" placeholder="请输入官网地址" />
        </el-form-item>
        <el-form-item label="API文档" prop="apiDocUrl">
          <el-input v-model="form.apiDocUrl" 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="AiProvider">
import { listProvider, getProvider, delProvider, addProvider, updateProvider } from '@/api/system/ai/provider'

const { proxy } = getCurrentInstance()

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

const rules = {
  providerCode: [{ required: true, message: '厂商代码不能为空', trigger: 'blur' }],
  providerName: [{ required: true, message: '厂商名称不能为空', trigger: 'blur' }]
}

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

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

function reset() {
  form.value = { 
    providerId: null, 
    providerCode: null, 
    providerName: null, 
    logoUrl: null, 
    officialUrl: null, 
    apiDocUrl: null, 
    status: '0' 
  }
  proxy.resetForm('formRef')
}

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

function handleUpdate(row) {
  reset()
  getProvider(row.providerId).then(response => {
    form.value = response.data
    open.value = true
    title.value = '修改AI厂商'
  })
}

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

function handleDelete(row) {
  proxy.$modal.confirm('确认删除该厂商？').then(() => {
    return delProvider(row.providerId)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess('删除成功')
  }).catch(() => {})
}

getList()
</script>
