/*
 * @Author       : WuWei
 * @LastEditors: ChenYaJin
 * @Date         : 2021-04-27 16:39:43
 * @LastEditTime: 2023-08-09 15:54:20
 * @FilePath     : /wisdom-huiguanjia-app/task_package/lib/src/pages/statistic/components/department.dart
 * @Description  : 任务统计-部门统计
 */
part of '../index.dart';

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

  @override
  Widget build(BuildContext context) {
    final store = Provider.of<StatisticStore>(context, listen: true);
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;
    final size = MediaQuery.of(context).size;
    final mediaWidth = size.width;

    /// 计算字体长度
    calcTextSize(String? text, TextStyle style) {
      final TextPainter textPainter = TextPainter(
        text: TextSpan(text: text, style: style),
        textDirection: TextDirection.ltr,
        textScaleFactor: WidgetsBinding.instance.window.textScaleFactor,
      )..layout();
      return textPainter.size.width;
    }

    return WisScrollView(
      key: store.departScrViewKey,
      slivers: [
        SliverToBoxAdapter(
          child: Observer(builder: (_) {
            return Column(
              children: [
                if (store.isStatShow) ...[
                  BuildAllTaskCard(),
                  BuildTaskListStat(),
                ] else ...[
                  Wisdom.row(
                      color: Color(0xFFFFFFFF),
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      margin: EdgeInsets.only(bottom: 5.pt),
                      children: <Widget>[
                        Expanded(
                          child: WSearch(
                            hintText: "输入部门名称搜索",
                            onCancel: () => {store.onSearchDepart('')},
                            onSubmitted: (v) => {store.onSearchDepart(v)},
                            moonlight: false,
                            padding: EdgeInsets.symmetric(vertical: 10.pt),
                            isDark: true,
                          ),
                        )
                      ]),
                  WisCard(
                    margin:
                        EdgeInsets.only(left: 15.pt, right: 15.pt, top: 5.pt),
                    color: Colors.white,
                    child: Wisdom.column(
                      padding: EdgeInsets.all(15.pt),
                      color: Colors.white,
                      children: [
                        Row(
                          children: [
                            WisText.c222(
                              "请先选择管理部门",
                              weight: FontWeight.w600,
                              size: 15.pt,
                            ),
                          ],
                        ),
                        Observer(builder: (_) {
                          return Column(
                            children: [
                              if (store.departmentList.length > 0) ...[
                                for (int i = 0;
                                    i < store.departmentList.length;
                                    i++)
                                  Wisdom.row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceBetween,
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    margin: EdgeInsets.only(top: 10.pt),
                                    onTap: () => store.onSelectOrganItem(i),
                                    children: [
                                      Observer(builder: (_) {
                                        return Wisdom(
                                          margin: EdgeInsets.only(right: 25.pt),
                                          child: Image(
                                            image: store.departmentList[i]
                                                            .isSelect ==
                                                        null ||
                                                    store.departmentList[i]
                                                            .isSelect ==
                                                        false
                                                ? AssetList.$dxh_png_image
                                                : AssetList.$fxxz_png_image,
                                            width: 17.pt,
                                          ),
                                        );
                                      }),
                                      Column(
                                        crossAxisAlignment:
                                            CrossAxisAlignment.start,
                                        children: [
                                          Wisdom.row(
                                            margin:
                                                EdgeInsets.only(bottom: 10.pt),
                                            children: [
                                              WisText(
                                                '${store.departmentList[i].name ?? '--'}',
                                                size: 15.pt,
                                                weight: FontWeight.w600,
                                              ).asExpanded(),
                                            ],
                                          ),
                                          Wisdom.row(
                                            children: [
                                              /// a goddamn ellipsis on left side
                                              if (calcTextSize(
                                                    store.departmentList[i]
                                                        .parentName,
                                                    TextStyle(
                                                      fontSize: 14.pt,
                                                      color: colorScheme.minor,
                                                      fontWeight:
                                                          FontWeight.w300,
                                                    ),
                                                  ) >=
                                                  (mediaWidth - 72)) ...[
                                                WisText.c666(
                                                  "...${store.departmentList[i].parentName ?? '-'}",
                                                  weight: FontWeight.w300,
                                                  size: 14.pt,
                                                  maxLines: 1,
                                                  overflow:
                                                      TextOverflow.visible,
                                                ).asExpanded(),
                                              ] else ...[
                                                WisText.c666(
                                                  "${store.departmentList[i].parentName ?? '-'}",
                                                  weight: FontWeight.w300,
                                                  size: 14.pt,
                                                  maxLines: 1,
                                                  overflow:
                                                      TextOverflow.visible,
                                                ).asExpanded(),
                                              ]
                                            ],
                                          )
                                        ],
                                      ).asExpanded(),
                                    ],
                                  ),
                              ] else ...[
                                Observer(builder: (_) {
                                  return WEmptyWidget(
                                    text: Text(store.organName != null &&
                                            store.organName!.length > 0
                                        ? '搜索暂无数据'
                                        : '您当前没有部门管理权限，请联系公司管理员设置'),
                                  );
                                }),
                              ]
                            ],
                          );
                        }),
                      ],
                    ),
                  ),
                ],
                Wisdom.column(
                  padding: EdgeInsets.all(15.pt),
                  margin: EdgeInsets.only(bottom: 79.pt),
                  children: [
                    Wisdom.row(
                      margin: EdgeInsets.only(bottom: 15.pt),
                      children: [
                        WisText(
                          '统计说明',
                          size: 15.pt,
                          weight: FontWeight.w600,
                        ),
                      ],
                    ),
                    Row(
                      children: [
                        WisText(
                          '1. 当前展示为后台为您授权的部门，若数据有误请联系管理员授权。',
                          size: 13.pt,
                          weight: FontWeight.w300,
                          color: colorScheme.placeholder,
                        ).asExpanded(),
                      ],
                    ),
                    Row(
                      children: [
                        WisText(
                          '2. 只统计该部门直属下级人员的任务，不包含下级部门人员任务。',
                          size: 13.pt,
                          weight: FontWeight.w300,
                          color: colorScheme.placeholder,
                        ).asExpanded(),
                      ],
                    ),
                    Row(
                      children: [
                        WisText(
                          '3. 若需要查看下级部门，请同时勾选对应的下级部门。',
                          size: 13.pt,
                          weight: FontWeight.w300,
                          color: colorScheme.placeholder,
                        ).asExpanded(),
                      ],
                    ),
                  ],
                )
              ],
            );
          }),
        )
      ],
    );
  }
}
