/*
 * @Author       : WuWei
 * @LastEditors: wjc
 * @Date         : 2021-09-26 16:05:03
 * @LastEditTime: 2023-01-17 10:06:27
 * @Description  : Do not edit
 */
part of '../index.dart';

/// 作业标准表单
class _BuildStandard extends StatefulWidget {
  _BuildStandard({Key? key}) : super(key: key);

  @override
  __BuildStandardState createState() => __BuildStandardState();
}

class __BuildStandardState extends State<_BuildStandard> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final colorScheme = WTheme.of(context).colorScheme;
    return Wisdom.column(
      color: Colors.white,
      padding: EdgeInsets.all(15.pt),
      margin: EdgeInsets.only(bottom: 10.pt, right: 15.pt, left: 15.pt),
      borderRadius: BorderRadius.circular(10.pt),
      boxShadow: [
        BoxShadow(
          offset: Offset(0, 2.pt),
          color: Color(0x08000000),
          blurRadius: 10.pt,
          spreadRadius: 2.5.pt,
        ),
      ],
      children: [
        Wisdom.row(
          margin: EdgeInsets.only(
            bottom: 10.pt,
          ),
          children: <Widget>[
            WisText(
              '作业标准',
              color: colorScheme.mainTitle,
              size: 16.pt,
              bold: true,
            ),
          ],
        ),
        _BuildStandardForm(),
      ],
    );
  }
}

/// 表单内容
class _BuildStandardForm extends StatelessWidget {
  const _BuildStandardForm({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final store = Provider.of<PointFormStore>(context, listen: true);

    return Observer(builder: (context) {
      return Column(
        children: [
          for (num i = 0; i < store.formList.length; i++)
            __FormItem(
              item: store.formList[i as int],
              index: i,
            ),
        ],
      );
    });
  }
}

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

  @override
  Widget build(BuildContext context) {
    final store = Provider.of<PointFormStore>(context, listen: true);
    final colorScheme = WTheme.of(context).colorScheme;

    ///作业标准表单类型
    final formTypes = {
      'radio': '单选',
      'checkbox': '多选',
      'text': '简答',
      'img': '图片',
      'exception': '异常项'
    };

    /// 多行文本
    if (item?.type == 'text' &&
        item?.workStandardExtends[0]['inputRestrictionType'] != 'number')
      return Observer(builder: (context) {
        return WFInput(
          isRequired: item?.required,
          title: Wisdom.row(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Wisdom(
                color: colorScheme.primary,
                padding: EdgeInsets.symmetric(horizontal: 5.pt, vertical: 2.pt),
                margin: EdgeInsets.only(right: 5.pt),
                borderRadius: BorderRadius.circular(5.pt),
                child: WisText(
                  '${formTypes[item.type]}',
                  size: 11.pt,
                  weight: FontWeight.bold,
                  color: Colors.white,
                ),
              ),
              WisText(
                '${item?.title}',
                size: 15.pt,
                weight: FontWeight.w600,
                color: colorScheme.mainTitle,
              ).asFlexible(flex: 1),
            ],
          ),
          hintText: item?.hint ?? '',
          type: InputType.multiLine,
          maxLength: item?.workStandardExtends[0]['mostWordCount'] ?? 64,
          initialValue: store.formValueList[index]['workStandardRecord'] != null
              ? store.formValueList[index]['workStandardRecord']
              : null,
          onInput: (v) => {store.onSetFormPrs(index, v, false)},
        );
      });

