import 'package:flutter_riverpod/flutter_riverpod.dart';

import 'overtime_apply_api.dart';
import 'overtime_apply_model.dart';

final overtimeApplyStatusFilterProvider = StateProvider<String>((ref) => '');
final overtimeApplyPageProvider = StateProvider<int>((ref) => 1);
final overtimeApplyDateStartProvider = StateProvider<DateTime?>((ref) => null);
final overtimeApplyDateEndProvider = StateProvider<DateTime?>((ref) => null);
final overtimeApplyKeywordProvider = StateProvider<String>((ref) => '');
final overtimeApplyRefreshProvider = StateProvider<int>((ref) => 0);
final overtimeApplySortDirProvider = StateProvider<String>((ref) => 'DESC');

final now = DateTime.now();
final mockOvertimes = <OvertimeApplyModel>[
  OvertimeApplyModel(
    jbNo: 'JB20260720001',
    jbDd: DateTime(2026, 7, 20),
    salNo: 'U001',
    salName: '张三',
    dep: 'D001',
    depName: '销售部',
    reason: '项目上线前紧急测试与数据校验',
    usr: 'U001',
    rem: '测试备注',
    isAuditApproved: false,
    recordDd: DateTime(2026, 7, 20),
    details: [
      OvertimeApplyDetailModel(
        itm: 1,
        salNo: 'U001',
        salName: '张三',
        dep: 'D001',
        jbType: 'WORKING_DAY',
        jbDate: DateTime(2026, 7, 20),
        startTime: DateTime(2026, 7, 20, 18, 0),
        endTime: DateTime(2026, 7, 20, 20, 30),
        jbHours: 2.5,
        jbDays: 0.5,
        attPeriod: '2026-07',
        reason: '测试加班',
        compensationType: 'OVERTIME_PAY',
        compensationCount: 2.5,
        adr: '公司办公室',
      ),
    ],
  ),
  OvertimeApplyModel(
    jbNo: 'JB20260718002',
    jbDd: DateTime(2026, 7, 18),
    salNo: 'U002',
    salName: '李四',
    dep: 'D002',
    depName: '技术部',
    reason: '系统架构升级与数据库迁移',
    usr: 'U002',
    chkMan: 'MGR001',
    isAuditApproved: true,
    recordDd: DateTime(2026, 7, 18),
    clsDate: DateTime(2026, 7, 19),
    details: [
      OvertimeApplyDetailModel(
        itm: 1,
        salNo: 'U002',
        salName: '李四',
        dep: 'D002',
        jbType: 'REST_DAY',
        jbDate: DateTime(2026, 7, 18),
        startTime: DateTime(2026, 7, 18, 9, 0),
        endTime: DateTime(2026, 7, 18, 17, 30),
        jbHours: 8.0,
        jbDays: 1.0,
        attPeriod: '2026-07',
        reason: '架构升级',
        compensationType: 'COMPENSATORY_LEAVE',
        compensationCount: 1.0,
        adr: '公司办公室',
      ),
    ],
  ),
  OvertimeApplyModel(
    jbNo: 'JB20260725003',
    jbDd: DateTime(2026, 7, 25),
    salNo: 'U003',
    salName: '王五',
    dep: 'D003',
    depName: '财务部',
    reason: '季度财务报表汇总与审计准备',
    usr: 'U003',
    isAuditApproved: false,
    recordDd: DateTime(2026, 7, 25),
    details: [
      OvertimeApplyDetailModel(
        itm: 1,
        salNo: 'U003',
        salName: '王五',
        dep: 'D003',
        jbType: 'WORKING_DAY',
        jbDate: DateTime(2026, 7, 25),
        startTime: DateTime(2026, 7, 25, 18, 0),
        endTime: DateTime(2026, 7, 25, 21, 0),
        jbHours: 3.0,
        jbDays: 0.5,
        attPeriod: '2026-07',
        reason: '财务报表汇总',
        compensationType: 'OVERTIME_PAY',
        compensationCount: 3.0,
        adr: '公司办公室',
      ),
    ],
  ),
  OvertimeApplyModel(
    jbNo: 'JB20260714004',
    jbDd: DateTime(2026, 7, 14),
    salNo: 'U001',
    salName: '张三',
    dep: 'D001',
    depName: '销售部',
    reason: '节假日客户活动筹备与现场支持',
    usr: 'U001',
    rem: '测试备注',
    isAuditApproved: false,
    recordDd: DateTime(2026, 7, 14),
    details: [
      OvertimeApplyDetailModel(
        itm: 1,
        salNo: 'U001',
        salName: '张三',
        dep: 'D001',
        jbType: 'PUBLIC_HOLIDAY',
        jbDate: DateTime(2026, 7, 14),
        startTime: DateTime(2026, 7, 14, 8, 0),
        endTime: DateTime(2026, 7, 14, 12, 0),
        jbHours: 4.0,
        jbDays: 0.5,
        attPeriod: '2026-07',
        reason: '节假日活动筹备',
        compensationType: 'COMPENSATORY_LEAVE',
        compensationCount: 0.5,
        adr: '公司办公室',
      ),
    ],
  ),
  OvertimeApplyModel(
    jbNo: 'JB20260722005',
    jbDd: DateTime(2026, 7, 22),
    salNo: 'U002',
    salName: '李四',
    dep: 'D002',
    depName: '技术部',
    reason: '夜间服务器例行维护与安全加固',
    usr: 'U002',
    isAuditApproved: false,
    recordDd: DateTime(2026, 7, 22),
    details: [
      OvertimeApplyDetailModel(
        itm: 1,
        salNo: 'U002',
        salName: '李四',
        dep: 'D002',
        jbType: 'WORKING_DAY',
        jbDate: DateTime(2026, 7, 22),
        startTime: DateTime(2026, 7, 22, 22, 0),
        endTime: DateTime(2026, 7, 23, 1, 0),
        jbHours: 3.0,
        jbDays: 0.5,
        attPeriod: '2026-07',
        reason: '服务器维护',
        compensationType: 'OVERTIME_PAY',
        compensationCount: 3.0,
        adr: '公司办公室',
      ),
    ],
  ),
  OvertimeApplyModel(
    jbNo: 'JB20260713006',
    jbDd: DateTime(2026, 7, 13),
    salNo: 'U001',
    salName: '张三',
    dep: 'D001',
    depName: '销售部',
    reason: '周末展会布展与客户接待',
    usr: 'U001',
    rem: '测试备注',
    chkMan: 'MGR001',
    isAuditApproved: true,
    recordDd: DateTime(2026, 7, 13),
    clsDate: DateTime(2026, 7, 14),
    details: [
      OvertimeApplyDetailModel(
        itm: 1,
        salNo: 'U001',
        salName: '张三',
        dep: 'D001',
        jbType: 'REST_DAY',
        jbDate: DateTime(2026, 7, 13),
        startTime: DateTime(2026, 7, 13, 8, 0),
        endTime: DateTime(2026, 7, 13, 17, 0),
        jbHours: 8.0,
        jbDays: 1.0,
        attPeriod: '2026-07',
        reason: '展会布展',
        compensationType: 'COMPENSATORY_LEAVE',
        compensationCount: 1.0,
        adr: '公司办公室',
      ),
    ],
  ),
];

final overtimeApplyListProvider =
    FutureProvider.family<List<OvertimeApplyModel>, String>((
      ref,
      status,
    ) async {
      ref.watch(overtimeApplyPageProvider);
      ref.watch(overtimeApplyRefreshProvider);
      ref.watch(overtimeApplyDateStartProvider);
      ref.watch(overtimeApplyDateEndProvider);
      ref.watch(overtimeApplyKeywordProvider);
      final api = ref.read(overtimeApplyApiProvider);
      final dateStart = ref.read(overtimeApplyDateStartProvider);
      final dateEnd = ref.read(overtimeApplyDateEndProvider);
      final keyword = ref.read(overtimeApplyKeywordProvider);
      final sortDir = ref.read(overtimeApplySortDirProvider);

      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;
    });
