<template>
  <Box title="气象数据">
    <template #center>
      <div
        class="text-center bg-clip-text text-transparent bg-gradient-to-t from-[#76E9F0] to-[#A7E6EE] text-14px font-bold"
      >
        {{ currentTabValue }}
      </div>
    </template>
    <template #right>
      <div class="py-10px relative" v-if="tabList.length > 1">
        <div class="">
          <Dropdown
            overlayClassName="dropdownClass"
            placement="bottomRight"
            trigger="click"
            :style="{ height: '300px' }"
          >
            <div class="cursor-pointer">
              <span class="text-white text-12px">更多</span>
              <DownOutlined :style="{ fontSize: '12px', color: '#FFF' }" />
            </div>
            <template #overlay>
              <Menu @click="onMenuClick">
                <menu-item v-for="item in tabList" :key="item.id">
                  <div>{{ item.name }}</div>
                </menu-item>
              </Menu>
            </template>
          </Dropdown>
        </div>
      </div>
    </template>
    <div class="pl-23px py-23px flex items-center h-full">
      <div class="grid grid-cols-3 gap-20px w-full">
        <div v-for="item in list" :key="item.key">
          <div class="text-11px text-white">{{ item.label }}</div>
          <div
            class="mt-11px bg-clip-text text-transparent bg-gradient-to-t from-[#76E9F0] to-[#A7E6EE]"
          >
            <!-- <span class="text-18px font-bold">{{ item.value }}</span> -->
            <CountTo :startVal="0" :endVal="item.value" class="text-18px font-bold" separator="" />
            <span class="text-11px ml-4px">{{ item.unit }}</span>
          </div>
        </div>
      </div>
    </div>
  </Box>
</template>

<script lang="ts">
  import { defineComponent, reactive, ref, onBeforeMount, computed, toRefs } from 'vue'
  import { DownOutlined } from '@ant-design/icons-vue'
  import { Dropdown, Menu } from 'ant-design-vue'
  import { getAgriculturalDeviceBasic, getDevices, getDeviceDataStatics } from '/@/api/sys/other'
  import { CountTo } from '/@/components/CountTo'
  import Box from './Box.vue'
  import { cloneDeep } from 'lodash'

  import { useVContext } from '../useVContext'

  const defaultList = [
    {
      label: '空气温度',
      value: 0,
      unit: '℃',
      key: 'air_temperature',
    },
    {
      label: '空气湿度',
      value: 0,
      unit: '%',
      key: 'air_humidity',
    },
    {
      label: '光照强度',
      value: 0,
      unit: 'lux',
      key: 'illumination',
    },
    {
      label: '降雨量',
      value: 0,
      unit: 'mm',
      key: 'current_rainfall',
    },
    {
      label: '风速',
      value: 0,
      unit: 'm/s',
      key: 'wind_speed',
    },
    {
      label: '大气压力',
      value: 0,
      unit: 'Kpa',
      key: 'air_pressure',
    },
  ]
  export default defineComponent({
    components: {
      Box,
      Dropdown,
      DownOutlined,
      Menu,
      MenuItem: Menu.Item,
      CountTo,
    },
    props: ['baseId'],
    setup(props) {
      console.log('===QXSZ')
      console.log(props.baseId)
      const { rootEmitter } = useVContext()
      const Data = reactive({
        tabList: ref<any>([]),
        currentTab: ref<number | string>(''),
        list: cloneDeep(defaultList),
      })

      const chartRef = ref<HTMLDivElement | null>(null)

      const currentTabValue = computed(
        () => Data.tabList.find((e) => e.id == Data.currentTab)?.name ?? '',
      )

      function onMenuClick({ key }) {
        if (Data.currentTab == key) return
        Data.currentTab = key
        getData()
      }

      async function getData() {
        const devices = await getDevices({
          base: props.baseId ?? Data.currentTab,
          type: 4,
          status: 1,
        })
        if (devices.length) {
          const { list } = await getDeviceDataStatics({ device_id: devices[0].id })
          Data.list.forEach((e) => (e.value = Number(list[e.key])))
        } else {
          Data.list.forEach((e) => (e.value = 0))
        }
      }

      async function getTabs() {
        const resData = await getAgriculturalDeviceBasic({
          device_type: 4,
        })
        Data.tabList = resData
        if (resData.length) onMenuClick({ key: resData[0].id })
      }

      const isBase = computed(() => !!props.baseId)

      let timerTabIndex = 1

      onBeforeMount(() => {
        if (isBase.value) {
          getData()
        } else {
          getTabs()
        }

        rootEmitter.on('interval:auto', () => {
          getData()
        })

        rootEmitter.on('interval:tab', () => {
          if (Data.tabList.length == 0) return
          const index = timerTabIndex % Data.tabList.length
          onMenuClick({ key: Data.tabList[index].id })
          timerTabIndex++
        })
      })

      return {
        ...toRefs(Data),
        chartRef,
        currentTabValue,
        onMenuClick,
      }
    },
  })
</script>
