import 'dart:convert';

import 'package:quiosco_soler_2/quiosco/models/response_model.dart';
import 'package:quiosco_soler_2/clientes/models/login_cliente.dart' as Cliente;
import 'package:http/http.dart' as http;

class LoginProvider{
  final String _url = "smart.solerpalau.mx";
  Response error = Response(
    result: Result(
      accessToken: 'no-data',
      dinum: 'no-data',
      user: 'no-data',
      role: 'no-data',
      lsec: 'no-data',
    ),
    response: false,
    message: 'no-data',
  );
  Cliente.LoginCliente errorCliente = Cliente.LoginCliente(
    result: Cliente.Result(
      accessToken: 'no-data',
      dinum: 'no-data',
      user: 'no-data',
      tipo: 'no-data',
    ),
    response: false,
    message: 'no-data',
  );
  
  Future<Response> fetchLogin(String dinum, String dipass) async{
    final url = Uri.https(_url, 'PR/api/v1/login');
    final response = await http.post(url,
      body: {
        'DINUM'    : dinum,
        'DIPASS'   : dipass,
        'DIPERFIL' : 'COLABORADOR',
      }
    );

    final decodedData = jsonDecode(response.body);
    if(decodedData['result'].isEmpty){
      return error;
    }

    final res = Response.fromJson(decodedData);
    return res;
  }

  Future<Cliente.LoginCliente> fetchLoginCliente(String dinum, String dipass) async{
    final url = Uri.https(_url, 'PR/api/v1/loginCliente');
    final response = await http.post(url,
        body: {
          'DINUM'  : dinum,
          'DIPASS' : dipass
        }
    );
    final decodedData = jsonDecode(response.body);
    if(decodedData['result'].isEmpty){
      return errorCliente;
    }
    final loginCliente = Cliente.LoginCliente.fromJson(decodedData);
    return loginCliente;
  }
}