/*
 * @Author: XianKaiQun
 * @Date: 2020-09-11 08:54:48
 * @LastEditors: ChenYaJin
 * @LastEditTime: 2023-04-06 14:30:28
 * @Description: 
 */
import 'package:dynamic_package/src/components/dynamic_card.dart';
import 'package:dynamic_package/src/view_models/private_index_store.dart';
import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';


part 'components/list.dart';

class DynamicIndexPage extends StatelessWidget {
  const DynamicIndexPage({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (providerContext) => PrivateIndexStore(context, providerContext),
      child: _PageView(),
      lazy: false,
    );
  }
}

class _PageView extends StatelessWidget {
  const _PageView({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    final colorScheme = WTheme.of(context).colorScheme;
    final store = Provider.of<PrivateIndexStore>(context, listen: true);

    return Scaffold(
      appBar: WAppBar(
        iconTheme: IconThemeData(
          color: colorScheme.mainTitle,
          size: 19.pt,
        ),
        backgroundColor: Colors.white,
        brightness: Brightness.light,
        title: WisText('${store.routingArgs['appBarTitle'] ?? '服务动态'}',
            size: 19.pt, weight: FontWeight.w600, color: colorScheme.mainTitle),
        actions: [
          Wisdom(
            padding: EdgeInsets.only(
              right: 15.pt,
              left: 5.pt,
            ),
            onTap: () => store.toFilter(),
            child: Image.asset(
              'lib/assets/shx.png',
              height: 15.pt,
              width: 15.pt,
              package: 'dynamic_package',
              color: colorScheme.mainTitle,
            ),
          ).asFlexible(flex: 1),
        ],
      ),
      body: Column(
        children: [
          Wisdom.row(
            color: Colors.white,
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              if (store.index == 0) ...[
                /// 输入框
                Expanded(
                  child: WSearch(
                    key: store.searchKey1,
                    hintText: '搜索服务动态内容..',
                    isControlOutside: true,
                    initValue: store.title,
                    onSubmitted: (v) => store.onSearch(v),
                    onCancel: () => store.onSearch(''),
                    moonlight: false,
                    padding: EdgeInsets.symmetric(vertical: 10.pt),
                    isDark: true,
                  ),
                ),
              ] else ...[
                /// 输入框
                Expanded(
                  child: WSearch(
                    key: store.searchKey1,
                    initValue: store.titleDraft,
                    hintText: '搜索草稿箱..',
                    isControlOutside: true,
                    onSubmitted: (v) => store.onSearchDraft(v),
                    onCancel: () => store.onSearchDraft(''),
                    moonlight: false,
                    padding: EdgeInsets.symmetric(vertical: 10.pt),
                    isDark: true,
                  ),
                ),
              ]
            ],
          ),
          WisTabPage(
            key: store.tabKey,
            onChange: (index) => {store.onSetTabIndex(index)},
            divider: false,
            isMarginBottom: false,
            initialIndex: store.index,
            tabs: [
              WisText('已发布', size: 15.pt),
              WisText('草稿箱', size: 15.pt),
            ],
            views: [
              WisScrollView(
                key: store.scrollViewKey,
                onInit: () => store.getList(isLoad: false),
                onRefresh: () => store.getList(isLoad: false),
                onLoad: () => store.getList(isLoad: true),
                slivers: [
                  _ListComponent(),
                ],
              ),
              WisScrollView(
                key: store.scrollViewKey1,
                onInit: () => store.getDraftList(isLoad: false),
                onRefresh: () => store.getDraftList(isLoad: false),
                onLoad: () => store.getDraftList(isLoad: true),
                slivers: [
                  _DraftListComponent(),
                ],
              ),
            ],
          ).asFlexible(flex: 1),
        ],
      ),
      bottomNavigationBar: SafeArea(
        child: Wisdom.row(
          height: 60.pt,
          color: Colors.white,
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          padding: EdgeInsets.only(left: 10.pt),
          children: <Widget>[
            Wisdom(
              onTap: () => store.route2add(),
              minWidth: 100.pt,
              alignment: Alignment.center,
              child: Container(
                height: 44.pt,
                margin: EdgeInsets.only(right: 15.pt),
                decoration: BoxDecoration(
                  gradient: LinearGradient(
                    colors: [
                      Color.fromRGBO(77, 128, 255, 1),
                      Color.fromRGBO(15, 103, 248, 1)
                    ],
                    begin: Alignment.centerLeft,
                    end: Alignment.centerRight,
                  ),
                  borderRadius: BorderRadius.circular(25.pt),
                ),
                child: Center(
                  child: Text(
                    '新增服务动态',
                    style: TextStyle(
                      fontSize: 14.pt,
                      color: Colors.white,
                    ),
                  ),
                ),
              ),
            ).asExpanded(),
          ],
        ),
      ),
    );
  }
}

class _DraftListComponent extends StatelessWidget {
  const _DraftListComponent({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    final store = Provider.of<PrivateIndexStore>(context);
    return WisSliverList.childBuilder(
        childCount: store.draftlist.length,
        builder: (_, i) {
          final item = store.draftlist[i];
          final photos =
              (item.url ?? '') == '' ? <String>[] : item.url!.split(',');
          return DynamicCardComponent(
            type: item.type,
            id: item.id,
            content: item.title,
            dateTime: item.updateAt,
            creator: item.createdByName,
            photos: photos,
            onEdit: item.type == 'serviceBulletin'
                ? () => store.onEdit(item)
                : null,
            onDelete: () => store.onDelete(item.id),
          );
        });
  }
}
