<template>
  <!-- 列表 -->
  <ContentWrap>
    <el-button type="primary" plain @click="openForm('create')" v-hasPermi="['keyboard:ai-companion:create']">
      <Icon icon="ep:plus" class="mr-5px" /> 新增
    </el-button>
    <el-button type="danger" plain :disabled="isEmpty(checkedIds)" @click="handleDeleteBatch"
      v-hasPermi="['keyboard:ai-companion:delete']">
      <Icon icon="ep:delete" class="mr-5px" /> 批量删除
    </el-button>
    <el-table row-key="id" v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true"
      @selection-change="handleRowCheckboxChange">
      <el-table-column type="selection" width="55" />
      <!-- <el-table-column label="主表ID" align="center" prop="companionId" width="120px" /> -->
      <el-table-column label="语言标识" align="center" prop="locale" width="120px" />
      <el-table-column label="角色名称" align="center" prop="name" width="180px" />
      <el-table-column label="一句话描述" align="center" prop="shortDesc" width="200px" />
      <el-table-column label="详细介绍" align="center" prop="introText" min-width="300px" />
      <el-table-column label="开场白" align="center" prop="prologue" min-width="300px" />
      <el-table-column label="开场白音频" align="center" prop="prologueAudio" min-width="300px" />
      <!-- <el-table-column
        label="创建时间"
        align="center"
        prop="createdAt"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="更新时间"
        align="center"
        prop="updatedAt"
        :formatter="dateFormatter"
        width="180px"
      /> -->
      <el-table-column label="操作" align="center" width="140" :show-overflow-tooltip="false">
        <template #default="scope">
          <div class="table-actions">
            <el-button link type="primary" @click="openForm('update', scope.row.id)"
              v-hasPermi="['keyboard:ai-companion:update']">
              编辑
            </el-button>
            <el-button link type="danger" @click="handleDelete(scope.row.id)"
              v-hasPermi="['keyboard:ai-companion:delete']">
              删除
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </ContentWrap>
  <!-- 表单弹窗：添加/修改 -->
  <AiCompanionI18nForm ref="formRef" @success="getList" />
</template>
<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import { isEmpty } from '@/utils/is'
import { AiCompanionApi, AiCompanionI18n } from '@/api/keyboard/aicompanion'
import AiCompanionI18nForm from './AiCompanionI18nForm.vue'

const { t } = useI18n()
const message = useMessage()
const props = defineProps<{
  id?: number
}>()
const loading = ref(false)
const list = ref([])
const total = ref(0)
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  id: undefined as unknown
})

watch(
  () => props.id,
  (val: number) => {
    if (!val) {
      list.value = []
      return
    }
    queryParams.id = val
    handleQuery()
  },
  { immediate: true, deep: true }
)

const getList = async () => {
  loading.value = true
  try {
    const data = await AiCompanionApi.getAiCompanionI18nPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

const formRef = ref()
const openForm = (type: string, id?: number) => {
  if (!props.id) {
    message.error('请选择一个 AI 陪聊角色后再操作国际化数据')
    return
  }
  formRef.value.open(type, id, props.id)
}

const handleDelete = async (id: number) => {
  try {
    await message.delConfirm()
    await AiCompanionApi.deleteAiCompanionI18n(id)
    message.success(t('common.delSuccess'))
    await getList()
  } catch { }
}

const handleDeleteBatch = async () => {
  try {
    await message.delConfirm()
    await AiCompanionApi.deleteAiCompanionI18nList(checkedIds.value)
    checkedIds.value = []
    message.success(t('common.delSuccess'))
    await getList()
  } catch { }
}

const checkedIds = ref<number[]>([])
const handleRowCheckboxChange = (records: AiCompanionI18n[]) => {
  checkedIds.value = records.map((item) => item.id!)
}
</script>

<style scoped>
.table-actions {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  white-space: nowrap;
}

.table-actions :deep(.el-button) {
  margin-right: 0;
  margin-bottom: 0;
}
</style>
