<script lang="ts" setup>
// 导入部分
import type { VbenFormProps } from '@vben/common-ui';
import type { VxeGridProps } from '#/adapter/vxe-table';
import { Page } from '@vben/common-ui';
import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { ElButton, ElSpace, ElTabs, ElTabPane, ElDialog } from 'element-plus';
import { ref } from 'vue';

import {
  oilLossDetailColumns, OIL_LOSS_DETAIL_MOCK_DATA, oilLossDetailQueryFormSchema,
  oilLossStatisticsColumns, OIL_LOSS_STATISTICS_MOCK_DATA, oilLossStatisticsQueryFormSchema,
  abnormalInvestigationColumns, ABNORMAL_INVESTIGATION_MOCK_DATA, abnormalInvestigationQueryFormSchema
} from './oilDamageReport-data';

// 当前激活的Tab
const activeTab = ref('detail');

// 损益异常规则说明弹窗
const ruleDialogVisible = ref(false);

// 表单公共配置
const commonFormConfig: Partial<VbenFormProps> = {
  commonConfig: {
    labelWidth: 80,
    componentProps: { allowClear: true },
  },
  wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-4',
};

// 油品损溢明细表单配置
const oilLossDetailFormOptions: VbenFormProps = {
  ...commonFormConfig,
  schema: oilLossDetailQueryFormSchema(),
};

// 油品损溢明细表格配置
const oilLossDetailGridOptions: VxeGridProps = {
  columns: oilLossDetailColumns,
  height: '800px',
  proxyConfig: {
    autoLoad: true,
    ajax: {
      query: async () => ({ items: OIL_LOSS_DETAIL_MOCK_DATA, total: OIL_LOSS_DETAIL_MOCK_DATA.length }),
    },
  },
  rowConfig: { keyField: 'id' },
  pagerConfig: { enabled: true, pageSize: 100, pageSizes: [10, 20, 50, 100] },
};

// 油品损溢统计表单配置
const oilLossStatisticsFormOptions: VbenFormProps = {
  ...commonFormConfig,
  schema: oilLossStatisticsQueryFormSchema(),
};

// 油品损溢统计表格配置
const oilLossStatisticsGridOptions: VxeGridProps = {
  columns: oilLossStatisticsColumns,
  height: '800px',
  proxyConfig: {
    autoLoad: true,
    ajax: {
      query: async () => ({ items: OIL_LOSS_STATISTICS_MOCK_DATA, total: OIL_LOSS_STATISTICS_MOCK_DATA.length }),
    },
  },
  rowConfig: { keyField: 'id' },
  pagerConfig: { enabled: true, pageSize: 100, pageSizes: [10, 20, 50, 100] },
};

// 异常调查统计表单配置
const abnormalInvestigationFormOptions: VbenFormProps = {
  ...commonFormConfig,
  schema: abnormalInvestigationQueryFormSchema(),
};

// 异常调查统计表格配置
const abnormalInvestigationGridOptions: VxeGridProps = {
  columns: abnormalInvestigationColumns,
  height: '800px',
  proxyConfig: {
    autoLoad: true,
    ajax: {
      query: async () => ({ items: ABNORMAL_INVESTIGATION_MOCK_DATA, total: ABNORMAL_INVESTIGATION_MOCK_DATA.length }),
    },
  },
  rowConfig: { keyField: 'id' },
  pagerConfig: { enabled: true, pageSize: 100, pageSizes: [10, 20, 50, 100] },
};

// 创建表格实例
const [OilLossDetailTable] = useVbenVxeGrid({ formOptions: oilLossDetailFormOptions, gridOptions: oilLossDetailGridOptions });
const [OilLossStatisticsTable] = useVbenVxeGrid({ formOptions: oilLossStatisticsFormOptions, gridOptions: oilLossStatisticsGridOptions });
const [AbnormalInvestigationTable] = useVbenVxeGrid({ formOptions: abnormalInvestigationFormOptions, gridOptions: abnormalInvestigationGridOptions });

// 导出按钮点击事件
const handleExport = () => {
  console.log('导出数据');
  // TODO: 添加导出逻辑
};
</script>

<template>
  <Page :auto-content-height="true">
    <div class="oil-damage-report-container">
      <!-- 油品损溢表Tab切换 -->
      <ElTabs v-model="activeTab" class="mb-4">
        <ElTabPane label="油品损溢明细" name="detail">
          <!-- 油品损溢明细表格 -->
          <OilLossDetailTable table-title="油品损溢明细">
            <template #toolbar-tools>
              <ElSpace>
                <ElButton @click="handleExport">导出</ElButton>
                <ElButton type="primary" @click="ruleDialogVisible = true">损益异常规则说明</ElButton>
              </ElSpace>
            </template>
          </OilLossDetailTable>
        </ElTabPane>
        <ElTabPane label="油品损溢统计" name="statistics">
          <!-- 油品损溢统计表格 -->
          <OilLossStatisticsTable table-title="油品损溢统计">
            <template #toolbar-tools>
              <ElSpace>
                <ElButton @click="handleExport">导出</ElButton>
              </ElSpace>
            </template>
          </OilLossStatisticsTable>
        </ElTabPane>
        <ElTabPane label="异常调查统计" name="abnormal">
          <!-- 异常调查统计表格 -->
          <AbnormalInvestigationTable table-title="异常调查统计">
            <template #toolbar-tools>
              <ElSpace>
                <ElButton @click="handleExport">导出</ElButton>
              </ElSpace>
            </template>
          </AbnormalInvestigationTable>
        </ElTabPane>
      </ElTabs>

      <!-- 损益异常规则说明弹窗 -->
      <ElDialog
        v-model="ruleDialogVisible"
        title="损益异常规则说明"
        width="500px"
      >
        <div class="rule-content">
          <p>转损溢异常调查任务规则：</p>
          <ul>
            <li>0# 单日油站损溢率大于5或小于-5</li>
            <li>92# 单日油站损溢率大于3或小于-3</li>
            <li>95# 单日油站损溢率大于3或小于-3</li>
            <li>98# 单日油站损溢率大于3或小于-3</li>
          </ul>
        </div>
      </ElDialog>
    </div>
  </Page>
</template>

<style scoped lang="scss">
.oil-damage-report-container {
  min-height: calc(100vh - 120px);
  padding: 16px;
  background-color: #fff;
}

.rule-content {
  line-height: 1.8;
}

.rule-content ul {
  padding-left: 20px;
  margin-top: 10px;
}

.rule-content li {
  margin-bottom: 5px;
}
</style>
