<!-- websocket -->
<template>
  <div>
    <div class="baseInfo_box">
      <div class="baseInfo">
        <bg-info :data="apiBaseInfo" />
      </div>
      <div class="apaas_button test_btn">
        <el-button type="primary" @click="serviceTest">
          <bg-icon style="font-size: 12px; color: #fff; margin-right: 8px" icon="#bg-ic-checklist"></bg-icon>
          服务测试
        </el-button>
      </div>
    </div>
    <div class="req_box">
      <p class="input-title">
        <span class="before_title"></span>
          接口文档介绍
        <span class="after_icon" @click="res_body_open=!res_body_open">
          <bg-icon style="font-size: 8px; color: #000;" icon="#bg-ic-arrow-up" v-if="res_body_open"></bg-icon>
          <bg-icon style="font-size: 8px; color: #000;" icon="#bg-ic-arrow-down" v-else></bg-icon>
        </span>
      </p>
      <div v-if="res_body_open">
        <div v-html="detail_data.doc_desc" class="doc_desc"></div>
      </div>
    </div>

    <!-- 测试结果弹窗 -->
    <el-dialog
      custom-class="dialog_box result_box"
      title="提示"
      v-model="showDialog"
      width="400px"
      :before-close="
        () => {
          dialogClose()
        }
      "
    >
      <div class="dialog_info">
        <bg-icon v-if="dialogInfo.state" style="font-size: 26px; color: #429e8a; margin-right: 10px" icon="#bg-ic-circle-check"></bg-icon>
        <bg-icon v-else style="font-size: 26px; color: #d75138; margin-right: 10px" icon="#bg-ic-circle-close"></bg-icon>
        <span>{{dialogInfo.msg}}</span>
      </div>
      <template v-slot:footer>
        <div class="apaas_button">
          <el-button @click="dialogClose">取 消</el-button>
          <el-button type="primary" @click="dialogClose">确 定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { reactive, ref,onBeforeMount,toRefs,getCurrentInstance  } from 'vue'
import { useRouter,useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
const { proxy } = getCurrentInstance()
const { $axios,$message } = proxy
const props = defineProps({
  data: {
    type: Object,
    default: null
  }
})
const emit = defineEmits(["dialogClose"])
const router = useRouter()
const route = useRoute()
const state = reactive({data1:1})
const {data1} = toRefs(state)
const detailData = reactive({
  apiBaseInfo: [
    {
      name: "能力名称",
      value: "",
      nameWidth: 104
    },
    {
      name: "调用地址",
      value: "",
      nameWidth: 104
    },
    {
      name: "通信协议",
      value: "",
      full: true,
      nameWidth: 104
    },
  ], // 基本信息
  detail_data: {},
  res_body_open: true,
  showDialog: false,
  dialogInfo: {
    msg: "该服务地址正常",
    state: true
  },
})
const detail = props.data
detailData.detail_data = props.data
if (detail) {
  detailData.apiBaseInfo[0].value = detail.service_name
  detailData.apiBaseInfo[1].value = detail.req_url
  detailData.apiBaseInfo[2].value = ['','ws','wss'][detail.ws_protocol]
}
const serviceTest = () => {
  let params = {
    interface_type: 3,
    url: detail.req_url,
    id: detail.id.toString()
  }
  $axios
    .post(`/apaas/service/v5/service/test`,params)
    .then((res) => {
      if (res.data.code !== 200) {
        $message.error(res.data.data)
      } else {
        detailData.showDialog = true
        if (res.data.data.response_code == 200) { 
          detailData.dialogInfo = {
            msg: "该服务地址正常",
            state: true
          }
        }else {
          detailData.dialogInfo = {
            msg: "该服务地址异常",
            state: false
          }
        }
      }
    })
}
const dialogClose = () => {
  detailData.showDialog = false
  emit("refresh",detail.ability_tree_code)
}
const {
  apiBaseInfo,
  detail_data,
  res_body_open,
  showDialog,
  dialogInfo,
} = toRefs(detailData)
</script>

<style lang="scss" scoped>
.baseInfo_box {
  width: 100%;
  display: flex;
  justify-content: space-between;
  .baseInfo {
    width: 872px;
  }
}
.req_box {
  width: 872px;
  margin-top: 30px;
  .input-title {
    font-size: 16px;
    color: #202531;
    margin-top: 9px;
    margin-bottom: 15px;
    font-weight: 700;
    line-height: 1;
    .before_title {
      display: inline-block;
      width: 4px;
      height: 14px;
      background-color: #2b4695;
      margin-right: 8px;
      border-radius: 2px;
    }
    .after_icon {
      float: right;
      cursor: pointer;
    }
  }
  .doc_desc {
    width: 100%;
    height: 406px;
    padding: 16px 24px;
    background-color: #fafafa;
    border-radius: 4px;
    color: #202531;
    font-size: 14px;
  }
}
.dialog_info {
  padding: 27px 0 11px;
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>