/*
 * @Author: ChenYaJin
 * @Date: 2023-05-29 10:14:21
 * @LastEditors: wuwei
 * @LastEditTime: 2023-12-27 15:06:30
 * @Description: 
 */
part of '../index.dart';

class WorkDetailModalView extends StatelessWidget {
  const WorkDetailModalView({Key? key, this.instructionList, this.detail})
      : super(key: key);
  final List<TaskOperationInstructionList>? instructionList;
  final TaskOperationFormModel? detail;

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (_) => PointFormStore(context),
      lazy: false,
      child: _WorkDetailModalView(
          instructionList: instructionList, detail: detail),
    );
  }
}

class _WorkDetailModalView extends StatefulWidget {
  _WorkDetailModalView({
    Key? key,
    this.instructionList,
    this.detail,
  }) : super(key: key);
  final List<TaskOperationInstructionList>? instructionList;
  final TaskOperationFormModel? detail;
  @override
  __WorkDetailModalViewState createState() => __WorkDetailModalViewState();
}

class __WorkDetailModalViewState extends State<_WorkDetailModalView> {
  bool loading = false;
  TakOperationInstruction? _detail;
  int readIndex = 0;

  PointFormStore get store => Provider.of(context, listen: false);
  void initState() {
    if (widget.instructionList!.length > 0) {
      onGetInstructionDetail(widget.instructionList?[readIndex].id);
    }
    super.initState();
  }

  Future<void> onGetInstructionDetail(id) async {
    if (id == null) {
      return;
    }
    var res = await QrcodeServiceImpl.instance!
        .getOperationInstructionDetail({'id': id});
    _detail = res.data ?? TakOperationInstruction();
    setState(() {});
  }

  Future<void> onClose() async {
    Navigator.of(WNavUtil.instance!.navigatorKey.currentContext!).pop();
    store.setPlatformNodeRead(widget.detail?.id);
  }

  Future<void> onNextOne() async {
    readIndex += 1;
    setState(() {});
    onGetInstructionDetail(widget.instructionList?[readIndex].id);
  }

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

    return Center(
      child: Wisdom.column(
        padding: EdgeInsets.only(top: 30.pt, bottom: 20.pt),
        height: MediaQuery.of(context).size.height * .65,
        width: MediaQuery.of(context).size.width * .8,
        borderRadius: BorderRadius.circular(2.pt),
        color: Colors.white,
        children: [
          Wisdom(
            padding: EdgeInsets.only(bottom: 15.pt),
            alignment: Alignment.center,
            child: WisText(
              '作业指导',
              size: 16.pt,
              weight: FontWeight.w600,
              color: colorScheme.mainTitle,
            ),
          ),
          Expanded(
            child: Padding(
              padding: EdgeInsets.symmetric(horizontal: 25.pt),
              child: WisScrollView(
                slivers: [
                  HtmlWidget(
                      _detail?.content ?? ' ',
                    ).asSliver(),
                  
                ],
              ),
            ),
          ),
          Wisdom.row(
            padding: EdgeInsets.fromLTRB(15.pt, 20.pt, 15.pt, 0),
            children: [
              if (widget.instructionList != null &&
                  widget.instructionList!.length > (readIndex + 1))
                WButton(
                  height: 44.pt,
                  onPressed: () => onNextOne(),
                  color: Colors.white,
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(20.pt),
                    side: BorderSide(
                      width: 1,
                      color: colorScheme.primary,
                      style: BorderStyle.solid,
                    ),
                  ),
                  margin: EdgeInsets.only(
                    right: 15.pt,
                  ),
                  child: WisText(
                    '下一个',
                    size: 16.pt,
                    color: colorScheme.primary,
                  ),
                ).asExpanded(),
              WButton(
                height: 44.pt,
                onPressed: () => onClose(),
                color: colorScheme.primary,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(20.pt),
                  side: BorderSide(
                    width: 1,
                    color: colorScheme.primary,
                    style: BorderStyle.solid,
                  ),
                ),
                child: WisText(
                  '已知晓',
                  size: 16.pt,
                  color: Colors.white,
                ),
              ).asExpanded(),
            ],
          )
        ],
      ),
    );
  }
}
