<template>
  <Box title="设备运行状态" v-bind="$attrs">
    <div class="h-full flex flex-col">
      <div class="py-10px relative">
        <div
          class="text-center bg-clip-text text-transparent bg-gradient-to-t from-[#76E9F0] to-[#A7E6EE] text-15px font-bold"
        >
          {{ currentTabValue }}
        </div>
        <div
          class="absolute right-18px top-1/2 transform -translate-y-1/2"
          v-if="tabList.length > 1"
        >
          <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>
      <div class="flex-1 px-30px grid grid-cols-2 gap-y-10px gap-x-10px pb-16px">
        <div class="flex" v-for="(item, index) in list" :key="index">
          <div class="text-center">
            <img class="w-65.5px h-65.5px" :src="item.img" alt="" srcset="" />
            <div class="text-12px font-bold text-white mt-11px">{{ item.name }}</div>
          </div>
          <div class="ml-18px">
            <div class="flex items-center h-22px">
              <div class="w-7px h-7px bg-[#76E9F0]"></div>
              <div class="ml-11px text-11px text-white">在线</div>
              <div class="text-white ml-10px">{{ item.value[0] }}</div>
            </div>
            <div class="flex items-center h-22px">
              <div class="w-7px h-7px bg-[#F7B379]"></div>
              <div class="ml-11px text-11px text-white">离线</div>
              <div class="text-white ml-10px">{{ item.value[1] }}</div>
            </div>
            <div class="flex items-center h-22px">
              <div class="w-7px h-7px bg-[#EB313E]"></div>
              <div class="ml-11px text-11px text-white">故障</div>
              <div class="text-white ml-10px">{{ item.value[2] }}</div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </Box>
</template>

<script lang="ts">
  import { defineComponent, reactive, ref, onBeforeMount, toRefs, computed, watch } from 'vue'
  import Box from './Box.vue'
  import { getAgriculturalBasic, getDevicesNum } from '/@/api/sys/other'
  import { useVisualizationStore } from '/@/store/modules/visualization'
  import { Dropdown, Menu } from 'ant-design-vue'
  import { DownOutlined } from '@ant-design/icons-vue'
  import { cloneDeep } from 'lodash-es'
  import d01 from '/@/assets/images/d01.png'
  import d02 from '/@/assets/images/d02.png'
  import d03 from '/@/assets/images/d03.png'
  import d04 from '/@/assets/images/d04.png'

  const defaultDevice = [
    {
      name: 'AI智能监控',
      value: [0, 0, 0],
      img: d01,
    },
    {
      name: '水质监测',
      value: [0, 0, 0],
      img: d02,
    },
    {
      name: '土壤监测',
      value: [0, 0, 0],
      img: d03,
    },
    {
      name: '气象监测',
      value: [0, 0, 0],
      img: d04,
    },
  ]

  export default defineComponent({
    components: {
      Box,
      Dropdown,
      Menu,
      MenuItem: Menu.Item,
      DownOutlined,
    },
    props: {
      baseId: {
        type: Number,
      },
    },
    setup(props) {
      console.log('===SBYZT')
      console.log(props.baseId)

      const Data = reactive({
        tabList: ref<any>([]),
        currentTab: ref<number | string>(''),
        list: cloneDeep(defaultDevice),
      })

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

      const visualizationStore = useVisualizationStore()

      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 getTabs() {
        const resData = await getAgriculturalBasic({
          parent_id: visualizationStore.getAddresId,
          type: 1,
        })
        Data.tabList = resData
        let defaultId = ''
        if (resData.length) defaultId = resData[0].id
        onMenuClick({ key: defaultId })
      }

      async function getData() {
        const resData = await getDevicesNum({
          base_id: props.baseId ?? Data.currentTab,
        })
        Object.keys(resData).map((e, index) => {
          Data.list[index].value = resData[e].slice(1)
        })
      }

      onBeforeMount(() => {
        if (isBase.value) {
          getData()
        } else {
          getTabs()
        }
      })
      const isBase = computed(() => !!props.baseId)

      watch(
        () => visualizationStore.getAddresId,
        () => {
          getTabs()
        },
      )

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

<style scoped lang="less">
  .active {
    @apply font-bold text-15px text-[#76E9F0];
  }
</style>
