import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:intl/intl.dart';

import '../common/settings.dart';


class SupplierOrdersModel {
  String typeofwater='';
  String supplierName = '';
  String supplierId = '';
  String date = '';
  String dbId = '';
  String time = '';
  String acceptedTime = '';
  String address='';
  String displayAddress = '';
  String capacity = '';
  String quantity = '';
  String quotedAmount='';
  String bookingCharges='';
  String advancePaid='';
  String status='';
  double lat=0;
  double lng=0;
  double distanceInMeters=0;
  Color cardColor=Colors.white;

  SupplierOrdersModel();

  factory SupplierOrdersModel.fromJson(Map<String, dynamic> json){
    SupplierOrdersModel rtvm = new SupplierOrdersModel();

    rtvm.status = json['status'] ?? '';
    rtvm.typeofwater = json['type_of_water'] ?? '';
    rtvm.date = json['date'] ?? '';
    rtvm.time = json['time'] ?? '';
    rtvm.dbId=json['_id']?? '';
    rtvm.capacity = json['capacity'] ?? '';
    rtvm.quantity = json['quantity'] ?? '';
    final suppliers = json['requested_suppliers'] as List?;

    if (suppliers != null && suppliers.isNotEmpty) {
      final supplier = suppliers[0];
      rtvm.quotedAmount = supplier['quoted_amount']?.toString() ?? '0.00';
      rtvm.bookingCharges = supplier['delivery_charges']?.toString() ?? '0.00';
      rtvm.advancePaid = supplier['advance_paid']?.toString() ?? '0.00';
      rtvm.supplierName = supplier['supplier_details']?['supplierName'] ?? '';
      rtvm.supplierId = supplier['supplier_details']?['supplierId'] ?? '';
      rtvm.address = supplier['supplier_details']?['address'] ?? '';
      rtvm.acceptedTime = supplier['time'] ?? '';
      rtvm.lat =supplier['supplier_details']?['latitude'] ?? '';
      rtvm.lng = supplier['supplier_details']?['longitude'] ?? '';
    } else {
      rtvm.quotedAmount = '';
      rtvm.bookingCharges='';
      rtvm.supplierName = '';
      rtvm.address = '';
      rtvm.acceptedTime = '';
      rtvm.supplierId='';
      rtvm.lat =0.0;
      rtvm.lng =0.0;
    }
    List<String> parts = rtvm.address.split(',');
    if (parts.length > 4) {
      rtvm.displayAddress = parts[2].trim();
    } else {
      rtvm.displayAddress = rtvm.address; // fallback
    }



    rtvm.distanceInMeters = double.parse((Geolocator.distanceBetween(
        rtvm.lat,
        rtvm.lng,
        AppSettings.userLatitude,
        AppSettings.userLongitude
    ) / 1000).toStringAsFixed(2));

    return rtvm;
  }

}