<template>
  <div>
    <el-card>
      <div class="top">
        JIRA项目:
        <el-select v-model="queryInfo.jiraProjectKey" placeholder="请选择JIRA项目" clearable>
          <el-option v-for="item in jiraProjectList" :key="item.key" :label="item.name" :value="item.key"></el-option>
        </el-select>
        环境名称:
        <el-select v-model="queryInfo.namespace" placeholder="请选择环境" clearable>
          <el-option v-for="item in namespaceList" :key="item.name" :label="item.name" :value="item.name"></el-option>
        </el-select>
        <!-- 新增状态查询 -->
        状 态:
        <el-select v-model="queryInfo.status" placeholder="请选择状态" clearable>
          <el-option v-for="item in statusList" :key="item.label" :label="item.label" :value="item.value"></el-option>
        </el-select>
        <el-input v-model="queryInfo.projectName" clearable placeholder="请输入项目名称" style="width: 300px;">
        </el-input>
        <el-button type="primary" style="margin-left:10px" icon="el-icon-search" @click="queryData">查询</el-button>
        <el-button type="primary" style="margin-left:10px" icon="el-icon-circle-plus-outline" @click="addPipeline"
          v-permission="('qa:process:add')">新增</el-button>
      </div>

      <el-table :data="dingRobotList" border style="width: 100%">
        <el-table-column type="index" label="序号" width="50px">
          <template slot-scope="scope">
            <span>{{(queryInfo.pageNum -1 ) * queryInfo.pageSize + scope.$index + 1}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="dingRobot.projectName" label="项目名" width="120px"></el-table-column>
        <el-table-column label="服务->分支" width="250px">
          <template slot-scope="scope">
            <div v-for="item in scope.row.pipelineList" :key="item.id"><b>{{ item.serviceName }}</b> ->
              {{ item.serviceBranch }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="dingRobot.namespace" label="测试环境" width="80px"></el-table-column>
        <el-table-column prop="dingRobot.status" label="状 态" width="80px">
          <template slot-scope="scope">
            <p v-if="scope.row.dingRobot.status===1">进行中</p>
            <p v-if="scope.row.dingRobot.status===2">已完成</p>
            <p v-if="scope.row.dingRobot.status===3">已挂起</p>
          </template>
        </el-table-column>
        <el-table-column prop="dingRobot.creator" label="创建人" width="80px"></el-table-column>
        <el-table-column prop="dingRobot.jiraProjectName" label="JIRA项目" width="100px"></el-table-column>
        <el-table-column prop="dingRobot.createTime" label="创建时间" width="175px"></el-table-column>
        <el-table-column label="操作">
          <slot slot-scope="scope">
            <el-button type="primary" size="small" @click="openEditDialog(scope.row)"
              v-permission="('qa:process:edit')">编辑</el-button>
            <el-button type="warning" size="small" @click="openSendSmokingDialog(scope.row)"
              v-permission="('qa:process:smoke')" style="margin-left:1px">冒烟测试</el-button>
            <el-button type="success" size="small" @click="openSendDialog(scope.row)"
              v-permission="('qa:process:schedule')" style="margin-left:1px">发送进度</el-button>

            <!-- 完成按钮 -->
            <el-button v-if="scope.row.dingRobot.status===1||scope.row.dingRobot.status===3" type="success" size="small"
              @click="finishDialog(scope.row)" v-permission="('qa:process:finish')" style="margin-left:1px">完成</el-button>

            <!-- 添加刮起按钮 -->
            <el-button v-if="scope.row.dingRobot.status===1" type="info" size="small"
              @click="HangDialog(scope.row.dingRobot.id)" style="margin-left:1px">挂起
            </el-button>
            <el-button v-if="scope.row.dingRobot.status===3" type="info" size="small" @click="EnableDialog(scope.row)">
              启用
            </el-button>

            <el-button type="danger" size="small" @click="delProjectRobot(scope.row.dingRobot.id)"
              v-permission="('qa:process:del')">删除</el-button>
          </slot>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 分页 -->
    <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
      :current-page.sync="queryInfo.pageNum" :page-size="queryInfo.pageSize" layout="total, prev, pager, next"
      :total="totalNum"></el-pagination>
    <!-- 新增对话框 -->
    <el-dialog title="新增测试流程" :visible.sync="addDialogVisible" width="60%" @close="closeAddDialog">
      <el-form ref="addRobotFormRef" :rules="formRules" :model="pipelineFrom" label-width="150px">
        <el-form-item label="jira项目" prop="dingRobot.jiraProjectKey">
          <el-select v-model="pipelineFrom.dingRobot.jiraProjectKey" placeholder="请选择Jira项目" @change="getProjectName">
            <el-option v-for="item in jiraProjectList" :key="item.key" :label="item.name" :value="item.key">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="项目名称" prop="dingRobot.projectName">
          <el-select v-model="pipelineFrom.dingRobot.projectName" placeholder="请选择项目" @change="getDetail">
            <el-option v-for="item in projectNameList" :key="item.id" :label="item.projectName"
              :value="item.projectName">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="钉钉通知地址" prop="dingRobot.dingUrl">
          <el-input v-model="pipelineFrom.dingRobot.dingUrl" placeholder="请输入钉钉通知地址" type="textarea" :rows="3">
          </el-input>
        </el-form-item>
        <el-form-item label="环 境" prop="dingRobot.namespace">
          <el-select v-model="pipelineFrom.dingRobot.namespace" placeholder="请选择环境" filterable clearable>
            <el-option v-for="item in namespaceList" :key="item.name" :label="item.name" :value="item.name">
            </el-option>
          </el-select>
        </el-form-item>
        <!-- 添加 Pipeline -->
        <el-form-item label="Pipeline">
          <el-row>
            <el-col :span="6">
              <div style="text-align: center">
                服务
              </div>
            </el-col>
            <el-col :span="6">
              <div style="text-align: center">
                分支
              </div>
            </el-col>
            <el-col :span="6">
              <div style="text-align: center">
                自动部署
              </div>
            </el-col>
            <el-col :span="6">
              <div>
                操作
              </div>
            </el-col>
          </el-row>
          <el-row v-for="(item, index) in pipelineFrom.pipelineList" :key="index">
            <el-col :span="6">
              <div style="text-align: center;margin-top:5px">
                <el-select v-model="item.serviceName" filterable placeholder="请选择服务"
                  @change="selectServiceModel(item.serviceName)" clearable>
                  <el-option v-for="item2 in serviceList" :key="item2.name" :label="item2.name" :value="item2.name">
                  </el-option>
                </el-select>
              </div>
            </el-col>
            <el-col :span="6">
              <div style="text-align: center;margin-top:5px">
                <el-select v-model="item.serviceBranch" filterable placeholder="请选择分支" clearable
                  @focus="focusevent(item.serviceName)">
                  <el-option v-for="item2 in serviceBranchList" :key="item2.name" :label="item2.name"
                    :value="item2.name"></el-option>
                </el-select>
              </div>
            </el-col>
            <el-col :span="6">
              <div style="text-align: center;margin-top:5px">
                <el-switch v-model="item.enable" active-color="#13ce66" inactive-color="#A9A9A9" active-text="是"
                  inactive-text="否" :active-value="1" :inactive-value="0"></el-switch>
              </div>
            </el-col>
            <el-col :span="6">
              <div style="margin-left: -15px;margin-top:5px">
                <el-button size="mini" icon="el-icon-delete" @click="pipelineDelete(index)">删除</el-button>
              </div>
            </el-col>
          </el-row>
          <el-row>
            <el-button size="mini" type="primary" icon="el-icon-circle-plus-outline" @click="AddPipelineListRow()"
              class="btn-style">添加</el-button>
          </el-row>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addRobotProject">保 存</el-button>
      </span>
    </el-dialog>

    <!-- 编辑对话框 -->
    <el-dialog title="编辑测试流程" :visible.sync="editDialogVisible" width="60%" @close="closeEditDialog">
      <el-form ref="editRobotFormRef" :rules="formRules" :model="editPipelineForm" label-width="150px">
        <el-form-item label="jira项目" prop="dingRobot.jiraProjectKey">
          <el-select v-model="editPipelineForm.dingRobot.jiraProjectKey" placeholder="请选择Jira项目"
            @change="getEditProjectName">
            <el-option v-for="item in jiraProjectList" :key="item.key" :label="item.name" :value="item.key">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="项目名称" prop="dingRobot.projectName">
          <el-select v-model="editPipelineForm.dingRobot.projectName" placeholder="请选择项目" @change="getEditDetail">
            <el-option v-for="item in editProjectNameList" :key="item.id" :label="item.projectName"
              :value="item.projectName">
            </el-option>
          </el-select>
        </el-form-item>
        <el-tooltip content="钉钉机器人关键字设置为'测试'" placement="left-start" effect="light">
          <el-form-item label="钉钉通知地址" prop="dingRobot.dingUrl">
            <el-input v-model="editPipelineForm.dingRobot.dingUrl" placeholder="请输入钉钉通知地址" type="textarea" :rows="3">
            </el-input>
          </el-form-item>
        </el-tooltip>
        <el-form-item label="环 境" prop="dingRobot.namespace">
          <el-select v-model="editPipelineForm.dingRobot.namespace" placeholder="请选择环境" filterable clearable>
            <el-option v-for="item in namespaceList" :key="item.name" :label="item.name" :value="item.name">
            </el-option>
          </el-select>
        </el-form-item>
        <!-- 编辑pipeline -->
        <el-form-item label="Pipeline">
          <el-row>
            <el-col :span="6">
              <div style="text-align: center">
                服务
              </div>
            </el-col>
            <el-col :span="6">
              <div style="text-align: center">
                分支
              </div>
            </el-col>
            <el-col :span="6">
              <div style="text-align: center">
                自动部署
              </div>
            </el-col>
            <el-col :span="6">
              <div>
                操作
              </div>
            </el-col>
          </el-row>
          <el-row v-for="(item, index) in editPipelineForm.pipelineList" :key="index">
            <el-col :span="6">
              <div style="text-align: center;margin-top:5px">
                <el-select v-model="item.serviceName" filterable placeholder="请选择服务"
                  @change="selectServiceModel(item.serviceName)" clearable>
                  <el-option v-for="item2 in serviceList" :key="item2.name" :label="item2.name" :value="item2.name">
                  </el-option>
                </el-select>
              </div>
            </el-col>
            <el-col :span="6">
              <div style="text-align: center;margin-top:5px">
                <el-select v-model="item.serviceBranch" filterable placeholder="请选择分支" clearable
                  @focus="focusevent(item.serviceName)">
                  <el-option v-for="item2 in serviceBranchList" :key="item2.name" :label="item2.name"
                    :value="item2.name"></el-option>
                </el-select>
              </div>
            </el-col>
            <el-col :span="6">
              <div style="text-align: center;margin-top:5px">
                <el-switch v-model="item.enable" active-color="#13ce66" inactive-color="#A9A9A9" active-text="是"
                  inactive-text="否" :active-value="1" :inactive-value="0"></el-switch>
              </div>
            </el-col>
            <el-col :span="6">
              <div style="margin-left: -15px;margin-top:5px">
                <el-button size="mini" icon="el-icon-delete" @click="editPipelineDelete(index)">删除</el-button>
              </div>
            </el-col>
          </el-row>
          <el-button size="mini" type="primary" @click="EditPipelineListRow()" icon="el-icon-circle-plus-outline"
            class="btn-style">添加</el-button>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editRobotProject">保 存</el-button>
      </span>
    </el-dialog>

    <!-- 发送进度对话框 -->
    <el-dialog title="发送进度报告" :visible.sync="sendScheduleDialogVisible" width="60%" @close="closeSendDialog">
      <el-form ref="sendFormRef" :rules="sendFormRules" :model="sendScheduleForm" label-width="150px">
        <el-form-item label="测试进度" prop="progress">
          <el-input v-model="sendScheduleForm.progress" maxlength="3" placeholder="请输入测试进度百分比（不需要%）"></el-input>
        </el-form-item>
        <el-form-item label="风险点" prop="risk">
          <el-input v-model="sendScheduleForm.risk" type="textarea" placeholder="请输入测试风险点（非必填）" :rows="3"></el-input>
        </el-form-item>
      </el-form>

      <p>未解决BUG列表：</p>
      <el-table :data="scheduleUnsolvedIssueList" border style="width: 100%">
        <el-table-column prop="key" label="关键字" width="150">
        </el-table-column>
        <el-table-column prop="summary" label="标题">
        </el-table-column>
        <el-table-column prop="priority" label="优先级" width="100">
        </el-table-column>
        <el-table-column prop="assignee" label="经办人" width="80">
        </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button @click="sendScheduleDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="sendSchedule">发 送</el-button>
      </span>
    </el-dialog>

    <!-- 冒烟测试结果对话框 -->
    <el-dialog title="发送冒烟测试结果" :visible.sync="smokingDialogVisible" width="60%" @close="closeSmokingDialog">
      <el-form ref="smokingFormRef" :rules="smokingFormRules" :model="sendSmokingForm" label-width="150px">
        <el-form-item label="冒烟测试结果" prop="result">
          <el-select v-model="sendSmokingForm.result" placeholder="请选择冒烟测试结果">
            <el-option v-for="item in smokingResult" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="冒烟测试进度" prop="progress">
          <el-input v-model="sendSmokingForm.progress" maxlength="3" placeholder="请输入测试进度百分比（不需要%）"></el-input>
        </el-form-item>
        <el-form-item label="风险点" prop="risk">
          <el-input v-model="sendSmokingForm.risk" type="textarea" placeholder="请输入测试风险点（非必填）" :rows="3"></el-input>
        </el-form-item>
      </el-form>

      <p>未解决BUG列表：</p>
      <el-table :data="smokingUnsolvedIssueList" border style="width: 100%">
        <el-table-column prop="key" label="关键字" width="150">
        </el-table-column>
        <el-table-column prop="summary" label="标题">
        </el-table-column>
        <el-table-column prop="priority" label="优先级" width="100">
        </el-table-column>
        <el-table-column prop="assignee" label="经办人" width="80">
        </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button @click="smokingDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="sendSmoking">发 送</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getJiraProjectList,
  getPipelineList,
  addPipeline,
  editPipeline,
  sendScheduleReport,
  sendSmokingResult,
  getGitProjectList,
  getGitBranchList,
  getJiraIssueList,
  delPipeline,
  changePipelineStatus
} from '@/api/jira'
// import { getNamespaceList } from '@/api/getXyqbData'
import { getNamespaceList } from '@/api/k8s'
// import { getGitlabProject } from '@/api/qaApi'
import {
  detailTestDescription,
  getTestProjectNameList
} from '@/api/TestDesciption'

export default {
  data() {
    return {
      projectNameList: [],
      detailForm: {
        projectName: '',
        jiraProjectKey: ''
      },
      statusList: [
        {
          label: '进行中',
          value: 1
        },
        {
          label: '已完成',
          value: 2
        },
        {
          label: '已挂起',
          value: 3
        }
      ],
      dingRobotList: [],
      jiraProjectList: [],
      queryInfo: {
        pageNum: 1,
        pageSize: 10,
        jiraProjectKey: '',
        projectName: '',
        namespace: '',
        status: ''
      },
      totalNum: 0,
      addDialogVisible: false,
      editDialogVisible: false,
      sendScheduleDialogVisible: false,
      smokingDialogVisible: false,
      pipelineFrom: {
        dingRobot: {
          projectName: '',
          dingUrl: '',
          jiraProjectKey: '',
          namespace: '',
          status: 1,
          submitTestInstructionId: ''
        },
        pipelineList: []
      },
      // 编辑数据对象
      editPipelineForm: {
        dingRobot: {
          id: '',
          projectName: '',
          dingUrl: '',
          jiraProjectKey: '',
          namespace: '',
          status: '',
          submitTestInstructionId: ''
        },
        pipelineList: [
          {
            id: '',
            ding_robot_id: '',
            serviceName: '',
            serviceBranch: '',
            serviceType: '',
            namespace: '',
            enable: 1
          }
        ]
      },
      editRobotForm: {},
      sendProjectRobot: {},
      // 冒烟未解决BUG列表
      smokingUnsolvedIssueList: [],
      // 发送进度未解决BUG列表
      scheduleUnsolvedIssueList: [],
      // 发送进度报告参数
      sendScheduleData: {},
      // 发送冒烟结果参数
      sendSmokingData: {},
      delRobotParams: {
        dingRobotId: ''
      },
      sendScheduleForm: {},
      smokingResult: [
        {
          label: '冒烟通过',
          value: true
        },
        {
          label: '测试打回',
          value: false
        }
      ],
      formRules: {
        dingRobot: {
          projectName: [
            {
              required: true,
              message: '请输入项目名称',
              trigger: 'blur'
            }
          ],
          dingUrl: [
            {
              required: true,
              message: '请输入钉钉机器人url',
              trigger: 'blur'
            }
          ],
          jiraProjectKey: [
            {
              required: true,
              message: '请选择对应的Jira项目',
              trigger: 'blur'
            }
          ],
          namespace: [
            { required: true, message: '请输入创建人', trigger: 'blur' }
          ]
        }
      },
      sendFormRules: {
        progress: [
          {
            required: true,
            message: '请输入测试进度百分比，不需要%',
            trigger: 'blur'
          }
        ]
      },
      sendSmokingForm: {},
      smokingFormRules: {
        result: [
          {
            required: true,
            message: '请选择冒烟测试结果',
            trigger: 'blur'
          }
        ]
      },
      serviceName: '',
      serviceType: '',
      serviceList: [],
      serviceBranchList: [],
      serviceMode: {
        is_active: true,
        deploy_to_testenv: true
      },
      namespaceList: [],
      projectName: '',
      newBranchName: '',
      newIndex: 0,
      newList: [],
      env: window.sessionStorage.getItem('env'),
      editProjectNameList: [],
      detailEditForm: {
        projectName: '',
        jiraProjectKey: ''
      }
    }
  },
  methods: {
    // 根据jirakey获取项目名称
    getProjectName() {
      getTestProjectNameList({
        jiraProjectKey: this.pipelineFrom.dingRobot.jiraProjectKey
      }).then((resp) => {
        this.projectNameList = resp.data.data
      })
    },
    // 根据项目名称获取项目下的详细信息
    getDetail() {
      this.detailForm.jiraProjectKey =
        this.pipelineFrom.dingRobot.jiraProjectKey
      this.detailForm.projectName = this.pipelineFrom.dingRobot.projectName
      this.pipelineFrom.pipelineList = []
      detailTestDescription(this.detailForm).then((resp) => {
        var data = resp.data.data
        this.pipelineFrom.dingRobot.submitTestInstructionId = data.id
        var serviceForm = JSON.parse(data.serviceAndBranch)
        serviceForm.forEach((item) => {
          this.pipelineFrom.pipelineList.push({
            serviceName: item.serviceName,
            serviceBranch: item.branch,
            enable: 1
          })
        })
      })
    },
    // 编辑页面
    getEditProjectName() {
      getTestProjectNameList({
        jiraProjectKey: this.editPipelineForm.dingRobot.jiraProjectKey
      }).then((resp) => {
        this.editProjectNameList = resp.data.data
      })
    },
    // 编辑页面项目名称下拉框改变事件
    getEditDetail() {
      this.detailEditForm.jiraProjectKey =
        this.editPipelineForm.dingRobot.jiraProjectKey
      this.detailEditForm.projectName =
        this.editPipelineForm.dingRobot.projectName
      this.editPipelineForm.pipelineList = []
      detailTestDescription(this.detailEditForm).then((resp) => {
        var data = resp.data.data
        this.editPipelineForm.dingRobot.submitTestInstructionId = data.id
        var serviceForm = JSON.parse(data.serviceAndBranch)
        serviceForm.forEach((item) => {
          this.editPipelineForm.pipelineList.push({
            serviceName: item.serviceName,
            serviceBranch: item.branch,
            enable: 1
          })
        })
      })
    },

    focusevent(name) {
      getGitBranchList({ projectName: name }).then((resp) => {
        this.serviceBranchList = resp.data.data.data
      })
    },
    // 选择服务下拉框触发事件
    selectServiceModel(item) {
      getGitBranchList({ projectName: item }).then((resp) => {
        this.serviceBranchList = resp.data.data.data
      })
    },
    // 获取ding_robot列表
    getProjectRobotList() {
      getPipelineList(this.queryInfo).then((resp) => {
        this.dingRobotList = resp.data.data.list
        this.totalNum = resp.data.data.total
      })
    },
    // 获取jira项目列表
    getJiraProjectList() {
      getJiraProjectList().then((resp) => {
        this.jiraProjectList = resp.data.data
      })
    },
    // 新增
    addRobotProject() {
      this.$refs.addRobotFormRef.validate((valid) => {
        if (!valid) {
          return false
        } else {
          // 循环数组
          this.pipelineFrom.pipelineList.forEach((pipeline) => {
            this.serviceList.forEach((service) => {
              if (service.name === pipeline.serviceName) {
                pipeline.serviceType = service.type
              }
              pipeline.namespace = this.pipelineFrom.dingRobot.namespace
            })
            if (pipeline.serviceName === '') {
              this.pipelineFrom.pipelineList.pop()
            }
          })
          addPipeline(this.pipelineFrom).then((resp) => {
            if (resp.data.data === true) {
              this.addDialogVisible = false
              this.pipelineFrom.pipelineList = []
              this.pipelineFrom.pipelineList.push({
                serviceName: '',
                serviceBranch: '',
                enable: 1
              })
              this.getProjectRobotList()
              return this.$message.success('新增成功！')
            } else if (resp.data.data === false) {
              return this.$message.error(resp.data.msg)
            }
          })
        }
      })
    },
    // 编辑
    editRobotProject() {
      this.$refs.editRobotFormRef.validate((valid) => {
        if (!valid) {
          return false
        } else {
          this.editPipelineForm.pipelineList.forEach((pipeline) => {
            this.serviceList.forEach((service) => {
              if (service.name === pipeline.serviceName) {
                pipeline.serviceType = service.type
              }
              pipeline.namespace = this.editPipelineForm.dingRobot.namespace
            })
            if (pipeline.serviceName === '') {
              this.editPipelineForm.pipelineList.pop()
            }
          })
          editPipeline(this.editPipelineForm).then((resp) => {
            if (resp.data.data === true) {
              this.editDialogVisible = false
              this.getProjectRobotList()
              return this.$message.success('修改成功！')
            } else if (resp.data.data === false) {
              return this.$message.error(resp.data.msg)
            }
          })
        }
      })
    },
    // 删除
    delProjectRobot(robotId) {
      this.delRobotParams.dingRobotId = robotId
      this.$messageBox
        .confirm('此操作将永久删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        .then(() => {
          delPipeline(this.delRobotParams).then((resp) => {
            if (resp.data.data === true) {
              this.$message.success('删除成功！')
              return this.getProjectRobotList()
            } else if (resp.data.data === false) {
              return this.$message.error(resp.data.msg)
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 发送进度报告
    sendSchedule() {
      this.sendScheduleData.jiraProjectKey =
        this.sendProjectRobot.jiraProjectKey
      this.sendScheduleData.testProjectName = this.sendProjectRobot.projectName
      this.sendScheduleData.progress = this.sendScheduleForm.progress + '%'
      this.sendScheduleData.risk = this.sendScheduleForm.risk
      this.sendScheduleData.webhook = this.sendProjectRobot.dingUrl
      this.$refs.sendFormRef.validate((valid) => {
        if (!valid) {
          return false
        } else {
          sendScheduleReport(this.sendScheduleData).then((resp) => {
            if (resp.data.data === true) {
              this.sendScheduleDialogVisible = false
              return this.$message.success('发送成功!')
            } else if (resp.data.data === false) {
              return this.$message.error('发送失败!')
            }
          })
        }
      })
    },
    // 发送冒烟测试结果
    sendSmoking() {
      this.sendSmokingData.jiraProjectKey = this.sendProjectRobot.jiraProjectKey
      this.sendSmokingData.testProjectName = this.sendProjectRobot.projectName
      this.sendSmokingData.progress = this.sendSmokingForm.progress + '%'
      this.sendSmokingData.risk = this.sendSmokingForm.risk
      this.sendSmokingData.webhook = this.sendProjectRobot.dingUrl
      this.sendSmokingData.result = this.sendSmokingForm.result
      this.$refs.smokingFormRef.validate((valid) => {
        if (!valid) {
          return false
        } else {
          sendSmokingResult(this.sendSmokingData).then((resp) => {
            if (resp.data.data === true) {
              this.smokingDialogVisible = false
              return this.$message.success('发送成功!')
            } else {
              return this.$message.error('发送失败!')
            }
          })
        }
      })
    },
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize
      this.getProjectRobotList()
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pageNum = newPage
      this.getProjectRobotList()
    },
    // 点击编辑按钮
    openEditDialog(projectRobot) {
      this.editDialogVisible = true
      this.getServiceList()
      this.getNamespaceList()
      this.editPipelineForm.dingRobot = projectRobot.dingRobot
      this.editPipelineForm.pipelineList = projectRobot.pipelineList
    },
    // 打开发送进度对话框
    openSendDialog(projectRobot) {
      this.sendScheduleDialogVisible = true
      this.sendProjectRobot = projectRobot.dingRobot
      getJiraIssueList({
        jiraProjectKey: this.sendProjectRobot.jiraProjectKey,
        projectName: this.sendProjectRobot.projectName,
        unsolved: true
      }).then((resp) => {
        this.scheduleUnsolvedIssueList = resp.data.data
      })
    },
    // 对话框方法
    openMessage(message, confirmText, doit) {
      this.$messageBox
        .confirm(message, '确定', {
          cancelButtonText: '取消',
          confirmButtonText: confirmText,
          type: 'warning'
        })
        .then(() => {
          doit()
        })
        .catch(() => {})
    },
    // 完成按钮
    finishDialog(row) {
      var formdata = new FormData()
      formdata.set('dingRobotId', row.dingRobot.id)
      formdata.set('status', 2)
      this.openMessage('是否已经完成？', '确定', () => {
        changePipelineStatus(formdata)
          .then((resp) => {
            if (resp.data.data === true) {
              this.$message.success('标记完成成功！')
              this.showMsg = false
              return this.getProjectRobotList()
            } else {
              this.$message.error(resp.data.msg)
            }
          })
          .catch((error) => {
            this.$message({ type: 'error', message: error })
          })
      })
    },
    // 打开发送冒烟测试结果对话框
    openSendSmokingDialog(projectRobot) {
      this.smokingDialogVisible = true
      this.sendProjectRobot = projectRobot.dingRobot
      getJiraIssueList({
        jiraProjectKey: this.sendProjectRobot.jiraProjectKey,
        projectName: this.sendProjectRobot.projectName,
        unsolved: true
      }).then((resp) => {
        this.smokingUnsolvedIssueList = resp.data.data
      })
    },
    closeAddDialog() {
      this.$refs.addRobotFormRef.resetFields()
      this.pipelineFrom.pipelineList = []
      this.pipelineFrom.pipelineList.push({
        serviceName: '',
        serviceBranch: '',
        enable: 1
      })
    },
    closeEditDialog() {
      this.$refs.editRobotFormRef.resetFields()
      this.getProjectRobotList()
    },
    closeSendDialog() {
      this.$refs.sendFormRef.resetFields()
    },
    closeSmokingDialog() {
      this.$refs.smokingFormRef.resetFields()
    },

    // 添加行方法
    AddPipelineListRow() {
      this.pipelineFrom.pipelineList.push({
        serviceName: '',
        serviceBranch: '',
        enable: 1
      })
    },
    // 删除行
    pipelineDelete(index) {
      this.pipelineFrom.pipelineList.splice(index, 1)
    },
    // 编辑pipeline添加按钮
    EditPipelineListRow() {
      this.editPipelineForm.pipelineList.push({
        serviceName: '',
        serviceBranch: '',
        enable: 1
      })
    },
    // 编辑删除pipeline按钮
    editPipelineDelete(index) {
      this.editPipelineForm.pipelineList.splice(index, 1)
    },
    // 环境列表
    getNamespaceList() {
      getNamespaceList({ env: 'all' }).then((resp) => {
        this.namespaceList = resp.data.data
      })
    },
    // 获取服务列表
    getServiceList() {
      getGitProjectList().then((resp) => {
        this.serviceList = resp.data.data
      })
    },
    // 添加按钮
    addPipeline() {
      this.addDialogVisible = true
      // 获取服务列表
      this.getServiceList()
      this.getNamespaceList()
    },
    // 查询方法
    queryData() {
      this.getProjectRobotList()
    },
    // 挂起按钮
    HangDialog(id) {
      var formdata = new FormData()
      formdata.set('dingRobotId', id)
      formdata.set('status', 3)
      this.openMessage('是否需要挂起？', '确定', () => {
        changePipelineStatus(formdata)
          .then((resp) => {
            if (resp.data.data === true) {
              this.$message.success('挂起成功！')
              return this.getProjectRobotList()
            } else {
              this.$message.error(resp.data.msg)
            }
          })
          .catch((error) => {
            this.$message({ type: 'error', message: error })
          })
      })
    },
    // 启用按钮
    EnableDialog(row) {
      var formdata = new FormData()
      formdata.set('dingRobotId', row.dingRobot.id)
      formdata.set('status', 1)
      this.openMessage('是否需要启用？', '确定', () => {
        changePipelineStatus(formdata)
          .then((resp) => {
            if (resp.data.data === true) {
              this.$message.success('启用成功！')
              return this.getProjectRobotList()
            } else {
              this.$message.error(resp.data.msg)
            }
          })
          .catch((error) => {
            this.$message({ type: 'error', message: error })
          })
      })
    }
  },
  created() {
    this.getNamespaceList()
    this.getJiraProjectList()
    this.getProjectRobotList()
  }
}
</script>

<style lang="less" scoped>
.el-input {
  width: 300px;
  margin-right: 20px;
}
.top {
  margin-bottom: 20px;
}
.btn-style {
  margin-top: 10px;
  margin-left: 8px;
}
.el-select {
  margin: 0 10px 0 5px;
}
</style>
