/*
 * @Author: WuWei
 * @Date: 2020-07-01 17:10:47
 * @LastEditors: ChenYaJin
 * @LastEditTime: 2023-03-27 14:37:29
 * @Description:
 */
import 'package:app_package/app_package.dart';
import 'package:flutter/material.dart';
import 'package:repair_package/src/assets.gen.dart';
import 'package:wisdom_cli/wisdom_cli.dart';
import '../../view_models/private_detail_model.dart';
import '../../models/repair_detail.dart';
import '../../models/repair_dialog.dart';
import '../../components/list_item.dart';

part './components/consult.dart';
part './components/detail.dart';
part './components/score.dart';
part './components/bottom_detail.dart';
part './components/bottom_consult.dart';

// part 'components/list.dart';
class DetailPage extends StatelessWidget {
  const DetailPage({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (_) => RepairDetailStore(context),
      child: _PageView(),
      lazy: false,
    );
  }
}

class _PageView extends StatefulWidget {
  _PageView({Key? key}) : super(key: key);

  @override
  __PageViewState createState() => __PageViewState();
}

class __PageViewState extends State<_PageView> {
  RepairDetailStore get store => Provider.of(context);
  @override
  void deactivate() {
    super.deactivate();
    var bool = ModalRoute.of(context)!.isCurrent;
    if (bool) {
      initState(); //需要调用的方法
    }
  }

  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final _store = Provider.of<RepairDetailStore>(context, listen: true);
    final colorScheme = WTheme.of(context).colorScheme;
    return Scaffold(
      backgroundColor: Color(0xFFF8F8F8),
      appBar: WAppBar(
        iconTheme: IconThemeData(
          color: colorScheme.mainTitle,
          size: 19.pt,
        ),
        backgroundColor: Colors.white,
        brightness: Brightness.light,
        title: WisText('报修详情',
            size: 19.pt, weight: FontWeight.w600, color: colorScheme.mainTitle),
      ),
      body: Column(children: [
        if (_store.repairDetail?.statusName != '待受理') ...[
          if (_store.repairDetail?.linkId != null &&
              _store.repairDetail?.linkId != '') ...[
            _InCludeScore().asFlexible(flex: 1),
          ] else ...[
            _NotInCludeScore().asFlexible(flex: 1),
          ],
        ] else ...[
          _TabDetailComponent().asFlexible(flex: 1),
        ]
      ]),
      bottomNavigationBar: SafeArea(
        child: _store.tabIndex == 0
            ? _BottomDetailComponent()
            : _store.repairDetail?.linkId != null &&
                    _store.repairDetail?.linkId != '' &&
                    _store.tabIndex == 2
                ? _BottomConsultComponent()
                : (_store.repairDetail?.linkId == null ||
                            _store.repairDetail?.linkId == '') &&
                        _store.tabIndex == 1
                    ? _BottomConsultComponent()
                    : Container(height: 0),
      ),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    final _store = Provider.of<RepairDetailStore>(
      context,
      listen: true,
    );
    return WisTabPage(
      initialIndex: _store.args['index'] ?? 0,
      onChange: (index) => {_store.setTabIndex(index)},
      tabs: <Widget>[
        WisText('报修详情'),
        WisText('在线沟通'),
        WisText('查看评分'),
      ],
      views: <Widget>[
        _TabDetailComponent(),
        _TabScoreComponent(),
        _TabConsultComponent(),
      ],
      indicatorPadding: EdgeInsets.symmetric(horizontal: 23.pt),
      indicatorWeight: 4.0,
      indicator: RoundUnderlineTabIndicator(
        width: 25.pt,
      ),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    final _store = Provider.of<RepairDetailStore>(
      context,
      listen: true,
    );
    return WisTabPage(
      initialIndex: _store.args['index'] ?? 0,
      onChange: (index) => {_store.setTabIndex(index)},
      tabs: <Widget>[
        WisText('报修详情'),
        WisText('在线沟通'),
      ],
      views: <Widget>[
        _TabDetailComponent(),
        _TabConsultComponent(),
      ],
      indicatorPadding: EdgeInsets.symmetric(horizontal: 23.pt),
      indicatorWeight: 4.0,
      indicator: RoundUnderlineTabIndicator(
        width: 25.pt,
      ),
    );
  }
}
