<!-- Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements.  See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License.  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. -->
<template>
  <div class="config-panel">
    <Icon
      class="cp mr-5 close"
      iconName="cancel"
      size="middle"
      @click="closePopper"
    />
    <div class="item mb-10">
      <span class="label">{{ t("unit") }}</span>
      <el-input
        class="input"
        v-model="currentMetric.unit"
        size="small"
        placeholder="Please input unit"
        @change="changeConfigs(index, { unit: currentMetricConfig.unit })"
      />
    </div>
    <div class="item mb-10">
      <span class="label">{{ t("metricLabel") }}</span>
      <el-input
        class="input"
        v-model="currentMetric.label"
        size="small"
        placeholder="Please input a name"
        @change="changeConfigs(index, { label: currentMetricConfig.label })"
      />
    </div>
    <div class="item mb-10">
      <span class="label">{{ t("labelsIndex") }}</span>
      <el-input
        class="input"
        v-model="currentMetric.labelsIndex"
        size="small"
        placeholder="auto"
        @change="
          changeConfigs(index, { label: currentMetricConfig.labelsIndex })
        "
      />
    </div>
    <div class="item mb-10">
      <span class="label">{{ t("aggregation") }}</span>
      <Selector
        :value="currentMetric.calculation"
        :options="CalculationOpts"
        size="small"
        placeholder="Select a option"
        @change="changeConfigs(index, { calculation: $event[0].value })"
        class="aggregation"
        :clearable="true"
      />
    </div>
    <div class="item">
      <span class="label">{{ t("sortOrder") }}</span>
      <Selector
        :value="currentMetric.sortOrder || 'DES'"
        :options="SortOrder"
        size="small"
        placeholder="Select a sort order"
        class="aggregation"
        @change="changeConfigs(index, { sortOrder: $event[0].value })"
      />
    </div>
  </div>
</template>
<script lang="ts" setup>
import { ref } from "vue";
import type { PropType } from "vue";
import { useI18n } from "vue-i18n";
import { SortOrder, CalculationOpts } from "../../../data";
import { useDashboardStore } from "@/store/modules/dashboard";
import { MetricConfigOpt } from "@/types/dashboard";

/*global defineEmits, defineProps */
const props = defineProps({
  currentMetricConfig: {
    type: Object as PropType<MetricConfigOpt>,
    default: () => ({ unit: "" }),
  },
  index: { type: Number, default: 0 },
});
const { t } = useI18n();
const emit = defineEmits(["update", "close"]);
const dashboardStore = useDashboardStore();
const currentMetric = ref<MetricConfigOpt>(props.currentMetricConfig);

function changeConfigs(index: number, param: { [key: string]: string }) {
  const metricConfig = dashboardStore.selectedGrid.metricConfig || [];
  metricConfig[index] = { ...metricConfig[index], ...param };
  dashboardStore.selectWidget({
    ...dashboardStore.selectedGrid,
    metricConfig,
  });
  emit("update");
}

function closePopper() {
  emit("close");
}
</script>
<style lang="scss" scoped>
.config-panel {
  padding: 10px 5px;
  position: relative;
}

.label {
  width: 150px;
  display: inline-block;
  font-size: 12px;
}

.close {
  position: absolute;
  top: -8px;
  right: -15px;
}

.aggregation {
  width: 365px;
}
</style>
