/*
 * @Author       : WuWei
 * @LastEditors  : WuWei
 * @Date         : 2021-12-10 16:35:54
 * @LastEditTime : 2023-03-31 10:17:57
 * @Description  : Do not edit
 */

import 'package:app_package/src/view_models/message_store.dart';
import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

import '../../../../app_package.dart';

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

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

  @override
  Widget build(BuildContext context) {
    final store = Provider.of<PrivateMessageStore>(context, listen: true);
    final colorScheme = WTheme.of(context).colorScheme;
    return Scaffold(
      appBar: WAppBar(
        brightness: Brightness.light,
        title: WisText(
          '${store.title}',
          size: 19.pt,
          weight: FontWeight.w600,
          color: colorScheme.mainTitle,
        ),
        iconTheme: IconThemeData(
          color: colorScheme.mainTitle,
          size: 19.pt,
        ),
        backgroundColor: Colors.white,

        /// 顶部按钮
        actions: <Widget>[
          Wisdom.row(
            children: <Widget>[
              Wisdom(
                onTap: () => store.toEditForm(),
                margin: EdgeInsets.only(right: 25.pt),
                child: WisText('编辑'),
              ),
            ],
          ),
        ],
      ),
      body: WisScrollView(
        onInit: () => store.onInitDetail(),
        slivers: [
          SliverToBoxAdapter(
            child: Column(
              children: [
                SizedBox(height: 5.pt),
                if (store.formValue.length > 0) ...[
                  /// 去除标题展示
                  if (store.projectItem != null) ...[
                    _HisFormItem(item: store.projectItem),
                  ],
                  if (store.periodItem != null) ...[
                    _HisFormItem(item: store.periodItem),
                  ],
                ],
                if (store.formHtml.length > 0) ...[
                  for (int i = 0; i < store.formHtml.length; i++)
                    _HisFormItem(item: store.formHtml[i]),
                ]
              ],
            ),
          )
        ],
      ),
    );
  }
}

class _HisFormItem extends StatelessWidget {
  const _HisFormItem({Key? key, this.item}) : super(key: key);
  final item;

  @override
  Widget build(BuildContext context) {
    final colorScheme = WTheme.of(context).colorScheme;
    final store = Provider.of<PrivateMessageStore>(context, listen: true);
    return WisCard(
      margin: EdgeInsets.only(
        top: 10.pt,
        left: 15.pt,
        right: 15.pt,
      ),
      child: Wisdom.column(
          color: Colors.white,
          padding: EdgeInsets.all(15.pt),
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            WisText(
              item['name'] ?? '--',
              color: colorScheme.mainTitle,
              size: 15.pt,
              weight: FontWeight.w600,
            ),
            Wisdom(height: 10.pt),
            if (item['tag'] == 'el-upload') ...[
              if (item['value'] is List && item['value'].length > 0) ...[
                if (item['__config__']['tagIcon'] == 'imgUpload') ...[
                  Wrap(
                    runSpacing: 10.pt,
                    spacing: 10.pt,
                    children: [
                      for (var file in item['value'])
                        Wisdom(
                          onTap: () => store.onShowImage(file['id']),
                          child: DecoratedBox(
                            position: DecorationPosition.foreground,
                            decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(4.pt),
                              border: Border.all(
                                  color: Colors.black12, width: 0.5.pt),
                            ),
                            child: WImage.network(
                              AppParseUtil.image('${file['id']}'),
                              width: 66.pt,
                              height: 66.pt,
                              fit: BoxFit.fill,
                              borderRadius: BorderRadius.circular(4.pt),
                            ),
                          ),
                        ),
                    ],
                  ),
                ] else ...[
                  for (var file in item['value'])
                    Wisdom(
                      onTap: () => store.onHandleFile(file),
                      padding: EdgeInsets.only(right: 5.pt, top: 5.pt),
                      child: Text(
                        '${file['name']}',
                        style: TextStyle(
                          color: colorScheme.primary,
                          fontSize: 15.pt,
                        ),
                      ),
                    ),
                ],
              ] else ...[
                WisText(
                  '--',
                  color: colorScheme.mainTitle,
                  size: 15.pt,
                  weight: FontWeight.w300,
                ),
              ]
            ] else if (item['tag'] == 'quillEditor') ...[
              /// 富文本图片上传前缀匹配，PC端上传未带域名
              // Container(
              //   child: HtmlWidget(
              //     data: item['value'] == null || item['value'] == ''
              //         ? '--'
              //         : item['value'].replaceAll(
              //             RegExp(r'\"/9ad134a361f8d778'), '"$baseUrl'),
              //     style: {
              //       'body': Style(
              //         fontSize: FontSize(15.pt),
              //       ),
              //     },
              //   ),
              // ),
            ] else ...[
              WisText(
                item['value'] is String && item['value'] != ''
                    ? item['value']
                    : item['value'] == null || item['value'] == ''
                        ? '--'
                        : item['value'].toString(),
                color: colorScheme.mainTitle,
                size: 15.pt,
                weight: FontWeight.w300,
              )
            ],
          ]),
    );
  }
}
