/*
 * @Author: XianKaiQun
 * @Date: 2020-09-23 18:39:13
 * @LastEditors: ChenYaJin
 * @LastEditTime: 2022-10-21 14:50:10
 * @Description: 
 */

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:wisdom_cli/wisdom_cli.dart';
import '../../src/view_models/notice_list_store.dart';

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

  @override
  Widget build(BuildContext context) {
    return _PageView();
  }
}

class _PageView extends StatefulWidget {
  _PageView({Key? key}) : super(key: key);
  @override
  _PageViewState createState() => _PageViewState();
}

class _PageViewState extends State<_PageView> {
  @override
  Widget build(BuildContext context) {
    final NoticeListStore store = NoticeListStore();
    final colorScheme = WTheme.of(context).colorScheme;
    return Scaffold(
      appBar: WAppBar(
        brightness: Brightness.light,
        title: WisText(
            '${Get.arguments != null ? Get.arguments['appBarTitle'] : '通知列表'}',
            size: 19.pt,
            weight: FontWeight.w600,
            color: colorScheme.mainTitle),
        iconTheme: IconThemeData(
          color: colorScheme.mainTitle,
          size: 19.pt,
        ),
        backgroundColor: Colors.white,
      ),
      body: Column(
        children: [
          _ListComponent(),
        ],
      ),
      bottomNavigationBar: SafeArea(
        child: Wisdom.row(
          color: Colors.white,
          children: [
            Wisdom(
                padding:
                    EdgeInsets.symmetric(horizontal: 15.pt, vertical: 10.pt),
                child: WButton(
                  onPressed: () => store.routeToAdd(),
                  color: colorScheme.primary,
                  height: 44.pt,
                  child: WisText(
                    '发布',
                    size: 16.pt,
                  ),
                  shape: StadiumBorder(),
                )).asExpanded(),
          ],
        ),
      ),
    );
  }
}

class _ListComponent extends GetView<NoticeListStore> {
  const _ListComponent({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final NoticeListStore store = NoticeListStore();
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;

    return Column(
      children: [
        WSearch(
          isDark: true,
          backColor: Colors.white,
          hintText: '输入公告标题搜索',
          onSubmitted: (v) => store.onSearch(v),
          onCancel: () => store.onSearch(''),
        ),
        WisScrollView(
          key: store.scrollViewKey,
          onInit: () => store.onInitPage(),
          onRefresh: () => store.getList(isLoad: false),
          onLoad: () => store.getList(),
          slivers: <Widget>[
            Obx(() {
              return WisSliverList.childBuilder(
                childCount: store.list.length,
                builder: (_, i) {
                  var item = store.list[i];
                  return InkWell(
                    onTap: () => store.routeToDetail(item),
                    child: WisCard(
                      margin: EdgeInsets.only(
                        top: 10.pt,
                        left: 15.pt,
                        right: 15.pt,
                      ),
                      child: Wisdom.column(
                        padding: EdgeInsets.all(15.pt),
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: <Widget>[
                          buildNoticeType(item),
                          Wisdom.row(
                            border: Border(
                              bottom: Divider.createBorderSide(context),
                            ),
                            padding: EdgeInsets.only(
                              bottom: 8.pt,
                            ),
                            children: [
                              Wisdom(
                                margin: EdgeInsets.only(right: 10.pt),
                                child: Text(
                                  "主题词:",
                                  style: TextStyle(
                                    fontWeight: FontWeight.w300,
                                    fontSize: 15.pt,
                                    color: colorScheme.minor,
                                  ),
                                ),
                              ),
                              Wisdom(
                                child: WisText(
                                  item.theme ?? '--',
                                  size: 15.pt,
                                  overflow: TextOverflow.ellipsis,
                                  color: colorScheme.minor,
                                ),
                              ).asFlexible(flex: 1),
                            ],
                          ),
                          Container(
                            child: Wisdom.row(
                                padding: EdgeInsets.only(
                                  top: 10.pt,
                                ),
                                children: [
                                  Text(
                                    "发布人: ${item.publisherName ?? '-'}",
                                    style: TextStyle(
                                        color: colorScheme.grey,
                                        fontSize: 14.pt),
                                  ).nestExpanded(),
                                  Text(
                                    "发布时间: ${item.publishAt ?? '-'}",
                                    style: TextStyle(
                                        color: colorScheme.grey,
                                        fontSize: 14.pt),
                                  ),
                                ]),
                          ),
                        ],
                      ),
                    ),
                  );
                },
              );
            }),
          ],
        ).nestExpanded(),
      ],
    ).nestExpanded();
  }

  Widget buildNoticeType(final item) {
    if (item.type == 'importance') {
      return buildNoticeTypeContainer(
          '重要通知', item.title ?? '', Color.fromRGBO(253, 144, 37, 1));
    } else if (item.type == 'prompt') {
      return buildNoticeTypeContainer(
          '温馨提示', item.title ?? '', Color.fromRGBO(37, 180, 30, 1));
    } else if (item.type == 'urgency') {
      return buildNoticeTypeContainer(
          '紧急通知', item.title ?? '', Color.fromRGBO(249, 87, 89, 1));
    } else if (item.type == 'general') {
      return buildNoticeTypeContainer(
          '普通通知', item.title ?? '', Color.fromRGBO(52, 142, 237, 1));
    } else if (item.type == 'PropertyPublicity') {
      return buildNoticeTypeContainer(
          '物业公示', item.title ?? '', Color.fromRGBO(37, 180, 30, 1));
    } else if (item.type == 'propertyCommittee') {
      return buildNoticeTypeContainer(
          '业委会公示', item.title ?? '', Color.fromRGBO(37, 180, 30, 1));
    } else {
      return buildNoticeTypeContainer(
          '', item.title ?? '', Color.fromRGBO(52, 142, 237, 1));
    }
  }

  Widget buildNoticeTypeContainer(String text, String title, Color textColor) {
    return Container(
      padding: EdgeInsets.fromLTRB(0, 0, 0, 10.pt),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.center,
        textBaseline: TextBaseline.alphabetic,
        children: <Widget>[
          WTag(
            color: textColor,
            child: Text(
              text,
            ),
          ),
          SizedBox(
            width: 10.pt,
          ),
          Expanded(
            child: WisText.c000(
              title,
              size: 17.pt,
              bold: true,
              overflow: TextOverflow.ellipsis,
              maxLines: 1,
            ),
          ),
        ],
      ),
    );
  }
}
