<template>
  <div class="box">
    <router-link :to="config.path || ''">
      <div class="theme1" v-if="config.showType == DashboardShowType.ONE">
        <div class="lef-icon" v-if="config.imgType == ImgType.ICON">
          <Icon
            v-if="config.imgIconColor"
            :icon="config.imgIcon"
            :size="60"
            :color="config.imgIconColor"
          />
          <IconFontSymbol icon="tishi" v-else />
        </div>
        <div class="lef-img" v-if="config.imgType == ImgType.PICTURE">
          <img v-if="config.pictureUrl" :src="config.pictureUrl" />
          <IconFontSymbol icon="tishi" v-else />
        </div>
        <div class="right">
          <div class="title">{{ config.title }}</div>
          <div class="mainIndex" :style="numColorStyle">
            {{ config.mainIndexValue }}
            <!-- <CountTo
              prefix=""
              :style="numColorStyle"
              :startVal="0"
              :endVal="config.mainIndexValue"
              class="text-2xl"
            /> -->
          </div>
          <div class="desc">
            <span v-if="config.des">{{ config.des }}</span>
            <span v-if="config.secondaryIndexValue">{{ config.secondaryIndexValue }}</span>
            <span v-if="config.flagIsUp == 0" class="green">
              <Icon icon="ant-design:caret-up-filled" />
            </span>
            <span v-if="config.flagIsUp && config.flagIsUp > 0" class="red">
              <Icon icon="ant-design:caret-up-filled" />
            </span>
          </div>
        </div>
      </div>
      <div class="theme2" v-if="config.showType == DashboardShowType.TWO">
        <div class="bg-img">
          <img v-if="config.pictureUrl" :src="config.pictureUrl" />
        </div>
        <div class="right">
          <div class="mainIndex" :style="numColorStyle">
            <CountTo prefix="" :startVal="0" :endVal="config.mainIndexValue" class="text-2xl" />
          </div>
          <div class="title">{{ config.title }}</div>
        </div>
      </div>
      <div class="theme3" v-if="config.showType == DashboardShowType.THREE">
        <div class="title">{{ config.title }}</div>
        <div class="card-box">
          <div class="card">
            <div class="card-title">{{ config.title1 }}</div>
            <div class="card-number">
              <CountTo
                prefix=""
                :startVal="0"
                :style="numColorStyle"
                :endVal="config.mainIndexValue"
                class="text-2xl"
              />
            </div>
          </div>
          <div class="card">
            <div class="card-title">{{ config.title2 }}</div>
            <div class="card-number">
              <CountTo
                prefix=""
                :style="numColorStyle"
                :startVal="0"
                :endVal="config.mainIndex2Value"
                class="text-2xl"
              />
            </div>
          </div>
          <div class="flag-box">
            <div class="flag-item"></div>
            <div class="flag-item"></div>
            <div class="flag-item"></div>
            <div class="flag-item"></div>
            <div class="card-number" :style="numColorStyle">
              {{ config.secondaryIndexValue
              }}<span v-if="config.flagIsUp" :class="config.flagIsUp > 0 ? 'red' : 'green'">
                <Icon icon="ant-design:caret-up-filled" />
              </span>
            </div>
            <div class="card-title">{{ config.des }}</div>
          </div>
        </div>
      </div>
    </router-link>
  </div>
</template>

