/*
 * @Author       : WuWei
 * @LastEditors: ChenYaJin
 * @Date         : 2021-04-15 11:03:45
 * @LastEditTime: 2023-04-03 17:21:10
 * @FilePath     : /wisdom-huiguanjia-app/search_package/lib/src/pages/add_resident/index.dart
 * @Description  : Do not edit
 */
import 'package:flutter/material.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:search_package/src/models/cascade_house.dart';
import 'package:wisdom_cli/wisdom_cli.dart';
import '../../view_models/add_resident_store.dart';
part 'index.g.dart';

///路由传递的参数
class AddresidentPageArguments {
  AddresidentPageArguments({this.id, this.isEdit});

  final String? id;
  final bool? isEdit;

  ///
  ///
  ///
  ///
  ///
  ///
  ///
//***以下为模板代码谨慎修改***
  factory AddresidentPageArguments.fromJson(Map<String, dynamic> json) {
    return _$AddresidentPageArgumentsFromJson(json);
  }
  factory AddresidentPageArguments.fromRouteArguments(BuildContext context) {
    return _$AddresidentPageArgumentsFromJson(
        (ModalRoute.of(context)?.settings.arguments ??
            new Map<String, dynamic>()) as Map<String, dynamic>);
  }
  Map<String, dynamic> toJson() => _$AddresidentPageArgumentsToJson(this);
}

///
///
///
///
///
//***以下为模板代码谨慎修改***
class AddresidentPage extends StatelessWidget {
  const AddresidentPage({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_) => AddResidentStore(context)),
      ],
      child: AddresidentPageView(),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    final colorScheme = WTheme.of(context).colorScheme;
    final store = Provider.of<AddResidentStore>(context, listen: false);
    return Scaffold(
      appBar: WAppBar(
        iconTheme: IconThemeData(
          color: colorScheme.mainTitle,
          size: 19.pt,
        ),
        backgroundColor: Colors.white,
        brightness: Brightness.light,
        title: WisText('${store.isEdit == true ? '编辑住户' : '添加住户'}',
            size: 19.pt, weight: FontWeight.w600, color: colorScheme.mainTitle),
      ),
      body: WisScrollView(
        onInit: () => store.getResidentDetail(),
        slivers: [
          SliverToBoxAdapter(
            child: Observer(builder: (_) {
              return Column(
                children: [
                  if (store.isEdit != true || store.detail != null) ...[
                    _BuildFormRequired(),
                    _BuildFormNotRequired(),
                  ],
                ],
              );
            }),
          ),
        ],
      ),
      bottomSheet: _BuildBottomBar(),
    );
  }
}

/// 底部按钮
class _BuildBottomBar extends StatelessWidget {
  const _BuildBottomBar({Key? key}) : super(key: key);

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

