<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 } from "../modules/interface.js";
import axios from "@/request/http.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 }, () => {
    Cancle();
  });
};
const Cancle = () => {
  router.go(-1);
};
const getInfoData = () => {
  axios
    .get("/v1/api/metric_config", {
      params: {
        id: id,
      },
    })
    .then((res) => {
      if (res.data.code == 200) {
        let res_d = res.data.data;
        infoData.value = {
          name: res_d.metric_name,
          indicator_expression: res_d.expr,
          rule_type: res_d.alert_rule_type,
          time: res_d.duration,
          unit: res_d.duration_unit,
          inspection_cycle: res_d.check_period,
          warningScopeRows:
            res_d.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_d.is_enabled,
          res: res_d,
        };
      } else {
        ElMessage.error(res.data.data);
      }
    });
};
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>