<script setup lang="ts">
  import { dashboardProperties } from '../config/properties';
  import { CountTo } from '/@/components/CountTo/index';
  import { DesktopComponent, DashboardShowType, ImgType } from '/@/enums/desktop';
  import { DashboardConfig } from '/@/model/desktop/designer';
  import { Icon } from '/@/components/Icon';
  import IconFontSymbol from '/@/components/IconFontSymbol/Index.vue';
  import { computed, nextTick, onMounted, watch } from 'vue';
  import { isNumber } from 'lodash-es';
  import useApiRequest from '/@/hooks/event/useApiRequest';
  const { changeApiOptions } = useApiRequest();
  const props = withDefaults(
    defineProps<{
      type: DesktopComponent;
      w: number;
      h: number;
      title: string;
      config: DashboardConfig;
    }>(),
    {
      type: DesktopComponent.DEFAULT,
      w: 0,
      h: 0,
      title: '',
      config: () => {
        return dashboardProperties;
      },
    },
  );
  const emit = defineEmits(['update:config']);
  watch(
    () => props.config.renderKey,
    (val) => {
      val && resizeLayout();
    },
    {
      deep: true,
    },
  );
  async function resizeLayout() {
    await nextTick();
    await changeData();
  }
  const numColorStyle = computed(() => {
    return 'color:' + (props.config.numColor ? props.config.numColor : '#5e95ff');
  });
  onMounted(async () => {
    await nextTick();
    await changeData();
  });
  async function changeData() {
    let config = props.config;
    if (config.apiConfig.path) {
      try {
        let res = await changeApiOptions(config.apiConfig);
        config.apiData = res;
        config.apiColumns = [];
        for (let [key, value] of Object.entries(config.apiData)) {
          if (isNumber(value)) {
            config.apiColumns.push(key);
          }
        }
        if (config.mainIndex) {
          config.mainIndexValue = isNumber(config.apiData[config.mainIndex])
            ? config.apiData[config.mainIndex]
            : 0;
        }
        emit('update:config', config);
      } catch (error) {}
    }
  }
</script>

<style lang="less" scoped>
  .red {
    color: #ee3b3b;
  }

  .green {
    color: #3dba84;
  }

  .title {
    font-size: 14px;
    color: #707070 !important;
  }

  .mainIndex {
    font-size: 24px;
  }

  .theme1 {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100%;

    .lef-icon {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 64px;
      margin-left: 20px;
    }

    .lef-img {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 64px;
      margin-left: 20px;
      font-size: 64px;

      img {
        width: 100%;
        height: 100%;
      }
    }

    .right {
      flex: 1;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
    }
  }

  .theme2 {
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
    width: 100%;
    height: 100%;

    .bg-img {
      position: absolute;
      inset: 0;

      img {
        width: 100%;
        height: 100%;
      }
    }

    .right {
      text-align: center;
      z-index: 2;

      .mainIndex {
        line-height: 1;
        font-weight: 700;
      }

      .text-2xl {
        font-size: 48px;
      }
    }
  }

  .theme3 {
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    width: 100%;
    height: 100%;

    .title {
      font-size: 16px;
      font-weight: 700;
      padding-left: 16px;
    }

    .card-box {
      display: flex;
      justify-content: space-around;
      align-items: center;
      padding-left: 16px;
      padding-right: 16px;

      .card {
        flex-basis: 33%;

        .card-title {
          font-size: 14px;
          color: #333;
        }

        .card-number {
          font-size: 24px;
          font-weight: 700;
        }
      }

      .flag-box {
        position: relative;
        width: 80px;
        height: 56px;
        border: 1px solid #5e95ff;
        background: linear-gradient(180deg, hsl(0deg 0% 100% / 0%), rgb(94 148 255 / 50.2%));
        display: flex;
        flex-direction: column;
        justify-content: center;

        .card-number {
          display: flex;
          justify-content: center;
          align-items: center;
          font-size: 14px;
          color: #5e95ff;
          position: relative;
          text-align: center;
        }

        .card-title {
          font-size: 14px;
          color: #5e95ff;
          position: relative;
          text-align: center;
        }
      }

      .flag-box .flag-item:first-child {
        position: absolute;
        top: 8px;
        left: -1px;
        height: 40px;
        width: 1px;
        background-color: #fff;
      }

      .flag-box .flag-item:nth-child(2) {
        position: absolute;
        top: -1px;
        left: 8px;
        height: 1px;
        width: 64px;
        background-color: #fff;
      }

      .flag-box .flag-item:nth-child(3) {
        position: absolute;
        top: 8px;
        right: -1px;
        height: 40px;
        width: 1px;
        background-color: #fff;
      }

      .flag-box .flag-item:nth-child(4) {
        position: absolute;
        bottom: -1px;
        left: 8px;
        height: 1px;
        width: 64px;
        background-color: #fff;
      }
    }
  }
</style>
