<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="querySonarProjectConfigParam">
                <el-form-item label="项目名称">
                    <el-select v-model="querySonarProjectConfigParam.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 label="项目所属团队">
                    <el-select v-model="querySonarProjectConfigParam.team" filterable placeholder="请选择项目所属团队" clearable>
                        <el-option v-for="(item, index) in teamList" :label="item" :value="item" :key="index"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="项目类型">
                    <el-select v-model="querySonarProjectConfigParam.projectType" filterable placeholder="请选择项目类型" clearable>
                        <el-option v-for="(item, index) in projectTypeList" :label="item" :value="item" :key="index"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="querySonarProjectConfigByPage">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="showCreateDialog">新增项目配置</el-button>
                </el-form-item>
            </el-form>
            <!--    列表    -->
            <el-table :data="sonarProjectConfigTable" style="{width: 100%}"
                      border :header-cell-style="{'text-align':'center'}" :cell-style="{'text-align':'center'}">
                <el-table-column type="index"/>
                <el-table-column prop="projectName" label="项目名称" min-width="200px" showOverflowTooltip/>
                <el-table-column prop="projectType" label="项目类型"/>
                <el-table-column prop="compileType" label="编译类型" :formatter="compileTypeFormatter" min-width="100px" showOverflowTooltip/>
                <el-table-column prop="checkType" label="检查类型" min-width="150px" showOverflowTooltip/>
                <el-table-column prop="projectAddr" label="项目地址" min-width="300px" showOverflowTooltip/>
                <el-table-column label="是否执行单测" min-width="100px">
                    <template v-slot="{row}">
                        {{ tagMap[row.ifTest] }}
                    </template>
                </el-table-column>
                <el-table-column prop="testJacocoPath" label="单测覆盖率报告路径" min-width="200px" showOverflowTooltip/>
                <el-table-column prop="checkExclusion" label="检查排除项" min-width="100px" showOverflowTooltip/>
                <el-table-column label="是否发送企微通知" min-width="120px">
                    <template v-slot="{row}">
                        {{ tagMap[row.isSend] }}
                    </template>
                </el-table-column>
                <el-table-column prop="team" label="项目所属团队" min-width="100px" showOverflowTooltip/>
                <el-table-column prop="channel" label="项目所属渠道" min-width="150px" showOverflowTooltip/>
                <el-table-column label="项目维护" align="center" min-width="100px">
                    <template v-slot="{row}">
                        <el-tooltip
                            effect="dark"
                            content="修改"
                            placement="top"
                            :enterable="false">
                            <el-button type="primary" icon="el-icon-edit" size="mini" circle @click="showEditDialog(row.id)"></el-button>
                        </el-tooltip>
                        <el-tooltip
                            effect="dark"
                            content="删除"
                            placement="top"
                            :enterable="false">
                            <el-button type="danger" icon="el-icon-delete" size="mini" circle @click="removeById(row.id)"></el-button>
                        </el-tooltip>
                    </template>
                </el-table-column>
                <el-table-column label="模块维护" align="center" min-width="333px">
                    <template v-slot="{row}">
                        <el-button type="primary" size="mini" v-if="row.projectType === 'JAVA'" @click="showCreateModuleDialog(row.id)">新增模块</el-button>
                        <el-button type="primary" size="mini" v-if="row.projectType === 'JAVA'" @click="showEditModuleDialog(row.id)">编辑模块</el-button>
                        <el-button type="primary" size="mini" v-if="row.projectType === 'JAVA'" @click="showAutoAnalysisDialog(row.id)">自动分析模块</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!--    分页    -->
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :page-sizes="[10, 20, 50, 100, 200]"
                :page-size="querySonarProjectConfigParam.size"
                :current-page="querySonarProjectConfigParam.page"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </el-card>
        <!--    新增项目的Dialog    -->
        <el-dialog
            title="新增项目配置"
            :visible.sync="createDialogVisible"
            width="1260px"
            top="8vh"
            @close="createDialogClosed">
            <div class="el-dialog-div">
                <el-form
                    :model="createSonarProjectConfigForm"
                    :rules="createSonarProjectConfigFormRules"
                    ref="createSonarProjectConfigFormRef"
                    label-width="170px">
                    <el-form-item label="项目名称" prop="projectName">
                        <el-input v-model.trim="createSonarProjectConfigForm.projectName"/>
                    </el-form-item>
                    <el-form-item label="项目地址" prop="projectAddr">
                        <el-input v-model.trim="createSonarProjectConfigForm.projectAddr" placeholder="ssh://"/>
                    </el-form-item>
                    <el-form-item label="包名" prop="packageName">
                        <el-input v-model.trim="createSonarProjectConfigForm.packageName" placeholder="com.qike366"/>
                    </el-form-item>
                    <el-form-item label="项目类型" prop="projectType">
                        <el-select v-model="createSonarProjectConfigForm.projectType" placeholder="请选择项目类型"
                                   @change="projectTypeChange(createSonarProjectConfigForm)" clearable>
                            <el-option v-for="(item, index) in projectTypeList" :label="item" :value="item" :key="index"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="编译类型" prop="compileType">
                        <el-select v-model="createSonarProjectConfigForm.compileType" placeholder="请选择项目类型" clearable>
                            <el-option v-for="(item, index) in compileTypeList" :label="item" :value="item" :key="index"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="是否执行单测" prop="ifTest" v-if="createSonarProjectConfigForm.projectType === 'JAVA'">
                        <el-radio-group v-model="createSonarProjectConfigForm.ifTest" size="medium">
                            <el-radio border :label="1">是</el-radio>
                            <el-radio border :label="0">否</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="单测覆盖率报告路径" prop="testJacocoPath" v-if="createSonarProjectConfigForm.ifTest === 1">
                        <el-input v-model.trim="createSonarProjectConfigForm.testJacocoPath" placeholder="/target/jacoco-ut/jacoco.xml"/>
                    </el-form-item>
                    <el-form-item label="检查类型" prop="checkType">
                        <el-select v-model="createSonarProjectConfigForm.checkType" placeholder="请选择项目类型" clearable>
                            <el-option v-for="(item, index) in checkTypeList" :label="item" :value="item" :key="index"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="检查排除项" prop="checkExclusion">
                        <el-input v-model.trim="createSonarProjectConfigForm.checkExclusion"/>
                        <p style="color: red; font-size: 12px">支持部分正则(*, **, ?), 多个排除项用英文逗号隔开, 示例: **/node_modules/**,**/src/services/**</p>
                    </el-form-item>
                    <el-form-item label="是否多模块项目" prop="isMultiModule">
                        <el-radio-group v-model="createSonarProjectConfigForm.isMultiModule" size="medium">
                            <el-radio border :label="1">是</el-radio>
                            <el-radio border :label="0">否</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="是否发送企微通知" prop="isSend">
                        <el-radio-group v-model="createSonarProjectConfigForm.isSend" size="medium">
                            <el-radio border :label="1">是</el-radio>
                            <el-radio border :label="0">否</el-radio>
                        </el-radio-group>
                        <p style="color: red; font-size: 12px">选择是, 则项目触发检查后, 如果存在新增问题, 则会发送企微通知</p>
                    </el-form-item>
                    <el-form-item label="项目所属团队" prop="team">
                        <el-select v-model="createSonarProjectConfigForm.team" placeholder="请选择项目所属团队" clearable>
                            <el-option v-for="(item, index) in teamList" :label="item" :value="item" :key="index"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="项目所属渠道" prop="channel">
                        <el-select v-model="createSonarProjectConfigForm.channel" placeholder="请选择项目所属渠道" clearable>
                            <el-option v-for="(item, index) in channelList" :label="item" :value="item" :key="index"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="CR名单" prop="crName">
                        <el-input v-model.trim="createSonarProjectConfigForm.crName"/>
                    </el-form-item>
                </el-form>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="createDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="createSonarProjectConfig">确 定</el-button>
            </span>
        </el-dialog>
        <!--    编辑项目的Dialog    -->
        <el-dialog
            title="修改项目"
            :visible.sync="editDialogVisible"
            width="1260px"
            top="8vh"
            @close="editDialogClosed">
            <div class="el-dialog-div">
                <el-form
                    :model="editSonarProjectConfigForm"
                    :rules="editSonarProjectConfigFormRules"
                    ref="editSonarProjectConfigFormRef"
                    label-width="170px">

                    <el-form-item label="项目名称" prop="projectName">
                        <el-input v-model.trim="editSonarProjectConfigForm.projectName"/>
                    </el-form-item>
                    <el-form-item label="项目地址" prop="projectAddr">
                        <el-input v-model.trim="editSonarProjectConfigForm.projectAddr"/>
                    </el-form-item>
                    <el-form-item label="包名" prop="packageName">
                        <el-input v-model.trim="editSonarProjectConfigForm.packageName" placeholder="com.qike366"/>
                    </el-form-item>
                    <el-form-item label="项目类型" prop="projectType">
                        <el-select v-model="editSonarProjectConfigForm.projectType" placeholder="请选择项目类型"
                                   @change="projectTypeChange(editSonarProjectConfigForm)"
                                   clearable>
                            <el-option v-for="(item, index) in projectTypeList" :label="item" :value="item" :key="index"/>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="编译类型" prop="compileType">
                        <el-select v-model="editSonarProjectConfigForm.compileType" placeholder="请选择编译类型" clearable>
                            <el-option v-for="(item, index) in compileTypeList" :label="item" :value="item" :key="index"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="检查类型" prop="checkType">
                        <el-select v-model="editSonarProjectConfigForm.checkType" placeholder="请选择项目类型" clearable>
                            <el-option v-for="(item, index) in checkTypeList" :label="item" :value="item" :key="index"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="是否多模块项目" prop="isMultiModule">
                        <el-radio-group v-model="editSonarProjectConfigForm.isMultiModule" size="medium">
                            <el-radio border :label="1">是</el-radio>
                            <el-radio border :label="0">否</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="是否执行单测" prop="ifTest" v-if="editSonarProjectConfigForm.projectType === 'JAVA'">
                        <el-radio-group v-model="editSonarProjectConfigForm.ifTest" size="medium">
                            <el-radio border :label="1">是</el-radio>
                            <el-radio border :label="0">否</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="单测覆盖率报告路径" prop="testJacocoPath" v-if="editSonarProjectConfigForm.ifTest === 1">
                        <el-input v-model.trim="editSonarProjectConfigForm.testJacocoPath" placeholder="/target/jacoco-ut/jacoco.xml"/>
                    </el-form-item>
                    <el-form-item label="检查排除项" prop="checkExclusion">
                        <el-input v-model.trim="editSonarProjectConfigForm.checkExclusion"/>
                        <p style="color: red; font-size: 12px">支持部分正则(*, **, ?), 多个排除项用英文逗号隔开, 示例: **/node_modules/**,**/src/services/**</p>
                    </el-form-item>
                    <el-form-item label="是否发送企微通知" prop="isSend">
                        <el-radio-group v-model="editSonarProjectConfigForm.isSend" size="medium">
                            <el-radio border :label="1">是</el-radio>
                            <el-radio border :label="0">否</el-radio>
                        </el-radio-group>
                        <p style="color: red; font-size: 12px">选择是, 则项目触发检查后, 如果存在新增问题, 则会发送企微通知</p>
                    </el-form-item>
                    <el-form-item label="项目所属团队" prop="team">
                        <el-select v-model="editSonarProjectConfigForm.team" placeholder="请选择项目所属团队" clearable>
                            <el-option v-for="(item, index) in teamList" :label="item" :value="item" :key="index"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="项目所属渠道" prop="channel">
                        <el-select v-model="editSonarProjectConfigForm.channel" placeholder="请选择项目所属渠道" clearable>
                            <el-option v-for="(item, index) in channelList" :label="item" :value="item" :key="index"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="CR名单" prop="crName">
                        <el-input v-model.trim="editSonarProjectConfigForm.crName"/>
                    </el-form-item>
                </el-form>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="editSonarProjectConfig">确 定</el-button>
            </span>
        </el-dialog>

        <!--    创建项目模块的Dialog    -->
        <el-dialog
            title="新增项目模块"
            :visible.sync="createModuleDialogVisible"
            width="1440px"
            top="8vh"
            @close="createModuleDialogClosed">
            <el-form ref="createModuleFormRef" :inline="true" :model="createModuleForm" label-width="120px">
                <div v-for="(item, index) in createModuleForm.moduleList" :key="index">
                    <el-form-item
                        label="模块名称"
                        style="width: 420px"
                        :prop="'moduleList.' + index + '.moduleName'"
                        :rules="{ required: true, message: '模块名称不能为空', trigger: 'blur' }">
                        <el-input style="width: 300px" v-model="item.moduleName"></el-input>
                    </el-form-item>
                    <el-form-item
                        label="Jar名称"
                        style="width: 420px"
                        :prop="'moduleList.' + index + '.jarName'"
                        :rules="{ required: true, message: 'Jar名称不能为空', trigger: 'blur' }">
                        <el-input style="width: 300px" v-model="item.jarName"></el-input>
                    </el-form-item>
                    <el-form-item
                        label="是否为服务模块"
                        label-width="150px"
                        style="width: 420px"
                        :prop="'moduleList.' + index + '.isServer'"
                        :rules="{ required: true, message: '是否服务模块为必选项', trigger: 'blur' }">
                        <el-radio-group style="width: 162px" v-model="item.isServer">
                            <el-radio border :label="1">是</el-radio>
                            <el-radio border :label="0">否</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item style="width: 122px">
                        <el-button v-if="index+1 === createModuleForm.moduleList.length" @click="addCreateModule" type="primary">增加</el-button>
                        <el-button v-if="index !== 0" @click="deleteCreateModule(item, index)" type="danger">删除</el-button>
                    </el-form-item>
                </div>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="createModuleDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="createModuleForProject">确 定</el-button>
            </span>
        </el-dialog>
        <!--    编辑项目模块的Dialog    -->
        <el-dialog
            title="编辑项目模块"
            :visible.sync="editModuleDialogVisible"
            width="1440px"
            top="8vh"
            @close="editModuleDialogClosed">
            <el-form ref="editModuleFormRef" :inline="true" :model="editModuleForm" label-width="120px">
                <div v-for="(item, index) in editModuleForm.moduleList" :key="index">
                    <el-form-item
                        label="模块名称"
                        style="width: 420px"
                        :prop="'moduleList.' + index + '.moduleName'"
                        :rules="{ required: true, message: '模块名称不能为空', trigger: 'blur' }">
                        <el-input style="width: 300px" v-model="item.moduleName"></el-input>
                    </el-form-item>
                    <el-form-item
                        label="Jar名称"
                        style="width: 420px"
                        :prop="'moduleList.' + index + '.jarName'"
                        :rules="{ required: true, message: 'Jar名称不能为空', trigger: 'blur' }">
                        <el-input style="width: 300px" v-model="item.jarName"></el-input>
                    </el-form-item>
                    <el-form-item
                        label="是否为服务模块"
                        label-width="150px"
                        style="width: 420px"
                        :prop="'moduleList.' + index + '.isServer'"
                        :rules="{ required: true, message: '是否服务模块为必选项', trigger: 'blur' }">
                        <el-radio-group style="width: 162px" v-model="item.isServer">
                            <el-radio border :label="true">是</el-radio>
                            <el-radio border :label="false">否</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item style="width: 122px">
                        <el-button v-if="index+1 === editModuleForm.moduleList.length" @click="addEditModule" type="primary">增加</el-button>
                        <el-button v-if="index !== 0" @click="deleteEditModule(item, index)" type="danger">删除</el-button>
                    </el-form-item>
                </div>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editModuleDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="editModuleForProject">确 定</el-button>
            </span>
        </el-dialog>

        <!--    自动分析模块的分支填写Dialog    -->
        <el-dialog
            title="请填写分析项目使用的分析"
            :visible.sync="autoAnalysisDialogVisible"
            top="8vh"
            width="30%"
            :before-close="autoAnalysisDialogClose">
            <el-input v-model="autoAnalysisBranch" placeholder="master" clearable/>
            <span slot="footer" class="dialog-footer">
                <el-button @click="autoAnalysisDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="autoAnalysis">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    name: "ProjectPage",
    data() {
        return {
            // 分页查询参数
            querySonarProjectConfigParam: {
                projectName: null,
                page: 1,
                size: 10
            },
            total: 0,
            // 项目配置列表数据
            sonarProjectConfigTable: [],
            // 项目名称列表, 用户搜索提示
            projectNameList: [],

            // 新增表单是否显示
            createDialogVisible: false,
            // 新增表单默认数据
            createSonarProjectConfigForm: {},
            // 新增表单校验
            createSonarProjectConfigFormRules: {
                projectName: [
                    {required: true, message: '请输入项目名称', trigger: 'blur'},
                ],
                projectAddr: [
                    {required: true, message: '请输入项目地址', trigger: 'blur'},
                ],
                projectType: [
                    {required: true, message: '请输入项目类型', trigger: 'blur'},
                ],
                compileType: [
                    {required: true, message: '请输入编译类型', trigger: 'blur'},
                ],
                checkType: [
                    {required: true, message: '请输入检查类型', trigger: 'blur'},
                ],
                team: [
                    {required: true, message: '请输入项目所属团队', trigger: 'blur'},
                ],
                channel: [
                    {required: true, message: '请输入项目所属渠道', trigger: 'blur'},
                ],
                testJacocoPath: [
                    {required: true, message: '请输入单测覆盖率路径', trigger: 'blur'},
                ],
                ifTest: [
                    {required: true, message: '请选择是否执行单测', trigger: 'blur'},
                ],
                isSend: [
                    {required: true, message: '请选择检查后是否发送企微通知', trigger: 'blur'},
                ]
            },

            // 编辑表单是否显示
            editDialogVisible: false,
            // 编辑表单默认数据
            editSonarProjectConfigForm: {},
            // 编辑表单校验
            editSonarProjectConfigFormRules: {
                projectName: [
                    {required: true, message: '请输入项目名称', trigger: 'blur'},
                ],
                projectAddr: [
                    {required: true, message: '请输入项目地址', trigger: 'blur'},
                ],
                projectType: [
                    {required: true, message: '请输入项目类型', trigger: 'blur'},
                ],
                compileType: [
                    {required: true, message: '请输入编译类型', trigger: 'blur'},
                ],
                checkType: [
                    {required: true, message: '请输入检查类型', trigger: 'blur'},
                ],
                team: [
                    {required: true, message: '请输入项目所属团队', trigger: 'blur'},
                ],
                channel: [
                    {required: true, message: '请输入项目所属渠道', trigger: 'blur'},
                ],
                testJacocoPath: [
                    {required: true, message: '请输入单测覆盖率路径', trigger: 'blur'},
                ],
                isMultiModule: [
                    {required: true, message: '请选择该项目是否为多模块', trigger: 'blur'},
                ],
                ifTest: [
                    {required: true, message: '请选择是否执行单测', trigger: 'blur'},
                ],
                isSend: [
                    {required: true, message: '请选择检查后是否发送企微通知', trigger: 'blur'},
                ]
            },
            // 项目类型
            projectTypeList: ['JAVA', 'REACT', 'PYTHON'],
            // 编译类型
            compileTypeList: [],
            javaCompileTypeList: ['MAVEN', 'GRADLE', 'NOT_COMPILE'],
            webCompileTypeList: ['YARN', 'NOT_COMPILE'],
            pythonCompileTypeList: ['NOT_COMPILE'],
            // 检查类型
            checkTypeList: ['MAVEN_SONAR', 'GRADLE_SONAR', 'SONAR_SCANNER'],
            // 是否通知
            tagMap: {
                0: '否',
                1: '是'
            },
            channelList: [],
            // 团队集合
            teamList: [],


            createModuleDialogVisible: false,
            tempCreateProjectConfigId: 0, // 记录打开创建模块的弹框时的配置id
            createModuleForm: {
                moduleList: [
                    {
                        projectConfigId: 0,
                        moduleName: "",
                        jarName: "",
                        isServer: false
                    }
                ]
            },
            editModuleDialogVisible: false,
            tempEditProjectConfigId: 0,
            editModuleForm: {
                moduleList: []
            },

            // 自动分析模块的分支选择框
            autoAnalysisDialogVisible: false,
            // 自动分析模块的默认分支
            autoAnalysisBranch: 'dev',
            projectConfigId: 0,
        }
    },
    methods: {
        // 列表中项目类型展示处理
        compileTypeFormatter(row, column, colValue) {
            return colValue === 'NOT_COMPILE' ? '不执行编译' : colValue
        },
        // 查询所有项目名称
        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;
        },
        // 分页查询
        async querySonarProjectConfigByPage() {
            const res = await this.$api.post("/sonar/project/config/page", this.querySonarProjectConfigParam)
            if (res.code !== 200 || res.data === null) return this.$message.error(res.message)
            this.sonarProjectConfigTable = res.data.records
            this.total = res.data.total
        },
        // 修改页大小
        handleSizeChange(newSize) {
            this.querySonarProjectConfigParam.size = newSize
            this.querySonarProjectConfigByPage()
        },
        // 修改页码
        handleCurrentChange(newPage) {
            this.querySonarProjectConfigParam.page = newPage
            this.querySonarProjectConfigByPage()
        },
        // 根据id删除项目
        async removeById(id) {
            const confirmResult = await this.$confirm(
                '此操作将永久删除该记录, 是否继续?',
                '提示',
                {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                }
            ).catch((err) => err)

            if (confirmResult === 'cancel') {
                return this.$message.info('已取消删除！')
            }
            // 确认删除时, 调用接口
            if (confirmResult === 'confirm') {
                const res = await this.$api.postForm("/sonar/project/config/removeById", {id: id})
                if (res.code !== 200 || res.data === null) return this.$message.error(res.message)
                this.$message.success('项目删除成功！')
                await this.querySonarProjectConfigByPage()
            }
        },

        // 显示新增Dialog
        async showCreateDialog() {
            await this.queryAllChannel()
            this.createDialogVisible = true
        },
        // 新增Dialog关闭时重置表单
        createDialogClosed() {
            this.$refs.createSonarProjectConfigFormRef.resetFields()
        },
        // 新增项目配置
        createSonarProjectConfig() {
            this.$refs.createSonarProjectConfigFormRef.validate(async (valid) => {
                if (!valid) return
                // 校验通过, 先查询该项目是否存在
                const queryRes = await this.$api.postForm("/sonar/project/config/getByProjectAddr", {projectAddr: this.createSonarProjectConfigForm.projectAddr})
                if (queryRes.data !== null) {
                    return this.$message.error("项目已经存在, 请确认")
                }
                // 新增项目不存在, 则保存
                const res = await this.$api.post(
                    "/sonar/project/config/save",
                    this.createSonarProjectConfigForm
                )
                if (res.code !== 200) return this.$message.error('新增项目信息失败！')
                this.createDialogVisible = false
                await this.querySonarProjectConfigByPage()
                this.$message.success('新增项目信息成功！')
            })
        },

        // 显示编辑Dialog
        async showEditDialog(id) {
            const res = await this.$api.get(`/sonar/project/config/getById?id=${id}`)
            if (res.code !== 200 || res.data === null) return this.$message.error(res.message)
            this.editSonarProjectConfigForm = res.data
            await this.queryAllChannel()

            // 根据当前编辑的项目的 项目类型, 修改编译类型的可选项内容
            if (this.editSonarProjectConfigForm.projectType === 'REACT') {
                this.compileTypeList = this.webCompileTypeList
            }
            if (this.editSonarProjectConfigForm.projectType === 'JAVA') {
                this.compileTypeList = this.javaCompileTypeList
            }
            if (this.editSonarProjectConfigForm.projectType === 'PYTHON') {
                this.compileTypeList = this.pythonCompileTypeList
            }
            this.editDialogVisible = true
        },
        // 编辑Dialog关闭时重置表单
        editDialogClosed() {
            this.$refs.editSonarProjectConfigFormRef.resetFields()
        },
        // 编辑项目配置
        editSonarProjectConfig() {
            this.$refs.editSonarProjectConfigFormRef.validate(async (valid) => {
                if (!valid) return
                const res = await this.$api.post(
                    "/sonar/project/config/updateById",
                    this.editSonarProjectConfigForm
                )
                if (res.code !== 200) return this.$message.error('更新项目信息失败！')
                this.editDialogVisible = false
                await this.querySonarProjectConfigByPage()
                this.$message.success('更新项目信息成功！')
            })
        },
        // 选择项目类型时, 修改编译类型的可选项内容
        projectTypeChange(sonarProjectConfigForm) {
            if (sonarProjectConfigForm.projectType === 'REACT') {
                this.compileTypeList = this.webCompileTypeList
            }
            if (sonarProjectConfigForm.projectType === 'JAVA') {
                this.compileTypeList = this.javaCompileTypeList
            }
            if (sonarProjectConfigForm.projectType === 'PYTHON') {
                this.compileTypeList = this.pythonCompileTypeList
            }
            // 编辑的时候,修改项目类型,不修改原始的编译类型数据
            if (sonarProjectConfigForm === this.createSonarProjectConfigForm && sonarProjectConfigForm.compileType !== undefined) {
                sonarProjectConfigForm.compileType = undefined
            }
        },
        // 查询所有部门名称,用于创建和修改时选择团队
        async queryAllDepartmentName() {
            const res = await this.$api.get('/sonar/department/getAllName')
            if (res.code !== 200 || res.data === null) return this.$message.error(res.message)
            this.teamList = res.data
        },
        // 查询所有渠道
        async queryAllChannel() {
            const res = await this.$api.get('/sonar/department/getAllChannel')
            if (res.code !== 200 || res.data === null) return this.$message.error(res.message)
            this.channelList = res.data
        },

        // -------------------- 模块部分 --------------------------------
        showCreateModuleDialog(projectConfigId) {
            this.createModuleDialogVisible = true
            this.tempCreateProjectConfigId = projectConfigId
            this.createModuleForm.moduleList[0].projectConfigId = projectConfigId
        },
        createModuleDialogClosed() {
            this.createModuleDialogVisible = false
            this.createModuleForm = {
                moduleList: [
                    {
                        projectConfigId: 0,
                        moduleName: "",
                        jarName: "",
                        isServer: false
                    }
                ]
            }
        },
        addCreateModule() {
            this.createModuleForm.moduleList.push({
                projectConfigId: this.tempCreateProjectConfigId,
                moduleName: "",
                jarName: "",
                isServer: false
            });
        },
        deleteCreateModule(item, index) {
            this.createModuleForm.moduleList.splice(index, 1);
        },
        createModuleForProject() {
            this.$refs.createModuleFormRef.validate(async (valid) => {
                if (!valid) return
                const res = await this.$api.post("/sonar/project/module/config/save", this.createModuleForm)
                if (res.code !== 200) return this.$message.error('新增项目模块失败！')
                this.createModuleDialogVisible = false
                this.$message.success('新增项目模块成功！')
            });
        },
        editModuleDialogClosed() {
            this.editModuleDialogVisible = false
            this.tempEditProjectConfigId = 0
        },
        addEditModule() {
            this.editModuleForm.moduleList.push({
                projectConfigId: this.tempEditProjectConfigId,
                moduleName: "",
                jarName: "",
                isServer: false
            });
        },
        deleteEditModule(item, index) {
            this.editModuleForm.moduleList.splice(index, 1);
        },
        async showEditModuleDialog(projectConfigId) {
            const res = await this.$api.get("/sonar/project/module/config/getById", {projectConfigId: projectConfigId})
            if (res.code !== 200) return this.$message.error('新增项目模块失败！')
            if (res.data && res.data.length > 0) {
                this.tempEditProjectConfigId = res.data[0].projectConfigId
            }
            this.editModuleForm.moduleList = res.data
            if (this.editModuleForm.moduleList.length !== 0) {
                this.editModuleDialogVisible = true
            } else {
                this.$message.error('项目尚未配置模块!')
            }
        },
        editModuleForProject() {
            if (this.editModuleForm.moduleList.length === 0) {
                this.editModuleDialogVisible = false
            }
            this.$refs.editModuleFormRef.validate(async (valid) => {
                if (!valid) return
                const res = await this.$api.post("/sonar/project/module/config/saveOrUpdate", this.editModuleForm)
                if (res.code !== 200) return this.$message.error('新增项目模块失败！')
                this.editModuleDialogVisible = false
                this.$message.success('新增项目模块成功！')
            });
        },
        showAutoAnalysisDialog(id) {
            this.autoAnalysisDialogVisible = true
            this.projectConfigId = id

        },
        // 自动分析项目模块
        async autoAnalysis() {
            const res = await this.$api.postForm('/sonar/project/config/analyzeByProjectConfigId',
                {
                    id: this.projectConfigId,
                    branch: this.autoAnalysisBranch
                }
            )
            if (res.code !== 200) return this.$message.error(res.message)
            this.autoAnalysisDialogVisible = false
            this.$message.info("项目模块分析中, 请稍等!")
        },
        autoAnalysisDialogClose() {
            this.autoAnalysisBranch = 'dev'
            this.autoAnalysisDialogVisible = false
        }
    },
    created() {
        this.queryAllDepartmentName()
        this.querySonarProjectConfigByPage()
        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;
    }

    .el-table /deep/ .cell {
        white-space: nowrap;
    }

    .el-pagination {
        text-align: center;
        margin-top: 15px;
    }
}

.el-dialog {

    .el-form {
        .el-form-item {
            margin: 10px auto;
        }
    }

    .el-dialog-div {
        height: 65vh;
        overflow: auto;
    }
}
</style>
