<template>
    <div class="scholar-container">
        <div class="sidebar">
            <el-card class="params-card" shadow="never">
                <div slot="header" class="card-header" style="font-weight: bold;"><i class="el-icon-search"></i> Scholar Search</div>

                <el-form :model="form" ref="searchForm" :rules="rules" label-position="top">
                    <el-form-item label="Keyword" prop="keyword">
                        <el-input v-model="form.keyword" placeholder="e.g. cancer" clearable></el-input>
                    </el-form-item>

                    <div class="form-row">
                        <el-form-item label="Per Page (Articles)" class="half-width">
                            <el-input-number v-model="per_page" :controls="false" :min="1" :max="100"></el-input-number>
                        </el-form-item>

                        <el-form-item label="Page Number" class="half-width">
                            <el-input-number v-model="page" :controls="false" :min="1" :max="maxPages"></el-input-number>
                        </el-form-item>
                    </div>
                    <div style="display: flex; gap: 10px; margin-top: 15px;">
    <el-button type="primary" class="fetch-btn" style="flex: 3;" :loading="loading" @click="handleExecute">
        <i v-if="!loading" class="el-icon-coin"></i>
        {{ loading ? 'Fetching...' : 'Execute Fetch' }}
    </el-button>
    
    <el-button @click="resetForm" class="custom-reset-btn">
        <i class="el-icon-refresh-left"></i> Reset
    </el-button>
</div>          
                </el-form>
            </el-card>

            <div class="info-box">
                <h4>INFORMATION</h4>

                <p>
                    "This tool allows for systematic extraction of scholar data from academic publications. Note that per_page refers to the
                    number of articles scanned, which may yield multiple scholar records."
                </p>
            </div>
        </div>

        <div class="main-content" :style="{border:hasSearched && !loading ? 'none' : '2px solid #e2e2e2'}">
            <div v-if="!hasSearched && !loading" class="main-content-center">
                <div class="empty-state">
                    <div class="state-icon"><i class="el-icon-user"></i></div>

                    <h3>Scholar Search</h3>

                    <p>
                        Enter a research keyword and specify the search depth to retrieve expert profiles from professional academic
                        databases.
                    </p>
                </div>
            </div>

            <div v-if="loading" class="main-content-center">
                <div class="processing-state">
                    <div class="spinner-box"><i class="el-icon-loading"></i></div>

                    <h3>PROCESSING REQUEST</h3>

                    <p>Connecting to api.tmrjournals.com...</p>
                </div>
            </div>

            <div v-if="hasSearched && !loading" class="results-container">
                <div class="results-header">
                    <div class="results-stats">
                        <i class="el-icon-user"></i> RESULTS FOUND: <strong>{{ total }}</strong>
                        <span class="divider">|</span>
                        <span class="page-info">PAGE {{ page }} OF APPROX. {{ maxPages }}</span>
                        <template v-if="Source">
<span class="divider">|</span>
                        <span class="page-info">Source {{ Source }}</span>
                        </template>
                        
                    </div>

                    <div class="results-nav">
                        <el-button
                            class="nav-btn"
                            size="mini"
                            icon="el-icon-arrow-left"
                            :disabled="page <= 1"
                            @click="changePage(-1)"
                        ></el-button>

                        <el-button
                            class="nav-btn"
                            size="mini"
                            icon="el-icon-arrow-right"
                            :disabled="page >= maxPages"
                            @click="changePage(1)"
                        ></el-button>
                    </div>
                </div>

                <el-table :data="exportFiles" class="custom-table" border stripe header-row-class-name="dark-table-header">
                    <el-table-column prop="name" label="File name" min-width="200"></el-table-column>
                    <el-table-column prop="url" label="File url" min-width="250">
                        <template slot-scope="scope">
                            <el-link class="file-link" :href="mediaUrl+scope.row.url" target="_blank">
                                {{ scope.row.url }}
                                <i class="el-icon-download download-icon"></i>
                            </el-link>
                        </template>
                    </el-table-column>
                    <el-table-column prop="count" label="Count" width="120" align="center"></el-table-column>
                </el-table>
            </div>
        </div>
    </div>
</template>

