/*
 * @Author: XianKaiQun
 * @Date: 2020-08-26 15:53:21
 * @LastEditors: ChenYaJin
 * @LastEditTime: 2023-05-26 14:56:05
 * @Description: 
 */
part of '../view.dart';

class _FormComponent extends StatefulWidget {
  _FormComponent({Key? key}) : super(key: key);

  @override
  State<_FormComponent> createState() => __FormComponentState();
}

class __FormComponentState extends State<_FormComponent> {
  IndexPageStore get store => Provider.of(context, listen: false);
  @override
  void initState() {
    store.getPersonList();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final store = Provider.of<IndexPageStore>(context);
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;
    final entCode = WisStorageUtil.get(key: 'storage-user-entCode');

    return Observer(builder: (context) {
      return WForm(
        key: store.formKey,
        initialValues: store.formValue,
        child: WisScrollView(
          slivers: <Widget>[
            WisCard(
              isSliver: true,
              color: Colors.white,
              margin: EdgeInsets.symmetric(horizontal: 15.pt, vertical: 5.pt),
              child: Wisdom.column(
                color: Colors.white,
                padding: EdgeInsets.symmetric(horizontal: 15.pt),
                children: ListTile.divideTiles(
                  context: context,
                  tiles: [
                    WFormTile(
                      leading: WisText('房号'),
                      child: WInfinityPickerFormField.textField(
                        field: 'house',
                        title: WisText('选择报${store.placeStr}业主房号'),
                        getData: (List<CascadeHouseModel>? list) =>
                            store.getHouseData(list!),
                        buildName: (List<CascadeHouseModel>? list) =>
                            list != null && list.length > 0
                                ? list.last.name ?? ''
                                : '',
                        isLast: (List<CascadeHouseModel>? list) async =>
                            list != null && list.length > 0
                                ? list.last.status == 'house'
                                : false,
                        decoration: InputDecoration(
                          hintText: '选择报${store.placeStr}业主房号',
                        ),
                      ),
                    ),
                    WFormTile(
                      required: true,
                      leading: WisText('姓名'),
                      child: WTextFormField(
                        field: 'name',
                        inputFormatters: [
                          _SingleLineInputFormatter(maxLength: 32),
                        ],
                        decoration: InputDecoration(
                          hintText: '输入报${store.placeStr}业主姓名',
                        ),
                      ),
                    ),
                    WFormTile(
                      required: true,
                      leading: WisText('联系方式'),
                      child: Wisdom.row(
                        children: [
                          Wisdom.row(
                            onTap: () => store.setContactType(true),
                            children: [
                              if (store.isPhoneNumber) ...[
                                Image(
                                  image: AssetList.$radio_select_png_image,
                                  height: 14.pt,
                                ),
                              ] else ...[
                                Image(
                                  image: AssetList.$radio_unselect_png_image,
                                  height: 14.pt,
                                ),
                              ],
                              SizedBox(width: 6.pt),
                              WisText(
                                '手机号',
                                size: 14.pt,
                                color: Color(0xff222222),
                                weight: FontWeight.w300,
                              ),
                            ],
                          ),
                          SizedBox(width: 30.pt),
                          Wisdom.row(
                            onTap: () => store.setContactType(false),
                            children: [
                              if (store.isPhoneNumber == false) ...[
                                Image(
                                  image: AssetList.$radio_select_png_image,
                                  height: 14.pt,
                                ),
                              ] else ...[
                                Image(
                                  image: AssetList.$radio_unselect_png_image,
                                  height: 14.pt,
                                ),
                              ],
                              SizedBox(width: 6.pt),
                              WisText(
                                '固定电话',
                                size: 14.pt,
                                color: Color(0xff222222),
                                weight: FontWeight.w300,
                              ),
                            ],
                          ),
                        ],
                      ),
                    ),
                    WFormTile(
                      required: true,
                      leading: WisText('联系号码'),
                      child: WTextFormField(
                        keyboardType: TextInputType.phone,
                        field: 'mobile',
                        decoration: InputDecoration(
                          hintText: '输入业主联系号码',
                        ),
                      ),
                    ),
                  ],
                ).toList(),
              ),
            ),
            WisCard(
              isSliver: true,
              color: Colors.white,
              margin: EdgeInsets.symmetric(horizontal: 15.pt, vertical: 5.pt),
              child: Wisdom.column(
                color: Colors.white,
                padding: EdgeInsets.symmetric(horizontal: 15.pt),
                margin: EdgeInsets.only(bottom: 15.pt),
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: ListTile.divideTiles(
                  context: context,
                  tiles: [
                    if (store.isRepair) ...[
                      WFormTile(
                        required: true,
                        leading: WisText('提报类型'),
                        child: WSinglePickerFormField.textField(
                          field: 'repairBusinessType',
                          title: WisText('请选择业主提报类型'),
                          getData: () => store.getRepairTypeList(),
                          decoration: InputDecoration(
                            hintText: '请选择业主提报类型',
                          ),
                        ),
                      ),
                      WFormTile(
                        required: false,
                        leading: WisText('分类'),
                        child: WSinglePickerFormField.textField(
                          field: 'repairSort',
                          title: WisText('请选择业主分类'),
                          getData: () => store.getRepairSortList()
                              as Future<List<WPickerEntity>>,
                          decoration: InputDecoration(
                            hintText: '请选择业主分类',
                          ),
                        ),
                      ),
                    ] else ...[
                      WFormTile(
                        required: true,
                        leading: WisText('提报类型'),
                        child: WSinglePickerFormField.textField(
                          field: 'reportBusinessType',
                          title: WisText('请选择业主提报类型'),
                          getData: () => store.getReportTypeList(),
                          decoration: InputDecoration(
                            hintText: '请选择业主提报类型',
                          ),
                        ),
                      ),
                      WFormTile(
                        required: false,
                        leading: WisText('分类'),
                        child: WSinglePickerFormField.textField(
                          field: 'reportSort',
                          title: WisText('请选择业主分类'),
                          getData: () => store.getReportSortList()
                              as Future<List<WPickerEntity>>,
                          decoration: InputDecoration(
                            hintText: '请选择业主分类',
                          ),
                        ),
                      ),
                    ],
                    WFormTile(
                      direction: Axis.vertical,
                      required: true,
                      leading: WisText('内容描述'),
                      child: WTextFormField(
                        field: 'description',
                        maxLines: 4,
                        minLines: 4,
                        maxLength: 256,
                        decoration: InputDecoration(
                          hintText: '输入具体报${store.placeStr}内容',
                        ),
                      ),
                    ),
                    Wisdom.wrap(
                      padding: EdgeInsets.symmetric(vertical: 10.pt),
                      runSpacing: 10.pt,
                      spacing: 10.pt,
                      children: [
                        ...WImagePreview.buildList(
                          images: store.files,
                          onDelete: store.filesDelete,
                        ),
                        WImageSelecter(
                          maxCount: 3,
                          currentCount: store.files.length,
                          onSelected: (files) => store.filesChanged(files),
                        ),
                      ],
                    ),
                  ],
                ).toList(),
              ),
            ),
            WisCard(
              isSliver: true,
              color: Colors.white,
              margin: EdgeInsets.symmetric(horizontal: 15.pt, vertical: 5.pt),
              child: Wisdom.column(
                color: Colors.white,
                padding: EdgeInsets.symmetric(horizontal: 15.pt),
                margin: EdgeInsets.only(bottom: 15.pt),
                children: ListTile.divideTiles(
                  context: context,
                  tiles: [
                    if (store.isRepair) ...[
                      WFormTile(
                        required: entCode != 460102 && entCode != 460101
                            ? false
                            : true,
                        leading: WisText('内部分类'),
                        child: WSinglePickerFormField.textField(
                          field: 'repairInternalSortId',
                          title: WisText('请选择员工内部分类'),
                          getData: () => store.getRepairInnerSortList(),
                          decoration: InputDecoration(
                            hintText: '请选择员工内部分类',
                          ),
                        ),
                      ),
                    ] else ...[
                      WFormTile(
                        required: entCode != 460102 && entCode != 460101
                            ? false
                            : true,
                        leading: WisText('内部分类'),
                        child: WSinglePickerFormField.textField(
                          field: 'reportInternalSortId',
                          title: WisText('请选择员工内部分类'),
                          getData: () => store.getReportInnerSortList(),
                          decoration: InputDecoration(
                            hintText: '请选择员工内部分类',
                          ),
                        ),
                      ),
                    ],
                    Observer(builder: (_) {
                      return WFTimePicker(
                        title: WisText('预约时间'),
                        type: 'datetime',
                        hintText: '选择报${store.placeStr}预约时间',
                        initialDate: store.appointedTime,
                        firstDate: DateTime(
                          DateTime.now().year,
                          DateTime.now().month,
                          DateTime.now().day,
                          DateTime.now().hour,
                          DateTime.now().minute -
                              (DateTime.now().minute % 10) +
                              10,
                        ),
                        minuteInterval: 10,
                        onChanged: (v) => store.setTime(v),
                      );
                    }),
                    Observer(builder: (_) {
                      return WFTreePicker(
                        title: WisText(
                          '处理人',
                          size: 15.pt,
                          color: colorScheme.mainTitle,
                        ),
                        isRequired: true,
                        dataList: store.dealPersonList,
                        initialValue: store.dealPersonId,
                        isMulti: false,
                        label: 'name',
                        value: 'id',
                        child: 'child',
                        firstName: 'name',
                        firstValue: 'id',
                        hintText: '选择处理人',
                        isSearch: true,
                        onChanged: (v) => store.setDealPerson(v),
                      );
                    }),
                    Observer(builder: (_) {
                      return WFTreePicker(
                        title: WisText(
                          '抄送人',
                          size: 15.pt,
                          color: colorScheme.mainTitle,
                        ),
                        isRequired: false,
                        dataList: store.dealPersonList,
                        initialValue: store.copyPerson,
                        isMulti: true,
                        label: 'name',
                        value: 'id',
                        child: 'child',
                        firstName: 'name',
                        firstValue: 'id',
                        hintText: '选择抄送人',
                        isSearch: true,
                        onChanged: (v) => store.setCopyPerson(v),
                      );
                    }),
                    WFormTile(
                      direction: Axis.vertical,
                      leading: WisText('备注'),
                      child: WTextFormField(
                        field: 'remark',
                        maxLines: 4,
                        minLines: 4,
                        maxLength: 64,
                        decoration: InputDecoration(
                          hintText: '输入具体报${store.placeStr}内容',
                        ),
                      ),
                    ),
                  ],
                ).toList(),
              ),
            ),
          ],
        ),
      );
    });
  }
}

///单行输入（限制输入`maxLength`字）
class _SingleLineInputFormatter extends TextInputFormatter {
  _SingleLineInputFormatter({this.maxLength});
  final int? maxLength;

  @override
  TextEditingValue formatEditUpdate(_, newValue) {
    final _maxLength = maxLength ?? 64;
    TextEditingValue value = newValue;
    final length = value.text.length;
    if (length > _maxLength)
      value = value.copyWith(
        text: value.text.substring(0, _maxLength),
      );
    return value;
  }
}
