<template>
    <div>
        <!-- 面包屑导航 -->
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>代码质量管理</el-breadcrumb-item>
            <el-breadcrumb-item>代码检测问题汇总</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card>
            <!--    搜索    -->
            <el-form :inline="true" :model="recordInfo">
                <el-form-item label="项目名称">
                    <el-select v-model="recordInfo.projectName" filterable placeholder="请选择项目名称" clearable>
                        <el-option v-for="(item, index) in projectNameList" :label="item" :value="item" :key="index"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="queryMRRecordByPage">查询</el-button>
                </el-form-item>
            </el-form>
            <!--    列表    -->
            <el-table :data="recordTable" style="width: 100%" border :header-cell-style="{'text-align':'center'}" :cell-style="{'text-align':'center'}">
                <el-table-column type="index"/>
                <el-table-column prop="gitProjectName" label="项目名称" min-width="50px" showOverflowTooltip/>
                <el-table-column prop="gitProjectHttpUrl" label="项目地址" min-width="100px" showOverflowTooltip/>
                <el-table-column prop="sonarProjectTag" label="项目唯一标识" min-width="40px" showOverflowTooltip/>
                <el-table-column prop="bugs" label="总Bug数" min-width="20px" showOverflowTooltip/>
                <el-table-column prop="codeSmells" label="总异味数" min-width="20px" showOverflowTooltip/>
                <el-table-column prop="vulnerabilities" label="总漏洞数" min-width="20px" showOverflowTooltip/>
                <el-table-column prop="version" label="版本号" min-width="70px" showOverflowTooltip/>
                <el-table-column label="操作" align="center" min-width="30px" showOverflowTooltip>
                    <template slot-scope="scope">
                        <div v-if="scope.row.bugs === 0 && scope.row.codeSmells === 0 && scope.row.vulnerabilities === 0" class="perfect">
                            <span>完美项目</span>
                        </div>
                        <div v-else>
                            <span @click="toSummaryIssuesDetail(scope.row.gitProjectName, scope.row.eventType)" class="issuesDetail">查看问题</span>
                        </div>
                    </template>
                </el-table-column>
            </el-table>
            <!--    分页    -->
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="recordInfo.page"
                :page-sizes="[10, 20, 30, 40, 50, 100]"
                :page-size="recordInfo.size"
                layout="total, sizes, prev, pager, next, jumper"
                :total="recordTotal">
            </el-pagination>
        </el-card>
    </div>
</template>

<script>
export default {
    name: "SonarSummaryIssues",
    data() {
        return {
            // record 分页查询默认参数
            recordInfo: {
                projectName: null,
                page: 1,
                size: 10,
            },
            // record列表数据默认总数
            recordTotal: 0,
            // record列表默认数据
            recordTable: [],
            // 项目名称列表, 用户搜索提示
            projectNameList: [],
        }
    },
    methods: {
        // record表 mr类型的数据分页查询
        async queryMRRecordByPage(){
            const respage = await this.$api.postForm('/sonar/record/page', this.recordInfo)
            this.recordTable = respage.data.records
            this.recordTotal = respage.data.total;
        },
        // 修改页大小
        handleSizeChange(size) {
            this.recordInfo.size = size;
            this.queryMRRecordByPage()
        },
        // 修改当前页
        handleCurrentChange(index) {
            this.recordInfo.page = index;
            this.queryMRRecordByPage()
        },
        // 查询所有项目名称
        async queryAllProjectName() {
            const res = await this.$api.get("/sonar/project/config/queryAllProjectName")
            if (res.code !== 200) return this.$message.error(res.message)
            this.projectNameList = res.data;
        },
        // 查询指定项目的问题汇总
        toSummaryIssuesDetail(projectName, eventType) {
            this.$router.push(
                {
                    path:'/sonar/summaryIssuesDetail',
                    query: {
                        projectName: projectName,
                        eventType: eventType
                    }
                }
            );
        }
    },
    created() {
        this.queryMRRecordByPage()
        this.queryAllProjectName();
    }
}
</script>
<style lang="less" scoped>
    .el-breadcrumb {
        height: 32px;
        line-height: 32px;
    }
    .el-card {
        box-shadow: 0 1px 1px rgba(0, 0, 0, 0.15) !important;

        .el-table {
            margin-top: 15px;
            margin-bottom: 15px;

            .perfect {
                color: #67c23a;
                font-weight: 500;
            }
            .issuesDetail {
                color: #F56C6C;
                font-weight: 500;
            }
            .issuesDetail:hover {
                border-bottom: 1px solid #F56C6C;
                cursor: pointer;
            }
            /deep/ .cell {
                white-space: nowrap;
            }
        }
        .el-pagination {
            text-align: center;
            margin-top: 15px;
        }
    }
</style>
