/*
 * @Author       : WuWei
 * @LastEditors: wjc
 * @Date         : 2022-05-20 10:14:31
 * @LastEditTime: 2023-04-03 11:26:51
 * @Description  : Do not edit
 */
part of '../index.dart';

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

  @override
  Widget build(BuildContext context) {
    final store = Provider.of<PrivateDetailStore>(context, listen: true);
    return WisScrollView(
      key: store.scorllViewTimingKey,
      onInit: () => store.getTimingList(),
      onLoad: () => store.getTimingList(isLoad: true),
      emptyWidget: SliverFillRemaining(
        child: Container(
          color: Colors.white,
          child: Wisdom.column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              WEmptyWidget(
                text: Text('暂无数据'),
              ),
            ],
          ),
        ),
      ),
      slivers: [
        SliverToBoxAdapter(
          child: Wisdom.column(
            margin: EdgeInsets.only(top: 5.pt),
            children: [
              if (store.timingList.length > 0) ...[
                for (var item in store.timingList)
                  _TimingItem(
                    item: item,
                  ),
              ]
            ],
          ),
        ),
      ],
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    final store = Provider.of<PrivateDetailStore>(context, listen: true);
    final colorScheme = WTheme.of(context).colorScheme;
    return WisCard(
      margin: EdgeInsets.symmetric(vertical: 5.pt, horizontal: 15.pt),
      padding: EdgeInsets.all(15.pt),
      child: Wisdom.column(
        onTap: () => store.route2TimingPage(item),
        color: item?.enable == true ? Colors.white : colorScheme.background2,
        // padding: EdgeInsets.all(15.pt),
        children: [
          Wisdom.row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Wisdom.column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.start,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        WisText(
                          '${CronUtil.cron2hourAndMinutes(item?.cronExpression)}',
                          size: 28.pt,
                          color: item?.enable == true
                              ? colorScheme.mainTitle
                              : colorScheme.disabled,
                        ),
                        SizedBox(
                          width: 20.pt,
                        ),
                        WisText(
                          '${item?.args?.behavior == 'OPEN' ? '合闸' : '拉闸'}',
                          size: 16.pt,
                          color: item?.enable == true
                              ? item?.args?.behavior == 'OPEN'
                                  ? Color(0XFF27CEAC)
                                  : Color(0XFFFF495E)
                              : item?.args?.behavior == 'OPEN'
                                  ? Colors.green.shade100
                                  : Colors.red.shade100,
                        ),
                      ],
                    ),
                    SizedBox(height: 10.pt),
                    Wrap(
                      children: [
                        WisText(
                          '周期设置: ${item.cronExpression.split(' ')[5] == '2,3,4,5,6' ? '工作日' : item.cronExpression.split(' ')[5] == '1,2,3,4,5,6,7' ? '每天' : CronUtil.cron2week(item?.cronExpression) == '' ? '未设置' : CronUtil.cron2week(item?.cronExpression)}',
                          size: 14.pt,
                          color: item?.enable == true
                              ? colorScheme.mainTitle
                              : colorScheme.disabled,
                        ),
                      ],
                    ),
                  ]).asFlexible(flex: 1),
              Switch(
                value: item?.enable ?? false,
                onChanged: (v) => {
                  store.handleDeviceOnOff(item, v),
                },
              ),
            ],
          )
        ],
      ),
    );
  }
}
