<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue';

import { $t } from '@easyflow/locales';

import { InfoFilled } from '@element-plus/icons-vue';
import {
  ElButton,
  ElForm,
  ElFormItem,
  ElInputNumber,
  ElMessage,
  ElTooltip,
} from 'element-plus';

import { api } from '#/api/request';

const props = defineProps({
  documentCollectionId: {
    type: String,
    required: true,
  },
  manageable: {
    type: Boolean,
    default: true,
  },
});

onMounted(() => {
  getDocumentCollectionConfig();
});
const baseOptions = ref<Record<string, any>>({});
const getDocumentCollectionConfig = () => {
  api
    .get(`/api/v1/documentCollection/detail?id=${props.documentCollectionId}`)
    .then((res) => {
      const { data } = res;
      const options = data.options || {};
      baseOptions.value = { ...options };
      searchConfig.docRecallMaxNum = options.docRecallMaxNum
        ? Number(options.docRecallMaxNum)
        : 5;
      searchConfig.simThreshold = options.simThreshold
        ? Number(options.simThreshold)
        : 0.6;
    });
};

const searchConfig = reactive({
  docRecallMaxNum: 5,
  simThreshold: 0.6,
});

const submitConfig = () => {
  if (!props.manageable) {
    ElMessage.warning($t('documentCollection.managePermissionHint'));
    return;
  }
  const submitData = {
    id: props.documentCollectionId,
    options: {
      ...baseOptions.value,
      docRecallMaxNum: searchConfig.docRecallMaxNum,
      simThreshold: searchConfig.simThreshold,
    },
  };

  api
    .post('/api/v1/documentCollection/update', submitData)
    .then(() => {
      baseOptions.value = { ...submitData.options };
      ElMessage.success($t('documentCollectionSearch.message.saveSuccess'));
    })
    .catch((error) => {
      ElMessage.error($t('documentCollectionSearch.message.saveFailed'));
      console.error('保存配置失败：', error);
    });
};
</script>

<template>
  <div class="search-config-sidebar">
    <div class="config-header">
      <h3>{{ $t('documentCollectionSearch.title') }}</h3>
      <div v-if="!props.manageable" class="config-readonly-tip">
        {{ $t('documentCollection.managePermissionHint') }}
      </div>
    </div>

    <ElForm
      class="config-form"
      :model="searchConfig"
      :disabled="!props.manageable"
      label-width="100%"
      size="small"
    >
      <ElFormItem prop="docRecallMaxNum" class="form-item">
        <div class="form-item-label">
          <span>{{
            $t('documentCollectionSearch.docRecallMaxNum.label')
          }}</span>
          <ElTooltip
            :content="$t('documentCollectionSearch.docRecallMaxNum.tooltip')"
            placement="top"
            effect="dark"
            class="label-tooltip"
          >
            <InfoFilled class="info-icon" />
          </ElTooltip>
        </div>
        <div class="form-item-content">
          <ElInputNumber
            v-model="searchConfig.docRecallMaxNum"
            :min="1"
            :max="50"
            :step="1"
            :placeholder="$t('documentCollectionSearch.placeholder.count')"
            class="form-control"
          >
            <template #append>
              {{ $t('documentCollectionSearch.unit.count') }}
            </template>
          </ElInputNumber>
        </div>
      </ElFormItem>

      <ElFormItem prop="simThreshold" class="form-item">
        <div class="form-item-label">
          <span>{{ $t('documentCollectionSearch.simThreshold.label') }}</span>
          <ElTooltip
            :content="$t('documentCollectionSearch.simThreshold.tooltip')"
            placement="top"
            effect="dark"
            class="label-tooltip"
          >
            <InfoFilled class="info-icon" />
          </ElTooltip>
        </div>
        <div class="form-item-content">
          <ElInputNumber
            v-model="searchConfig.simThreshold"
            :min="0"
            :max="1"
            :step="0.01"
            show-input
            class="form-control"
          />
        </div>
      </ElFormItem>
    </ElForm>

    <div class="config-hint">
      {{ $t('documentCollectionSearch.engineHint') }}
    </div>

    <div class="config-footer">
      <ElButton
        type="primary"
        @click="submitConfig"
        class="submit-btn"
        :disabled="!props.manageable"
      >
        {{ $t('documentCollectionSearch.button.save') }}
      </ElButton>
    </div>
  </div>
</template>

<style scoped>
.search-config-sidebar {
  box-sizing: border-box;
  width: 60%;
  height: 100%;
  padding: 16px;
  overflow: hidden auto;
}

.config-header {
  padding-bottom: 8px;
  margin-bottom: 16px;
  border-bottom: 1px solid #e6e6e6;
}

.config-header h3 {
  margin: 0;
  font-size: 15px;
  font-weight: 600;
}

.config-readonly-tip {
  margin-top: 8px;
  font-size: 12px;
  line-height: 1.6;
  color: var(--el-text-color-secondary);
}

.config-form {
  margin-bottom: 24px;
}

.form-item {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-bottom: 20px;
}

.form-item-label {
  display: flex;
  gap: 4px;
  align-items: center;
  font-size: 14px;
  line-height: 1.4;
  color: #606266;
}

.label-tooltip {
  display: inline-block;
  cursor: help;
}

:deep(.form-item .el-form-item__content) {
  width: 100%;
  margin-left: 0 !important;
}

.form-item-content {
  display: flex;
  gap: 8px;
  align-items: center;
  width: 100%;
  margin-top: 4px;
}

.form-control {
  flex: 1;
  width: 100%;
  min-width: 0;
}

.switch-control {
  flex: none;
  width: auto;
  min-width: 80px;
}

.info-icon {
  flex: none;
  flex-shrink: 0;
  width: 16px;
  height: 16px;
  font-size: 14px;
  color: #909399;
  cursor: help;
}

.info-icon:hover {
  color: #409eff;
}

.submit-btn {
  width: 100%;
  padding: 8px 0;
}

.config-hint {
  margin: 12px 0 4px;
  font-size: 12px;
  line-height: 1.6;
  color: var(--el-text-color-secondary);
}

.config-footer {
  position: sticky;
  bottom: 0;
  padding-top: 8px;
}

:deep(.el-form-item__content) {
  box-sizing: border-box;
  width: 100%;
}

:deep(.el-slider) {
  --el-slider-input-width: 60px;
}

:deep(.el-input-number),
:deep(.el-select) {
  width: 100%;
}
</style>
