/*
 * @Author: ChenYaJin
 * @Date: 2021-11-08 18:05:36
 * @LastEditors: ChenYaJin
 * @LastEditTime: 2023-03-30 15:28:15
 * @Description:  服务费用详情页
 */
import 'package:app_package/app_package.dart';
import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';
import '../../view_models/private_fee_model.dart';
import './components/list.dart';
import '../../models/service_fee_detail.dart';

part './components/empty.dart';
part './components/qrcode.dart';

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

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

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

class __PageViewState extends State<_PageView> {
  @override
  Widget build(BuildContext context) {
    FeeIndexStore store;
    store = Provider.of<FeeIndexStore>(context);
    final colorScheme = WTheme.of(context).colorScheme;
    return Scaffold(
      appBar: WAppBar(
        brightness: Brightness.light,
        iconTheme: IconThemeData(
          color: colorScheme.mainTitle,
          size: 19.pt,
        ),
        backgroundColor: Colors.white,
        title: WisText('服务费用',
            size: 19.pt, weight: FontWeight.w600, color: colorScheme.mainTitle),
        actions: <Widget>[_TopActionComponent()],
      ),
      body: WisScrollView(
        key: store.feeDetailKey,
        onInit: () => store.getFeeDetail(),
        onRefresh: () => store.getFeeDetail(),
        slivers: <Widget>[
          if (!store.hasServiceFee) _EmptyDataComponent(),
          if (store.hasServiceFee && store.serviceFeeDetail?.id != null)
            _ListComponent(),
          if (store.hasServiceFee &&
              store.serviceFeeDetail?.id != null &&
              store.ableManage)
            _QrcodeComponent(),
        ],
      ),
      bottomSheet: !store.isPaied
          ? BottomAppBar(
              child: Wisdom.row(
                height: 60.pt,
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                padding: EdgeInsets.only(left: 10.pt),
                children: <Widget>[
                  Expanded(
                    child: Wisdom(
                      height: 44.pt,
                      onTap: () => store.route2AddFee(),
                      color: Color(0XFF0F67F8),
                      borderRadius: BorderRadius.circular(25.pt),
                      child: Wisdom(
                        alignment: Alignment.center,
                        child: WisText.white('添加费用', size: 17.pt),
                      ),
                    ),
                  ),
                ],
              ),
            )
          : Wisdom(
              height: 0.pt,
            ),
    );
  }
}

class _TopActionComponent extends StatelessWidget {
  const _TopActionComponent({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    FeeIndexStore store;
    store = Provider.of<FeeIndexStore>(context);
    final colorScheme = WTheme.of(context).colorScheme;
    return store.hasServiceFee && store.ableManage
        ? Wisdom(
            padding: EdgeInsets.symmetric(vertical: 10.pt, horizontal: 15.pt),
            onTap: () => store.route2ManageFee(),
            child: WisText('管理', size: 14.pt, color: colorScheme.mainTitle),
          )
        : Wisdom(
            height: 0.pt,
          );
  }
}

class _ListComponent extends StatelessWidget {
  const _ListComponent({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    FeeIndexStore store;
    store = Provider.of<FeeIndexStore>(context);
    ServiceFeeDetailModel? detail = store.serviceFeeDetail;
    final listViewItem = ListViewItem(
      detail: detail,
      showCheck: false,
    );
    return Container(
      margin: EdgeInsets.only(top: 10.pt),
      child: listViewItem,
    ).nestSliverToBoxAdapter();
  }
}
