import type { VxeGridProps } from '#/adapter/vxe-table';
import type { FormSchemaGetter } from '#/adapter/form';

// 油品损溢明细数据结构
export interface OilLossDetailData {
  id: number;
  station: string; // 油站
  time: string; // 时间
  oilType: string; // 油品号
  plannedDispatch: number; // 计划发油（A）
  actualDispatch: number; // 油库实发（B）
  tankReceived: number; // 地罐实收（C）
  pumpSales: number; // 油机销量（D）
  returnTank: number; // 回罐（E）
  systemSales: number; // 系统销量（F）
  actualSales: number; // 实际销量（G）
  dispatchLoss: number; // 发油损溢（H=B-A）
  dispatchLossRate: number; // 发油损溢率‰(I=H/A*1000)
  transportLoss: number; // 运输损溢(J=C-B)
  transportLossRate: number; // 运输损溢率‰(K=J/B*1000)
  stationLoss: number; // 油站损溢(L)
  stationLossRate: number; // 油站损溢率‰(M=L/G*1000)
  dailyTotalLoss: number; // 单日总损溢(N=H+J+L)
  dailyTotalLossRate: number; // 单日总损溢率‰(O=N/G*1000)
  cumulativeTotalLoss: number; // 累计总损溢(P)
  cumulativeTotalSales: number; // 累计总销售(Q)
  cumulativeTotalLossRate: number; // 累计总损溢率‰(R=P/Q*1000)
  isLossAbnormal: string; // 损溢异常
}

// 油品损溢统计数据结构
export interface OilLossStatisticsData {
  id: number;
  station: string; // 油站
  oilType: string; // 油品
  dispatchLoss: number; // 发油损溢（L）
  transportLoss: number; // 运输损溢（L）
  stationLoss: number; // 油站损溢（L）
  totalLoss: number; // 总损溢（L）
  actualSales: number; // 实际销量（L）
  dispatchLossRate: number; // 发油损溢率（‰）
  transportLossRate: number; // 运输损溢率（‰）
  stationLossRate: number; // 油站损溢率（‰）
  totalLossRate: number; // 总损溢率（‰）
  abnormalInvestigationCount: number; // 损溢异常调查启动数
  completedInvestigationCount: number; // 已完成的调查数量
}

// 异常调查统计数据结构
export interface AbnormalInvestigationData {
  id: number;
  investigationResult: string; // 调查结果
  count: number; // 数量
  percentage: string; // 占比
}

// 油站选项
export const stationOptions = [
  { label: '机场油站', value: '机场油站' },
  { label: '北京东加油站', value: '北京东加油站' },
  { label: '北京西加油站', value: '北京西加油站' },
  { label: '北京南加油站', value: '北京南加油站' },
  { label: '北京北加油站', value: '北京北加油站' },
  { label: '河北油站', value: '河北油站' },
  { label: '龙飞虎加油站', value: '龙飞虎加油站' },
  { label: '法站加油站', value: '法站加油站' },
];

// 损溢异常选项
export const lossAbnormalOptions = [
  { label: '是', value: '是' },
  { label: '否', value: '否' },
];

// 油品号选项
export const oilTypeOptions = [
  { label: '0#', value: '0#' },
  { label: '92#', value: '92#' },
  { label: '95#', value: '95#' },
  { label: '98#', value: '98#' },
];

// 生成随机日期
const generateRandomDate = (startDate: Date, endDate: Date): string => {
  const randomTime = startDate.getTime() + Math.random() * (endDate.getTime() - startDate.getTime());
  const date = new Date(randomTime);
  return date.toISOString().slice(0, 19).replace('T', ' ');
};

