<template>
  <div class="classification-of-seized-quantity">
    <div class="total">
      <div class="count-inner">
        <div class="item-title">今日查获总数</div>
        <div class="count">
          <vue-count-to :duration="2600" :end-val="totalNum.total" :start-val="0" class="card-panel-num" />
        </div>
        <div class="tips">
          <span class="icon"><svg-icon icon-class="top" /> 较昨日{{ totalPercentage }}</span>

        </div>
      </div>
      <div class="count-inner">
        <div class="item-title">移交公安数量</div>
        <div class="count">
          <vue-count-to :duration="2600" :end-val="totalNum.policeTotal" :start-val="0" class="card-panel-num" />
        </div>
        <div class="tips">
          <span class="icon up"><svg-icon icon-class="top" />较昨日{{ policePercentage }}</span>
        </div>
      </div>
      <div class="count-inner">
        <div class="item-title">隐匿夹带数量</div>
        <div class="count">
          <vue-count-to :duration="2600" :end-val="totalNum.concealTotal" :start-val="0" class="card-panel-num" />
        </div>
        <div class="tips">
          <span class="icon down"><svg-icon icon-class="top" />较昨日{{ concealPercentage }}</span>
        </div>
      </div>
    </div>
    <div class="dashboard-title">查获位置分布</div>
    <div ref="pieChart" class="echarts" />
  </div>
</template>
<script setup>
import { onMounted, ref } from 'vue';
import VueCountTo from '@/components/VueCountTo/vue-countTo.vue';
import { executeSqlByKeyAndParam } from '@/api/system/sql.js';
import { useEcharts } from '@/hooks/chart.js';
import SvgIcon from '@/components/SvgIcon/index.vue';
import { getTotalSome } from "@/api/largeScreen/largeScreen.js"
import { getPosition } from "@/api/largeScreen/largeScreen.js"



let totalNum = ref({
  total: 0,
  policeTotal: 0,
  concealTotal: 0,
  policeTotalBefore: 0,
  totalBefore: 0,
  concealTotalBefore: 0,
});
const policePercentage = computed(() => {
  if (totalNum.value.policeTotalBefore == totalNum.value.policeTotal) return '无变化'
  if (totalNum.value.policeTotalBefore == 0) return '+100%'
  else {
    let num = (totalNum.value.policeTotal - totalNum.value.policeTotalBefore) / totalNum.value.policeTotalBefore * 100
    return num > 0 ? '+' + num.toFixed(2) + '%' : num.toFixed(2) + '%'
  }
});
const totalPercentage = computed(() => {
  if (totalNum.value.totalBefore == totalNum.value.total) return '无变化'
  if (totalNum.value.totalBefore == 0) return '+100%'
  else {
    let num = (totalNum.value.total - totalNum.value.totalBefore) / totalNum.value.totalBefore * 100
    return num > 0 ? '+' + num.toFixed(2) + '%' : num.toFixed(2) + '%'
  }
});
const concealPercentage = computed(() => {
  if (totalNum.value.concealTotalBefore == totalNum.value.concealTotal) return '无变化'
  if (totalNum.value.concealTotalBefore == 0) return '+100%'
  else {
    let num = (totalNum.value.concealTotal - totalNum.value.concealTotalBefore) / totalNum.value.concealTotalBefore * 100
    return num > 0 ? '+' + num.toFixed(2) + '%' : num.toFixed(2) + '%'
  }
});

