import 'package:flutter_riverpod/flutter_riverpod.dart';

import 'vehicle_model.dart';

final vehicleStatusFilterProvider = StateProvider<String>((ref) => '');
final vehiclePageProvider = StateProvider<int>((ref) => 1);
final vehicleDateStartProvider = StateProvider<DateTime?>((ref) => null);
final vehicleDateEndProvider = StateProvider<DateTime?>((ref) => null);
final vehicleKeywordProvider = StateProvider<String>((ref) => '');
final vehicleRefreshProvider = StateProvider<int>((ref) => 0);
final vehicleSortDirProvider = StateProvider<String>((ref) => 'DESC');

final now = DateTime.now();

final mockVehicles = <VehicleModel>[
  VehicleModel(
    ycNo: 'YC20260520001',
    ycDd: DateTime(2026, 5, 20),
    salNo: 'U001',
    dep: 'D001',
    purpose: 'reception',
    reason: '拜访重要客户，需前往深圳分公司洽谈合作',
    origin: '公司总部',
    destinAdr: '深圳分公司',
    passengerCount: 3,
    passengerName: '王五,赵六',
    licensePlate: '京A88888',
    vehicleType: 'sedan',
    brand: '丰田凯美瑞',
    seats: 5,
    driverName: '张师傅',
    startTime: DateTime(2026, 5, 22, 8, 0),
    endTime: DateTime(2026, 5, 22, 18, 0),
    recordDd: DateTime(2026, 5, 20),
    usr: 'U001',
    chkMan: '',
    clsDate: null,
    isReturn: false,
    applicantName: '张三',
    deptName: '市场部',
    isAuditApproved: false,
  ),
  VehicleModel(
    ycNo: 'YC20260601002',
    ycDd: DateTime(2026, 6, 1),
    salNo: 'U002',
    dep: 'D002',
    purpose: 'business',
    reason: '运送服务器设备至数据中心',
    origin: '公司仓库',
    destinAdr: '数据中心',
    passengerCount: 2,
    passengerName: '钱七',
    licensePlate: '京B66666',
    vehicleType: 'van',
    brand: '福特全顺',
    seats: 7,
    driverName: '李师傅',
    startTime: DateTime(2026, 6, 2, 9, 0),
    endTime: DateTime(2026, 6, 2, 17, 0),
    recordDd: DateTime(2026, 6, 1),
    usr: 'U002',
    chkMan: 'MGR001',
    clsDate: DateTime(2026, 6, 2),
    isReturn: false,
    applicantName: '李四',
    deptName: '技术部',
    isAuditApproved: true,
  ),
  VehicleModel(
    ycNo: 'YC20260615003',
    ycDd: DateTime(2026, 6, 15),
    salNo: 'U003',
    dep: 'D003',
    purpose: 'official',
    reason: '前往税务局办理税务登记',
    origin: '公司总部',
    destinAdr: '税务局',
    passengerCount: 1,
    passengerName: '',
    licensePlate: '京C12345',
    vehicleType: 'sedan',
    brand: '大众帕萨特',
    seats: 5,
    driverName: '王师傅',
    startTime: DateTime(2026, 6, 16, 9, 0),
    endTime: DateTime(2026, 6, 16, 12, 0),
    recordDd: DateTime(2026, 6, 15),
    usr: 'U003',
    chkMan: 'MGR001',
    clsDate: DateTime(2026, 6, 16),
    isReturn: true,
    returnTime: DateTime(2026, 6, 16, 11, 45),
    odometer: 25680.0,
    amtn: 35.0,
    remark: '停车费35元',
    applicantName: '赵六',
    deptName: '财务部',
    isAuditApproved: true,
  ),
  VehicleModel(
    ycNo: 'YC20260701004',
    ycDd: DateTime(2026, 7, 1),
    salNo: 'U001',
    dep: 'D001',
    purpose: 'reception',
    reason: '接待审计团队，需前往机场接机',
    origin: '公司总部',
    destinAdr: '机场T3航站楼',
    passengerCount: 4,
    passengerName: '孙八,周九,吴十',
    licensePlate: '京D99999',
    vehicleType: 'mpv',
    brand: '别克GL8',
    seats: 7,
    driverName: '刘师傅',
    startTime: DateTime(2026, 7, 5, 14, 0),
    endTime: DateTime(2026, 7, 5, 17, 0),
    recordDd: DateTime(2026, 7, 1),
    usr: 'U001',
    chkMan: '',
    clsDate: null,
    isReturn: false,
    applicantName: '张三',
    deptName: '市场部',
    isAuditApproved: false,
  ),
  VehicleModel(
    ycNo: 'YC20260710005',
    ycDd: DateTime(2026, 7, 10),
    salNo: 'U004',
    dep: 'D004',
    purpose: 'business',
    reason: '参加行业技术交流大会',
    origin: '公司总部',
    destinAdr: '国际会展中心',
    passengerCount: 2,
    passengerName: '郑十一',
    licensePlate: '京E55555',
    vehicleType: 'suv',
    brand: '本田CR-V',
    seats: 5,
    driverName: '陈师傅',
    startTime: DateTime(2026, 7, 12, 8, 0),
    endTime: DateTime(2026, 7, 12, 18, 0),
    recordDd: DateTime(2026, 7, 10),
    usr: 'U004',
    chkMan: 'MGR001',
    clsDate: DateTime(2026, 7, 11),
    isReturn: true,
    returnTime: DateTime(2026, 7, 12, 17, 30),
    odometer: 12350.0,
    amtn: 120.0,
    remark: '路桥费60元，停车费60元',
    applicantName: '周七',
    deptName: '研发部',
    isAuditApproved: true,
  ),
  VehicleModel(
    ycNo: 'YC20260715006',
    ycDd: DateTime(2026, 7, 15),
    salNo: 'U002',
    dep: 'D002',
    purpose: 'other',
    reason: '运送活动物资至年会会场',
    origin: '公司仓库',
    destinAdr: '年会会场',
    passengerCount: 3,
    passengerName: '王十二,冯十三',
    licensePlate: '京B66666',
    vehicleType: 'van',
    brand: '福特全顺',
    seats: 7,
    driverName: '李师傅',
    startTime: DateTime(2026, 7, 18, 7, 0),
    endTime: DateTime(2026, 7, 18, 10, 0),
    recordDd: DateTime(2026, 7, 15),
    usr: 'U002',
    chkMan: '',
    clsDate: null,
    isReturn: false,
    applicantName: '李四',
    deptName: '技术部',
    isAuditApproved: false,
  ),
];

