<template>
  <div class="detail_container">
    <bg-breadcrumb></bg-breadcrumb>
    <div class="main_container">
      <div class="add-form-main bg-scroll">
        <add-form ref="add_form" :row="infoData"></add-form>
      </div>
      <div class="add-btns">
        <el-button size="default" @click="Cancle">取消</el-button>
        <el-button type="primary" size="default" @click="SaveSubmit">保存</el-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { onBeforeMount, ref } from "vue";
import bgBreadcrumb from "@/components/bg-breadcrumb.vue";
import addForm from "../modules/add-form.vue";
import { Save, URL } from "../modules/interface.js";
import { ElMessage } from "element-plus";
import { useRouter, useRoute } from "vue-router";
const router = useRouter();
const route = useRoute();
const { id, class_id } = route.query;
const infoData = ref({});
const add_form = ref(null);
const SaveSubmit = async () => {
  let res = await add_form.value.Submit();
  if (!res) return;
  Save(res, { id, class_id });
};
const Cancle = () => {
  router.go(-1);
};
const getInfoData = () => {
  //   axios
  //     .get(URL, {
  //       params: {
  //         id: id,
  //       },
  //     })
  //     .then((res) => {
  //       if (res.data.code == 200) {
  //         console.log(res.data.data);
  //       } else {
  //         ElMessage.error(res.data.data);
  //       }
  //     });
  let res = {
    id: "20da8f87-628a-4f0c-bd9c-0ad176d18d59",
    class_id: 101,
    metric_name: "xx请求次数告警",
    expr: 'shttp_requests_total{method="GET",$pod$}',
    duration: 5,
    duration_unit: "m",
    check_period: 3,
    is_enabled: 1,
    alert_rule_type: "9f1e6170-65e8-4e14-9c17-6a7b87a900a7",
    created_by: "",
    created_at: "2023-06-28 17:28:29",
    updated_by: "",
    updated_at: "2023-06-28 17:54:33",
    alert_range: [
      {
        variable_name: "$pod$",
        metric_name: "shttp_requests_total",
        metric_label: "pod",
        chinese_name: "demoString",
        is_required: true,
        is_linked: true,
      },
    ],
  };
  infoData.value = {
    name: res.metric_name,
    indicator_expression: res.expr,
    rule_type: res.alert_rule_type,
    time: res.duration,
    unit: res.duration_unit,
    inspection_cycle: res.check_period,
    warningScopeRows:
      res.alert_range.map((e) => {
        return {
          key: e.variable_name,
          input_indicator_tag: e.metric_label,
          indicator_scope: e.metric_name,
          indicator_tag: e.metric_label,
          cname: e.chinese_name,
          is_required: e.is_required ? 1 : 0,
          is_linkage: e.is_linked ? 1 : 0,
        };
      }) || [],
    state: res.is_enabled,
    res: res,
  };
};
onBeforeMount(() => {
  getInfoData();
});
</script>

<style lang="scss" scoped>
.detail_container {
  width: 100%;
  height: calc(100vh - 56px);
  padding: 0 24px 16px;
  min-height: 100%;
  .main_container {
    height: calc(100% - 46px);
    padding: 0;
    .add-form-main {
      padding: 24px;
      height: calc(100% - 68px);
    }
    .add-btns {
      height: 68px;
      display: flex;
      align-items: center;
      justify-content: flex-end;
      border-top: 1px solid #ddd;
      padding: 0 24px;
    }
  }
}
</style>
