<template>
  <div class="alarm-history-table">
    <BasicTable ref="alarmHistory" @register="registerTable">
      <template #bodyCell="{ column, record }">
        <template v-if="column.dict">
          <!-- 除了 101（蓝色预警）其他都是红色字体 -->
          <span v-if="column.dataIndex === 'nwartype'" :class="{ 'color-#ff3823': ['102', '103', '104', '201', '1001'].includes(record.nwartype) }">
            {{ render.renderDictText(record.nwartype, 'leveltype') || '-' }}
          </span>
          <span v-else>
            {{ render.renderDictText(record[column.dataIndex], column.dict) || '-' }}
          </span>
        </template>
      </template>
    </BasicTable>
  </div>
</template>

<script lang="ts" setup>
  //ts语法
  import { watch, ref, defineExpose, inject, onMounted } from 'vue';
  import { BasicTable, BasicColumn } from '/@/components/Table';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { defHttp } from '/@/utils/http/axios';
  import dayjs from 'dayjs';
  import { getAutoScrollContainer } from '/@/utils/common/compUtils';
  import { render } from '/@/utils/common/renderUtils';

  const props = defineProps({
    scroll: {
      type: Object,
      default: { y: 0 },
    },
    list: {
      type: Function,
      default: (params) => defHttp.get({ url: '/safety/ventanalyAlarmLog/getHZGateAlarmLog', params }),
    },
  });

  const alarmHistory = ref();
  const columns: BasicColumn[] = [
    {
      title: '安装地点',
      dataIndex: 'content',
      width: 300,
      align: 'center',
    },
    {
      title: '报警事件',
      dataIndex: 'createTime',
      width: 300,
      align: 'center',
    },
  ];

  const tableScroll = props.scroll.y ? ref({ y: props.scroll.y - 100 }) : ref({});

  watch(
    () => props.scroll.y,
    (newVal) => {
      if (newVal) {
        tableScroll.value = { y: newVal - 100 };
      } else {
        tableScroll.value = {};
      }
    }
  );

  // 列表页面公共参数、方法
  const { tableContext, onExportXls } = useListPage({
    tableProps: {
      api: props.list,
      columns: columns,
      canResize: true,
      showTableSetting: false,
      showActionColumn: false,
      bordered: false,
      size: 'small',
      scroll: tableScroll,
      showIndexColumn: true,
      formConfig: {
        showAdvancedButton: false,
        // autoAdvancedCol: 2,
        schemas: [
          {
            field: 'createTime_begin',
            label: '开始时间',
            component: 'DatePicker',
            defaultValue: dayjs().add(-30, 'day').format('YYYY-MM-DD HH:mm:ss'),
            required: true,
            componentProps: {
              showTime: true,
              valueFormat: 'YYYY-MM-DD HH:mm:ss',
              getPopupContainer: getAutoScrollContainer,
            },
            colProps: {
              span: 4,
            },
          },
          {
            field: 'createTime_end',
            label: '结束时间',
            component: 'DatePicker',
            defaultValue: dayjs(),
            required: true,
            componentProps: {
              showTime: true,
              valueFormat: 'YYYY-MM-DD HH:mm:ss',
              getPopupContainer: getAutoScrollContainer,
            },
            colProps: {
              span: 4,
            },
          },
          {
            label: '查询设备',
            field: 'deviceid',
            component: 'Input',
            colProps: {
              span: 4,
            },
          },
        ],
      },
      fetchSetting: {
        listField: 'records',
      },
      pagination: {
        current: 1,
        pageSize: 10,
        pageSizeOptions: ['10', '30', '50', '100'],
      },
    },
  });
  //注册table数据
  const [registerTable, { setLoading }] = tableContext;

  onMounted(async () => {});

  defineExpose({ setLoading });
</script>

<style scoped lang="less">
  @ventSpace: zxm;

  :deep(.ventSpace-table-body) {
    height: auto !important;
  }
  :deep(.zxm-picker) {
    height: 30px !important;
  }
  .alarm-history-table {
    width: 100%;
    :deep(.jeecg-basic-table-form-container) {
      .@{ventSpace}-form {
        padding: 0 !important;
        border: none !important;
        margin-bottom: 0 !important;
        .@{ventSpace}-picker,
        .@{ventSpace}-select-selector {
          width: 100% !important;
          background: #00000017;
          border: 1px solid #b7b7b7;
          input,
          .@{ventSpace}-select-selection-item,
          .@{ventSpace}-picker-suffix {
            color: #fff;
          }
          .@{ventSpace}-select-selection-placeholder {
            color: #ffffffaa;
          }
        }
      }
      .@{ventSpace}-table-title {
        min-height: 0 !important;
      }
    }
  }
</style>
