/*
 * @Author: WuXiangNan
 * @Date: 2021-02-26 15:53:50
 * @LastEditTime : 2023-05-15 17:29:16
 * @LastEditors  : WuWei
 * @Description: 考勤规则
 */

import 'package:attendance_package/src/models/response_model/attendance_rule/rule_time_model.dart';
import 'package:attendance_package/src/view_models/attendance_rule_store.dart';
import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';
import '../../view_models/attendance_store.dart';
import '../../assets.gen.dart';
import '../../components/calendar.dart';
import '../../models/enum.dart';

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

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(
          create: (_) => AttendanceStore(context),
          lazy: false,
        ),
        ChangeNotifierProvider(
          create: (_) => RuleStore(context),
          lazy: false,
        ),
      ],
      child: _PageView(),
    );
  }
}

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

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

class __PageViewState extends State<_PageView> {
  RuleStore get store => Provider.of<RuleStore>(context, listen: false);
  @override
  void initState() {
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      if (store.args['groupId'] != null)
        store.onGetAttendanceRule(store.args['groupId']);
    });

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final attendanceStore = Provider.of<AttendanceStore>(context);
    final store = Provider.of<RuleStore>(context);
    final colorScheme = WTheme.of(context).colorScheme;

    return Scaffold(
      appBar: WAppBar(
        brightness: Brightness.light,
        title: WisText('考勤统计',
            size: 19.pt, weight: FontWeight.w600, color: colorScheme.mainTitle),
        iconTheme: IconThemeData(
          color: colorScheme.mainTitle,
          size: 19.pt,
        ),
        backgroundColor: Colors.white,
      ),
      body: Container(
        child: Wisdom.column(
          children: [
            WisCard(
              margin: EdgeInsets.only(
                top: 10.pt,
                left: 15.pt,
                right: 15.pt,
              ),
              child: Wisdom.row(
                padding: EdgeInsets.fromLTRB(15.pt, 20.pt, 15.pt, 20.pt),
                color: Colors.white,
                children: [
                  // WisImage.network(
                  //   'https://wy-test.huiguanjia.cn/9ad134a361f8d778/file/images/378945170022985728',
                  //   width: 42.pt,
                  //   height: 42.pt,
                  //   fit: BoxFit.fill,
                  //   borderRadius: BorderRadius.circular(100.pt),
                  // ),
                  Image(
                    image: AssetList.$rtx_png_image,
                    height: 42.pt,
                    width: 42.pt,
                    fit: BoxFit.fill,
                  ),
                  Wisdom.column(
                    children: [
                      Wisdom.row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        width: 287.pt,
                        margin: EdgeInsets.only(left: 15.pt, bottom: 5.pt),
                        children: [
                          WisText(
                            attendanceStore.userInfo!.name!,
                            bold: true,
                            size: 16.pt,
                          ),
                        ],
                      ),
                      Wisdom.row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        width: 287.pt,
                        margin: EdgeInsets.only(left: 15.pt),
                        children: [
                          WTag(
                            color: Color(0xFF25B41E),
                            child: Text(store.ruleData.shiftType ?? ''),
                          ),
                          WisText(
                            '考勤组：${store.ruleData.groupName ?? '无'}',
                            color: colorScheme.minor,
                            size: 14.pt,
                          )
                        ],
                      ),
                    ],
                  ).asFlexible(flex: 1),
                ],
              ),
            ),
            ListView(
              children: [
                if (store.ruleData.shiftType != null &&
                    store.ruleData.shiftType == '排班制')
                  WisCard(
                    margin: EdgeInsets.only(
                      top: 10.pt,
                      left: 15.pt,
                      right: 15.pt,
                    ),
                    child: Wisdom.column(
                      margin: EdgeInsets.only(top: 10.pt),
                      padding: EdgeInsets.only(right: 15.pt),
                      color: Colors.white,
                      children: [
                        Wisdom.row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            buildTitle('考勤班次'),
                            Wisdom.row(
                              onTap: () {
                                WBottomModalUtil.show(
                                  context,
                                  title: Text('排班'),
                                  body: SingleChildScrollView(
                                    child: CalendarPage(),
                                  ),
                                  bodyConstraints: BoxConstraints(
                                      maxHeight: 500, minHeight: 250),
                                );
                              },
                              children: [
                                WisText(
                                  '查看班次',
                                  color: colorScheme.primary,
                                ),
                                Icon(
                                  Icons.chevron_right,
                                  color: colorScheme.primary,
                                )
                              ],
                            ),
                          ],
                        ),
                        for (RuleTimeModel item in store.ruleData.ruleTime!)
                          buildRuleTime(item),
                      ],
                    ),
                  ),
                if (store.ruleData.ruleTime != null &&
                    store.ruleData.shiftType != '排班制')
                  WisCard(
                    margin: EdgeInsets.only(
                      top: 10.pt,
                      left: 15.pt,
                      right: 15.pt,
                    ),
                    child: Wisdom.column(
                      margin: EdgeInsets.only(top: 10.pt),
                      color: Colors.white,
                      children: [
                        buildTitle('考勤时间'),
                        for (RuleTimeModel item in store.ruleData.ruleTime!)
                          buildRuleTime(item),
                      ],
                    ),
                  ),
                if (store.ruleData.rule != null &&
                    store.ruleData.shiftType != '自由工时')
                  WisCard(
                    margin: EdgeInsets.only(
                      top: 10.pt,
                      left: 15.pt,
                      right: 15.pt,
                    ),
                    child: Wisdom.column(
                      margin: EdgeInsets.only(top: 10.pt),
                      color: Colors.white,
                      children: [
                        buildTitle('考勤规则'),
                        for (RuleTimeModel item in store.ruleData.rule!)
                          buildRuleTime(item),
                      ],
                    ),
                  ),
                WisCard(
                  margin: EdgeInsets.only(
                    top: 10.pt,
                    left: 15.pt,
                    right: 15.pt,
                  ),
                  child: Wisdom.column(
                    margin: EdgeInsets.only(top: 10.pt),
                    color: Colors.white,
                    children: [
                      buildTitle('外勤规则'),
                      for (var i = 0; i < legWorkSettingText.length; i++)
                        buildOutsideRule(legWorkSettingText[i], i),
                    ],
                  ),
                ),
                WisCard(
                  margin: EdgeInsets.only(
                    top: 10.pt,
                    left: 15.pt,
                    right: 15.pt,
                  ),
                  child: Wisdom.column(
                    margin: EdgeInsets.only(top: 10.pt),
                    color: Colors.white,
                    children: [
                      buildTitle('考勤地点'),
                      for (var item in store.ruleData.location ?? [])
                        Wisdom(
                          alignment: Alignment.centerLeft,
                          margin: EdgeInsets.fromLTRB(15.pt, 0, 15.pt, 15.pt),
                          child: WisText(
                            ' $item;',
                            color: colorScheme.minor,
                          ),
                        )
                    ],
                  ),
                ),
              ],
            ).asExpanded(),
          ],
        ),
      ),
    );
  }

  Widget buildTitle(String title) {
    return Wisdom.row(
      padding: EdgeInsets.fromLTRB(15.pt, 20.pt, 15.pt, 20.pt),
      children: [
        Wisdom(
          color: Color(0XFF0F67F8),
          width: 3.pt,
          height: 15.pt,
          margin: EdgeInsets.only(right: 10.pt),
        ),
        WisText(
          title,
          size: 16.pt,
          bold: true,
        ),
      ],
    );
  }

  Widget buildOutsideRule(String rule, int index) {
    final store = Provider.of<RuleStore>(context);
    return Wisdom.row(
      margin: EdgeInsets.only(left: 15.pt, right: 15.pt, bottom: 10.pt),
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        WisText(
          rule,
          size: 14.pt,
          color: Color(0xFF999999),
        ),
        if (store.legWork?.indexOf(index.toString()) != -1)
          Image(
            image: AssetList.$dg_png_image,
            width: 17.pt,
          ),
      ],
    );
  }

  Widget buildRuleTime(RuleTimeModel content) {
    return Wisdom.column(
      margin: EdgeInsets.fromLTRB(15.pt, 0, 15.pt, 20.pt),
      children: [
        Wisdom.row(
          margin: EdgeInsets.only(
            bottom: 5.pt,
          ),
          children: [
            WisText(
              content.type ?? '',
              bold: true,
              size: 14.pt,
            ),
          ],
        ),
        for (var info in content.infos!)
          Wisdom.row(
            margin: EdgeInsets.only(bottom: 5.pt),
            children: [
              WisText(
                info,
                color: Colors.grey,
                maxLines: 2,
                size: 14.pt,
              ).asExpanded(),
            ],
          )
      ],
    );
  }
}
