<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="6" v-for="item in pendingList" :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="warning" size="small">待验收</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 type="primary" size="small"
                                                @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="6" 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: 12,
                xmzt: '待验收'
            },
            // 已验收列表
            completedList: [],
            completedLoading: false,
            completedTotal: 0,
            completedQueryParams: {
                pageNum: 1,
                pageSize: 12,
                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(() => { })
        },
    }
}
</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: scale(1.02);
}

.project-card {
    height: 240px;
    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: translateY(-4px);
    box-shadow: 0 14px 32px rgba(64, 158, 255, 0.18), 0 4px 10px rgba(0, 0, 0, 0.06);
    border-color: #8cb7ff;
}

.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;
}

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

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

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

.button-section {
    display: flex;
    align-items: center;
    justify-content: center;
    padding-top: 6px;
    /* Ensure always visible, not hover-only */
    opacity: 1 !important;
    visibility: visible !important;
    display: flex !important;
}

.button-section .el-button {
    width: 70px;
    height: 28px;
    font-size: 12px;
    font-weight: 500;
    border-radius: 14px;
    background: linear-gradient(135deg, #28a745 0%, #20c997 100%);
    border: none;
    box-shadow: 0 2px 4px rgba(40, 167, 69, 0.25);
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    color: white;
    position: relative;
    overflow: hidden;
    z-index: 1;
    /* stay above any overlays */
}

.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.2), transparent);
    transition: left 0.5s;
}

.button-section .el-button:hover {
    background: linear-gradient(135deg, #20c997 0%, #28a745 100%);
    box-shadow: 0 6px 12px rgba(40, 167, 69, 0.35);
    transform: translateY(-2px);
}

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

.button-section .el-button:active {
    transform: translateY(0);
    box-shadow: 0 3px 6px rgba(40, 167, 69, 0.25);
}

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

/* Ensure button itself is not hidden by global rules */
.project-card .button-section .el-button {
    opacity: 1 !important;
    visibility: visible !important;
}

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

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

/* 响应式设计 */
@media (max-width: 1400px) {
    .el-col {
        width: 25% !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>
