<!-- eslint-disable vue/multi-word-component-names -->
<template>
  <div class="monitoring-page">
    <!-- 新增Tabs组件区分晚报/历史数据 -->
    <Tabs v-model:activeKey="activeTab" type="line" class="common-page-tabs">
      <TabPane tab="晚报监测" key="realtime">
        <!-- 晚报数据表格 -->
        <BasicTable @register="registerRealtimeTable" :scroll="{ x: 'max-content' }" :style="{ padding: 0 }">
          <template #action="{ record }">
            <div class="action-buttons">
              <!-- 操作按钮 -->
              <button @click="openModal(record)" class="resolved-btn" title="解决">
                <SvgIcon name="details" />
              </button>
            </div>
          </template>
          <template #resetBefore>
            <a-button type="default" class="ml-8px" preIcon="mdi:download" @click="onExportXlsTime"> 导出 </a-button>
          </template>
        </BasicTable>
      </TabPane>

      <TabPane tab="历史数据" key="history">
        <!-- 历史数据表格 -->
        <BasicTable @register="registerHistoryTable" :scroll="{ x: 'max-content' }" :style="{ padding: 0 }">
          <!-- <template #action="{ record }">
            <div class="action-buttons">
              <button @click="openModal(record, 'history')" class="action-btn">
                <SvgIcon name="details" />
              </button>
            </div>
          </template> -->
          <template #resetBefore>
            <a-button type="default" class="ml-8px" preIcon="mdi:download" @click="onExportXls"> 导出 </a-button>
          </template>
          <template #form-goaf-select>
            <a-select v-model:value="goafId" :options="goafOptions" placeholder="请选择" />
          </template>
        </BasicTable>
      </TabPane>
    </Tabs>
    <!-- 弹窗组件 -->
    <BasicModal @register="registerModal" :width="600" :minHeight="100" centered title="处理情况" @ok="handleResolve">
      <a-textarea class="ml-10px" :style="{ width: `calc(100% - 20px)` }" v-model:value="resolveValue" placeholder="请输入解决情况" :rows="4" />
    </BasicModal>
  </div>
</template>

<script setup lang="ts">
  import { ref } from 'vue';
  import { BasicTable } from '/@/components/Table';
  import { Tabs, TabPane, message } from 'ant-design-vue';
  import { SvgIcon } from '/@/components/Icon';
  // 引入模拟数据
  import { columns } from './reportAnalysis.data';
  import { getProvinceAlarm, getProvinceAlarmHistory } from './report.api';
  import { useInitForm } from '../../common/analysis';
  import { BasicModal } from '/@/components/Modal/index';
  import { historicalFormSchema } from '/@/views/monitor/sealedMonitor/monitor.data';
  // import { useIntervalFn } from '@vueuse/core';
  import { useListPage } from '/@/hooks/system/useListPage';

  // 激活的Tab页签
  const activeTab = ref('realtime');

  // 注册晚报数据表格
  const { tableContext: ctxRealtime, onExportXls: onExportXlsTime } = useListPage({
    tableProps: {
      columns,
      api: getProvinceAlarm,
      formConfig: {
        labelWidth: 120,
        schemas: [
          {
            label: '煤矿名称',
            field: 'deptId',
            component: 'MineCascader', // 自定义组件名
            colProps: { span: 6 },
            rules: [],
          },
        ],
        showAdvancedButton: false,
        schemaGroupNames: ['常规查询'],
      },
      pagination: true,
      striped: true,
      useSearchForm: true,
      bordered: true,
      showIndexColumn: false,
      actionColumn: {
        width: 80,
        title: '操作',
        dataIndex: 'action',
        slots: { customRender: 'action' },
        fixed: undefined,
      },
    },
    exportConfig: {
      url: '/ventanaly-province/province/alarm/exportProvinceAlarmReal',
      name: '自燃发火隐患分级',
      params: {
        alarmType: 'lateReport',
      },
    },
  });
  const [registerRealtimeTable] = ctxRealtime;
  const { goafOptions, goafId, hiscode, resolveValue, registerModal, openModal, initGoafOptions, handleResolve } = useInitForm();
  // 注册历史数据表格
  const { tableContext, onExportXls } = useListPage({
    tableProps: {
      columns,
      // columns: historyColumns,
      api: (params) => {
        if (!goafId.value) {
          message.info('请先选择煤矿及老空区');
          return Promise.reject();
        }
        params.goafId = goafId.value;
        return getProvinceAlarmHistory(params);
      },
      formConfig: {
        model: { customField: hiscode },
        labelWidth: 120,
        schemas: [
          {
            label: '煤矿名称',
            field: 'customField',
            component: 'MineCascader', // 自定义组件名
            componentProps: {
              initFromStore: false,
              syncToStore: false,
              changeOnSelect: false,
              onChange: (e) => {
                historyTable.setLoading(true);
                initGoafOptions(e).finally(() => {
                  historyTable.setLoading(false);
                });
              },
            },
            colProps: { span: 6 },
            rules: [],
          },
          ...historicalFormSchema,
        ],
        showAdvancedButton: false,
        schemaGroupNames: ['常规查询'],
      },
      pagination: true,
      striped: true,
      useSearchForm: true,
      bordered: true,
      showIndexColumn: true,
      showActionColumn: false,
    },
    exportConfig: {
      url: '/province/alarm/exportProvinceAlarmHistory',
      name: '历史数据',
      params: {
        alarmType: 'lateReport',
        goafId,
      },
    },
  });

  const [registerHistoryTable, historyTable] = tableContext;
</script>

<style lang="less" scoped>
  @import url(../../common/board.less);
</style>
