<template>
  <div class="layout">
    <div class="das-warpper">
      <div class="das">
        <div class="diamond-warpper" :id="'war' + ran">
          <span class="diamond">
            <span class="triangle-top"></span>
            <span class="triangle-down"></span>
          </span>
        </div>
        <div
          class="annular"
          :style="
            'background: linear-gradient(90deg, #1c81cf 0%, transparent ' +
            per +
            '%);'
          "
        ></div>
        <div class="text-warpper"></div>
        <div class="text">{{ per }mash</div>
      </div>
    </div>
    <div class="bottom-box">
      <span class="left-gradient"></span>
      <span :style="'font-size: ' + fontSize + 'px;'" class="title">{{
        title
      }}</span>
      <span class="right-gradient"></span>
    </div>
  </div>
</template>
<script setup>
import { ref, onMounted, watch } from "vue";
import { toRefs } from "vue";
const props = defineProps({
  title: {
    type: String,
    default: "仪表盘",
  },
  per: {
    type: Number,
    default: 66.66,
  },
  fontSize: {
    type: Number,
    default: 20,
  },
});
const { title, per } = toRefs(props);
console.log(title.value, per.value);
watch(
  () => per.value,
  (newVal, oldVal) => {
    console.log(newVal, oldVal);
    changeper();
  }
);
onMounted(() => {
  changeper();
});
let ran = ref(Math.floor(Math.random() * 100));
const changeper = () => {
  let p = per.value;
  let deg = (180 * p) / 100 - 90;
  const newKeyFrames = `@keyframes run${ran.value} {
      from {
        transform: rotateZ(-90deg);
      }
      to {
        transform: rotateZ(${deg}deg);
      }
    }`;
  // // 将原有的 keyframes 规则替换为新的规则
  const styleSheet = document.styleSheets[0];
  // styleSheet.deleteRule(0);
  styleSheet.insertRule(newKeyFrames, 0);
  const diamondWarpper = document.getElementById("war" + ran.value);
  diamondWarpper.style = `transform:rotateZ(${deg}deg);animation : run${ran.value} 2s 1;`;
  // diamondWarpper.style.animation = `run${ran} 2s 2`;
};
</script>
<style lang="css" scoped>
.das-warpper {
  height: 160px;
  overflow: hidden;
}

.das {
  box-sizing: border-box;
  position: relative;
  width: 330px;
  height: 330px;
  border: 1px dashed #1a75bf;
  border-radius: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

.das .annular {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 90%;
  height: 90%;
  border-radius: 50%;
  -webkit-mask: radial-gradient(transparent, transparent 60%, #000 60%);
}

.das .diamond-warpper {
  position: absolute;
  display: inline-block;
  width: 50%;
  height: 90%;
  z-index: 200;
  display: flex;
  justify-content: center;
  align-items: flex-start;
}

.das .diamond-warpper .diamond {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

.das .diamond-warpper .diamond .triangle-top {
  width: 0;
  height: 0;
  border-left: 4px solid transparent;
  border-right: 4px solid transparent;
  border-bottom: 10px solid #00a0e9;
  margin-bottom: -1px;
}

.das .diamond-warpper .diamond .triangle-down {
  width: 0;
  height: 0;
  border-left: 4px solid transparent;
  border-right: 4px solid transparent;
  border-top: 10px solid #00a0e9;
}

.das .text-warpper {
  position: absolute;
  width: 40%;
  height: 40%;
  border-radius: 100%;
  z-index: 200;
}

.das .text {
  position: absolute;
  top: 20%;
  font-size: 50px;
  font-weight: bold;
  z-index: 300;
}

.bottom-box {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  /* margin-top: 3px; */
  position: relative;
  top: -20px;
 
}

.bottom-box .left-gradient {
  width: 50px;
  height: 10px;
  background: linear-gradient(to left, #1c81cf, transparent);
}

.bottom-box .right-gradient {
  width: 50px;
  height: 10px;
  background: linear-gradient(to right, #1c81cf, transparent);
}

.bottom-box .title {
  border-radius: 5px;
  text-align: center;
  margin: 0px 4px;
  font-size: 22px;
}

.layout {
  background-color: #032468;
  color: #fff;
  /* width: 150px; */
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px;
}

</style>
