
import 'dart:convert';

import 'Subproduct.dart';
import 'product_attribute.dart';

class Product {
  int? id;
  int? businessId;
  int? categoryId;
  String? name;
  double? price;
  double? regularPrice;
  String? description;
  String? detailDescription;
  String? imagePath;
  String? secondImagePath;
  double? monthSales;
  int? rate;
  double? leftNum;
  List<ProductAttribute>? productAttributes;
  bool? nonInventory;
  String? extraData;
  List<Subproduct>? subproducts;

  Product(int id, int businessId, String name, double price, String description,
      String imagePath, List<ProductAttribute> productAttributes) {
    this.id = id;
    this.businessId = businessId;
    this.name = name;
    this.price = price;
    this.description = description;
    this.imagePath = imagePath;
    this.secondImagePath = secondImagePath;
    this.productAttributes = productAttributes;
  }

  Product.fromJson(Map<String, dynamic> json)
      : id = json['id'],
        businessId = json['business_id'],
        categoryId = json['category_id'],
        name = json['name'],
        price = double.parse(json['price'].toString()),
        regularPrice = double.parse(json['regular_price'].toString()),
        description = json['description'],
        detailDescription = json['detail_description'],
        imagePath = json['image_path'],
        secondImagePath = json['second_image_path'],
        monthSales = json['month_sales'] != null ? double.parse(json['month_sales'].toString()) : 0.0,
        rate = json['rate'],
        leftNum = json['left_num'] != null ? double.parse(json['left_num'].toString()) : 0.0,
        productAttributes = json['productattributes'] != null ? (json['productattributes'] as List).map((i) => ProductAttribute.fromJson(i)).toList() : [],
        nonInventory = json['non_inventory'],
        extraData = json['extra_data'],
        subproducts = json['subproducts'] != null ? (json['subproducts'] as List).map((e) => Subproduct.fromJson(e)).toList() : [];

  Map<String, dynamic> toJson() => {
    'id': id,
    'business_id': businessId,
    'category_id': categoryId,
    'name': name,
    'price': price,
    'regular_price': regularPrice,
    'description': description,
    'detail_description': detailDescription,
    'image_path': imagePath,
    'second_image_path': secondImagePath,
    'month_sales': monthSales,
    'rate': rate,
    'left_num': leftNum,
    'productattributes': productAttributes,
    'non_inventory': nonInventory,
    'extra_data': extraData,
    'subproducts': subproducts,
  };

  @override
  String toString() {
    return json.encode(this);
  }
}