<script>
import Common from '@/components/common/common'
export default {
    name: 'scholarCrawlers',
    data() {
        return {
            mediaUrl: Common.mediaUrl,
            form: { keyword: '' },
            rules: {
                keyword: [{ required: true, message: 'Required', trigger: 'blur' }]
            },
            loading: false,
            exportLoading: false,
            hasSearched: false,
            page: 1,
            per_page: 20,
            maxPages: 100, // 初始上限
            total: 0,
            Source: '',
            exportFiles: [] // 模拟图二的文件列表数据
        };
    },
    methods: {
        resetForm() {
            this.form = { keyword: '' };
            this.page = 1;
            this.per_page=20;
            this.$refs.searchForm.resetFields();
        },
        async handleExecute() {
            this.$refs.searchForm.validate(async (valid) => {
                if (!valid) return;
                this.loading = true;
                this.hasSearched = false;

                try {
                    // 1. 调用 Search 接口获取页数上限
                    const searchRes = await this.$api.post('/api/expert_finder/search', {
                        keyword: this.form.keyword,
                        page: this.page,
                        per_page: this.per_page
                    });

                    if (searchRes && searchRes.code === 0) {
                        this.total = searchRes.data.total || 0;
                        this.maxPages = searchRes.data.total_pages || 1; // 2. 模拟处理延迟（配合图一动画）
                        this.Source = searchRes.data.source || ''; 
                        await new Promise((resolve) => setTimeout(resolve, 1500)); // 3. 调用 Export 接口生成文件

                        const exportRes = await this.$api.post('/api/expert_finder/export', {
                            keyword: this.form.keyword,
                            page: this.page,
                            per_page: this.per_page
                        });

                        if (exportRes && (exportRes.code === 0 || exportRes.status === 1)) {
                            // 构造图二展示的数据格式
                            const fileUrl = exportRes.data ? exportRes.data.file_url || exportRes.file_url : '';
                            const fileName = fileUrl ? fileUrl.split('/').pop() : `experts_${this.form.keyword}_p${this.page}.xlsx`;
                            this.exportFiles = [
                                {
                                    url: fileUrl,
                                    name: fileName,
                                    count: searchRes.data.experts ? searchRes.data.experts.length : 0
                                }
                            ];
                            this.hasSearched = true;
                        }
                    }
                } catch (error) {
                    this.$message.error('Execution failed');
                    console.error(error);
                } finally {
                    this.loading = false;
                }
            });
        },
        changePage(step) {
            this.page += step;
            this.handleExecute();
        }
    }
};
</script>

<style scoped>
/* 全局基础：背景色与字体 */
.scholar-container {
    display: flex;
    /* background-color: #E6E6E6;  */

    padding: 20px 0;
    box-sizing: border-box;
    /* font-family: "Georgia", "Times New Roman", serif; */
    color: #1a1a1a;
}

/* --- 左侧边栏 --- */
.sidebar {
    width: 420px;
    margin-right: 30px;
}

/* 参数卡片：去掉 Element 默认圆角和阴影 */
.params-card {
    border: 2px solid #000 !important;
    border-radius: 0 !important;
    background: #fff;
    /* 核心：右下角的硬阴影效果 */
    box-shadow: 6px 6px 0px 0px rgba(0, 0, 0, 0.8) !important;
    margin-bottom: 25px;
}

/* 卡片头部标题 */
.card-header {
    font-size: 1.2rem;
    font-style: italic;
    font-weight: 500;
    display: flex;
    align-items: center;
    padding: 5px 0;
}

.card-header i {
    margin-right: 8px;
    font-style: normal;
}

/* 表单元素重置 */
/deep/ .el-form-item__label {
    font-family: 'Arial', sans-serif; /* 标签用无衬线体形成对比 */
    font-size: 12px;
    font-weight: 800;
    color: #333;
    padding: 0 !important;
    line-height: 20px !important;
    letter-spacing: 0.5px;
}

/deep/ .el-input__inner {
    border-radius: 0 !important;
    border: 1.5px solid #000 !important;
    height: 40px;
    font-family: 'Georgia', serif;
    background-color: #fcfcfc;
    /* 内部微阴影感 */
    box-shadow: inset 1px 1px 2px rgba(0, 0, 0, 0.1);
}

.form-row {
    display: flex;
    gap: 15px;
}

/deep/ .el-input-number {
    width: 100%;
}

