<script setup lang="ts">
import { computed, ref, watch } from 'vue';

import SvgIcon from '@/components/SvgIcon.vue';
import { useRequest } from '@/hooks/request';
import { useViewVisible } from '@/hooks/view-visible';
import { getDeviceParams } from '@/api';

import type { DeviceParamGroup, DeviceParamItem, OptionItem } from '@/types';

interface Props {
  devId: number;
}

const props = defineProps<Props>();

const emit = defineEmits<{
  viewHistoryData: [paramCode: string];
}>();

const { visible, showView, hideView } = useViewVisible();
const { isLoading, handleRequest } = useRequest();
const allParamGroups = ref<DeviceParamGroup[]>([]);
const activeGroup = ref(-1);

const groupList = computed<OptionItem<number>[]>(() => {
  return allParamGroups.value.map((item) => ({
    value: item.id,
    label: item.deviceParamGroupName,
  }));
});

const paramList = computed<DeviceParamItem[]>(() => {
  const params = allParamGroups.value.find((item) => item.id === activeGroup.value);

  if (params) {
    return params.valueVos;
  }

  return [];
});

watch(visible, () => {
  if (visible.value) {
    getAllParamGroups();
  }
});

const getAllParamGroups = () => {
  handleRequest(async () => {
    const data = await getDeviceParams(props.devId, false);
    allParamGroups.value = data;

    if (data.length) {
      activeGroup.value = data[0].id;
    }
  });
};

const viewData = (param: DeviceParamItem) => {
  emit('viewHistoryData', param.deviceParamCode);
};

const handleClose = () => {
  allParamGroups.value = [];
  activeGroup.value = -1;
};

defineExpose({
  showView,
  hideView,
});
</script>

<template>
  <AModal
    v-model:open="visible"
    wrap-class-name="dev-work-params-modal"
    :title="$t('createDevice.parameters')"
    :width="920"
    centered
    :footer="null"
    :after-close="handleClose"
  >
    <AEmpty v-show="!groupList.length" />
    <ASpin class="center-loading" :spinning="isLoading" />
    <ATabs class="hide-tabs-border" v-model:active-key="activeGroup">
      <ATabPane v-for="item in groupList" :key="item.value" :tab="item.label" />
    </ATabs>
    <div class="param-list">
      <ARow :gutter="[16, 16]">
        <ACol v-for="item in paramList" :key="item.deviceParamCode" :span="8">
          <div class="param-container">
            <div class="param-header">
              <div class="ellipsis-text param-name">{{ item.paramName }}</div>
              <div class="param-view" @click="viewData(item)">{{ $t('deviceWorkStatus.viewData') }}</div>
              <SvgIcon name="right" :size="12" color="rgba(0, 0, 0, 0.25)" />
            </div>
            <div class="param-body">
              <div class="param-value">{{ item.value }}{{ item.unit }}</div>
              <div class="param-time">{{ item.time }}</div>
            </div>
          </div>
        </ACol>
      </ARow>
    </div>
  </AModal>
</template>

<style lang="scss">
.dev-work-params-modal {
  .ant-modal,
  .ant-modal > div,
  .ant-modal-content {
    height: 640px;
  }

  .ant-modal-content {
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }

  .ant-modal-body {
    display: flex;
    flex-direction: column;
    height: calc(100% - 32px);
  }

  .ant-empty {
    margin-top: 150px;
  }
}
</style>

<style lang="scss" scoped>
.param-list {
  height: 100%;
  overflow: hidden auto;
}

.param-container {
  width: 280px;
  height: 156px;
  background: #fff;
  border: 1px solid #e4e7ed;
  border-radius: 8px;
}

.param-header {
  display: flex;
  align-items: center;
  height: 50px;
  padding-right: 12px;
  padding-left: 16px;
  border-bottom: 1px solid #e4e7ed;
}

.param-name {
  flex: 1;
  margin-right: 16px;
  font-size: 14px;
  line-height: 22px;
  color: var(--antd-color-text);
}

.param-view {
  margin-right: 4px;
  font-size: 14px;
  font-weight: 500;
  line-height: 24px;
  color: var(--antd-color-primary);
  cursor: pointer;
}

.param-body {
  padding: 24px 16px;
}

.param-value {
  margin-bottom: 8px;
  font-size: 18px;
  font-weight: 600;
  line-height: 26px;
  color: var(--antd-color-text);
}

.param-time {
  font-size: 14px;
  line-height: 24px;
  color: var(--antd-color-text-tertiary);
}
</style>