    return BottomAppBar(
      child: Wisdom.row(
        height: 60.pt,
        crossAxisAlignment: CrossAxisAlignment.stretch,
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        padding: EdgeInsets.only(left: 10.pt),
        children: <Widget>[
          Wisdom(
            onTap: () => store.onAddResident(),
            minWidth: 100.pt,
            alignment: Alignment.center,
            child: Container(
              height: 44.pt,
              margin: EdgeInsets.only(right: 15.pt),
              decoration: BoxDecoration(
                gradient: LinearGradient(
                  colors: [
                    Color.fromRGBO(77, 128, 255, 1),
                    Color.fromRGBO(15, 103, 248, 1)
                  ],
                  begin: Alignment.centerLeft,
                  end: Alignment.centerRight,
                ),
                borderRadius: BorderRadius.circular(25.pt),
              ),
              child: Center(
                child: Text(
                  '确认${store.isEdit ? '编辑' : '添加'}',
                  style: TextStyle(
                    fontSize: 14.pt,
                    color: Colors.white,
                  ),
                ),
              ),
            ),
          ).asExpanded(),
        ],
      ),
    );
  }
}

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

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

    return WisCard(
      color: Colors.white,
      margin: EdgeInsets.symmetric(vertical: 10.pt, horizontal: 15.pt),
      elevation: 0,
      child: WForm(
        key: store.formKey,
        initialValues: store.formValue,
        child: Wisdom.column(
          color: Colors.white,
          padding: EdgeInsets.symmetric(horizontal: 15.pt),
          margin: EdgeInsets.only(bottom: 10.pt),
          children: [
            if (store.isEdit == true) ...[
              Wisdom.row(mainAxisAlignment: MainAxisAlignment.start, children: [
                Container(
                  width: 100.pt,
                  height: 44.pt,
                  alignment: Alignment.centerLeft,
                  child: Text.rich(
                    TextSpan(
                      children: [
                        WidgetSpan(
                          child: SizedBox(
                              width: 10.pt,
                              child: Text(
                                '*',
                                style: TextStyle(
                                  color: colorScheme.danger,
                                  fontSize: 16.pt,
                                  fontWeight: FontWeight.w300,
                                ),
                              )),
                        ),
                        WidgetSpan(
                          child: WisText(
                            '房号',
                            color: colorScheme.mainTitle,
                            size: 15.pt,
                            weight: FontWeight.w300,
                          ),
                        ),
                      ],
                    ),
                    style: TextStyle(
                      color: colorScheme.mainTitle,
                      fontSize: 15.pt,
                      fontWeight: FontWeight.w300,
                    ),
                  ),
                ),
                SizedBox(width: 12.pt),
                Observer(builder: (_) {
                  return WisText(
                    '${store.detail?.roomNo ?? '--'}',
                    color: colorScheme.mainTitle,
                    size: 15.pt,
                    weight: FontWeight.w600,
                  );
                })
              ])
            ] else ...[
              WFormTile(
                minWidth: 100.pt,
                required: true,
                leading: WisText(
                  '房号',
                  color: colorScheme.mainTitle,
                  size: 15.pt,
                  weight: FontWeight.w300,
                ),
                child: WInfinityPickerFormField.textField(
                  field: 'house',
                  title: WisText(
                    '选择住户房号',
                    color: colorScheme.mainTitle,
                    size: 15.pt,
                    weight: FontWeight.w300,
                  ),
                  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!.last.status == 'house',
                  decoration: InputDecoration(
                    fillColor: Colors.white,
                    hintText: '选择住户房号',
                    hintStyle: TextStyle(
                      fontSize: 14.pt,
                      color: colorScheme.placeholder,
                      fontWeight: FontWeight.w300,
                    ),
                  ),
                ),
              ),
            ],
            Observer(builder: (context) {
              return WFInput(
                isRequired: true,
                title: WisText(
                  '姓名',
                  color: colorScheme.mainTitle,
                  size: 15.pt,
                  weight: FontWeight.w300,
                ),
                hintText: "输入姓名",
                type: InputType.singleLine,
                initialValue: store.name ?? null,
                maxLength: 32,
                onInput: (v) => store.onSetName(v),
              );
            }),
            Observer(builder: (context) {
              return WFInput(
                isRequired: true,
                initialValue: store.formValue['mobile'] ?? null,
                type: store.formValue['identity'] == 'enterprise'
                    ? InputType.tel
                    : InputType.phoneNumber,
                title: WisText(
                  '手机号',
                  weight: FontWeight.w300,
                  color: colorScheme.mainTitle,
                  size: 15.pt,
                ),
                hintText: "输入11位手机号",
                onInput: (v) => store.onSetMobile(v),
              );
            }),
            Observer(builder: (_) {
              return WFRadioPicker(
                isRequired: true,
                title: WisText(
                  '性别',
                  color: colorScheme.mainTitle,
                  size: 15.pt,
                  weight: FontWeight.w300,
                ),
                initialValue: store.formValue['sex'] ?? null,
                onChanged: (v) => store.onSetSex(v),
                dataList: [
                  {"value": "male", "label": "男"},
                  {"value": "female", "label": "女"},
                ],
              );
            }),
            Observer(builder: (_) {
              return WFSinglePicker(
                isRequired: true,
                dataList: [
                  {'label': "独立产权人", "value": "selfOwner"},
                  {'label': "共有产权人", "value": "jointOwner"},
                  {'label': "租户", "value": "tenant"},
                  {'label': "亲属", "value": "relatives"},
                  {'label': "企事业单位", "value": "enterprise"},
                ],
                title: WisText(
                  '住户性质',
                  color: colorScheme.mainTitle,
                  size: 15.pt,
                  weight: FontWeight.w300,
                ),
                hintText: "选择住户性质",
                onChanged: (v) => store.onSetProperty(v),
                initialValue: store.formValue['identity'] ?? null,
              );
            }),
            Observer(builder: (_) {
              return WFRadioPicker(
                isRequired: true,
                title: WisText(
                  '账单权属人',
                  color: colorScheme.mainTitle,
                  size: 15.pt,
                  weight: FontWeight.w300,
                ),
                onChanged: (v) => store.onSetPeople(v),
                dataList: [
                  {"value": true, "label": "是"},
                  {"value": false, "label": "否"},
                ],
                initialValue: store.formValue['billOwner'] ?? null,
              );
            }),
          ],
        ),
      ),
    );
  }
}

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

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

    return WisCard(
      color: Colors.white,
      margin: EdgeInsets.fromLTRB(15.pt, 0, 15.pt, 15.pt),
      elevation: 0,
      child: Wisdom.column(
        color: Colors.white,
        padding: EdgeInsets.symmetric(horizontal: 15.pt),
        margin: EdgeInsets.only(bottom: 88.pt),
        children: [
          Observer(builder: (_) {
            return WFSinglePicker(
              title: WisText(
                '证件类型',
                color: colorScheme.mainTitle,
                size: 15.pt,
                weight: FontWeight.w300,
              ),
              dataList: [
                {'label': "身份证", "value": "IdCard"},
                {'label': "港澳通行证", "value": "HKAndMacaoPass"},
                {'label': "军官证", "value": "officerCertificate"},
                {'label': "其他", "value": "other"},
              ],
              hintText: "选择证件类型",
              onChanged: (v) => store.onSetCertType(v),
              initialValue: store.formValue['certificateType'] ?? null,
            );
          }),
          Observer(builder: (_) {
            return WFInput(
              isRequired:
                  store.formValue['certificateType'] != null ? true : false,
              title: WisText(
                '证件号码',
                color: colorScheme.mainTitle,
                size: 15.pt,
                weight: FontWeight.w300,
              ),
              hintText: "输入证件号码",
              onInput: (v) => store.onSetCertNum(v),
              initialValue: store.formValue['idNumber'] ?? null,
              maxLength: 32,
            );
          }),
          Observer(builder: (_) {
            return WFDatePicker(
              title: WisText(
                '居住起始日期',
                color: colorScheme.mainTitle,
                size: 15.pt,
                weight: FontWeight.w300,
              ),
              hintText: "选择居住起始日期",
              onChanged: (v) => store.onSetStarDate(v),
              lastDate: store.residenceEndAt,
              initialDateTime: store.formValue['residenceStartAt'] ?? null,
            );
          }),
          Observer(builder: (_) {
            return WFDatePicker(
              title: WisText(
                '居住结束日期',
                color: colorScheme.mainTitle,
                size: 15.pt,
                weight: FontWeight.w300,
              ),
              hintText: "选择居住结束日期，不填为长期",
              firstDate: store.residenceStartAt,
              onChanged: (v) => store.onSetEndDate(v),
              initialDateTime: store.formValue['residenceEndAt'] ?? null,
            );
          }),
          Observer(builder: (_) {
            return WFInput(
              title: WisText(
                '其他号码',
                color: colorScheme.mainTitle,
                size: 15.pt,
                weight: FontWeight.w300,
              ),
              maxLength: 64,
              hintText: "输入其他联系电话号码，支持多个，用逗号分隔",
              onInput: (v) => store.onSetOtherNum(v),
              initialValue: store.formValue['otherMobile'] ?? null,
            );
          }),
          Observer(builder: (_) {
            return WFInput(
              maxLength: 64,
              title: WisText(
                '常住地址',
                color: colorScheme.mainTitle,
                size: 15.pt,
                weight: FontWeight.w300,
              ),
              hintText: "输入住户常住联系地址",
              onInput: (v) => store.onSetAddress(v),
              initialValue: store.formValue['address'] ?? null,
            );
          }),
          Observer(builder: (_) {
            return WFInput(
              maxLength: 64,
              title: WisText(
                '备注',
                color: colorScheme.mainTitle,
                size: 15.pt,
                weight: FontWeight.w300,
              ),
              hintText: "输入其他备注说明",
              onInput: (v) => store.onSetRemark(v),
              initialValue: store.formValue['remark'] ?? null,
            );
          }),
        ],
      ),
    );
  }
}