// 油品损溢明细表格列配置
export const oilLossDetailColumns: VxeGridProps['columns'] = [
  // 固定字段 - 单行表头
  { title: '油站', field: 'station', minWidth: 120, align: 'center', fixed: 'left' },
  { title: '时间', field: 'time', minWidth: 160, align: 'center', fixed: 'left' },
  { title: '油品号', field: 'oilType', minWidth: 80, align: 'center' , fixed: 'left'},
  
  // 计划发油 - 两行表头
  { 
    title: '计划发油', 
    children: [
      { title: 'A', field: 'plannedDispatch', minWidth: 100, align: 'right' }
    ]
  },
  
  // 油库实发 - 两行表头
  { 
    title: '油库实发', 
    children: [
      { title: 'B', field: 'actualDispatch', minWidth: 100, align: 'right' }
    ]
  },
  
  // 地罐实收 - 两行表头
  { 
    title: '地罐实收', 
    children: [
      { title: 'C', field: 'tankReceived', minWidth: 100, align: 'right' }
    ]
  },
  
  // 油机销量 - 两行表头
  { 
    title: '油机销量', 
    children: [
      { title: 'D', field: 'pumpSales', minWidth: 100, align: 'right' }
    ]
  },
  
  // 回罐 - 两行表头
  { 
    title: '回罐', 
    children: [
      { title: 'E', field: 'returnTank', minWidth: 100, align: 'right' }
    ]
  },
  
  // 系统销量 - 两行表头
  { 
    title: '系统销量', 
    children: [
      { title: 'F', field: 'systemSales', minWidth: 100, align: 'right' }
    ]
  },
  
  // 实际销量 - 两行表头
  { 
    title: '实际销量', 
    children: [
      { title: 'G', field: 'actualSales', minWidth: 100, align: 'right' }
    ]
  },
  
  // 发油损溢 - 两行表头
  { 
    title: '发油损溢', 
    children: [
      { title: 'H=B-A', field: 'dispatchLoss', minWidth: 100, align: 'right' }
    ]
  },
  
  // 发油损溢率‰ - 两行表头
  { 
    title: '发油损溢率‰', 
    children: [
      { title: 'I=H/A*1000', field: 'dispatchLossRate', minWidth: 120, align: 'right' }
    ]
  },
  
  // 运输损溢 - 两行表头
  { 
    title: '运输损溢', 
    children: [
      { title: 'J=C-B', field: 'transportLoss', minWidth: 100, align: 'right' }
    ]
  },
  
  // 运输损溢率‰ - 两行表头
  { 
    title: '运输损溢率‰', 
    children: [
      { title: 'K=J/B*1000', field: 'transportLossRate', minWidth: 120, align: 'right' }
    ]
  },
  
  // 油站损溢 - 两行表头
  { 
    title: '油站损溢', 
    children: [
      { title: 'L', field: 'stationLoss', minWidth: 100, align: 'right' }
    ]
  },
  
  // 油站损溢率‰ - 两行表头
  { 
    title: '油站损溢率‰', 
    children: [
      { title: 'M=L/G*1000', field: 'stationLossRate', minWidth: 120, align: 'right' }
    ]
  },
  
  // 单日总损溢 - 两行表头
  { 
    title: '单日总损溢', 
    children: [
      { title: 'N=H+J+L', field: 'dailyTotalLoss', minWidth: 100, align: 'right' }
    ]
  },
  
  // 单日总损溢率‰ - 两行表头
  { 
    title: '单日总损溢率‰', 
    children: [
      { title: 'O=N/G*1000', field: 'dailyTotalLossRate', minWidth: 120, align: 'right' }
    ]
  },
  
  // 累计总损溢 - 两行表头
  { 
    title: '累计总损溢', 
    children: [
      { title: 'P', field: 'cumulativeTotalLoss', minWidth: 100, align: 'right' }
    ]
  },
  
  // 累计总销售 - 两行表头
  { 
    title: '累计总销售', 
    children: [
      { title: 'Q', field: 'cumulativeTotalSales', minWidth: 100, align: 'right' }
    ]
  },
  
  // 累计总损溢率‰ - 两行表头
  { 
    title: '累计总损溢率‰', 
    children: [
      { title: 'R=P/Q*1000', field: 'cumulativeTotalLossRate', minWidth: 120, align: 'right' }
    ]
  },
  
  // 损溢异常 - 单行表头
  { title: '损溢异常', field: 'isLossAbnormal', minWidth: 100, align: 'center', }
];

// 油品损溢统计表格列配置
export const oilLossStatisticsColumns: VxeGridProps['columns'] = [
  { title: '油站', field: 'station', minWidth: 120, align: 'center' },
  { title: '油品', field: 'oilType', minWidth: 80, align: 'center' },
  { title: '发油损溢（L）', field: 'dispatchLoss', minWidth: 120, align: 'right' },
  { title: '运输损溢（L）', field: 'transportLoss', minWidth: 120, align: 'right' },
  { title: '油站损溢（L）', field: 'stationLoss', minWidth: 120, align: 'right' },
  { title: '总损溢（L）', field: 'totalLoss', minWidth: 120, align: 'right' },
  { title: '实际销量（L）', field: 'actualSales', minWidth: 120, align: 'right' },
  { title: '发油损溢率（‰）', field: 'dispatchLossRate', minWidth: 120, align: 'right' },
  { title: '运输损溢率（‰）', field: 'transportLossRate', minWidth: 120, align: 'right' },
  { title: '油站损溢率（‰）', field: 'stationLossRate', minWidth: 120, align: 'right' },
  { title: '总损溢率（‰）', field: 'totalLossRate', minWidth: 120, align: 'right' },
  { title: '损溢异常调查启动数', field: 'abnormalInvestigationCount', minWidth: 140, align: 'center' },
  { title: '已完成的调查数量', field: 'completedInvestigationCount', minWidth: 140, align: 'center' }
];

