/*
 * @Author: XianKaiQun
 * @Date: 2020-09-11 09:33:27
 * @LastEditors: ChenYaJin
 * @LastEditTime: 2023-03-31 10:13:24
 * @Description: 
 */
import 'package:dynamic_package/src/view_models/private_edit_store.dart';
import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

//part 'components/list.dart';
class EditPage extends StatelessWidget {
  const EditPage({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (providerContext) => PrivateEditStore(context, providerContext),
      lazy: false,
      child: Theme(
        data: Theme.of(context).copyWith(
          inputDecorationTheme: InputDecorationTheme(
            border: InputBorder.none,
            isCollapsed: false,
            isDense: false,
            contentPadding: EdgeInsets.symmetric(vertical: 11.pt),
          ),
        ),
        child: _PageView(),
      ),
    );
  }
}

class _PageView extends StatelessWidget {
  const _PageView({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    final colorScheme = WTheme.of(context).colorScheme;
    final store = Provider.of<PrivateEditStore>(context);
    return Scaffold(
      appBar: WAppBar(
        iconTheme: IconThemeData(
          color: colorScheme.mainTitle,
          size: 19.pt,
        ),
        backgroundColor: Colors.white,
        brightness: Brightness.light,
        title: WisText('${(store.isEdit ? '编辑' : '新增') + '服务动态'}',
            size: 19.pt, weight: FontWeight.w600, color: colorScheme.mainTitle),
      ),
      body: WForm(
        child: WisScrollView(
          onInit: () => store.getClassList(),
          slivers: [
            SliverToBoxAdapter(
              child: WisCard(
                color: Colors.white,
                margin:
                    EdgeInsets.symmetric(horizontal: 15.pt, vertical: 15.pt),
                child: Wisdom(
                  padding: EdgeInsets.symmetric(horizontal: 15.pt),
                  child: WFSinglePicker(
                    // isRequired: true,
                    initialValue: store.type,
                    dataList: store.classList,
                    label: "name",
                    value: "id",
                    title: WisText('类型'),
                    hintText: '请选择服务动态类型',
                    onChanged: (v) => store.chooseType(v),
                  ),
                ),
              ),
            ),
            SliverToBoxAdapter(
              child: WisCard(
                color: Colors.white,
                margin: EdgeInsets.symmetric(
                  horizontal: 15.pt,
                ),
                child: Wisdom.column(
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  padding: EdgeInsets.symmetric(horizontal: 15.pt),
                  color: Colors.white,
                  border: Border(
                    top: Divider.createBorderSide(context),
                    bottom: Divider.createBorderSide(context),
                  ),
                  children: [
                    WFormTile(
                      required: true,
                      direction: Axis.vertical,
                      leading: Text('服务内容'),
                      child: TextFormField(
                        initialValue: store.content,
                        onChanged: (value) => store.contentChange(value),
                        minLines: 3,
                        maxLines: 3,
                        maxLength: 256,
                        decoration: InputDecoration(
                          hintText: '输入物业服务内容',
                        ),
                      ),
                    ),
                    Divider(),
                    Wisdom.wrap(
                      padding: EdgeInsets.symmetric(vertical: 15.pt),
                      runSpacing: 10.pt,
                      spacing: 10.pt,
                      children: [
                        ...WImagePreview.buildList(
                          images: store.images,
                          onDelete: store.onDelete,
                          size: 80.pt,
                        ),
                        WImageSelecter(
                          maxCount: 6,
                          currentCount: store.images.length,
                          onSelected: (files) => store.filesChanged(files),
                          size: 80.pt,
                        ),
                      ],
                    ),
                  ],
                ),
              ),
            ),
            SliverToBoxAdapter(
              child: Wisdom(
                margin: EdgeInsets.all(
                  15.pt,
                ),
                child: WisText(
                  '注：手机仅发布服务简报，若需发布更详细内容或编辑详细内容，请使用电脑端',
                  size: 13.pt,
                  color: colorScheme.placeholder2,
                ),
              ),
            ),
          ],
        ),
      ),
      bottomNavigationBar: 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.submitDraft(),
              child: WisText(store.isEdit ? "更新草稿" : "保存草稿",
                  color: colorScheme.primary),
            ),
          ),
          SafeArea(
            child: WLoadingButton(
              height: 44.pt,
              margin: EdgeInsets.only(top: 8.pt, bottom: 8.pt, left: 10.pt),
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(25.pt),
              ),
              onPressed: () => store.submit(),
              child: Text("发布"),
            ),
          ).asExpanded(),
        ],
      ),
    );
  }
}
