/*
 * @Author: WuXiangNan
 * @Date: 2021-03-15 15:40:36
 * @LastEditTime : 2022-01-19 16:37:25
 * @LastEditors  : WuWei
 * @Description: 
 */

import 'package:attendance_package/src/public/location.dart';
import 'package:attendance_package/src/view_models/attendance_store.dart';
import 'package:attendance_package/src/view_models/time_store.dart';
import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

class ClockPageModel extends StatelessWidget {
  const ClockPageModel({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (_) => TimeStore(context),
      child: _PageView(),
    );
  }
}

class _PageView extends StatefulWidget {
  _PageView({Key? key}) : super(key: key);

  @override
  __PageViewState createState() => __PageViewState();
}

class __PageViewState extends State<_PageView> {
  TimeStore get store => Provider.of<TimeStore>(context, listen: false);

  @override
  void initState() {
    store.getDayTime();
    super.initState();
  }

  @override
  void deactivate() {
    store.destory();
    super.deactivate();
  }

  @override
  Widget build(BuildContext context) {
    final colorScheme = WTheme.of(context).colorScheme;
    final timeStore = Provider.of<TimeStore>(context);
    final attendance = Provider.of<AttendanceStore>(context);
    final store = Provider.of<LocationStore>(context);

    bool isNormal = attendance.clockStatusData!.clockStatusCode != 'full' &&
        attendance.clockStatusData?.clockStatusCode != 'noWorkTime';

    return Wisdom.column(
      key: attendance.clockInKey,
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        WLoadingWrapper(
          action: () => attendance.clockIn(),
          color: Colors.white,
          child: Wisdom.column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              WisText(
                // attendance.clockStatusData.clockStatusCode != 'full'
                //     ? attendance?.clockStatusData?.clockStatusName ?? ''
                //     : '免考勤无需打卡',
                attendance.status ??
                    (attendance.clockStatusData!.clockStatusCode != 'full'
                        ? attendance.clockStatusData?.clockStatusName ?? ''
                        : '免考勤无需打卡'),
                textAlign: TextAlign.center,
                color: Colors.white,
                size: 18.pt,
              ),
              WisText(
                timeStore.time,
                color:
                    attendance.clockStatusData?.clockStatusCode != 'noWorkTime'
                        ? attendance.clockRangeStatus == 'normal' &&
                                attendance.clockStatusData!.clockStatusCode !=
                                    'late' &&
                                attendance.clockStatusData!.clockStatusCode !=
                                    'earlyLeave'
                            ? Color(0xFFBEDEFF)
                            : Color(0xFFFFE3CA)
                        : Color(0xFFDFE3E7),
                size: 13.pt,
              )
            ],
          ),
          builder: (_, method, child) {
            return Wisdom(
              onTap: method,
              height: 125.pt,
              width: 125.pt,
              alignment: Alignment.center,
              borderRadius: BorderRadius.circular(74),
              gradient: LinearGradient(
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter,
                colors: <Color>[
                  isNormal
                      ? attendance.clockRangeStatus == 'normal' &&
                              attendance.clockStatusData!.clockStatusCode !=
                                  'late' &&
                              attendance.clockStatusData!.clockStatusCode !=
                                  'earlyLeave'
                          ? Color(0xFF52BBFF)
                          : Color(0xFFFFCC52)
                      : Color(0xFFD1D4D8),
                  isNormal
                      ? attendance.clockRangeStatus == 'normal' &&
                              attendance.clockStatusData!.clockStatusCode !=
                                  'late' &&
                              attendance.clockStatusData!.clockStatusCode !=
                                  'earlyLeave'
                          ? Color(0xFF2589F3)
                          : Color(0xFFFD882A)
                      : Color(0xFF9FA1A4),
                ],
              ),
              child: child,
            );
          },
        ),
        if (attendance.clockStatusData?.workDay == false)
          Wisdom.row(
            margin: EdgeInsets.only(top: 30.pt),
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              WisText(
                '当日未排班',
                color: colorScheme.minor,
              )
            ],
          ),
        if (attendance.clockStatusData?.workDay == true)
          Wisdom.row(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.start,
            margin: EdgeInsets.only(top: 30.pt),
            children: [
              if (store.addr != null && attendance.clockRangeStatus == 'normal')
                Wisdom.row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Icon(
                      Icons.check_circle,
                      color: colorScheme.success,
                      size: 15.pt,
                    ),
                    WisText(
                      ' 已进入考勤范围   ',
                      color: colorScheme.minor,
                      size: 13.pt,
                    ),
                  ],
                )
              else if (attendance.clockRangeStatus == 'legwork')
                WTag(
                  child: Text('外勤'),
                  color: Colors.orange,
                ),
              // Wisdom.row(
              //     mainAxisAlignment: MainAxisAlignment.center,
              //     crossAxisAlignment: CrossAxisAlignment.start,
              //     children: [
              //       Icon(
              //         Icons.location_on,
              //         color: colorScheme.primary,
              //         size: 15.pt,
              //       ),
              //       Wisdom(
              //         maxWidth: 160.pt,
              //         child: WisText(
              //           attendance.address ?? store.addr ?? '未知地址',
              //           bold: true,
              //           size: 13.pt,
              //           maxLines: 2,
              //           textAlign: TextAlign.center,
              //           overflow: TextOverflow.ellipsis,
              //         ),
              //       )
              //     ]),
              // Wisdom(
              //   onTap: () => store.reStartLocation(),
              //   child: WisText(
              //     ' 重新定位',
              //     color: colorScheme.primary,
              //     size: 13.pt,
              //   ),
              // )
            ],
          )
      ],
    );
  }
}
