/*
 * @Author       : WuWei
 * @LastEditors: wjc
 * @Date         : 2022-05-24 09:02:28
 * @LastEditTime: 2023-04-03 11:22:53
 * @Description  : Do not edit
 */

import 'package:flutter/material.dart';
import 'package:smart_devices_package/src/view_models/timing_set_model.dart';
import 'package:wisdom_cli/wisdom_cli.dart';
import '../../assets.gen.dart';

// part 'components/list.dart';
class TimingSetPage extends StatelessWidget {
  const TimingSetPage({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(
          create: (providerContext) => TimingSetStore(context, providerContext),
          lazy: false,
        ),
      ],
      child: _PageView(),
    );
  }
}

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<TimingSetStore>(context, listen: true);
    var item = store.routingArgs['item'];
    final dataList = [
      {'label': '工作日', 'value': 'work'},
      {'label': '每天', 'value': 'day'},
      {'label': '自定义', 'value': 'set'},
    ];
    final weeks = [
      {'label': '周一', 'value': '2'},
      {'label': '周二', 'value': '3'},
      {'label': '周三', 'value': '4'},
      {'label': '周四', 'value': '5'},
      {'label': '周五', 'value': '6'},
      {'label': '周六', 'value': '7'},
      {'label': '周日', 'value': '1'},
    ];
    final colorScheme = WTheme.of(context).colorScheme;
    return new WillPopScope(
      onWillPop: () {
        Navigator.pop(context, item);
        return new Future(() => false);
      },
      child: Scaffold(
        appBar: WAppBar(
          brightness: Brightness.light,
          iconTheme: IconThemeData(
            color: colorScheme.mainTitle,
            size: 14.pt,
          ),
          backgroundColor: Colors.white,
          title: WisText('定时设置',
              size: 19.pt,
              weight: FontWeight.w600,
              color: colorScheme.mainTitle),
        ),
        body: WisScrollView(
          onInit: () => store.onInit(),
          slivers: [
            SliverToBoxAdapter(
              child: WisCard(
                margin:
                    EdgeInsets.symmetric(horizontal: 15.pt, vertical: 10.pt),
                padding: EdgeInsets.all(15.pt),
                child: Wisdom.column(
                  color: Colors.white,
                  children: [
                    if (store.routingArgs['item'] != null &&
                        store.routingArgs['item']['enable'] == true) ...[
                      Wisdom.row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        margin: EdgeInsets.only(bottom: 10.pt, top: 8.pt),
                        children: [
                          Wisdom.row(children: [
                            Wisdom(
                              margin: EdgeInsets.only(
                                left: 11.pt,
                              ),
                              child: WisText(
                                '*',
                                color: Color(0XFFFF495E),
                              ),
                            ),
                            Wisdom(
                              margin: EdgeInsets.only(
                                right: 20.pt,
                              ),
                              child: WisText('操作类型'),
                            ),
                          ]),
                          WisText(
                            '${store.type == true ? '合闸' : '拉闸'}',
                            color: Colors.grey,
                          ),
                        ],
                      ),
                      Wisdom.row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        margin: EdgeInsets.only(bottom: 10.pt, top: 8.pt),
                        children: [
                          Wisdom.row(children: [
                            Wisdom(
                              margin: EdgeInsets.only(
                                left: 11.pt,
                              ),
                              child: WisText(
                                '*',
                                color: Color(0XFFFF495E),
                              ),
                            ),
                            Wisdom(
                              margin: EdgeInsets.only(
                                right: 20.pt,
                              ),
                              child: WisText('时间选择'),
                            ),
                          ]),
                          WisText(
                            store.time ?? '--',
                            color: Colors.grey,
                          ),
                        ],
                      ),
                    ] else ...[
                      WFSinglePicker(
                        isRequired: true,
                        title: WisText('操作类型'),
                        hintText: '选择设备合闸或拉闸',
                        initialValue: store.type,
                        isSearch: false,
                        label: 'name',
                        value: 'id',
                        dataList: [
                          {'name': '合闸', 'id': true},
                          {'name': '拉闸', 'id': false},
                        ],
                        onChanged: (v) => store.onSetType(v),
                      ),
                      WFTimePicker(
                        isRequired: true,
                        title: WisText('时间选择'),
                        hintText: '选择时间',
                        initialDate: store.time,
                        minuteInterval: 1,
                        onChanged: (v) => store.onSetTime(v),
                      ),
                    ],
                    Wisdom.row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      margin: EdgeInsets.only(bottom: 10.pt, top: 8.pt),
                      children: [
                        Wisdom(
                          margin: EdgeInsets.only(
                            left: 11.pt,
                            right: 20.pt,
                          ),
                          child: WisText('周期设置'),
                        ),
                        Wisdom.column(
                          children: [
                            Wisdom.row(
                              children: [
                                Wisdom.wrap(
                                  alignment: Alignment.centerLeft,
                                  padding: EdgeInsets.only(left: 10.pt),
                                  runSpacing: 10,
                                  children: [
                                    for (int i = 0; i < 3; i++)
                                      Wisdom(
                                        width: 100.pt,
                                        maxWidth: 100.pt,
                                        child: Wisdom.row(
                                          onTap: () => {
                                            if (store.routingArgs['item'] ==
                                                    null ||
                                                store.routingArgs['item']
                                                        ['enable'] ==
                                                    false)
                                              {
                                                store.onSetCycle(
                                                    dataList[i]['value'])
                                              }
                                          },
                                          mainAxisAlignment:
                                              MainAxisAlignment.start,
                                          crossAxisAlignment:
                                              CrossAxisAlignment.center,
                                          children: [
                                            if (dataList[i]['value'] ==
                                                store.cycleType) ...[
                                              if (store.routingArgs['item'] ==
                                                      null ||
                                                  store.routingArgs['item']
                                                          ['enable'] ==
                                                      false) ...[
                                                Image(
                                                    image: AssetList
                                                        .$danxuan_png_image,
                                                    height: 14.pt),
                                              ] else ...[
                                                Image(
                                                    image: AssetList
                                                        .$dxh2_png_image,
                                                    height: 14.pt),
                                              ]
                                            ] else ...[
                                              Image(
                                                  image: AssetList
                                                      .$danxuanyuan_png_image,
                                                  height: 14.pt),
                                            ],
                                            SizedBox(width: 6.pt),
                                            WisText(
                                              '${dataList[i]['label']}',
                                              size: 14.pt,
                                              color: Color(0xff222222),
                                              weight: FontWeight.w300,
                                            ),
                                          ],
                                        ),
                                      ),
                                  ],
                                ).asFlexible(flex: 1),
                              ],
                            ),
                            if (store.cycleType == 'set') ...[
                              SizedBox(height: 10.pt),
                              Wisdom.wrap(
                                children: [
                                  for (int i = 0; i < weeks.length; i++)
                                    Wisdom(
                                      width: 73.pt,
                                      maxWidth: 73.pt,
                                      child: Wisdom.row(
                                        onTap: () => {
                                          if (store.routingArgs['item'] ==
                                                  null ||
                                              store.routingArgs['item']
                                                      ['enable'] ==
                                                  false)
                                            {
                                              store.onSetWeeks(
                                                  weeks[i]['value']),
                                            }
                                        },
                                        mainAxisAlignment:
                                            MainAxisAlignment.start,
                                        crossAxisAlignment:
                                            CrossAxisAlignment.center,
                                        children: [
                                          if (store.weeks
                                              .contains(weeks[i]['value'])) ...[
                                            if (store.routingArgs['item'] ==
                                                    null ||
                                                store.routingArgs['item']
                                                        ['enable'] ==
                                                    false) ...[
                                              Image(
                                                  image:
                                                      AssetList.$fxxz_png_image,
                                                  height: 14.pt),
                                            ] else ...[
                                              Image(
                                                  image:
                                                      AssetList.$dxh_png_image,
                                                  height: 14.pt),
                                            ]
                                          ] else ...[
                                            Image(
                                                image: AssetList.$dx_png_image,
                                                height: 14.pt),
                                          ],
                                          SizedBox(width: 6.pt),
                                          WisText(
                                            '${weeks[i]['label']}',
                                            size: 14.pt,
                                            color: Color(0xff222222),
                                            weight: FontWeight.w300,
                                          ),
                                        ],
                                      ),
                                    ),
                                ],
                              ),
                            ],
                          ],
                        ).asFlexible(flex: 1)
                      ],
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
        bottomNavigationBar: Wisdom.row(
          color: Colors.white,
          height: 80.pt,
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.stretch,
          padding: EdgeInsets.symmetric(horizontal: 15.pt, vertical: 18.pt),
          children: [
            if (store.routingArgs['item'] != null) ...[
              WLoadingButton(
                color: Color(0XFFFF495E),
                height: 44.pt,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(25.pt),
                ),
                onPressed: () => store.onDelete(),
                child: WisText(
                  "删除",
                  color: Colors.white,
                ),
              ).asExpanded(),
            ],
            if (store.routingArgs['item'] == null ||
                store.routingArgs['item']['enable'] == false) ...[
              WLoadingButton(
                height: 44.pt,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(25.pt),
                ),
                onPressed: () => store.onSave(),
                child: WisText("保存"),
              ).asExpanded(),
            ],
          ],
        ),
      ),
    );
  }
}
