import 'dart:convert';

import '../models/saldos_cartera.dart' as cartera;
import '../models/saldos_contacto.dart';
import 'package:http/http.dart' as http;

class SaldosProvider{
  final String _url = "smart.solerpalau.mx";

  Future<SaldosContacto> fetchSaldosContacto(String token) async{
    final url = Uri.https(_url, 'PR/api/v1/quiosco/saldos/obtenerDatosContacto');
    final res = await http.post(url,
        headers: {
          'Authorization' : 'Bearer $token'
        }
    );

    final decodedData = jsonDecode(res.body);
    final saldosContacto = SaldosContacto.fromJson(decodedData);
    return saldosContacto;
  }

  Future<cartera.SaldosCartera> fetchSaldosCartera(String token, String dinum, String dinombre) async{
    final url = Uri.https(_url, 'PR/api/v1/quiosco/saldos/obtenerCartera');
    final res = await http.post(url,
        headers: {
          'Authorization' : 'Bearer $token'
        },
        body: {
          'DINUM' : '$dinum',
          'DINOMBRE' : '$dinombre'
        }
    );

    final decodedData = jsonDecode(res.body);
    if(decodedData['result']['registros'].isEmpty && decodedData['result']['datosGenerales'].isEmpty){
      return cartera.SaldosCartera(
        result: cartera.Result(
          carteraVencer: {},
          datosGenerales: cartera.DatosGenerales(
            mxp: cartera.Mxp(
              moneda: '',
              totalCartera: '',
              totalCarteraVencida: '',
              totalCarteraVencer: '',
              pendientePorAplicar: '',
              total: '',
            ),
            usd: cartera.Usd(
              moneda: '',
              totalCartera: '',
              totalCarteraVencida: '',
              totalCarteraVencer: '',
              pendientePorAplicar: '',
              total: '',
            ),
          ),
          registros: [],
        ),
        message: '',
        response: false,
      );
    }

    final saldosCartera = cartera.SaldosCartera.fromJson(decodedData);
    return saldosCartera;
  }
}