<template>
    <div class="app-containersy">
        <!-- 搜索条件 -->
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
            label-width="68px">
            <el-form-item label="项目名称" prop="xmmc">
                <el-input v-model="queryParams.xmmc" placeholder="请输入项目名称" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <!-- Tab页面 -->
        <el-tabs v-model="activeTab" @tab-click="handleTabClick" style="margin-top: -10px;" class="tabClass">
            <!-- 待验收Tab -->
            <el-tab-pane label="待验收" name="pending">
                <div class="card-container">
                    <el-row :gutter="16" v-loading="pendingLoading">
                        <el-col :span="4" v-for="item in pendingList" :key="item.id" class="card-item">
                            <el-card class="project-card" shadow="hover">
                                <div class="card-header" @click="goNav2(item)" style="cursor: pointer;">
                                    <h3 class="project-title">{{ item.xmmc }}</h3>
                                    <el-tag size="small" class="status-tag status-pending">待验收</el-tag>
                                </div>
                                <div class="card-content">
                                    <div class="content-wrapper">
                                        <div class="info-section">
                                            <div class="info-item">
                                                <span class="label">区块：</span>
                                                <span class="value">{{ item.qk || '-' }}</span>
                                            </div>
                                            <div class="info-item">
                                                <span class="label">甲方单位：</span>
                                                <span class="value">{{ item.jfdw || '-' }}</span>
                                            </div>
                                            <div class="info-item">
                                                <span class="label">结合单位：</span>
                                                <span class="value">{{ item.jhdw || '-' }}</span>
                                            </div>
                                            <div class="info-item">
                                                <span class="label">项目日期：</span>
                                                <span class="value">{{ item.xmrq || '-' }}</span>
                                            </div>
                                        </div>
                                        <div class="button-section">
                                            <el-button size="mini" @click="handleComplete(item)">完成</el-button>
                                        </div>
                                    </div>
                                </div>
                            </el-card>
                        </el-col>
                    </el-row>
                    <!-- 分页 -->
                    <div class="pagination-wrapper">
                        <pagination v-show="pendingTotal > 0" :total="pendingTotal"
                            :page.sync="pendingQueryParams.pageNum" :limit.sync="pendingQueryParams.pageSize"
                            @pagination="getPendingList" />
                    </div>
                </div>
            </el-tab-pane>

            <!-- 已验收Tab -->
            <el-tab-pane label="已验收" name="completed">
                <div class="card-container">
                    <el-row :gutter="16" v-loading="completedLoading">
                        <el-col :span="4" v-for="item in completedList" :key="item.id" class="card-item">
                            <el-card class="project-card" shadow="hover">
                                <div class="card-header">
                                    <h3 class="project-title">{{ item.xmmc }}</h3>
                                    <el-tag type="success" size="small">已验收</el-tag>
                                </div>
                                <div class="card-content">
                                    <div class="info-item">
                                        <span class="label">区块：</span>
                                        <span class="value">{{ item.qk || '-' }}</span>
                                    </div>
                                    <div class="info-item">
                                        <span class="label">甲方单位：</span>
                                        <span class="value">{{ item.jfdw || '-' }}</span>
                                    </div>
                                    <div class="info-item">
                                        <span class="label">结合单位：</span>
                                        <span class="value">{{ item.jhdw || '-' }}</span>
                                    </div>
                                    <div class="info-item">
                                        <span class="label">项目日期：</span>
                                        <span class="value">{{ item.xmrq || '-' }}</span>
                                    </div>
                                </div>
                            </el-card>
                        </el-col>
                    </el-row>
                    <!-- 分页 -->
                    <div class="pagination-wrapper">
                        <pagination v-show="completedTotal > 0" :total="completedTotal"
                            :page.sync="completedQueryParams.pageNum" :limit.sync="completedQueryParams.pageSize"
                            @pagination="getCompletedList" />
                    </div>
                </div>
            </el-tab-pane>
        </el-tabs>
    </div>
</template>

<script>
import { listYsqqXmxx, updateYsqqXmxx } from "@/api/ysqqXmxx/ysqqXmxx"

export default {
    name: "Ysgc",
    data() {
        return {
            // 当前激活的tab
            activeTab: 'pending',
            // 显示搜索条件
            showSearch: true,
            // 查询参数
            queryParams: {
                xmmc: null
            },
            // 待验收列表
            pendingList: [],
            pendingLoading: false,
            pendingTotal: 0,
            pendingQueryParams: {
                pageNum: 1,
                pageSize: 10,
                xmzt: '待验收'
            },
            // 已验收列表
            completedList: [],
            completedLoading: false,
            completedTotal: 0,
            completedQueryParams: {
                pageNum: 1,
                pageSize: 10,
                xmzt: '已验收'
            }
        }
    },
    created() {
        this.getPendingList()
    },
    methods: {
        /** 获取待验收列表 */
        getPendingList() {
            this.pendingLoading = true
            const params = {
                ...this.pendingQueryParams,
                ...this.queryParams
            }
            listYsqqXmxx(params).then(response => {
                this.pendingList = response.rows
                this.pendingTotal = response.total
                this.pendingLoading = false
            }).catch(() => {
                this.pendingLoading = false
            })
        },
        /** 获取已验收列表 */
        getCompletedList() {
            this.completedLoading = true
            const params = {
                ...this.completedQueryParams,
                ...this.queryParams
            }
            listYsqqXmxx(params).then(response => {
                this.completedList = response.rows
                this.completedTotal = response.total
                this.completedLoading = false
            }).catch(() => {
                this.completedLoading = false
            })
        },
        /** Tab切换 */
        handleTabClick(tab) {
            if (tab.name === 'pending') {
                this.getPendingList()
            } else if (tab.name === 'completed') {
                this.getCompletedList()
            }
        },
        /** 搜索按钮操作 */
        handleQuery() {
            if (this.activeTab === 'pending') {
                this.pendingQueryParams.pageNum = 1
                this.getPendingList()
            } else if (this.activeTab === 'completed') {
                this.completedQueryParams.pageNum = 1
                this.getCompletedList()
            }
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.queryParams.xmmc = null
            this.handleQuery()
        },
        /** 完成按钮操作 */
        handleComplete(row) {
            this.$modal.confirm('是否确认完成项目"' + row.xmmc + '"的验收？').then(() => {
                const updateData = {
                    ...row,
                    xmzt: '已验收'
                }
                return updateYsqqXmxx(updateData)
            }).then(() => {
                this.$modal.msgSuccess("验收完成")
                this.getPendingList()
                // 如果当前在已验收tab，也刷新已验收列表
                if (this.activeTab === 'completed') {
                    this.getCompletedList()
                }
            }).catch(() => { })
        },
        /** 跳转到 导航图2 页面 */
        goNav2(item) {
            const id = item && (item.id || item.zbid)
            this.$router.push({
                name: 'YsgcIndex2',
                query: id ? { id } : {}
            })
        },
    }
}
</script>

