import 'package:diplomaticquarterapp/config/size_config.dart';
import 'package:diplomaticquarterapp/core/viewModels/insurance_card_View_model.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart';
import 'package:diplomaticquarterapp/widgets/others/rounded_container.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../base/base_view.dart';

class InsuranceApproval extends StatefulWidget {
  int appointmentNo;

  InsuranceApproval({this.appointmentNo});

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

class _InsuranceApprovalState extends State<InsuranceApproval> {
  @override
  Widget build(BuildContext context) {
    return BaseView<InsuranceViewModel>(
      onModelReady: widget.appointmentNo != null
          ? (model) =>
              model.getInsuranceApproval(appointmentNo: widget.appointmentNo)
          : (model) => model.getInsuranceApproval(),
      builder: (BuildContext context, InsuranceViewModel model, Widget child) =>
          AppScaffold(
        isShowAppBar: true,
        baseViewModel: model,
        appBarTitle: TranslationBase.of(context).approvals,
        body: SingleChildScrollView(
          child: Container(
            margin: EdgeInsets.only(
              left: SizeConfig.screenWidth * 0.004,
              right: SizeConfig.screenWidth * 0.004,
              top: SizeConfig.screenWidth * 0.04,
            ),
            child: Column(
              children: <Widget>[
                Container(
                    width: double.infinity,
                    height: SizeConfig.screenHeight * 0.09,
                    color: Color(0xffEEEEEE),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: <Widget>[
                        Text(
                          TranslationBase.of(context).totalApproval,
                          style: TextStyle(
                            color: Color(0xff60688B),
                            fontSize: 19.0,
                            fontWeight: FontWeight.w600,
                          ),
                        ),
                        if (model.insuranceApproval.length > 0)
                          Container(
                              width: SizeConfig.widthMultiplier * 18.0,
                              height: SizeConfig.heightMultiplier * 2.8,
                              decoration: BoxDecoration(
                                  color: Color(0xffC5272D),
                                  borderRadius: BorderRadius.circular(19.0)),
                              child: Center(
                                child: Text(
                                  model.insuranceApproval[0].unUsedCount
                                      .toString(),
                                  style: TextStyle(
                                      color: Colors.white, fontSize: 19.0),
                                ),
                              ))
                      ],
                    )),
                ...List.generate(
                    model.insuranceApproval.length,
                    (index) => RoundedContainer(
                          backgroundColor: Colors.white,
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: <Widget>[
                              ExpansionTile(
                                title: Container(
                                  height: 120.0,
                                  child: Column(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: <Widget>[
                                      model.insuranceApproval[index]
                                                  .patientDescription ==
                                              "In Patient"
                                          ? Container(
                                              decoration: BoxDecoration(
                                                  color: Color(0xffB8372C),
                                                  borderRadius:
                                                      BorderRadius.circular(
                                                          16.0)),
                                              width: 95.0,
                                              padding:
                                                  EdgeInsets.only(left: 11.5),
                                              child: Text(
                                                model.insuranceApproval[index]
                                                            .patientDescription ==
                                                        null
                                                    ? ''
                                                    : model
                                                        .insuranceApproval[
                                                            index]
                                                        .patientDescription,
                                                style: TextStyle(
                                                    color: Colors.white),
                                              ),
                                            )
                                          : Container(
                                              decoration: BoxDecoration(
                                                  color: Color(0xff505A5D),
                                                  borderRadius:
                                                      BorderRadius.circular(
                                                          16.0)),
                                              width: 95.0,
                                              padding:
                                                  EdgeInsets.only(left: 11.5),
                                              child: Text(
                                                model.insuranceApproval[index]
                                                            .patientDescription ==
                                                        null
                                                    ? ''
                                                    : model
                                                        .insuranceApproval[
                                                            index]
                                                        .patientDescription,
                                                style: TextStyle(
                                                    color: Colors.white),
                                              ),
                                            ),
                                      Padding(
                                        padding: EdgeInsets.symmetric(
                                            vertical: 10.0),
                                        child: Text(
                                          model.insuranceApproval[index]
                                              .clinicName,
                                          style: TextStyle(
                                            fontSize: 20.0,
                                            color: Color(0xff60686B),
                                            fontWeight: FontWeight.w600,
                                          ),
                                        ),
                                      ),
                                      Text(
                                        model.insuranceApproval[index]
                                            .doctorName,
                                        style: TextStyle(
                                            fontSize: 17.0,
                                            fontStyle: FontStyle.italic),
                                      ),
                                    ],
                                  ),
                                ),
                                children: <Widget>[
                                  Padding(
                                    padding: EdgeInsets.symmetric(
                                        vertical: 12.0, horizontal: 12.0),
                                    child: Column(
                                      crossAxisAlignment:
                                          CrossAxisAlignment.start,
                                      children: <Widget>[
                                        Text(
                                          TranslationBase.of(context)
                                                  .approvalNo +
                                              model.insuranceApproval[index]
                                                  .approvalNo
                                                  .toString(),
                                          style: TextStyle(
                                            fontSize: 18.0,
                                            fontWeight: FontWeight.w600,
                                          ),
                                        ),
                                        Divider(
                                          color: Colors.black,
                                          height: 25.0,
                                          thickness: 1.0,
                                        ),
                                        Text(
                                          TranslationBase.of(context)
                                                  .procedureStatus +
                                              model.insuranceApproval[index]
                                                  .approvalStatusDescption,
                                          style: TextStyle(
                                              fontWeight: FontWeight.w600,
                                              fontSize: 17.5),
                                        ),
                                        Divider(
                                          color: Colors.black,
                                          height: 25.0,
                                          thickness: 1.0,
                                        ),
                                        Text(
                                          TranslationBase.of(context)
                                                  .unusedCount +
                                              model.insuranceApproval[index]
                                                  .unUsedCount
                                                  .toString(),
                                          style: TextStyle(
                                              fontSize: 17.5,
                                              fontWeight: FontWeight.w600),
                                        ),
                                        Divider(
                                          color: Colors.black,
                                          height: 25.0,
                                          thickness: 1.0,
                                        ),
//                                Text(
//                                  'Company Name:    ' +
//                                              model.insuranceApproval[index]
//                                                  .companyName ==
//                                          null
//                                      ? '000'
//                                      : model
//                                          .insuranceApproval[index].companyName,
//                                  style: TextStyle(
//                                      fontSize: 17.5,
//                                      fontWeight: FontWeight.w600),
//                                ),
                                        Text(
                                          TranslationBase.of(context)
                                              .companyName,
                                          style: TextStyle(
                                            fontWeight: FontWeight.w600,
                                            fontSize: 17.5,
                                          ),
                                        ),

                                        Divider(
                                          color: Colors.black,
                                          height: 25.0,
                                          thickness: 1.0,
                                        ),
                                        Text(
                                          TranslationBase.of(context)
                                                  .receiptOn +
                                              convertDateFormat(model
                                                  .insuranceApproval[index]
                                                  .rceiptOn),
                                          style: TextStyle(
                                            fontSize: 17.5,
                                            fontWeight: FontWeight.w600,
                                          ),
                                        ),
                                        Divider(
                                          color: Colors.black,
                                          height: 25.0,
                                          thickness: 1.0,
                                        ),
                                        Text(
                                          TranslationBase.of(context)
                                                  .expiryDate +
                                              convertDateFormat(model
                                                  .insuranceApproval[index]
                                                  .expiryDate),
                                          style: TextStyle(
                                            fontSize: 17.5,
                                            fontWeight: FontWeight.w600,
                                          ),
                                        ),
                                        Divider(
                                          color: Colors.black,
                                          height: 55.0,
                                          thickness: 1.2,
                                        ),
                                        IntrinsicHeight(
                                          child: Row(
                                            crossAxisAlignment:
                                                CrossAxisAlignment.center,
                                            children: <Widget>[
                                              Expanded(
                                                flex: 1,
                                                child: Column(
                                                  children: <Widget>[
                                                    Text(TranslationBase.of(
                                                            context)
                                                        .procedureName),
                                                    Padding(
                                                      padding: EdgeInsets.only(
                                                          top: SizeConfig
                                                                  .heightMultiplier *
                                                              3.0),
                                                      child: Text(
                                                        model
                                                                    .insuranceApproval[
                                                                        index]
                                                                    .approvalDetails ==
                                                                null
                                                            ? ''
                                                            : model
                                                                .insuranceApproval[
                                                                    index]
                                                                .approvalDetails
                                                                .procedureName,
                                                        style: TextStyle(
                                                            fontWeight:
                                                                FontWeight.w600,
                                                            fontSize: 15.5),
                                                      ),
                                                    ),
                                                  ],
                                                ),
                                              ),
                                              VerticalDivider(
                                                width: 10.0,
                                                thickness: 1.2,
                                                color: Colors.black,
                                              ),
                                              Expanded(
                                                flex: 1,
                                                child: Column(
                                                  children: <Widget>[
                                                    Text(TranslationBase.of(
                                                            context)
                                                        .procedureStatus),
                                                    Padding(
                                                      padding: EdgeInsets.only(
                                                          top: SizeConfig
                                                                  .heightMultiplier *
                                                              3.0),
                                                      child: Text(
                                                        model
                                                                    .insuranceApproval[
                                                                        index]
                                                                    .approvalDetails ==
                                                                null
                                                            ? ''
                                                            : model
                                                                .insuranceApproval[
                                                                    index]
                                                                .approvalDetails
                                                                .status,
                                                        style: TextStyle(
                                                            fontSize: 17.5,
                                                            fontWeight:
                                                                FontWeight
                                                                    .w600),
                                                      ),
                                                    ),
                                                  ],
                                                ),
                                              ),
                                              VerticalDivider(
                                                width: 2.3,
                                                thickness: 1.2,
                                                color: Colors.black,
                                              ),
                                              Expanded(
                                                flex: 1,
                                                child: Column(
                                                  children: <Widget>[
                                                    Text(TranslationBase.of(
                                                            context)
                                                        .usageStatus),
                                                    Padding(
                                                      padding: EdgeInsets.only(
                                                          top: SizeConfig
                                                                  .heightMultiplier *
                                                              3.0),
                                                      child: Text(
                                                        model
                                                                    .insuranceApproval[
                                                                        index]
                                                                    .approvalDetails ==
                                                                null
                                                            ? ''
                                                            : model
                                                                .insuranceApproval[
                                                                    index]
                                                                .approvalDetails
                                                                .isInvoicedDesc,
                                                        style: TextStyle(
                                                            fontWeight:
                                                                FontWeight.w600,
                                                            fontSize: 17.5),
                                                      ),
                                                    ),
                                                  ],
                                                ),
                                              ),
                                            ],
                                          ),
                                        ),
                                        Divider(
                                          color: Colors.black,
                                          height: 25.0,
                                          thickness: 1.0,
                                        ),
                                      ],
                                    ),
                                  ),
                                ],
                              ),
                            ],
                          ),
                        )),
              ],
            ),
          ),
        ),
      ),
    );
  }

  convertDateFormat(String Date) {
    const start = "/Date(";
    const end = "+0300)";

    final startIndex = Date.indexOf(start);
    final endIndex = Date.indexOf(end, startIndex + start.length);

    var date = new DateTime.fromMillisecondsSinceEpoch(
        int.parse(Date.substring(startIndex + start.length, endIndex)));
    String newDate = date.year.toString() +
        "-" +
        date.month.toString().padLeft(2, '0') +
        "-" +
        date.day.toString().padLeft(2, '0');

    return newDate.toString();
  }
}
