// 农资物料数据存储管理
import { Material } from '../types/machinery';

const MATERIAL_KEY = 'smart_agriculture_materials';

export const materialStorage = {
  // 获取所有农资物料
  getAllMaterials(): Material[] {
    const data = localStorage.getItem(MATERIAL_KEY);
    return data ? JSON.parse(data) : [];
  },

  // 获取单个农资物料
  getMaterial(id: string): Material | undefined {
    const all = this.getAllMaterials();
    return all.find(m => m.id === id);
  },

  // 保存农资物料
  saveMaterial(material: Material): void {
    const all = this.getAllMaterials();
    const index = all.findIndex(m => m.id === material.id);
    if (index >= 0) {
      all[index] = material;
    } else {
      all.push(material);
    }
    localStorage.setItem(MATERIAL_KEY, JSON.stringify(all));
  },

  // 删除农资物料
  deleteMaterial(id: string): void {
    const all = this.getAllMaterials();
    const filtered = all.filter(m => m.id !== id);
    localStorage.setItem(MATERIAL_KEY, JSON.stringify(filtered));
  },

  // 批量保存农资物料
  saveMaterials(materials: Material[]): void {
    localStorage.setItem(MATERIAL_KEY, JSON.stringify(materials));
  },

  // 根据类型获取农资
  getMaterialsByType(type: string): Material[] {
    const all = this.getAllMaterials();
    return all.filter(m => m.type === type);
  },

  // 获取有库存的农资（用于维护记录选择）
  getAvailableMaterials(): Material[] {
    const all = this.getAllMaterials();
    return all.filter(m => m.currentStock > 0 && m.status !== '已过期');
  }
};

// 初始化示例农资数据
export function initializeMaterialData() {
  const existing = materialStorage.getAllMaterials();
  if (existing.length > 0) {
    return; // 已有数据，不重复初始化
  }

  const mockMaterials: Material[] = [
    {
      id: 'material-1',
      code: 'PJ-001',
      name: '机油滤芯',
      type: '配件',
      spec: '10W-40',
      model: 'JD-001',
      supplier: '约翰迪尔配件中心',
      currentStock: 50,
      unit: '个',
      purchasePrice: 85,
      status: '正常',
    },
    {
      id: 'material-2',
      code: 'PJ-002',
      name: '空气滤芯',
      type: '配件',
      spec: '标准型',
      model: 'AF-2024',
      supplier: '农机配件商行',
      currentStock: 30,
      unit: '个',
      purchasePrice: 65,
      status: '正常',
    },
    {
      id: 'material-3',
      code: 'HC-001',
      name: '液压油',
      type: '耗材',
      spec: '46号',
      model: 'HM46',
      supplier: '中石化润滑油',
      currentStock: 200,
      unit: '升',
      purchasePrice: 28,
      status: '正常',
    },
    {
      id: 'material-4',
      code: 'HC-002',
      name: '齿轮油',
      type: '耗材',
      spec: '85W-90',
      model: 'GL-5',
      supplier: '壳牌石油',
      currentStock: 150,
      unit: '升',
      purchasePrice: 45,
      status: '正常',
    },
    {
      id: 'material-5',
      code: 'PJ-003',
      name: '燃油滤芯',
      type: '配件',
      spec: '柴油专用',
      model: 'FS-1000',
      supplier: '康明斯配件',
      currentStock: 25,
      unit: '个',
      purchasePrice: 95,
      status: '正常',
    },
    {
      id: 'material-6',
      code: 'HC-003',
      name: '防冻液',
      type: '耗材',
      spec: '-35℃',
      model: 'AF-35',
      supplier: '汽车用品店',
      currentStock: 80,
      unit: '升',
      purchasePrice: 18,
      status: '正常',
    },
    {
      id: 'material-7',
      code: 'PJ-004',
      name: '传动皮带',
      type: '配件',
      spec: 'A型',
      model: 'A-1200',
      supplier: '三星皮带',
      currentStock: 15,
      unit: '条',
      purchasePrice: 120,
      status: '正常',
    },
    {
      id: 'material-8',
      code: 'HC-004',
      name: '黄油（润滑脂）',
      type: '耗材',
      spec: '锂基脂',
      model: 'MP-3',
      supplier: '长城润滑油',
      currentStock: 60,
      unit: '公斤',
      purchasePrice: 32,
      status: '正常',
    },
    {
      id: 'material-9',
      code: 'PJ-005',
      name: '刀片组',
      type: '配件',
      spec: '收割机专用',
      model: 'KB-688',
      supplier: '久保田配件',
      currentStock: 8,
      unit: '套',
      purchasePrice: 580,
      status: '库存预警',
    },
    {
      id: 'material-10',
      code: 'HC-005',
      name: '清洗剂',
      type: '耗材',
      spec: '工业级',
      model: 'CL-100',
      supplier: '化工材料店',
      currentStock: 40,
      unit: '升',
      purchasePrice: 22,
      status: '正常',
    },
  ];

  materialStorage.saveMaterials(mockMaterials);
}
