<template>
    <section class="mapping-panel config-panel">
        <div class="panel-header">
            <div>
                <h2 class="panel-title">请求配置</h2>
                <p class="panel-description">这里直接编辑 request.indexSelection，默认值会在选择 ICD 文件后自动生成。</p>
            </div>
            <el-button
                v-if="showGenerateButton"
                type="primary"
                :icon="Connection"
                :loading="isSubmitting"
                :disabled="!canGenerate"
                @click="emit('generate')"
            >
                生成映射
            </el-button>
        </div>

        <div class="panel-content">
            <div class="panel-section result-card">
                <el-alert v-if="jsonError" :title="jsonError" type="error" :closable="false" class="json-alert" />

                <el-input
                    type="textarea"
                    class="index-selection-textarea"
                    :model-value="indexSelectionJson"
                    :disabled="isSubmitting"
                    :rows="18"
                    resize="none"
                    placeholder="ICD 解析完成后，这里会自动填充 request.indexSelection，可继续直接编辑。"
                    @update:model-value="value => emit('update:indexSelectionJson', String(value || ''))"
                />
            </div>

            <el-empty v-if="!hasDefaultJson" :description="emptyDescription" />
        </div>
    </section>
</template>

<script setup lang="ts">
import { Connection } from '@element-plus/icons-vue'

defineOptions({
    name: 'MappingConfigPanel'
})

defineProps<{
    indexSelectionJson: string
    isSubmitting: boolean
    canGenerate: boolean
    jsonError: string
    showGenerateButton: boolean
    hasDefaultJson: boolean
    emptyDescription: string
}>()

const emit = defineEmits<{
    (event: 'update:indexSelectionJson', value: string): void
    (event: 'generate'): void
}>()
</script>

<style scoped lang="scss">
.mapping-panel {
    display: flex;
    flex-direction: column;
    gap: 16px;
    min-height: 0;
    padding: 24px;
    border: 1px solid #e5e7eb;
    border-radius: 12px;
    background: #ffffff;
    box-shadow: 0 8px 24px rgba(15, 23, 42, 0.08);
    overflow: hidden;
}

.config-panel {
    min-height: 0;
}

.panel-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 16px;
}

.panel-title {
    margin: 0;
    font-size: 22px;
    font-weight: 600;
    line-height: 1.4;
    color: #1f2937;
}

.panel-description {
    margin: 8px 0 0;
    font-size: 14px;
    line-height: 1.7;
    color: #4b5563;
}

.panel-content {
    display: flex;
    flex: 1;
    flex-direction: column;
    gap: 16px;
    min-height: 0;
    overflow: hidden;
}

.panel-section {
    border: 1px solid #e5e7eb;
    border-radius: 12px;
    background: linear-gradient(180deg, #ffffff 0%, #f8fbff 100%);
}

.result-card {
    display: flex;
    flex: 1;
    flex-direction: column;
    min-height: 0;
    padding: 16px;
    overflow: hidden;
}

.index-selection-textarea {
    flex: 1;
    min-height: 0;
    margin-top: 16px;
}

.index-selection-textarea :deep(.el-textarea) {
    width: 100%;
    height: 100%;
}

.index-selection-textarea :deep(.el-textarea__inner) {
    height: 100%;
    font-family: Consolas, 'Courier New', monospace;
    line-height: 1.6;
    overflow-y: auto;
}

.json-alert {
    margin: 16px 0 0;
}

@media (max-width: 768px) {
    .mapping-panel {
        padding: 20px;
    }

    .panel-header {
        flex-direction: column;
        align-items: flex-start;
    }
}
</style>