// 异常调查统计表格列配置
export const abnormalInvestigationColumns: VxeGridProps['columns'] = [
  { title: '调查结果', field: 'investigationResult', minWidth: 200, align: 'center' },
  { title: '数量', field: 'count', minWidth: 100, align: 'center' },
  { title: '占比', field: 'percentage', minWidth: 100, align: 'center' }
];

// 油品损溢明细搜索表单配置
export const oilLossDetailQueryFormSchema: FormSchemaGetter = () => [
  { 
    component: 'Select',
    fieldName: 'station',
    label: '油站',
    componentProps: {
      placeholder: '请选择油站',
      options: stationOptions
    },
  },
  { 
    component: 'DatePicker',
    fieldName: 'timeRange',
    label: '时间',
    componentProps: {
      placeholder: '请选择时间范围',
      type: 'daterange',
      rangeSeparator: '至',
      startPlaceholder: '开始时间',
      endPlaceholder: '结束时间',
      valueFormat: 'YYYY-MM-DD HH:mm:ss'
    },
  },
  { 
    component: 'Input',
    fieldName: 'oilType',
    label: '油品',
    componentProps: {
      placeholder: '请输入油品号'
    },
  },
  { 
    component: 'Select',
    fieldName: 'isLossAbnormal',
    label: '损溢异常',
    componentProps: {
      placeholder: '请选择损溢异常',
      options: lossAbnormalOptions
    },
  },
];

// 油品损溢统计搜索表单配置
export const oilLossStatisticsQueryFormSchema: FormSchemaGetter = () => [
  { 
    component: 'Select',
    fieldName: 'station',
    label: '油站',
    componentProps: {
      placeholder: '请选择油站',
      options: stationOptions
    },
  },
  { 
    component: 'DatePicker',
    fieldName: 'timeRange',
    label: '时间',
    componentProps: {
      placeholder: '请选择时间范围',
      type: 'daterange',
      rangeSeparator: '至',
      startPlaceholder: '开始时间',
      endPlaceholder: '结束时间',
      valueFormat: 'YYYY-MM-DD HH:mm:ss'
    },
  },
];

// 异常调查统计搜索表单配置
export const abnormalInvestigationQueryFormSchema: FormSchemaGetter = () => [
  { 
    component: 'Select',
    fieldName: 'station',
    label: '油站',
    componentProps: {
      placeholder: '请选择油站',
      options: stationOptions
    },
  },
  { 
    component: 'DatePicker',
    fieldName: 'timeRange',
    label: '时间',
    componentProps: {
      placeholder: '请选择时间范围',
      type: 'daterange',
      rangeSeparator: '至',
      startPlaceholder: '开始时间',
      endPlaceholder: '结束时间',
      valueFormat: 'YYYY-MM-DD HH:mm:ss'
    },
  },
];

// 生成随机数（保留两位小数）
const generateRandomNumber = (min: number, max: number): number => {
  return parseFloat((Math.random() * (max - min) + min).toFixed(2));
};