final vehicleListProvider = FutureProvider.family<List<VehicleModel>, String>(
  (ref, status) async {
    ref.watch(vehiclePageProvider);
    ref.watch(vehicleRefreshProvider);
    ref.watch(vehicleDateStartProvider);
    ref.watch(vehicleDateEndProvider);
    ref.watch(vehicleKeywordProvider);
    ref.watch(vehicleSortDirProvider);
    await Future.delayed(const Duration(milliseconds: 1000));

    var list = status.isEmpty
        ? mockVehicles
        : mockVehicles
            .where((e) {
              // 状态匹配：通过 clsDate 和 isReturn 判断
              if (status == 'approved') return e.isAuditApproved;
              if (status == 'pending') return !e.isAuditApproved;
              if (status == 'returned') return e.isReturn;
              return false;
            })
            .toList();

    final dateStart = ref.read(vehicleDateStartProvider);
    final dateEnd = ref.read(vehicleDateEndProvider);
    if (dateStart != null) {
      list = list
          .where((e) =>
              e.recordDd != null &&
              e.recordDd!.isAfter(dateStart.subtract(const Duration(days: 1))))
          .toList();
    }
    if (dateEnd != null) {
      list = list
          .where((e) =>
              e.recordDd != null &&
              e.recordDd!.isBefore(dateEnd.add(const Duration(days: 1))))
          .toList();
    }

    final keyword = ref.read(vehicleKeywordProvider);
    if (keyword.isNotEmpty) {
      final kw = keyword.toLowerCase();
      list = list.where((e) {
        return e.ycNo.toLowerCase().contains(kw) ||
            e.driverName.toLowerCase().contains(kw) ||
            e.licensePlate.toLowerCase().contains(kw) ||
            (e.applicantName).toLowerCase().contains(kw) ||
            e.reason.toLowerCase().contains(kw);
      }).toList();
    }

    final sortDir = ref.read(vehicleSortDirProvider);
    if (sortDir == 'ASC') {
      list.sort((a, b) => (a.recordDd ?? DateTime(2000)).compareTo(b.recordDd ?? DateTime(2000)));
    } else {
      list.sort((a, b) => (b.recordDd ?? DateTime(2000)).compareTo(a.recordDd ?? DateTime(2000)));
    }

    return list;
  },
);
