<template>
  <view>
    <uni-card>
      <uni-row class="flex-row flex-wrap" >
        <!--   昨日工单数量   -->
        <uni-row class="flex-row flex-wrap align-center" style="flex: 1; padding: 10px">
          <uni-col class="count-label" :span="24">{{ $t('statistic.maintenance.dayCount') }}</uni-col>
          <uni-col class="count-label" :span="12">{{ $t('statistic.maintenance.count1') }}</uni-col>
          <uni-col class="count-label" :span="12">{{ $t('statistic.maintenance.count2') }}</uni-col>
          <uni-col class="count-value" :span="12" style="margin-top: 6px">{{ data.yesterday.total }}</uni-col>
          <uni-col class="count-value" :span="12" style="margin-top: 6px">{{ data.yesterday.todo }}</uni-col>
        </uni-row>
        <view class="divider-h" style="align-self: end" />
        <!--   近一周工单数量   -->
        <uni-row class="flex-row flex-wrap" style="flex: 1; padding: 10px">
          <uni-col class="count-label" :span="24">{{ $t('statistic.maintenance.weeklyCount') }}</uni-col>
          <uni-col class="count-label" :span="12">{{ $t('statistic.maintenance.count1') }}</uni-col>
          <uni-col class="count-label" :span="12">{{ $t('statistic.maintenance.count2') }}</uni-col>
          <uni-col class="count-value" :span="12" style="margin-top: 6px">{{ data.week.total }}</uni-col>
          <uni-col class="count-value" :span="12" style="margin-top: 6px">{{ data.week.todo }}</uni-col>
        </uni-row>
        <uni-col :span="24">
          <view class="divider-v" />
        </uni-col>
        <!--   近一月工单数量   -->
        <uni-row class="flex-row flex-wrap" style="flex: 1; padding: 10px">
          <uni-col class="count-label" :span="24">{{ $t('statistic.maintenance.monthlyCount') }}</uni-col>
          <uni-col class="count-label" :span="12">{{ $t('statistic.maintenance.count1') }}</uni-col>
          <uni-col class="count-label" :span="12">{{ $t('statistic.maintenance.count2') }}</uni-col>
          <uni-col class="count-value" :span="12" style="margin-top: 6px">{{ data.month.total }}</uni-col>
          <uni-col class="count-value" :span="12" style="margin-top: 6px">{{ data.month.todo }}</uni-col>
        </uni-row>
        <view class="divider-h" style="align-self: start" />
        <!--   总工单数量   -->
        <uni-row class="flex-row flex-wrap" style="flex: 1; padding: 10px">
          <uni-col class="count-label" :span="24">{{ $t('statistic.maintenance.totalCount') }}</uni-col>
          <uni-col class="count-label" :span="12">{{ $t('statistic.maintenance.count1') }}</uni-col>
          <uni-col class="count-label" :span="12">{{ $t('statistic.maintenance.count2') }}</uni-col>
          <uni-col class="count-value" :span="12" style="margin-top: 6px">{{ data.total.total }}</uni-col>
          <uni-col class="count-value" :span="12" style="margin-top: 6px">{{ data.total.todo }}</uni-col>
        </uni-row>
      </uni-row>
    </uni-card>
    <!--   保养工单状态统计   -->
    <uni-card>
      <uni-section :title="$t('statistic.inspection.workOrder.title')" />
      <view class="flex-row align-center">
        <view class="charts-box">
          <qiun-data-charts
              type="ring"
              :opts="opts"
              :chartData="data.statusChart"
          />
        </view>
      </view>
    </uni-card>
    <!--   今日工单状态统计   -->
    <uni-card>
      <uni-section :title="$t('statistic.maintenance.dayWorkOrder.title')" />
      <view class="flex-row align-center">
        <view class="charts-box">
          <qiun-data-charts
              type="ring"
              :opts="opts"
              :chartData="data.todayStatusChart"
          />
        </view>
      </view>
    </uni-card>
  </view>
</template>

<script setup>
import { getCurrentInstance, reactive, ref } from "vue";
import {
  getInspectionStatusStatistic,
  getInspectionTodayStatusStatistic,
  getMonthlyInspectionStatistic,
  getTotalInspectionStatistic,
  getWeeklyInspectionStatistic,
  getYesterdayInspectionStatistic,
} from "@/api/statistic";

const { appContext } = getCurrentInstance()
const t = appContext.config.globalProperties.$t