/* 黑色大按钮 */
.fetch-btn {
    width: 100%;
    height: 55px;
    background-color: #000 !important;
    border: none !important;
    border-radius: 0 !important;
    color: #fff !important;
    font-weight: 900;
    font-size: 16px;
    letter-spacing: 1.5px;
    margin-top: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

.fetch-btn:hover {
    background-color: #333 !important;
    transform: translate(-1px, -1px);
    box-shadow: 2px 2px 0 #888;
}

.fetch-btn i {
    font-size: 20px;
    margin-right: 10px;
}

/* 信息框：黑底白字 */
.info-box {
    background-color: #151515;
    color: #d0d0d0;
    padding: 25px;
    border-radius: 0;
    line-height: 1.6;
}

.info-box h4 {
    color: #fff;
    font-family: 'Arial', sans-serif;
    font-size: 13px;
    letter-spacing: 2px;
    margin: 0 0 15px 0;
    border-bottom: 1px solid #444;
    padding-bottom: 5px;
}

.info-box p {
    font-style: italic;
    font-size: 13px;
    margin: 0;
}

.main-content-center {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
}

.empty-state {
    text-align: center;
    max-width: 450px;
}

/* 数据库图标 */
.state-icon {
    font-size: 80px;
    color: #bcbcbc;
    margin-bottom: 15px;
}

.empty-state h3 {
    font-size: 24px;
    font-style: italic;
    font-weight: 400;
    color: #444;
    margin-bottom: 15px;
}

.empty-state p {
    font-size: 14px;
    color: #888;
    line-height: 1.5;
}

/* 加载动画状态 */
.processing-state {
    text-align: center;
}

.spinner-box {
    font-size: 50px;
    margin-bottom: 20px;
    animation: rotating 2s linear infinite;
}

@keyframes rotating {
    from {
        transform: rotate(0deg);
    }
    to {
        transform: rotate(360deg);
    }
}

.processing-state h3 {
    letter-spacing: 4px;
    font-weight: 900;
    color: #000;
}
/* 右侧容器改为顶部对齐 */
.main-content {
    flex: 1;
    border: 2px dashed #bbb;
    display: flex;
    align-items: center;
    justify-content: center;
    justify-content: center;
    align-items: flex-start; /* 确保列表从顶部开始 */
    /* padding: 20px; */
    background-color: rgba(255, 255, 255, 0.3);
}

.results-container {
    width: 100%;
    /* 模拟图二的实体阴影 */
    box-shadow: 8px 8px 0px 0px rgba(0, 0, 0, 0.1);
}

/* 统计栏样式 */
.results-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 15px 20px;
    background: #fff;
    border: 2px solid #000;
    border-bottom: none; /* 与下方表格衔接 */
}

.results-stats {
    font-family: 'Arial', sans-serif;
    font-weight: 800;
    font-size: 14px;
    letter-spacing: 0.5px;
    /* text-transform: uppercase; */
}

.results-stats strong {
    font-size: 18px;
    margin-left: 5px;
}

.divider {
    margin: 0 15px;
    color: #ccc;
    font-weight: 100;
}

.page-info {
    color: #999;
    font-size: 11px;
}

/* 导航按钮 */
.nav-btn {
    border: 1.5px solid #000 !important;
    border-radius: 0 !important;
    background: #fff !important;
    color: #000 !important;
    padding: 5px 10px !important;
}

.nav-btn:hover {
    background: #000 !important;
    color: #fff !important;
}

/* 表格主体还原 */
/deep/ .custom-table {
    border: 2px solid #000 !important;
}

/deep/ .dark-table-header th {
    background-color: #000 !important;
    color: #fff !important;
    /* font-family: "Arial", sans-serif; */
    font-weight: 900;
    letter-spacing: 1px;
    padding: 12px 0 !important;
}

/deep/ .el-table__row td {
    padding: 15px 0 !important;
    color: #333;
    /* font-family: "Georgia", serif; */
}

/* 链接样式 */
.file-link {
    font-size: 13px;
    text-decoration: none !important;
    color: #0066cc;
}

.file-link:hover {
    text-decoration: underline;
}

.download-icon {
    margin-left: 6px;
    vertical-align: middle;
    font-size: 14px;
}

.custom-reset-btn {
    flex: 1;
    background-color: #fff;
    border: 2px solid #000;
    color: #000;
    font-weight: 800;
    font-family: 'Arial', sans-serif;
    font-size: 14px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.1s;
    outline: none;
    margin-top: 10px
}

.custom-reset-btn i {
    margin-right: 4px;
    font-weight: 800;
}

/* 悬浮和点击效果：增加黑白对比度 */
.custom-reset-btn:hover {
    background-color: #f0f0f0;
}

.custom-reset-btn:active {
    transform: translate(1px, 1px);
    box-shadow: none;
}
</style>
