import 'package:intl/intl.dart';
import '../util/logging.dart';

class CustomerProperty with Logging {
  int? customerPropertyId;
  late int propertyId;
  late int customerId;
  DateTime? dateAdd;
  String? dateYmd;
  String? dateYm;
  String? dateY;
  late double propertyValue;
  bool newData = false;

  CustomerProperty(
      {required propertyId,
      required customerId,
      required dateAdd,
      required propertyValue});

  CustomerProperty.fromJson(Map json) {
    customerPropertyId = json['customerPropertyId'];
    propertyId = json['propertyId'];
    customerId = json['customerId'];
    dateAdd = DateTime.parse(json['dateAdd']);

    if (dateAdd != null) {
      dateYmd = DateFormat('yyyy-MM-dd').format(dateAdd!);
      dateYm = DateFormat('yyyy-MM').format(dateAdd!);
      dateY = DateFormat('yyyy').format(dateAdd!);
    }

    propertyValue = json['propertyValue'];

    log("Json $json, ${toString()}");
  }

  Map<String, dynamic> toJson() {
    if (customerPropertyId != null) {
      return {
        "customerPropertyId": customerPropertyId,
        "propertyId": propertyId,
        "customerId": customerId,
        "dateAdd": DateFormat('yyyy-MM-dd HH:mm:ss').format(dateAdd!),
        "propertyValue": propertyValue
      };
    } else {
      return {
        "propertyId": propertyId,
        "customerId": customerId,
        "dateAdd": DateFormat('yyyy-MM-dd HH:mm:ss').format(dateAdd!),
        "propertyValue": propertyValue
      };
    }
  }

  @override
  String toString() {
    Map<String, dynamic> json = {
      "customerPropertyId": customerPropertyId,
      "propertyId": propertyId,
      "customerId": customerId,
      "dateAdd": DateFormat('yyyy-MM-dd HH:mm:ss').format(dateAdd!),
      "propertyValue": propertyValue,
      "dateYmd": dateYmd,
      "dateYm": dateYm,
      "dateY": dateY,
    };
    return json.toString();
  }
}