<template>
  <div>
    <el-card>
      <!-- 顶部搜索栏 -->
      <div class="top">
        <el-button type="primary" icon="el-icon-circle-plus-outline" @click="addDialogVisible = true">新增</el-button>
      </div>

      <el-table :data="dingRobotList" border style="width: 100%">
        <!-- <el-table-column prop="id" label="编号" width="80px"></el-table-column> -->
        <el-table-column type="index" label="序号" width="50px"></el-table-column>
        <el-table-column prop="projectName" label="项目名" width="220px"></el-table-column>
        <el-table-column prop="dingUrl" label="钉钉url"></el-table-column>
        <el-table-column prop="creator" label="创建人" width="80px">
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180px"></el-table-column>
        <el-table-column label="操作" width="300px">
          <slot slot-scope="scope">
            <el-button type="primary" @click="openEditDialog(scope.row)">编辑</el-button>
            <el-button type="danger" @click="delProjectRobot(scope.row.id)">删除</el-button>
            <el-button type="success" @click="openSendDialog(scope.row)">发送进度</el-button>
          </slot>
        </el-table-column>
        <!-- <el-table-column prop="updateTime" label="更新时间"></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="新增robot" :visible.sync="addDialogVisible" width="60%" @close="closeAddDialog">
      <el-form ref="addRobotFormRef" :rules="formRules" :model="robotForm" label-width="150px">
        <el-form-item label="jira项目" prop="jiraProjectKey">
          <el-select v-model="robotForm.jiraProjectKey" placeholder="请选择Jira项目">
            <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="projectName">
          <el-input v-model="robotForm.projectName" placeholder="请输入项目名称"></el-input>
        </el-form-item>
        <el-form-item label="钉钉通知地址" prop="dingUrl">
          <el-input v-model="robotForm.dingUrl" placeholder="请输入钉钉通知地址" type="textarea"></el-input>
        </el-form-item>
        <el-form-item label="创建人" prop="creator">
          <el-input v-model="robotForm.creator"></el-input>
        </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="editRobotForm" label-width="150px">
        <el-form-item label="jira项目" prop="jiraProjectKey">
          <el-select v-model="editRobotForm.jiraProjectKey" placeholder="请选择Jira项目">
            <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="projectName">
          <el-input v-model="editRobotForm.projectName" placeholder="请输入项目名称"></el-input>
        </el-form-item>
        <el-form-item label="钉钉通知地址" prop="dingUrl">
          <el-input v-model="editRobotForm.dingUrl" placeholder="请输入钉钉通知地址" type="textarea"></el-input>
        </el-form-item>
        <el-form-item label="创建人" prop="creator">
          <el-input v-model="editRobotForm.creator"></el-input>
        </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="请输入测试风险点（非必填）"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="sendScheduleDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="sendSchedule">发 送</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getJiraProjectList,
  getProjectRobotList,
  addProjectRobot,
  modifyProjectRobot,
  delProjectRobot,
  sendScheduleReport
} from '@/api/jira'
export default {
  data() {
    return {
      dingRobotList: [],
      jiraProjectList: [],
      queryInfo: {
        pageNum: 1,
        pageSize: 10
      },
      totalNum: 0,
      addDialogVisible: false,
      editDialogVisible: false,
      sendScheduleDialogVisible: false,
      robotForm: {
        projectName: '',
        dingUrl: '',
        creator: '',
        jiraProjectKey: ''
      },
      editRobotForm: {},
      sendProjectRobot: {},
      sendScheduleData: {
        jiraProject: '',
        testProject: '',
        progress: '',
        risk: '',
        webhook: ''
      },
      delRobotParams: {
        projectRobotId: ''
      },
      sendScheduleForm: {},
      formRules: {
        projectName: [
          { required: true, message: '请输入项目名称', trigger: 'blur' }
        ],
        dingUrl: [
          { required: true, message: '请输入钉钉机器人url', trigger: 'blur' }
        ],
        jiraProjectKey: [
          { required: true, message: '请选择对应的Jira项目', trigger: 'blur' }
        ],
        creator: [{ required: true, message: '请输入创建人', trigger: 'blur' }]
      },
      sendFormRules: {
        progress: [
          {
            required: true,
            message: '请输入测试进度百分比，不需要%',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    getProjectRobotList() {
      getProjectRobotList(this.queryInfo).then((resp) => {
        this.dingRobotList = resp.data.data.list
        this.totalNum = resp.data.data.total
      })
    },
    getJiraProjectList() {
      getJiraProjectList().then((resp) => {
        this.jiraProjectList = resp.data.data
      })
    },
    // 新增
    addRobotProject() {
      this.$refs.addRobotFormRef.validate((valid) => {
        if (!valid) {
          return false
        } else {
          addProjectRobot(this.robotForm).then((resp) => {
            if (resp.data.data === true) {
              this.addDialogVisible = false
              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 {
          modifyProjectRobot(this.editRobotForm).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.projectRobotId = robotId
      this.$messageBox
        .confirm('此操作将永久删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        .then(() => {
          delProjectRobot(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('发送失败!')
            }
          })
        }
      })
    },
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize
      this.getProjectRobotList()
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pageNum = newPage
      this.getProjectRobotList()
    },
    openEditDialog(projectRobot) {
      this.editDialogVisible = true
      this.editRobotForm = projectRobot
    },
    // 打开发送进度对话框
    openSendDialog(projectRobot) {
      this.sendScheduleDialogVisible = true
      this.sendProjectRobot = projectRobot
    },
    closeAddDialog() {
      this.$refs.addRobotFormRef.resetFields()
    },
    closeEditDialog() {
      this.$refs.editRobotFormRef.resetFields()
    },
    closeSendDialog() {
      this.$refs.sendFormRef.resetFields()
    }
  },
  created() {
    this.getJiraProjectList()
    this.getProjectRobotList()
  }
}
</script>

<style lang="less" scoped>
.el-input {
  width: 300px;
  margin-right: 20px;
}
.top {
  margin-bottom: 20px;
}
</style>
