/*
 * @Author: XianKaiQun
 * @Date: 2020-04-26 15:26:25
 * @LastEditors: ChenYaJin
 * @LastEditTime: 2023-08-10 16:12:49
 * @Descripttion: 任务添加、编辑-入口页
 */
import 'package:flutter/material.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:task_package/src/assets.gen.dart';
import 'package:wisdom_cli/wisdom_cli.dart';
import '../../../src/view_models/edit_store.dart';

part 'components/title.dart';
part 'components/list.dart';
part 'components/open.dart';

part 'index.g.dart';

///路由传递的参数
class EditPageArguments {
  final String? id;
  final pData;

  final bool? isChildAdd;
  final bool? isChild;
  final data;
  final bool? isAbnormalAdd;
  final String? operationRecordAnswerId;
  EditPageArguments({
    this.id,
    this.pData,
    this.isChildAdd,
    this.isChild,
    this.data,
    this.isAbnormalAdd,
    this.operationRecordAnswerId,
  });

  ///
  ///
  ///
  ///
  ///
  ///
  ///
//***以下为模板代码谨慎修改***
  factory EditPageArguments.fromJson(Map<String, dynamic> json) {
    return _$EditPageArgumentsFromJson(json);
  }
  factory EditPageArguments.fromRouteArguments(BuildContext context) {
    return _$EditPageArgumentsFromJson(
        (ModalRoute.of(context)?.settings.arguments ??
            new Map<String, dynamic>()) as Map<String, dynamic>);
  }
  Map<String, dynamic> toJson() => _$EditPageArgumentsToJson(this);
}

///
///
///
///
///
//***以下为模板代码谨慎修改***
class EditPage extends StatelessWidget {
  const EditPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (providerContext) => EditStore(context),
      child: _PageView(),
      lazy: false,
    );
  }
}

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

class _PageViewState extends State<_PageView> {
  EditStore get store => Provider.of(context, listen: false);
  void initState() {
    store.onGetOperatorList();
    store.onGetTaskListItem();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;
    EditStore store = Provider.of(context);
    return Scaffold(
      appBar: WAppBar(
        iconTheme: IconThemeData(
          color: colorScheme.mainTitle,
          size: 19.pt,
        ),
        backgroundColor: Colors.white,
        brightness: Brightness.light,
        title: WisText('${store.title}',
            size: 19.pt, weight: FontWeight.w600, color: colorScheme.mainTitle),
      ),
      body: Container(
        margin: EdgeInsets.all(15.pt),
        child: WisForm(
          formKey: store.formKey,
          child: WisScrollView(
            slivers: <Widget>[
              if (store.isChildAdd) ...[
                SliverToBoxAdapter(
                  child: _TaskItem(
                    item: store.pData,
                  ),
                ),
              ],
              _TitleComponent(),
              _BuildTaskList(),
              _BuildTaskOpen(),
              SliverToBoxAdapter(
                child: Wisdom(
                  height: 85.pt,
                ),
              ),
            ],
          ),
        ),
      ),
      bottomSheet: Wisdom.row(
        height: 94.pt,
        color: Colors.white,
        children: [
          Wisdom(
              padding: EdgeInsets.symmetric(horizontal: 15.pt, vertical: 10.pt),
              child: WLoadingButton(
                onPressed: () => store.submit(),
                color: colorScheme.primary,
                height: 44.pt,
                child: WisText(
                  store.isCreate ? '确认添加' : '确认修改',
                  size: 16.pt,
                ),
                shape: StadiumBorder(),
              )).asExpanded(),
        ],
      ),
    );
  }
}

class _TaskItem extends StatelessWidget {
  const _TaskItem({Key? key, this.item}) : super(key: key);
  final item;

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

    ///tag状态颜色
    final statusColorMap = {
      // 1: '未开始',
      1: colorScheme.orange,
      // 2: '进行中',
      2: colorScheme.blue,
      // 3: '已完成',
      3: colorScheme.success,
      // 4: '超时',
      4: colorScheme.danger,
    };

    return Wisdom(
      color: Colors.white,
      margin: EdgeInsets.only(bottom: 10.pt),
      borderRadius: BorderRadius.circular(10.pt),
      boxShadow: [
        BoxShadow(
          offset: Offset(0, 2.pt),
          color: Color(0x08000000),
          blurRadius: 10.pt,
          spreadRadius: 2.5.pt,
        ),
      ],
      child: Wisdom.column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        padding: EdgeInsets.all(15.pt),
        children: <Widget>[
          Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              Text.rich(
                TextSpan(children: [
                  TextSpan(
                    text: item['title'] ?? '',
                    style: TextStyle(
                      fontSize: 16.pt,
                      fontWeight: FontWeight.w600,
                    ),
                  ),
                ]),
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
              ).asExpanded(),
            ],
          ),
          Wisdom.row(
            margin: EdgeInsets.only(
              top: 10.pt,
            ),
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              Text.rich(
                TextSpan(children: [
                  WidgetSpan(
                    child: WTag(
                      margin: EdgeInsets.only(right: 5.pt),
                      child: WisText(
                        '父任务',
                        size: 13.pt,
                      ),
                      color: statusColorMap[2],
                    ),
                  ),
                ]),
                maxLines: 2,
                overflow: TextOverflow.ellipsis,
              ),
              Text(
                "${item['createdAt']}",
                style: TextStyle(
                  fontWeight: FontWeight.w300,
                  fontSize: 13.pt,
                  color: colorScheme.time,
                ),
              )
            ],
          ),
        ],
      ),
    );
  }
}
