/*
 * @Author: XianKaiQun
 * @Date: 2020-04-24 16:10:35
 * @LastEditors: wuwei
 * @LastEditTime: 2024-01-26 15:08:48
 * @Descripttion: 任务详情-入口页
 */


import 'package:wisdom_cli/wisdom_cli.dart';
import 'package:app_package/app_package.dart';
import 'package:flutter/material.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
// import 'package:task_package/src/view_models/location_util_store.dart';
import '../../../src/assets.gen.dart';
import '../../../src/view_models/detail_store.dart';
part 'components/desc.dart';
part 'components/bottom.dart';
part 'components/child_task.dart';
part 'components/father.dart';
part 'components/info.dart';
part 'components/files.dart';
part 'components/route.dart';
part 'components/repair_form.dart';
part 'components/repair_point.dart';
part 'multi/index.dart';

class DetailPage extends StatelessWidget {
  const DetailPage({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(
          create: (_) => PrivateTaskDetailStore(context),
          lazy: false,
        ),
      ],
      child: _PageView(),
    );
  }
}

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

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

  @override
  void deactivate() {
    // store.destroy();
    super.deactivate();
  }

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

    /// 任务编辑入口
    final data = store.data;
    final showEdit = data?.status == 'no_open' &&
        (data?.iscreatedBy == true) &&
        (data?.preset == false) &&
        data?.taskMultiStepEnable != true &&
        data?.isMultiStep != true;
    return Scaffold(
      appBar: WAppBar(
          title: WisText(
            store.isChild == true ? '子任务详情' : '任务详情',
            size: 19.pt,
            weight: FontWeight.w600,
            color: Colors.white,
          ),
          iconTheme: IconThemeData(
            color: Colors.white,
            size: 19.pt,
          ),
          brightness: Brightness.dark,
          actions: <Widget>[
            Wisdom.row(
              color: colorScheme.primary,
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              margin: EdgeInsets.symmetric(horizontal: 10.pt),
              children: [
                if (showEdit) ...[
                  IconButton(
                    onPressed: () => store.route2edit(),
                    icon: Image(
                      image: AssetList.$edit_png_image,
                      width: 20.pt,
                    ),
                  )
                ]
              ],
            ),
          ]),
      body: (store.data?.isMultiStep == true ||
                  store.data?.taskMultiStepEnable == true) &&
              store.isMulti != true
          ? WisTabPage(
              divider: false,
              color: colorScheme.primary,
              onChange: (index) => {store.setTabIndex(index)},
              tabs: [
                WisText('任务内容', color: Colors.white, size: 16.pt),
                WisText('相关环节', color: Colors.white, size: 16.pt),
              ],
              views: [
                _BuildDetailBody(),
                _BuildMultiBody(),
              ],
              indicator: RoundUnderlineTabIndicator(
                borderSide: BorderSide(width: 4.0, color: Colors.white),
                width: 25.pt,
              ),
            )
          : _BuildDetailBody(),
      bottomSheet: store.tabIndex == 0
          ? _BottomComponent()
          : store.data?.taskMultiStepEnable == true &&
                  (store.isCheckMultiFinished() == false)
              ? Wisdom.column(
                  height: 80.pt,
                  color: Colors.white,
                  padding:
                      EdgeInsets.symmetric(horizontal: 15.pt, vertical: 10.pt),
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  children: [
                      WLoadingButton(
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(40.pt),
                        ),
                        onPressed: () => store.onPostTaskMultiUrge(),
                        color: colorScheme.primary,
                        disabledColor: colorScheme.disabled,
                        child: Text('一键催办'),
                      )
                    ])
              : Container(height: 0),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    final store = Provider.of<PrivateTaskDetailStore>(context);

    return WisScrollView(
      key: store.detailScrollviewKey,
      onInit: () => store.getData(),
      onRefresh: () => store.getData(),
      slivers: <Widget>[
        /// 顶部标题简介
        _DescComponent(),

        /// 预设清单任务--路线
        if (store.data?.preset == true) ...[
          _BuildRoute(),
        ],

        /// 简介信息
        _BuildInfo(),

        /// 父级任务
        if (store.data?.isChild == true) _FatherComponent(),

        /// 子任务列表
        if (store.data?.isChild != true && store.taskChildList.length > 0)
          _ChildTaskComponent(),

        SizedBox(
          height: 85.pt,
        ).asSliver(),
      ],
    );
  }
}