<style scoped>
.app-containersy {
    margin: 10px 15px 10px 15px;
}

.card-container {
    height: calc(100vh - 200px);
    padding: 10px 0;
    background: transparent;
    border-radius: 8px;
    margin: 0 -8px;
    padding: 0px 8px;
    position: relative;
}

.card-item {
    margin-bottom: 10px;
    transition: transform 0.2s ease;
}

.card-item:hover {
    transform: none;
}

.project-card {
    height: 220px;
    display: flex;
    flex-direction: column;
    border-radius: 12px;
    border: 1px solid #cfd8e6;
    background: #ffffff;
    box-shadow: 0 10px 24px rgba(64, 158, 255, 0.10), 0 2px 6px rgba(0, 0, 0, 0.04);
    transition: all 0.3s ease;
    position: relative;
    overflow: hidden;
}

.project-card:hover {
    transform: none;
    box-shadow: 0 10px 24px rgba(64, 158, 255, 0.10), 0 2px 6px rgba(0, 0, 0, 0.04);
    border-color: #cfd8e6;
}

.card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
    padding: 12px 12px 8px 12px;
    border-bottom: 1px solid #f0f2f5;
    background: #f9fbff;
}

.project-title {
    margin: 0;
    font-size: 15px;
    font-weight: 600;
    color: #2c3e50;
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    margin-right: 8px;
    position: relative;
}

.card-content {
    flex: 1;
    padding: 12px;
}

.content-wrapper {
    display: flex;
    height: 100%;
    gap: 12px;
}

.info-section {
    flex: 1;
}

.info-item {
    display: flex;
    margin-bottom: 6px;
    font-size: 13px;
    padding: 3px 0;
    border-radius: 4px;
    transition: background-color 0.2s ease;
    align-items: center;
}

.info-item:hover {
    background-color: #f8f9fa;
}

.label {
    color: #6c757d;
    min-width: 70px;
    flex-shrink: 0;
    font-weight: 500;
    white-space: nowrap;
}

.value {
    color: #495057;
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-weight: 400;
    min-width: 0;
}

.button-section {
    display: none;
    align-items: center;
    justify-content: center;
    padding-top: 6px;
    opacity: 0;
    visibility: hidden;
}

.button-section .el-button {
    width: 50px;
    height: 25px;
    font-size: 12px;
    font-weight: 500;
    border-radius: 20px;
    background: linear-gradient(135deg, #409EFF 0%, #66b1ff 100%);
    border: none;
    color: #ffffff !important;
    box-shadow: 0 1px 4px rgba(64, 158, 255, 0.2);
    transition: all 0.2s ease-out;
    position: relative;
    overflow: hidden;
    letter-spacing: 0.5px;
}

.button-section .el-button:hover {
    background: linear-gradient(135deg, #337ecc 0%, #5a9eff 100%);
    box-shadow: 0 2px 8px rgba(64, 158, 255, 0.25);
    transform: translateY(-0.5px);
    color: #ffffff !important;
}

.button-section .el-button:active {
    transform: translateY(0);
    box-shadow: 0 1px 3px rgba(64, 158, 255, 0.2);
    color: #ffffff !important;
}

.button-section .el-button::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.15), transparent);
    transition: left 0.3s ease-out;
}

.button-section .el-button:hover::before {
    left: 100%;
}

/* Safety: if any global rule hid buttons inside cards on non-hover, override */
.project-card:hover .button-section {
    display: flex;
    opacity: 1;
    visibility: visible;
}

/* Ensure button itself is not hidden by global rules */
/* 让按钮在显示时可见 */
.project-card:hover .button-section .el-button {
    opacity: 1;
    visibility: visible;
}

/* 状态标签：待验收（蓝色系，贴合系统主色） */
.status-tag {
    border: none !important;
    font-weight: 600;
}

.status-pending {
    color: #1f3b64;
    background: rgba(64, 158, 255, 0.12);
}

/* 分页固定在右下角 */
.pagination-wrapper {
    position: fixed;
    right: 20px;
    bottom: 20px;
    z-index: 1000;
}

/* 默认大屏下每行5列 */
.el-col {
    width: 20% !important;
}

/* 响应式设计 */
@media (max-width: 1400px) {
    .el-col {
        width: 20% !important;
    }
}

@media (max-width: 1200px) {
    .el-col {
        width: 33.333% !important;
    }
}

@media (max-width: 768px) {
    .el-col {
        width: 50% !important;
    }
}

@media (max-width: 480px) {
    .el-col {
        width: 100% !important;
    }
}
</style>