// 初始化环形图
const opts = {
  color: ['#0055BB', '#FF5D00'],
  padding: [0, 10, 0, 10],
  title: { name: '' },
  subtitle: { name: '' },
  legend: {
    position: 'bottom',
  },
  extra: {
    ring: {
      ringWidth: 30, // 圆环的宽度
      activeOpacity: 0.5, // 启用Tooltip点击时，突出部分的透明度
      activeRadius: 10, // 启用Tooltip点击时，突出部分的宽度（最大值不得超过labelWidth）
      offsetAngle: -90, // 起始角度偏移度数
      labelWidth: 10, // 数据标签到饼图外圆连线的长度
      customRadius: 80, // 自定义半径
      borderWidth: 3, // 分割线的宽度
      borderColor: "#FFFFFF" // 分割线的颜色
    }
  }
}

const data = reactive({
  yesterday: {},
  week: {},
  month: {},
  total: {},
  statusChart: { series: [] },
  todayStatusChart: { series: [] },
})

const isLoadData = ref(false)

// 加载巡检相关统计数据
const loadData = async () => {
  if (isLoadData.value) return // 已加载数据后不再加载数据
  isLoadData.value = true

  const getYesterdayInspectionStatisticAsync = getYesterdayInspectionStatistic()
  const getWeeklyInspectionStatisticAsync = getWeeklyInspectionStatistic()
  const getMonthlyInspectionStatisticAsync = getMonthlyInspectionStatistic()
  const getTotalInspectionStatisticAsync = getTotalInspectionStatistic()
  const getInspectionStatusStatisticAsync = getInspectionStatusStatistic()
  const getInspectionTodayStatusStatisticAsync = getInspectionTodayStatusStatistic()

  data.yesterday = (await getYesterdayInspectionStatisticAsync).data
  data.week = (await getWeeklyInspectionStatisticAsync).data
  data.month = (await getMonthlyInspectionStatisticAsync).data
  data.total = (await getTotalInspectionStatisticAsync).data

  const status = (await getInspectionStatusStatisticAsync).data
  const todayStatus = (await getInspectionTodayStatusStatisticAsync).data

  // 工单状态数据填充
  data.statusChart.series = [{
    data: [
      {
        name: t('statistic.maintenance.workOrder.status1'),
        value: status.todo || 0,
        labelText: t('statistic.maintenance.workOrder.status1') + ': ' + (status.todo || 0)
      },
      { name: t('statistic.maintenance.workOrder.status2'),
        value: status.finished || 0,
        labelText: t('statistic.maintenance.workOrder.status2') + ': ' + (status.finished || 0)
      },
    ],
  }]

  // 今日工单状态相关数据填充
  data.todayStatusChart.series = [{
    data: [
      {
        name: t('statistic.maintenance.workOrder.status1'),
        value: todayStatus.todo || 0,
        labelText: t('statistic.maintenance.workOrder.status1') + ': ' + (todayStatus.todo || 0)
      },
      { name: t('statistic.maintenance.workOrder.status2'),
        value: todayStatus.finished || 0,
        labelText: t('statistic.maintenance.workOrder.status2') + ': ' + (todayStatus.finished || 0)
      },
    ],
  }]
}

defineExpose({ loadData })
</script>

<style scoped lang="scss">
.charts-box {
  width: 100%;
  height: 300px;
}

:deep(.uni-card) {
  padding: 0 !important;

  .uni-card__content {
    padding: 0 !important;
  }

  .uni-section {
    margin-top: 8px;
    padding: 0 15px 0 20px;

    .uni-section-header {
      padding: 10px 0;
    }

    .uni-section__content-title {
      font-size: 16px !important;
      font-weight: 600;
    }
  }
}

:deep(.uni-section) {
  background-color: transparent;

  .uni-section-header {
    padding: 5px 10px;
  }
  .line {
    width: 3px;
    height: 14px;
    background-color: #004098;
  }
}

.divider {
  width: 1px;
  height: 114px;
  background-color: #CACCCF;
  margin: 0 24px;
}

.divider-v {
  width: auto;
  height: 1.5px;
  border-bottom: 1.5px #CACCCF dashed;
  margin: 0 20px;
}

.divider-h {
  width: 1.5px;
  height: 50px;
  border-left: 1.5px #CACCCF dashed;
  margin: 0 4px;
}

.count-label {
  color: #666666;
  font-size: 14px;
}

.count-value {
  color: #333333;
  font-size: 18px;
  font-weight: 500;
}

.mt-5 {
  margin-top: 5px;
}

.mt-8 {
  margin-top: 8px;
}

.pl-10 {
  padding-left: 10px;
}
</style>