    /// 数字
    if (item?.type == 'text' &&
        item?.workStandardExtends[0]['inputRestrictionType'] == 'number')
      return Observer(builder: (context) {
        return WFInputNumber(
          isRequired: item?.required,
          axis: Axis.vertical,
          title: Wisdom.row(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Wisdom(
                color: colorScheme.primary,
                padding: EdgeInsets.symmetric(horizontal: 5.pt, vertical: 2.pt),
                margin: EdgeInsets.only(right: 5.pt),
                borderRadius: BorderRadius.circular(5.pt),
                child: WisText(
                  '${formTypes[item.type]}',
                  size: 11.pt,
                  weight: FontWeight.w600,
                  color: Colors.white,
                ),
              ),
              WisText(
                '${item?.title}',
                size: 15.pt,
                weight: FontWeight.w600,
                color: colorScheme.mainTitle,
              ),
            ],
          ),
          hintText: item?.hint ?? '',
          type: InputType.number,
          isInt: true,
          maxLength: item?.workStandardExtends[0]['mostWordCount'] ?? 64,
          initialValue: store.formValueList[index]['workStandardRecord'] != null
              ? store.formValueList[index]['workStandardRecord']
              : null,
          onInput: (v) => {
            store.onSetFormPrs(
              index,
              v,
              store.onCheckInputErr(item, v),
            )
          },
          // validator: 
        );
      });

    /// 上传
    if (item?.type == 'img')
      return Observer(builder: (context) {
        return WFImageLocalPicker(
          maxCount: item?.workStandardExtends[0]['mostUploadCount'] ?? 9,
          currentCount:
              store.formValueList[index]['workStandardRecord'] != null &&
                      store.formValueList[index]['workStandardRecord'].length !=
                          null
                  ? store.formValueList[index]['workStandardRecord'].length
                  : 0,
          onDelete: (i) => store.onImageRemoveAt(index, i),
          onSelected: (images) => store.onSelectedImg(images, index),
          // images: store.formValueList[index]['workStandardRecord'] != null &&
          //         store.formValueList[index]['workStandardRecord'].length !=
          //             null
          //     ? store.formValueList[index]['workStandardRecord']
          //     : null,
          isRequired: item?.required,
          source: item?.workStandardExtends[0]['manOpen'] == true
              ? WImageSource.cameraAndGallery
              : WImageSource.camera,
          title: Wisdom.row(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Wisdom(
                color: colorScheme.primary,
                padding: EdgeInsets.symmetric(horizontal: 5.pt, vertical: 2.pt),
                margin: EdgeInsets.only(right: 5.pt),
                borderRadius: BorderRadius.circular(5.pt),
                child: WisText(
                  '${formTypes[item.type]}',
                  size: 11.pt,
                  weight: FontWeight.w600,
                  color: Colors.white,
                ),
              ),
              WisText(
                '${item?.title}',
                size: 15.pt,
                weight: FontWeight.w600,
                color: colorScheme.mainTitle,
              ).asFlexible(flex: 1),
            ],
          ),
        );
      });

    ///单选框
    if (item?.type == 'radio')
      return Observer(builder: (context) {
        return Column(
          children: [
            Observer(builder: (context) {
              return WFRadioPicker(
                axis: Axis.vertical,
                isRequired: item?.required ?? false,
                title: Wisdom.row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Wisdom(
                      color: colorScheme.primary,
                      padding: EdgeInsets.symmetric(
                          horizontal: 5.pt, vertical: 2.pt),
                      margin: EdgeInsets.only(right: 5.pt),
                      borderRadius: BorderRadius.circular(5.pt),
                      child: WisText(
                        '${formTypes[item.type]}',
                        size: 11.pt,
                        weight: FontWeight.w600,
                        color: Colors.white,
                      ),
                    ),
                    WisText(
                      '${item?.title}',
                      size: 15.pt,
                      weight: FontWeight.w600,
                      color: colorScheme.mainTitle,
                    ).asFlexible(flex: 1),
                  ],
                ),
                hintText: '请选择${item?.title}',
                initialValue:
                    store.formValueList[index]['workStandardRecord'] ?? null,
                label: 'content',
                value: 'id',
                dataList: item != null && item.workStandardExtends != null
                    ? item.workStandardExtends
                    : [],
                onChanged: (v) => {
                  store.onSetFormPrs(
                    index,
                    v,
                    item?.workStandardExtends[item?.workStandardExtends
                            ?.indexWhere(
                                (extendsItem) => extendsItem['id'] == v)]
                        ['exceptionItem'],
                  ),
                },
              );
            }),
            if (item?.illustrate == true) ...[
              Observer(builder: (context) {
                return Wisdom(
                  border: Border.all(
                    color: Color(0xfff0f0f0),
                    width: 0.5,
                  ),
                  borderRadius: BorderRadius.circular(12.pt),
                  padding: EdgeInsets.all(10.pt),
                  margin: EdgeInsets.only(bottom: 15.pt),
                  child: TextFormField(
                    onChanged: (v) => store.onSetFormPrsRemark(index, v),
                    maxLines: 1,
                    maxLength: 64,
                    decoration: InputDecoration(
                      hintText: '输入异常项说明，64字以内，非必填',
                      contentPadding: EdgeInsets.all(10.pt),
                      fillColor: Colors.white,
                      filled: true,
                      counterText: '',
                      hintStyle: TextStyle(
                        fontSize: 13.pt,
                      ),
                    ),
                  ),
                );
              }),
            ]
          ],
        );
      });
    // 多选框
    if (item?.type == 'checkbox')
      return Column(
        children: [
          Observer(builder: (context) {
            return WFCheckBoxPicker(
              isRequired: item?.required ?? false,
              title: Wisdom.row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Wisdom(
                    color: colorScheme.primary,
                    padding:
                        EdgeInsets.symmetric(horizontal: 5.pt, vertical: 2.pt),
                    margin: EdgeInsets.only(right: 5.pt),
                    borderRadius: BorderRadius.circular(5.pt),
                    child: WisText(
                      '${formTypes[item.type]}',
                      size: 11.pt,
                      weight: FontWeight.w600,
                      color: Colors.white,
                    ),
                  ),
                  WisText(
                    '${item?.title}',
                    size: 15.pt,
                    weight: FontWeight.w600,
                    color: colorScheme.mainTitle,
                  ).asFlexible(flex: 1),
                ],
              ),
              hintText: '请选择${item?.title}',
              label: 'content',
              value: 'id',
              dataList: item != null && item.workStandardExtends != null
                  ? item.workStandardExtends
                  : [],
              initialValue:
                  store.formValueList[index]['workStandardRecord'] ?? [],
              onChanged: (v) => {
                store.onSetFormPrs(
                  index,
                  v,

                  /// 单独校验多选异常项
                  store.onCheckErr(item.workStandardExtends, v),
                )
              },
            );
          }),
          if (item?.illustrate == true) ...[
            Observer(builder: (context) {
              return Wisdom(
                border: Border.all(
                  color: Color(0xfff0f0f0),
                  width: 0.5,
                ),
                borderRadius: BorderRadius.circular(12.pt),
                padding: EdgeInsets.all(10.pt),
                margin: EdgeInsets.only(bottom: 15.pt),
                child: TextFormField(
                  onChanged: (v) => store.onSetFormPrsRemark(index, v),
                  maxLines: 1,
                  maxLength: 64,
                  decoration: InputDecoration(
                    hintText: '输入异常项说明，64字以内，非必填',
                    contentPadding: EdgeInsets.all(10.pt),
                    fillColor: Colors.white,
                    filled: true,
                    counterText: '',
                    hintStyle: TextStyle(
                      fontSize: 13.pt,
                    ),
                  ),
                ),
              );
            }),
          ]
        ],
      );
    else
      return Container(
        width: 0.0,
        height: 0.0,
      );
  }
}
