/*
 * @Author: XianKaiQun
 * @Date: 2020-07-30 09:18:54
 * @LastEditors: XianKaiQun
 * @LastEditTime: 2020-09-21 08:59:07
 * @Description:
 */
part of '../index.dart';

class _FormComponent extends StatelessWidget {
  const _FormComponent({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    final PrivateSecurityStore store = Provider.of(context);
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;
    final theme = Theme.of(context);
    final textTheme = theme.textTheme;

    final checkboxs = store.checkboxs;
    return WisCard(
      isSliver: true,
      margin: EdgeInsets.all(15.pt),
      padding: EdgeInsets.all(15.pt),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: <Widget>[
          if (checkboxs.length != 0)
            Text(
              '巡逻清单(多选)',
              style: textTheme.titleMedium!.copyWith(
                fontWeight: FontWeight.bold,
              ),
            ),
          if (checkboxs.length != 0) SizedBox(height: 15.pt),
          ...ListTile.divideTiles(
            context: context,
            tiles: [
              for (var item in checkboxs)
                CheckboxListTile(
                  value: item['complete'] == true,
                  dense: true,
                  activeColor: colorScheme.primary,
                  controlAffinity: ListTileControlAffinity.leading,
                  onChanged: (v) => store.onCheckboxchange(item['item'], v),
                  contentPadding: EdgeInsets.zero,
                  title: WisText(item['item'] ?? ''),
                ),
            ],
          ).toList(),
          if (checkboxs.length != 0) SizedBox(height: 15.pt),
          Text(
            '工作内容',
            style: textTheme.titleMedium!.copyWith(
              fontWeight: FontWeight.bold,
            ),
          ),
          TextFormField(
            minLines: 4,
            maxLines: 4,
            maxLength: 200,
            onChanged: store.remarkChange,
            decoration: InputDecoration(
              hintText: '请输入作业说明',
            ),
          ).asPaddingSymmetric(vertical: 15.pt),
          Text(
            '异常备注',
            style: textTheme.titleMedium!.copyWith(
              fontWeight: FontWeight.bold,
            ),
          ),
          TextFormField(
            minLines: 4,
            maxLines: 4,
            maxLength: 200,
            onChanged: store.exRemarkChange,
            decoration: InputDecoration(
              hintText: '请输入其他异常',
            ),
          ).asPaddingSymmetric(vertical: 15.pt),
          Wisdom.rich(
            children: [
              if (store.cheatPhoto)
                TextSpan(
                  text: '*',
                  style: TextStyle(color: colorScheme.danger),
                ),
              TextSpan(
                text: '现场照片',
                style: textTheme.titleMedium!.copyWith(
                  fontWeight: FontWeight.bold,
                ),
              ),
            ],
          ),
          SizedBox(height: 15.pt),
          Wrap(
            runSpacing: 10.pt,
            spacing: 10.pt,
            children: <Widget>[
              for (var i = 0; i < store.imageIds.length; i++)
                () {
                  final imageId = store.imageIds[i];
                  final url = AppParseUtil.image(imageId);
                  final photoList = store.imageIds
                      .map((id) =>
                          CachedNetworkImageProvider(AppParseUtil.image(id)))
                      .toList();
                  return Stack(
                    children: <Widget>[
                      Wisdom(
                        margin: EdgeInsets.only(right: 8.pt, top: 8.pt),
                        onTap: () => WisPhotoViewUtil.show(photoList, i),
                        child: WImage.network(
                          url,
                          fit: BoxFit.cover,
                          width: 80.pt,
                          height: 80.pt,
                        ),
                      ),
                      Positioned(
                        top: 0,
                        right: 0,
                        child: WDot.delete(
                          onTap: () => store.removeimageId(i),
                        ),
                      ),
                    ],
                  );
                }(),
              if (store.imageIds.length < store.cheatPhotoMaxCount!)
                WLoadingWrapper(
                  action: () => store.photograph(),
                  builder: (context, action, child) {
                    return Wisdom(
                      margin: EdgeInsets.only(right: 8.pt, top: 8.pt),
                      onTap: action,
                      color: colorScheme.background,
                      width: 80.pt,
                      height: 80.pt,
                      alignment: Alignment.center,
                      child: child,
                    );
                  },
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Wisdom(
                        child: Icon(Icons.camera_alt),
                      ),
                      Text(
                        '${store.imageIds.length}/${store.cheatPhotoMaxCount}',
                        style: TextStyle(
                          color: Colors.grey,
                          fontSize: 10.pt,
                        ),
                      )
                    ],
                  ),
                ),
            ],
          ),
        ],
      ),
    );
  }
}
