<template>
  <div class="alarm-history-table">
    <BasicTable ref="alarmHistory" @register="registerTable">
      <template #form-onExportXls>
        <a-button type="primary" preIcon="ant-design:export-outlined" @click="onExportXls()"> 导出</a-button>
      </template>
    </BasicTable>
  </div>
</template>

<script lang="ts" name="system-user" setup>
  //ts语法
  import { watch, ref, defineExpose, onMounted, inject } from 'vue';
  import { BasicTable } from '/@/components/Table';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { getTableHeaderColumns } from '/@/hooks/web/useWebColumns';
  import { defHttp } from '/@/utils/http/axios';
  import dayjs from 'dayjs';

  const globalConfig = inject('globalConfig');

  const props = defineProps({
    columnsType: {
      type: String,
      required: true,
    },
    deviceType: {
      type: String,
      required: true,
    },
    deviceListApi: {
      type: Function,
      default: (params) => defHttp.get({ url: '/safety/ventanalyManageSystem/linkdevicelist', params }),
    },
    designScope: {
      type: String,
    },
    sysId: {
      type: String,
    },
    scroll: {
      type: Object,
      default: () => {},
    },
    list: {
      type: Function,
      default: (params) => defHttp.get({ url: '/safety/ventanalyAlarmLog/list', params }),
    },
  });
  const alarmHistory = ref();
  const columns = ref([]);
  const deviceOptions = ref([]);

  watch(
    () => {
      return props.columnsType;
    },
    (newVal) => {
      const column = getTableHeaderColumns(newVal + '_history');
      if (column && column.length < 1) {
        const arr = newVal.split('_');
        const columnKey = arr.reduce((prev, cur, index) => {
          if (index !== arr.length - 2) {
            return prev + '_' + cur;
          } else {
            return prev;
          }
        });
        columns.value = getTableHeaderColumns(arr[0] + '_history');
      } else {
        columns.value = column;
      }
      if (alarmHistory.value) reload();
    },
    {
      immediate: true,
    }
  );

  async function getDeviceList() {
    let result;
    debugger;
    if (globalConfig.History_Type == 'vent') {
      if (props.deviceListApi) {
        result = await props.deviceListApi();
      } else {
        result = await defHttp.get({
          url: '/safety/ventanalyManageSystem/linkdevicelist',
          params: { sysId: props.sysId, deviceType: props.deviceType, pageSize: 9999 },
        });
      }
    } else {
      result = await defHttp.get({
        url: '/safety/ventanalyManageSystem/linkdevicelist',
        params: { sysId: props.sysId, deviceType: props.deviceType.startsWith('vehicle') ? 'location_normal' : props.deviceType },
      });
    }
    if (result) {
      deviceOptions.value = [];
      deviceOptions.value = result.map((item) => {
        return {
          label: item['strname'],
          value: item['id'],
          strtype: item['strtype'],
          strinstallpos: item['strinstallpos'],
          devicekind: item['devicekind'],
        };
        // return { label: item['strname'], value: item['id']}
      });
      // globalConfig.History_Type == 'vent' && deviceOptions.value[0]
      //   ? getForm().setFieldsValue({ deviceId: deviceOptions.value[0] ? deviceOptions.value[0]['value'] : '' })
      //   : getForm().setFieldsValue({ deviceId: deviceOptions.value[0] ? deviceOptions.value[0]['value'] : '' });
      getForm().setFieldsValue({ deviceId: deviceOptions.value[0] ? deviceOptions.value[0]['value'] : '' });
    }
  }

  // 列表页面公共参数、方法
  const { tableContext, onExportXls } = useListPage({
    tableProps: {
      api: props.list,
      columns: columns,
      canResize: true,
      showTableSetting: false,
      showActionColumn: false,
      bordered: false,
      size: 'small',
      scroll: props.scroll,
      formConfig: {
        labelAlign: 'left',
        showAdvancedButton: false,
        // autoAdvancedCol: 2,
        schemas: [
          {
            label: '时间范围',
            field: 'createTime',
            component: 'RangePicker',
            defaultValue: [dayjs().subtract(1, 'day'), dayjs()],
            componentProps: {
              valueFormat: 'YYYY-MM-DD HH:mm:ss',
            },
          },
          {
            label: '查询设备',
            field: 'deviceId',
            component: 'Select',
            defaultValue: deviceOptions.value[0] ? deviceOptions.value[0]['value'] : '',
            required: true,
            componentProps: {
              options: deviceOptions,
            },
          },
          // {
          //   label: '查询设备',
          //   field: 'deviceId',
          //   component: 'ApiSelect',
          //   componentProps: {
          //     api: props.deviceListApi,
          //     resultField: 'records',
          //     labelField: 'strname',
          //     valueField: 'id',
          //   },
          // },
        ],
        fieldMapToTime: [['createTime', ['starttime', 'endtime'], '']],
      },
      fetchSetting: {
        listField: 'records',
      },
      pagination: {
        current: 1,
        pageSize: 10,
        pageSizeOptions: ['10', '30', '50', '100'],
      },
      beforeFetch(params) {
        params.devicetype = props.deviceType + '*';
        if (props.sysId) {
          params.sysId = props.sysId;
        }
      },
    },
    exportConfig: {
      name: '预警历史列表',
      url: '/safety/ventanalyAlarmLog/exportXls',
    },
  });
  //注册table数据
  const [registerTable, { reload, setLoading, getForm }] = tableContext;

  onMounted(async () => {
    await getDeviceList();
  });

  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>
