/*
 * @Author: XianKaiQun
 * @Date: 2020-08-17 17:01:06
 * @LastEditors  : WuWei
 * @LastEditTime : 2023-11-08 10:26:21
 * @Description: 
 */
part of '../index.dart';

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

    final receiptTickets =
        store.tickets!.where((e) => e['ticketType'] == 'receipt').toList();

    final invoiceTickets =
        store.tickets!.where((e) => e['ticketType'] == 'invoice').toList();
    // final theme = Theme.of(context);
    // final textTheme = theme.textTheme;

    return SliverToBoxAdapter(
        child: Stack(
      children: [
        Positioned(
          top: 0,
          left: -1000,
          right: 0,
          child: RepaintBoundary(
            key: store.wmglkey,
            child: new Wisdom.column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  WisText(
                    DateTime.now().toLocal().toFormat(),
                    color: Colors.white,
                    size: 50.pt,
                  ),
                  // WisText(
                  //   store.currentAddress ?? '',
                  //   color: Colors.white,
                  //   size: 50.pt,
                  // ),
                ]),
          ),
        ),
        WisCard(
          margin: EdgeInsets.only(
            bottom: 10.pt,
            left: 15.pt,
            right: 15.pt,
          ),
          child: Wisdom.column(
            padding: EdgeInsets.fromLTRB(0, 15.pt, 15.pt, 15.pt),
            children: <Widget>[
              FormItemComponent(
                leadingText: '缴费渠道',
                must: true,
                child: TextFormField(
                  controller: TextEditingController(text: store.payType?.name),
                  readOnly: true,
                  onTap: () => store.payTypePicker(),
                  decoration: InputDecoration(
                    filled: true,
                    fillColor: colorScheme.background4,
                    hintText: '选择缴费渠道',
                    suffixIcon: Icon(
                      Icons.arrow_right_rounded,
                      color: colorScheme.placeholder2,
                      size: 30.pt,
                    ),
                    suffixIconConstraints: BoxConstraints(),
                  ),
                ),
              ),
              FormItemComponent(
                leadingText: '缴费人',
                must: true,
                child: store.chargeSubject == 'owner'
                    ? Wisdom(
                        color: colorScheme.background4,
                        padding: EdgeInsets.symmetric(
                          horizontal: 10.pt,
                          vertical: 10.pt,
                        ),
                        child: WisText(
                          '${store.assetName}',
                          color: colorScheme.grey,
                        ),
                      )
                    : TextFormField(
                        controller:
                            TextEditingController(text: store.payer?.payer),
                        readOnly: true,
                        onTap: () => store.payerPicker(),
                        decoration: InputDecoration(
                          border: UnderlineInputBorder(
                            borderRadius: BorderRadius.circular(5.0),
                          ),
                          fillColor: colorScheme.background4,
                          hintText: '选择缴费人',
                          suffixIcon: Icon(
                            Icons.arrow_right_rounded,
                            color: colorScheme.placeholder2,
                            size: 30.pt,
                          ),
                          suffixIconConstraints: BoxConstraints(),
                        ),
                      ),
              ),
              if (store.bankAllList.length > 0) ...[
                FormItemComponent(
                  leadingText: '收款账户',
                  must: store.config?.notNullBankAccount ?? false,
                  child: TextFormField(
                    controller:
                        TextEditingController(text: store.curBank?.name),
                    readOnly: true,
                    onTap: () => store.bankPicker(),
                    decoration: InputDecoration(
                      border: UnderlineInputBorder(
                        borderRadius: BorderRadius.circular(5.0),
                      ),
                      fillColor: colorScheme.background4,
                      hintText: '选择收款账户',
                      suffixIcon: Icon(
                        Icons.arrow_right_rounded,
                        color: colorScheme.placeholder2,
                        size: 30.pt,
                      ),
                      suffixIconConstraints: BoxConstraints(),
                    ),
                  ),
                ),
              ],
              FormItemComponent(
                crossAxisAlignment: CrossAxisAlignment.start,
                leadingText: '缴费备注',
                child: TextFormField(
                  controller: TextEditingController(text: store.remark),
                  minLines: 4,
                  maxLines: 4,
                  maxLength: 64,
                  onChanged: (value) => store.onReMarkChange(value),
                  decoration: InputDecoration(
                    border: UnderlineInputBorder(
                      borderRadius: BorderRadius.circular(5.0),
                    ),
                    fillColor: colorScheme.background4,
                    hintText: '输入缴费备注，最多64字',
                  ),
                ),
              ),
              FormItemComponent(
                leadingText: '收据号',
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  children: [
                    Wrap(
                      children: [
                        for (var item in receiptTickets)
                          Chip(
                            label: Text('${item['ticketNo']}'),
                            onDeleted: () => store.removeTickets(item),
                          ).asPaddingOnly(right: 10.pt),
                      ],
                    ),
                    TextFormField(
                      controller:
                          TextEditingController(text: store.receiptText),
                      onChanged: (value) => store.onReceiptTextChange(value),
                      onFieldSubmitted: (_) => store.onReceiptTextSave(),
                      minLines: 1,
                      decoration: InputDecoration(
                        border: UnderlineInputBorder(
                          borderRadius: BorderRadius.circular(5.0),
                        ),
                        fillColor: colorScheme.background4,
                        hintText: '添加收据号',
                      ),
                    )
                  ],
                ),
              ),
              FormItemComponent(
                leadingText: '发票号',
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  children: [
                    Wrap(
                      children: [
                        for (var item in invoiceTickets)
                          Chip(
                            label: Text('${item['ticketNo']}'),
                            onDeleted: () => store.removeTickets(item),
                          ).asPaddingOnly(right: 10.pt),
                      ],
                    ),
                    TextField(
                      controller:
                          TextEditingController(text: store.invoiceText),
                      onChanged: (value) => store.onInvoiceTextChange(value),
                      onSubmitted: (_) => store.onInvoiceTextSave(),
                      minLines: 1,
                      decoration: InputDecoration(
                        border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(20),
                        ),
                        fillColor: colorScheme.background4,
                        hintText: '添加发票号',
                      ),
                    ),
                  ],
                ),
              ),
              FormItemComponent(
                leadingText: '缴费凭证',
                child: TextFormField(
                  controller: TextEditingController(
                      text: store.certificate != null
                          ? store.certificate['name']
                          : null),
                  readOnly: true,
                  onTap: () => store.imagePicker(),
                  decoration: InputDecoration(
                    border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(4.0.pt),
                      borderSide: BorderSide(color: Colors.white),
                    ),
                    fillColor: colorScheme.background4,
                    hintText: '拍照或上传凭证照片',
                    suffixIcon: Wisdom(
                      padding: EdgeInsets.only(right: 10.pt),
                      child: Icon(
                        Icons.file_upload_outlined,
                        color: colorScheme.placeholder2,
                        size: 20.pt,
                      ),
                    ),
                    suffixIconConstraints: BoxConstraints(),
                  ),
                ),
              ),
              if (store.certificate != null &&
                  store.certificate['id'] != null) ...[
                Wisdom.row(margin: EdgeInsets.only(left: 90.pt), children: [
                  Stack(
                    clipBehavior: Clip.none,
                    children: [
                      Wisdom(
                        onTap: () => store.onShowImage(store.certificate['id']),
                        child: WImage.network(
                          AppParseUtil.image(store.certificate['id']),
                          width: 100.pt,
                          height: 100.pt,
                          fit:BoxFit.cover,
                          borderRadius: BorderRadius.circular(8.pt),
                        ),
                      ),
                        Positioned(
                        top: -6.pt,
                        right: -10.pt,
                        child: Wisdom(
                          onTap: () => store.deleteImg(),
                          padding: EdgeInsets.all(2.pt),
                          color: colorScheme.divider2,
                          borderRadius: BorderRadius.circular(20.pt),
                          child: Icon(Icons.close_rounded, size: 18.pt),
                        ),
                      ),
                    ],
                  )
                ]),
              ]
            ],
          ),
        ),
      ],
    ));
  }
}
