/*
 * @Author: XianKaiQun
 * @Date: 2020-08-17 16:44:49
 * @LastEditors: XianKaiQun
 * @LastEditTime: 2020-08-18 08:56:55
 * @Description: 
 */
part of '../index.dart';

class _FormComponent extends StatelessWidget {
  const _FormComponent({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    final store = Provider.of<PrivateRegisterFormStore>(context);
    final colorScheme = WTheme.of(context).colorScheme;
    final params = store.params;
    return SliverToBoxAdapter(
      child: Wisdom.column(
        padding: EdgeInsets.all(15.pt),
        children: [
          FormItemComponent(
            leadingText: '所属小区',
            child: TextFormField(
              readOnly: true,
              initialValue: params['communityName'],
            ),
          ),
          FormItemComponent(
            leadingText: '二维码编号',
            child: TextFormField(
              readOnly: true,
              initialValue: params['qrNumber'],
            ),
          ),
          FormItemComponent(
            leadingText: '二维码类型',
            child: TextFormField(
              readOnly: true,
              initialValue: store.type == 'clean'
                  ? '保洁清洁'
                  : store.type == 'security'
                      ? '安保巡逻'
                      : '设备设施',
            ),
          ),
          if (store.type == 'implement') ...[
            FormItemComponent(
              must: true,
              leadingText: '设备编号',
              child: TextFormField(
                initialValue: params['number'],
                onChanged: (value) => store.onParamsChange('number', value),
                decoration: InputDecoration(
                  hintText: '输入小区唯一编号',
                ),
              ),
            ),
          ],
          FormItemComponent(
            must: true,
            leadingText: store.type == 'implement' ? '设备名称' : '位置名称',
            child: TextFormField(
              initialValue: params['name'],
              onChanged: (value) => store.onParamsChange('name', value),
              decoration: InputDecoration(
                hintText: store.type == 'implement' ? '输入设备名称' : '输入位置名称',
              ),
            ),
          ),
          if (store.type == 'implement') ...[
            FormItemComponent(
              must: true,
              leadingText: '设备状态',
              child: TextFormField(
                controller: TextEditingController(text: params['statusName']),
                readOnly: true,
                onTap: () => store.statusPicker(),
                decoration: InputDecoration(
                  hintText: '选择设备状态',
                  suffixIcon: Icon(
                    Icons.chevron_right,
                  ),
                ),
              ),
            ),
          ],
          WForm(
            key: store.formKey,
            child: Column(children: [
              WFormTile(
                required: true,
                leading: WisText(
                  '所属位置',
                  size: 15.pt,
                ),
                child: WInfinityPickerFormField.textField(
                  field: 'position',
                  title: Text(store.type == 'implement' ? '选择设备位置' : '选择所属位置'),
                  getData: (List<PlaceModel>? list) =>
                      store.getPlaceData(list!) as Future<List<PlaceModel>>,
                  buildName: (List<PlaceModel>? list) =>
                      list != null && list.length > 0
                          ? list.last.name ?? ''
                          : '',
                  isLast: (List<PlaceModel>? items) async {
                    if (store.type == 'implement') {
                      return items?.last.status == null ||
                          items?.last.status == 'house' ||
                          items?.last.status == "parent_other";
                    } else {
                      return items?.last.status == 'other' ||
                          items?.last.status == 'parKingLot' ||
                          items?.last.status == 'unit';
                    }
                  },
                  decoration: InputDecoration(
                    hintText: '选择所属位置',
                    hintStyle: TextStyle(
                      fontSize: 14.pt,
                      color: colorScheme.placeholder,
                    ),
                  ),
                ),
              ),
              if (store.type == 'implement') ...[
                WFormTile(
                  leading: WisText(
                    '设备品类',
                    size: 15.pt,
                  ),
                  child: WInfinityPickerFormField.textField(
                    field: 'catagory',
                    title: Text('选择设备品类'),
                    getData: (List<NameIdModel>? list) =>
                        store.getCatagoryList(list!),
                    buildName: (List<NameIdModel>? list) => list != null &&
                            list.length > 0
                        ? list
                            .map(
                                (item) => item.name! != '不选择' ? item.name! : '')
                            .join()
                        : '',
                    isLast: (items) async {
                      return items.length == 2;
                    },
                    decoration: InputDecoration(
                      hintText: '选择设备品类',
                      hintStyle: TextStyle(
                        fontSize: 14.pt,
                        color: colorScheme.placeholder,
                      ),
                    ),
                  ),
                ),
                WFormTile(
                  minWidth: 85.pt,
                  leading: Text('负责人'),
                  child: WSinglePickerFormField.textField(
                    field: 'person',
                    getData: () =>
                        store.getPersonList() as Future<List<WPickerEntity>>,
                    onChanged: (v) => store.choosePerson(v),
                    title: Text('选择负责人'),
                    decoration: InputDecoration(
                      hintText: '选择负责人',
                      hintStyle: TextStyle(
                        fontSize: 14.pt,
                        color: colorScheme.placeholder,
                      ),
                    ),
                  ),
                ),
                WFormTile(
                  minWidth: 85.pt,
                  leading: Text('出厂年月'),
                  child: WMonthPickerFormField.textField(
                    field: 'month',
                    buildName: (v) =>
                        v != null ? DateFormat('yyyy-MM').format(v) : '',
                    onChanged: (v) => store.chooseMonth(v),
                    decoration: InputDecoration(
                      hintText: '选择出厂年月',
                      suffixIcon: Icon(
                        Icons.chevron_right,
                      ),
                    ),
                  ),
                ),
                WFormTile(
                  minWidth: 85.pt,
                  leading: Text('出厂编号'),
                  child: WTextFormField(
                    field: 'productNumber',
                    decoration: InputDecoration(
                      hintText: '输入出厂编号',
                    ),
                  ),
                ),
                WFormTile(
                  minWidth: 85.pt,
                  leading: Text('安装日期'),
                  child: WDatePickerFormField.textField(
                    buildName: (v) =>
                        v != null ? DateFormat('yyyy-MM-dd').format(v) : '',
                    field: 'installDate',
                    decoration: InputDecoration(
                      hintText: '选择安装日期',
                      suffixIcon: Icon(
                        Icons.chevron_right,
                      ),
                    ),
                  ),
                ),
                WFormTile(
                  minWidth: 85.pt,
                  leading: Text('使用年限'),
                  child: Wisdom.row(
                    children: [
                      Wisdom(
                        child: WTextFormField(
                          field: 'uyear',
                          textAlign: TextAlign.end,
                          keyboardType: TextInputType.number,
                          maxLength: 4,
                          showClose: false,
                          decoration: InputDecoration(
                            counterText: '',
                          ),
                        ),
                      ).asFlexible(flex: 1),
                      Wisdom(
                        margin: EdgeInsets.symmetric(horizontal: 5.pt),
                        child: Text('年'),
                      ),
                      Wisdom(
                        child: WTextFormField(
                          field: 'umonth',
                          textAlign: TextAlign.end,
                          keyboardType: TextInputType.number,
                          maxLength: 2,
                          showClose: false,
                          decoration: InputDecoration(
                            counterText: '',
                          ),
                        ),
                      ).asFlexible(flex: 1),
                      Wisdom(
                        margin: EdgeInsets.symmetric(horizontal: 5.pt),
                        child: Text('月'),
                      ),
                      Wisdom(
                        child: WTextFormField(
                          field: 'uday',
                          textAlign: TextAlign.end,
                          keyboardType: TextInputType.number,
                          maxLength: 2,
                          showClose: false,
                          decoration: InputDecoration(
                            counterText: '',
                          ),
                        ),
                      ).asFlexible(flex: 1),
                      Wisdom(
                        margin: EdgeInsets.symmetric(horizontal: 5.pt),
                        child: Text('日'),
                      ),
                    ],
                  ),
                ),
                WFormTile(
                  minWidth: 85.pt,
                  leading: Text('巡检规范'),
                  child: Wisdom.row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    color: colorScheme.background2,
                    padding: EdgeInsets.only(
                      left: 12.pt,
                      bottom: 12.pt,
                      top: 12.pt,
                    ),
                    onTap: () => store.pollingNormPicker(),
                    children: [
                      WisText(
                        '${store.routinginspectionId?.name ?? '选择巡检规范'}',
                        color: store.routinginspectionId != null
                            ? colorScheme.mainTitle
                            : colorScheme.grey,
                        size: 15.pt,
                      ),
                      Icon(
                        Icons.chevron_right,
                        size: 15.pt,
                        color: colorScheme.grey,
                      ),
                    ],
                  ),
                ),
                WFormTile(
                  minWidth: 85.pt,
                  leading: Text('保养规范'),
                  child: Wisdom.row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    color: colorScheme.background2,
                    padding: EdgeInsets.only(
                      left: 12.pt,
                      bottom: 12.pt,
                      top: 12.pt,
                    ),
                    onTap: () => store.upkeepNormPicker(),
                    children: [
                      WisText(
                        '${store.maintenancecodeId != null ? store.maintenancecodeId?.name : '选择保养规范'}',
                        color: store.maintenancecodeId != null
                            ? colorScheme.mainTitle
                            : colorScheme.grey,
                        size: 15.pt,
                      ),
                      Icon(
                        Icons.chevron_right,
                        size: 15.pt,
                        color: colorScheme.grey,
                      ),
                    ],
                  ),
                ),
              ],
            ]),
          ),
        ],
      ),
    );
  }
}
