import 'package:flutter_riverpod/flutter_riverpod.dart';

import 'vehicle_apply_api.dart';
import 'vehicle_apply_model.dart';

final vehicleApplyStatusFilterProvider = StateProvider<String>((ref) => '');
final vehicleApplyPageProvider = StateProvider<int>((ref) => 1);
final vehicleApplyDateStartProvider = StateProvider<DateTime?>((ref) => null);
final vehicleApplyDateEndProvider = StateProvider<DateTime?>((ref) => null);
final vehicleApplyKeywordProvider = StateProvider<String>((ref) => '');
final vehicleApplyRefreshProvider = StateProvider<int>((ref) => 0);
final vehicleApplySortDirProvider = StateProvider<String>((ref) => 'DESC');

final now = DateTime.now();

final mockVehicles = <VehicleApplyModel>[
  VehicleApplyModel(
    ycNo: 'YC20260520001',
    ycDd: DateTime(2026, 5, 20),
    salNo: 'U001',
    dep: 'D001',
    reason: '拜访重要客户，需前往深圳分公司洽谈合作',
    originAdr: '公司总部',
    destAdr: '深圳分公司',
    passengerCount: 3,
    passengerName: '王五,赵六',
    licensePlate: '京A88888',
    vehicleType: 'sedan',
    brand: '丰田凯美瑞',
    seats: 5,
    odometerBegin: 15000,
    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,
  ),
  VehicleApplyModel(
    ycNo: 'YC20260601002',
    ycDd: DateTime(2026, 6, 1),
    salNo: 'U002',
    dep: 'D002',
    reason: '运送服务器设备至数据中心',
    originAdr: '公司仓库',
    destAdr: '数据中心',
    passengerCount: 2,
    passengerName: '钱七',
    licensePlate: '京B66666',
    vehicleType: 'van',
    brand: '福特全顺',
    seats: 7,
    odometerBegin: 32000,
    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,
  ),
  VehicleApplyModel(
    ycNo: 'YC20260615003',
    ycDd: DateTime(2026, 6, 15),
    salNo: 'U003',
    dep: 'D003',
    reason: '前往税务局办理税务登记',
    originAdr: '公司总部',
    destAdr: '税务局',
    passengerCount: 1,
    passengerName: '',
    licensePlate: '京C12345',
    vehicleType: 'sedan',
    brand: '大众帕萨特',
    seats: 5,
    odometerBegin: 25650,
    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),
    odometerEnd: 25680,
    amtn: 35.0,
    remark: '停车费35元',
    usrCheck: 'USR003',
    checkDd: DateTime(2026, 6, 16, 12, 0),
    applicantName: '赵六',
    deptName: '财务部',
    isAuditApproved: true,
  ),
  VehicleApplyModel(
    ycNo: 'YC20260701004',
    ycDd: DateTime(2026, 7, 1),
    salNo: 'U001',
    dep: 'D001',
    reason: '接待审计团队，需前往机场接机',
    originAdr: '公司总部',
    destAdr: '机场T3航站楼',
    passengerCount: 4,
    passengerName: '孙八,周九,吴十',
    licensePlate: '京D99999',
    vehicleType: 'mpv',
    brand: '别克GL8',
    seats: 7,
    odometerBegin: 8900,
    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,
  ),
  VehicleApplyModel(
    ycNo: 'YC20260710005',
    ycDd: DateTime(2026, 7, 10),
    salNo: 'U004',
    dep: 'D004',
    reason: '参加行业技术交流大会',
    originAdr: '公司总部',
    destAdr: '国际会展中心',
    passengerCount: 2,
    passengerName: '郑十一',
    licensePlate: '京E55555',
    vehicleType: 'suv',
    brand: '本田CR-V',
    seats: 5,
    odometerBegin: 12200,
    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),
    odometerEnd: 12350,
    amtn: 120.0,
    remark: '路桥费60元，停车费60元',
    usrCheck: 'USR004',
    checkDd: DateTime(2026, 7, 12, 17, 30),
    applicantName: '周七',
    deptName: '研发部',
    isAuditApproved: true,
  ),
  VehicleApplyModel(
    ycNo: 'YC20260715006',
    ycDd: DateTime(2026, 7, 15),
    salNo: 'U002',
    dep: 'D002',
    reason: '运送活动物资至年会会场',
    originAdr: '公司仓库',
    destAdr: '年会会场',
    passengerCount: 3,
    passengerName: '王十二,冯十三',
    licensePlate: '京B66666',
    vehicleType: 'van',
    brand: '福特全顺',
    seats: 7,
    odometerBegin: 35000,
    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 vehicleApplyListProvider =
    FutureProvider.family<List<VehicleApplyModel>, String>((ref, status) async {
      ref.watch(vehicleApplyPageProvider);
      ref.watch(vehicleApplyRefreshProvider);
      ref.watch(vehicleApplyDateStartProvider);
      ref.watch(vehicleApplyDateEndProvider);
      ref.watch(vehicleApplyKeywordProvider);
      ref.watch(vehicleApplySortDirProvider);
      final api = ref.read(vehicleApplyApiProvider);
      final dateStart = ref.read(vehicleApplyDateStartProvider);
      final dateEnd = ref.read(vehicleApplyDateEndProvider);
      final keyword = ref.read(vehicleApplyKeywordProvider);
      final sortDir = ref.read(vehicleApplySortDirProvider);

      final result = await api.fetchList(
        status: status,
        keyword: keyword,
        startDate: dateStart != null
            ? '${dateStart.year}-${dateStart.month.toString().padLeft(2, '0')}-${dateStart.day.toString().padLeft(2, '0')}'
            : '',
        endDate: dateEnd != null
            ? '${dateEnd.year}-${dateEnd.month.toString().padLeft(2, '0')}-${dateEnd.day.toString().padLeft(2, '0')}'
            : '',
        sortDir: sortDir,
      );

      return result.list;
    });
