/*
 * @Author: XianKaiQun
 * @Date: 2020-08-17 16:56:31
 * @LastEditors: XianKaiQun
 * @LastEditTime: 2020-08-17 16:59:57
 * @Description: 
 */
import 'package:flutter/material.dart';
import 'package:qrcode_package/src/view_models/private_norm_picker_store.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

// part 'components/list.dart';

class NormPickerPage extends StatelessWidget {
  const NormPickerPage({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (providerContext) =>
          PrivateNormPickerStore(context, providerContext),
      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) {
    final store = Provider.of<PrivateNormPickerStore>(context, listen: true);
    final style = WTheme.of(context);
    final colorScheme = style.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(
        onInit: () => store.getNormList(),
        slivers: <Widget>[
          SliverToBoxAdapter(
            child: Wisdom.column(
              color: Colors.white,
              children: [
                WSearch(
                  isDark: true,
                  hintText: '输入设备规范关键词',
                  onSubmitted: (v) => store.searchName(v),
                  onCancel: () => store.searchName(''),
                ),
                Divider(),
                if (store.allList.length > 0) ...[
                  for (var item in store.allList)
                    Wisdom.row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      border: Border(
                        bottom: BorderSide(
                          width: 0.5,
                          color: colorScheme.divider,
                        ),
                      ),
                      padding: EdgeInsets.all(15.pt),
                      children: [
                        Wisdom.row(
                          onTap: () => store.chooseItem(item),
                          children: [
                            WImage.network(
                              store.checkedItem?.id == item.id
                                  ? 'https://wy.huiguanjia.cn/9ad134a361f8d778/file/images/262296929919668225'
                                  : 'https://wy.huiguanjia.cn/9ad134a361f8d778/file/images/262297114636816384',
                              width: 20.pt,
                              height: 20.pt,
                            ),
                            Wisdom.column(
                              margin: EdgeInsets.only(left: 15.pt),
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                WisText(
                                  '${item.name}',
                                  color: colorScheme.mainTitle,
                                  size: 15.pt,
                                ),
                                SizedBox(height: 5.pt),
                                WisText(
                                  '${item.cycle}',
                                  color: colorScheme.grey,
                                  size: 13.pt,
                                  weight: FontWeight.w400,
                                ),
                              ],
                            ),
                          ],
                        ).asFlexible(flex: 1),
                        Wisdom.row(
                          onTap: () => store.toNormDetail(item),
                          width: 150.pt,
                          mainAxisAlignment: MainAxisAlignment.end,
                          children: [
                            WisText(
                              '详情',
                              color: colorScheme.primary,
                              size: 13.pt,
                            ),
                            SizedBox(width: 5.pt),
                            WImage.network(
                              'https://wy.huiguanjia.cn/9ad134a361f8d778/file/images/286814241511698432',
                              width: 6.pt,
                              height: 14.pt,
                            ),
                          ],
                        )
                      ],
                    )
                ]
              ],
            ),
          ),
        ],
      ),
      bottomNavigationBar: Row(
        children: [
          SafeArea(
            child: WLoadingButton(
              height: 44.pt,
              shape: RoundedRectangleBorder(),
              onPressed: () => store.routeReturn(),
              child: WisText('确认选择规范'),
            ),
          ).asExpanded(),
        ],
      ),
    );
  }
}
