<template>
  <div>
    <el-card>
      <div>
        场景名称：
        <el-input v-model="queryModuleInfo.sceneName" 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" @click="DialogVisible = true">新增</el-button>
      </div>
      <el-table :data="sceneList" border style="width: 100%;margin-top:20px">
        <el-table-column type='index' width="50px"></el-table-column>
        <el-table-column prop="name" label="场景名称" width="200px"></el-table-column>
        <el-table-column prop="projectName" label="项目名称" width="200px"></el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="200px"></el-table-column>
        <!-- <el-table-column prop="updateTime" label="修改时间" width="200px"></el-table-column> -->
        <el-table-column label="操作">
          <slot slot-scope="scope">
            <el-button type="success" icon="el-icon-link" @click="executeSceneFrom(scope.row)">执行</el-button>
            <el-button type="warning" icon="el-icon-edit" @click="editeSceneFrom(scope.row)">编辑场景</el-button>
            <el-button type="warning" icon="el-icon-edit" @click="editeSceneCaseFrom(scope.row)">编辑场景用例</el-button>
            <el-button type="danger" icon="el-icon-delete" @click="deleSceneFrom(scope.row)">删除</el-button>
          </slot>
        </el-table-column>
      </el-table>
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="queryModuleInfo.pageNum" :page-size="queryModuleInfo.pageSize" layout="total, prev, pager, next"
        :total="totalNum"></el-pagination>
    </el-card>
    <!-- 接口返回数据 -->
    <el-card v-if="isShow">
      <div slot="header">
        <el-row>
          <el-col :span="3">
            <div>响应结果 :</div>
          </el-col>
        </el-row>
        <json-viewer :expand-depth=3 :value="requestData"></json-viewer>
      </div>
    </el-card>
    <!-- 新增页面 /编辑页面-->
    <el-dialog title="新增场景" :visible.sync="DialogVisible" width="50%" @close="closeAddDialog">
      <el-form ref="sceneFormAddRef" :rules="rules" :model="sceneForm" label-width="90px">
        <el-form-item label="场景名称:" prop="name" style="width:400px">
          <el-input v-model="sceneForm.name"></el-input>
        </el-form-item>
        <el-form-item label="项目名称:" prop="projectId" style="width:400px">
          <el-select v-model="sceneForm.projectId" placeholder="请选择项目" clearable>
            <el-option v-for="item in projectList" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="DialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addSceneFrom">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  getSceneList,
  addSecene,
  delSecne,
  executeScene,
  editScene
} from '@/api/getAutoScene'
import { getProjectList } from '@/api/getAotoInterface'
export default {
  data() {
    return {
      queryModuleInfo: {
        pageNum: 1,
        pageSize: 20,
        sceneName: ''
      },
      queryProjectList: {
        pageNum: 1,
        pageSize: 20
      },
      sceneList: [],
      totalNum: 0,
      sceneForm: {
        name: '',
        projectId: ''
      },
      projectList: [],
      DialogVisible: false,
      rules: {
        name: [
          {
            required: true,
            message: '场景名称不能为空！',
            trigger: 'blur'
          }
        ],
        projectId: [
          {
            required: true,
            message: '项目名称不能为空！',
            trigger: 'blur'
          }
        ]
      },
      delSceneForm: {
        sceneId: ''
      },
      isShow: false,
      requestData: '',
      executeScene: {
        sceneId: '',
        namespace: ''
      }
    }
  },
  created() {
    this.getSecneList()
    this.getProjectList()
    this.$bus.$on('refreshScene', () => {
      this.getSecneList()
    })
  },
  methods: {
    // 获取场景列表
    getSecneList() {
      getSceneList(this.queryModuleInfo).then((resp) => {
        this.sceneList = resp.data.data.list
        this.totalNum = resp.data.data.total
      })
    },
    // 获取项目列表
    getProjectList() {
      getProjectList(this.queryProjectList).then((resp) => {
        this.projectList = resp.data.data.list
      })
    },
    // 查询方法
    queryData() {
      this.getSecneList()
    },
    // 执行场景
    executeSceneFrom(row) {
      // 获取用例id
      this.executeScene.sceneId = row.id
      this.executeScene.namespace = window.sessionStorage.getItem('env')
      this.isShow = true
      executeScene(this.executeScene).then((resp) => {
        this.requestData = resp.data.data
      })
    },
    // 编辑场景
    editeSceneFrom(row) {
      this.DialogVisible = true
      this.sceneForm = row
    },
    // 编辑场景用例
    editeSceneCaseFrom(row) {
      window.sessionStorage.setItem('curScene', JSON.stringify(row))
      this.$router.push({ path: 'EditScene' })
      // bus使用
      this.$nextTick(function () {
        // DOM 现在更新了

        this.$bus.$emit('scenemsg', row)
      })
    },
    // 对话框方法
    openMessage(message, confirmText, doit) {
      this.$messageBox
        .confirm(message, '确定', {
          cancelButtonText: '取消',
          confirmButtonText: confirmText,
          type: 'warning'
        })
        .then(() => {
          doit()
        })
        .catch(() => {})
    },
    // 删除场景
    deleSceneFrom(row) {
      this.delSceneForm.sceneId = row.id
      this.openMessage('您确定要删除吗？', '删除', () => {
        delSecne(this.delSceneForm)
          .then((resp) => {
            if (resp.data.data === true) {
              this.$message.success('删除成功！')
              this.getSecneList()
            } else if (resp.data.data === false) {
              this.$message.error(resp.data.msg)
            }
          })
          .catch((error) => {
            this.$message({ type: 'error', message: error })
          })
      })
    },
    handleSizeChange(newSize) {
      this.queryModuleInfo.pageSize = newSize
      this.getSecneList()
    },
    handleCurrentChange(newPage) {
      this.queryModuleInfo.pageNum = newPage
      this.getSecneList()
    },
    // 添加场景
    addSceneFrom() {
      this.$refs.sceneFormAddRef.validate((valid) => {
        if (!valid) {
          return false
        } else {
          if (this.sceneForm.id) {
            editScene(this.sceneForm).then((resp) => {
              if (resp.data.data === true) {
                this.DialogVisible = false
                this.$message.success('修改成功！')
                this.getSecneList()
              } else {
                this.$message.error(resp.data.msg)
              }
            })
          } else {
            addSecene(this.sceneForm).then((resp) => {
              if (resp.data.data === true) {
                this.DialogVisible = false
                this.$message.success('新增成功！')
                this.getSecneList()
              } else {
                this.$message.error(resp.data.msg)
              }
            })
          }
        }
      })
    },
    // 关闭清空数据
    closeAddDialog() {
      this.$refs.sceneFormAddRef.resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
.el-input {
  width: 300px;
}
.el-select {
  width: 300px;
}
</style>