const pieChart = ref(null);
const chartOption = ref({
  color: ['#5470c6', '#91cc75', '#fac858', '#ee6666'],
  tooltip: {
    trigger: 'item',
    formatter: '{a} <br/>{b}: {c} ({d}%)'
  },
  series: [
    {
      type: 'pie',
      name: '部位类别',
      height: '100%',
      radius: '50%',
      label: {
        position: 'inner',
      },
      emphasis: {
        label: {
          show: true,
          fontSize: '14',
          formatter: '{b}\n{c} ({d}%)'
        },
        itemStyle: {
          shadowBlur: 10,
          shadowOffsetX: 0,
          shadowColor: 'rgba(0, 0, 0, 0.5)'
        }
      },
      itemStyle: {
        borderWidth: 1, //设置border宽度
        borderColor: '#fff',
      },
      labelLine: {
        show: false,
      },
      data: [
        { name: '行李', value: 200 },
        { name: '人体', value: 80 },
        { name: '随身物品', value: 30 },
      ],
    },
    {
      type: 'pie',
      name: '位置详情',
      height: '100%',
      radius: ['50%', '80%'],
      labelLayout: {
        hideOverlap: false
      },
      label: {
        position: 'inner',
        rotate: 70
      },
      itemStyle: {
        borderWidth: 1,
        borderColor: '#fff',
      },
      data: [
        { name: '托运行李', value: 100 },
        { name: '携带行李', value: 100 },
        { name: '体外衣物', value: 50 },
        { name: '体内藏匿', value: 30 },
        { name: '随身包裹', value: 20 },
        { name: '其他物品', value: 10 },
      ],
    },
  ],
});

const { setOption } = useEcharts(pieChart);
const pollingIntervalNum = ref(null);
const pollingInterval = ref(null);
onMounted(() => {
  pollingIntervalNum.value = setInterval(fetchDataNum(), 5000);
  pollingInterval.value = setInterval(fetchData(), 5000);

});
function fetchDataNum() {
  getTotalSome().then(res => {
    totalNum.value = res.data;
  }).catch(error => {
    console.error(error);
    clearInterval(pollingIntervalNum.value);
  });
  return fetchDataNum
}
function fetchData() {
  getPosition().then(res => {
    chartOption.value.series[0].data = res.data.map((el, idx) => {
      if (el.checkPositionNameTwo == '无') {
        return {
          value: el.total,
          name: el.checkPositionNameOne,
        };
      }
    });
    chartOption.value.series[1].data = res.data.map((el, idx) => {
      if (el.checkPositionNameTwo != '无') {
        return {
          value: el.total,
          name: el.checkPositionNameTwo,
        };
      }
    });
    setOption?.(chartOption.value);
  }).catch(error => {
    console.error(error);
    clearInterval(pollingInterval.value);
  });
  return fetchData
}

</script>

<style lang="less" scoped>
.classification-of-seized-quantity {
  display: flex;
  flex-flow: column;
  height: 100%;
}

.total {
  display: flex;
  justify-content: space-between;

  .count-inner {
    width: calc(33.33% - 8px);
    padding: 20px;
    border: 1px solid #363F4C;
    border-radius: 8px;

    &:nth-child(1) {
      color: #92C4FC;
    }

    &:nth-child(2) {
      color: #C49929;
    }

    &:nth-child(3) {
      color: #A83A3F;
    }

    .item-title {
      margin-bottom: 8px;
    }

    .tips {
      color: #9499A0;
      font-size: 12px;
      margin-top: 16px;

      .icon {
        margin-right: 8px;
        color: #10B981;

        .svg-icon {
          fill: #10B981;
          transform: rotate(-90deg);
        }

        &.up {
          color: #EF4444;

          .svg-icon {
            fill: #EF4444;
          }
        }

        &.down {
          .svg-icon {
            transform: rotate(90deg);
          }
        }
      }
    }
  }
}

:deep(.card-panel-num) {
  margin: 8px 0;
  font-size: 20px;
  // color: #fbffff;
}

.echarts {
  flex: 1;
  width: 100%;
}

.dashboard-title {
  position: relative;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: nowrap;
  flex-direction: row;
  z-index: 1;
  align-content: flex-start;
  background: url(/src/assets/images/titlebg-4340cf1c.png);
  background-size: 100% 100%;
  font-weight: 400;
  font-size: 14px;
  color: #fbffff;
  padding: 0 20px;
  height: 42px;
}
</style>