<template>
  <section class="kf-panels">
    <span :title="label" class="kf-p-label">{{ label }}</span>
    <div class="kf-p-body flex-row-start-center">
      <div :title="config.value + config.unit" class="kf-p-body-value kf-p-body-value2">{{
        config.value + config.unit
      }}
      </div>
      <div class="pr kf-p-body-value5">
        <el-slider :max="config.maximum" :min="config.minimum" v-model="value" @input="sliderChange" />
      </div>
    </div>
  </section>
</template>

<script setup>
import { onMounted, toRefs, ref, watch } from 'vue';
import dataModel from '@/components/ui/model/data.js';

const props = defineProps(['config', 'confData', 'label']);
const { config, confData, label } = toRefs(props);

const value = ref(config.value.value);

if (config.value.minimum == undefined) {
  config.value.minimum = 0;
}
if (config.value.maximum == undefined) {
  config.value.maximum = 100;
}

watch(props, () => {
  value.value = parseInt(config.value.value);
});

onMounted(() => {
  try {
    if (config.value.value != undefined) {
      if (typeof config.value.value == 'string') {
        config.value.value = parseInt(config.value.value);
      }
    } else {
      config.value.value = 0;
    }
  } catch (e) {
    console.log(e);
  }

});

const sliderChange = (evt) => {
  if (evt == undefined) {
    return;
  }
  value.value = evt;
  const props = {
    props: { deviceId: confData.value.deviceId },
  };
  dataModel.doControl(props, confData.value.model, null, evt, config.value.name);
};
</script>

<style lang="scss">
.kf-p-body {
  position: relative;

  .el-switch__core {
    border: none;
  }

  [type="range"] {
    -webkit-appearance: none;
    appearance: none;
    margin: 0;
    outline: 0;
    background-color: transparent;
    width: 42%;
  }

  [type="range"]::-webkit-slider-runnable-track {
    height: 3px;
    background: #eee;
  }

  [type="range" i]::-webkit-slider-container {
    height: 15px;
    overflow: hidden;
  }

  [type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 8px;
    height: 10px;
    border-radius: 2px;
    background-color: #ffffff;
    border: 1px solid transparent;
    margin-top: -4px;
    border-image: linear-gradient(#0becd2, #0becd2) 0 fill / 4 10 4 0 / 0px 0px 0 2000px;
  }
}

/*.kf-p-body-value{
  position: absolute;
  top: 50%;
  left: 35%;
  transform: translateY(-50%);
  height: 25px;
  padding: 5px 8px;
  background-color: rgba(6, 241, 162,0.05);
  border: 1px solid rgba(6, 241, 162,0.15);
  border-radius: 3px;
  color: inherit;
  display: flex;
  align-items: center;
  justify-content: center;
}*/
.kf-panels {
  .kf-p-body {
    .kf-p-body-value.kf-p-body-value2 {
      position: static;
      width: 60px !important;
      transform: translateY(0);
      height: 25px;
      padding: 0;
      background-color: rgba(6, 241, 162, 0.05);
      border: 1px solid rgba(6, 241, 162, 0.15);
      border-radius: 3px;
      color: inherit;
      display: inline-block;
      line-height: 25px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      text-align: center;
    }

    .kf-p-body-value5 {
      width: calc(100% - 75px);
      margin-left: 15px;
      display: flex;
      align-items: center;

      .el-slider {
        width: 100%;

        .el-slider__runway {
          height: 8px;
          border-radius: 4px;
          border: 1px solid transparent;
        }
      }

      .el-slider__runway {
        height: 8px;
        border-radius: 4px;
        border: 1px solid transparent;
      }

      .el-slider__button-wrapper {
        width: 12px;
        height: 12px;
        top: -6px;

        .el-slider__button {
          border-width: 1px;
          width: 12px;
          height: 12px;
        }
      }
    }
  }
}

.pr {
  position: relative;
}
</style>