// 油品损溢明细模拟数据
export const OIL_LOSS_DETAIL_MOCK_DATA: OilLossDetailData[] = Array.from({ length: 20 }, (_, index) => {
  const id = index + 1;
  const randomStation = stationOptions[Math.floor(Math.random() * stationOptions.length)].value;
  const randomOilType = oilTypeOptions[Math.floor(Math.random() * oilTypeOptions.length)].value;
  
  // 生成基础数据
  const plannedDispatch = generateRandomNumber(1000, 5000);
  const actualDispatch = plannedDispatch + generateRandomNumber(-50, 50);
  const tankReceived = actualDispatch + generateRandomNumber(-30, 30);
  const pumpSales = generateRandomNumber(800, 4500);
  const returnTank = generateRandomNumber(0, 50);
  const systemSales = pumpSales + generateRandomNumber(-20, 20);
  const actualSales = systemSales - returnTank;
  
  // 计算损溢数据
  const dispatchLoss = parseFloat((actualDispatch - plannedDispatch).toFixed(2));
  const dispatchLossRate = parseFloat(((dispatchLoss / plannedDispatch) * 1000).toFixed(2));
  
  const transportLoss = parseFloat((tankReceived - actualDispatch).toFixed(2));
  const transportLossRate = parseFloat(((transportLoss / actualDispatch) * 1000).toFixed(2));
  
  const stationLoss = generateRandomNumber(-100, 100);
  const stationLossRate = parseFloat(((stationLoss / actualSales) * 1000).toFixed(2));
  
  const dailyTotalLoss = parseFloat((dispatchLoss + transportLoss + stationLoss).toFixed(2));
  const dailyTotalLossRate = parseFloat(((dailyTotalLoss / actualSales) * 1000).toFixed(2));
  
  const cumulativeTotalLoss = generateRandomNumber(-1000, 1000);
  const cumulativeTotalSales = generateRandomNumber(10000, 50000);
  const cumulativeTotalLossRate = parseFloat(((cumulativeTotalLoss / cumulativeTotalSales) * 1000).toFixed(2));
  
  // 判断是否异常
  let isLossAbnormal = '否';
  if (randomOilType === '0#') {
    isLossAbnormal = Math.abs(stationLossRate) > 5 ? '是' : '否';
  } else {
    isLossAbnormal = Math.abs(stationLossRate) > 3 ? '是' : '否';
  }
  
  // 生成随机时间
  const now = new Date();
  const time = generateRandomDate(new Date(now.getFullYear(), now.getMonth() - 1, now.getDate()), now);
  
  return {
    id,
    station: randomStation,
    time,
    oilType: randomOilType,
    plannedDispatch,
    actualDispatch,
    tankReceived,
    pumpSales,
    returnTank,
    systemSales,
    actualSales,
    dispatchLoss,
    dispatchLossRate,
    transportLoss,
    transportLossRate,
    stationLoss,
    stationLossRate,
    dailyTotalLoss,
    dailyTotalLossRate,
    cumulativeTotalLoss,
    cumulativeTotalSales,
    cumulativeTotalLossRate,
    isLossAbnormal
  };
});

// 油品损溢统计模拟数据
export const OIL_LOSS_STATISTICS_MOCK_DATA: OilLossStatisticsData[] = Array.from({ length: 20 }, (_, index) => {
  const id = index + 1;
  const randomStation = stationOptions[Math.floor(Math.random() * stationOptions.length)].value;
  const randomOilType = oilTypeOptions[Math.floor(Math.random() * oilTypeOptions.length)].value;
  
  // 生成统计数据
  const dispatchLoss = generateRandomNumber(-500, 500);
  const transportLoss = generateRandomNumber(-300, 300);
  const stationLoss = generateRandomNumber(-1000, 1000);
  const totalLoss = parseFloat((dispatchLoss + transportLoss + stationLoss).toFixed(2));
  const actualSales = generateRandomNumber(10000, 50000);
  
  // 计算损溢率
  const dispatchLossRate = parseFloat(((dispatchLoss / actualSales) * 1000).toFixed(2));
  const transportLossRate = parseFloat(((transportLoss / actualSales) * 1000).toFixed(2));
  const stationLossRate = parseFloat(((stationLoss / actualSales) * 1000).toFixed(2));
  const totalLossRate = parseFloat(((totalLoss / actualSales) * 1000).toFixed(2));
  
  // 调查数量
  const abnormalInvestigationCount = Math.floor(Math.random() * 10);
  const completedInvestigationCount = Math.floor(Math.random() * (abnormalInvestigationCount + 1));
  
  return {
    id,
    station: randomStation,
    oilType: randomOilType,
    dispatchLoss,
    transportLoss,
    stationLoss,
    totalLoss,
    actualSales,
    dispatchLossRate,
    transportLossRate,
    stationLossRate,
    totalLossRate,
    abnormalInvestigationCount,
    completedInvestigationCount
  };
});

// 异常调查统计模拟数据
export const ABNORMAL_INVESTIGATION_MOCK_DATA: AbnormalInvestigationData[] = [
  { id: 1, investigationResult: '设备故障', count: 15, percentage: '30%' },
  { id: 2, investigationResult: '人为操作失误', count: 10, percentage: '20%' },
  { id: 3, investigationResult: '环境因素影响', count: 8, percentage: '16%' },
  { id: 4, investigationResult: '数据录入错误', count: 7, percentage: '14%' },
  { id: 5, investigationResult: '其他原因', count: 10, percentage: '20%' }
];

