/*
 * @Author: ChenYaJin
 * @Date: 2021-11-10 10:34:03
 * @LastEditors: ChenYaJin
 * @LastEditTime: 2023-03-30 15:34:46
 * @Description:  服务费用管理页
 */
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';

class ManageFeePage extends StatelessWidget {
  const ManageFeePage({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),
      ),
      body: WisScrollView(
        key: store.feeDetailKey,
        onInit: () => store.getFeeDetail(),
        onRefresh: () => store.getFeeDetail(),
        slivers: <Widget>[
          _ListComponent(),
          _BottomTipComponent(),
        ],
      ),
      bottomSheet: _BottomButtom(),
    );
  }
}

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: true,
    );
    return Container(
      margin: EdgeInsets.only(top: 10.pt),
      child: listViewItem,
    ).nestSliverToBoxAdapter();
  }
}

class _BottomTipComponent extends StatelessWidget {
  const _BottomTipComponent({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.all(15.pt),
      child: WisText(
        '未结清费用可以删除服务费用项，但只能删除本人添加的服务费用项。',
        color: Color(0xFFF94F48),
        size: 13.pt,
      ),
    ).nestSliverToBoxAdapter();
  }
}

class _BottomButtom extends StatelessWidget {
  const _BottomButtom({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    FeeIndexStore store;
    store = Provider.of<FeeIndexStore>(context);
    final colorScheme = WTheme.of(context).colorScheme;
    return BottomAppBar(
      child: Wisdom.row(
        padding: EdgeInsets.symmetric(horizontal: 15.pt),
        color: Colors.white,
        children: [
          SafeArea(
            child: WLoadingButton(
              color: Colors.white,
              height: 44.pt,
              minWidth: 120.pt,
              margin: EdgeInsets.symmetric(vertical: 8.pt),
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(25.pt),
                side: BorderSide(
                  width: 1,
                  color: colorScheme.primary.withOpacity(0.5),
                  style: BorderStyle.solid,
                ),
              ),
              onPressed: () => store.route2FeeDetailNotRefresh(),
              child: WisText('取消', color: colorScheme.primary),
            ),
          ),
          SafeArea(
            child: WLoadingButton(
              height: 44.pt,
              margin: EdgeInsets.only(top: 8.pt, bottom: 8.pt, left: 10.pt),
              color: store.checkboxs.isNotEmpty
                  ? Color(0XFFFF495E)
                  : Color.fromRGBO(252, 85, 85, 0.6),
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(25.pt),
              ),
              onPressed: () => store.toDeleteFee(),
              child: Text("删除"),
            ),
          ).asExpanded(),
        ],
      ),
    );
  }
}
