<template>
  <div class="box">
    <div class="item-title">{{ props.title }}</div>

    <div class="barpercent" style="padding-top: 64px">
      <div class="barpercent-title">{{ t('单位') }}：{{ props.config.unit }}</div>
      <div class="barpercent-list" v-if="props.config && props.config.apiData.length > 0">
        <div class="barpercent-item" v-for="(item, index) in props.config.apiData" :key="index">
          <template v-if="item">
            <div class="label">{{ item.name }}</div>
            <div class="percent-bar">
              <div class="active" :style="'width: ' + item.barWidth + '%;'"></div>
              <div class="num">{{ item.value }}/{{ item.target }}</div>
            </div>
          </template>
        </div>
      </div>
      <EmptyBox v-else />
    </div>
  </div>
</template>

<script setup lang="ts">
  import { DesktopComponent } from '/@/enums/desktop';
  import { EmptyBox } from '/@/components/ModalPanel/index';
  import { nextTick, onMounted, watch } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import useApiRequest from '/@/hooks/event/useApiRequest';
  const { changeApiOptions } = useApiRequest();
  const { t } = useI18n();
  const props = withDefaults(
    defineProps<{
      type: DesktopComponent;
      w: number;
      h: number;
      title: string;
      config: any;
    }>(),
    {
      type: DesktopComponent.DEFAULT,
      w: 0,
      h: 0,
      config: null,
    },
  );
  const emit = defineEmits(['update:config']);
  watch(
    () => props.config.renderKey,
    (val) => {
      val && resizeLayout();
    },
    {
      deep: true,
    },
  );
  async function resizeLayout() {
    await nextTick();
    await changeData();
  }
  onMounted(async () => {
    await nextTick();
    await changeData();
  });
  async function changeData() {
    let config = props.config;
    if (config.apiConfig.path) {
      let res = await changeApiOptions(config.apiConfig);
      if (res.list && Array.isArray(res.list)) {
        config.apiData = res.list.map((ele) => {
          let barWidth = 0;
          if (ele[config.labelKey] && ele[config.valueKey] && ele[config.targetKey]) {
            if (
              ele[config.valueKey] &&
              ele[config.valueKey] >= 0 &&
              ele[config.targetKey] &&
              ele[config.targetKey] >= 0
            ) {
              let val = Math.ceil((ele[config.valueKey] / ele[config.targetKey]) * 100);
              barWidth = val <= 100 ? val : 100;
            }
            return {
              name: ele[config.labelKey],
              value: ele[config.valueKey],
              target: ele[config.targetKey],
              barWidth: barWidth,
            };
          }
        });
      }
      if (res.columns && Array.isArray(res.columns)) config.apiColumns = res.columns;
      emit('update:config', config);
    }
  }
</script>

<style lang="less" scoped>
  .box {
    padding-top: 40px;

    .item-title {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 40px;
      line-height: 40px;
      color: #262626;
      font-size: 14px;
      padding-left: 16px;
    }

    .item-title::after {
      content: '';
      display: block;
      position: absolute;
      height: 1px;
      bottom: 0;
      left: 0;
      right: 0;
      background-color: #f0f0f0;
    }
  }

  .barpercent {
    position: relative;
    width: 100%;
    height: 100%;
  }

  .barpercent-title {
    color: #707070;
    font-size: 12px;
    padding-left: 12px;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    padding-top: 12px;
  }

  .barpercent-list {
    position: relative;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    padding: 0 24px 24px 0;
  }

  .barpercent-item {
    position: relative;
    width: 100%;
    height: 24px;
    padding-left: 100px;
  }

  .barpercent-item .label {
    position: absolute;
    top: 0;
    left: 0;
    width: 100px;
    height: 100%;
    color: #707070;
    padding-right: 8px;
    text-align: right;
  }

  .barpercent-item .percent-bar {
    position: relative;
    height: 100%;
    width: 100%;
    background-color: rgb(94 149 255 / 50%);
  }

  .barpercent-item .active {
    position: relative;
    height: 100%;
    width: 0;
    background: linear-gradient(270deg, #5e95ff, #695eff);
  }

  .barpercent-item:hover .percent-bar {
    background-color: rgb(255 196 96 / 50%);
  }

  .barpercent-item:hover .active {
    background: linear-gradient(270deg, #ffc45e, #ff9100);
  }

  .barpercent-item .num {
    position: absolute;
    top: 0;
    right: 8px;
    font-size: 12px;
    color: #fff;
    line-height: 24px;
  }
</style>
