package com.ecep.contract.vm;

import com.ecep.contract.model.ContractItem;

import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = false)
public class ContractItemComposeViewModel extends BaseViewModel<ContractItemComposeViewModel.Compose> {

    @Data
    public static class Compose {

        /**
         * 税率
         */
        SimpleDoubleProperty taxRate = new SimpleDoubleProperty();
        /**
         * 不含税价格
         */
        SimpleDoubleProperty exclusiveTaxPrice = new SimpleDoubleProperty();
        /**
         * 含税价格
         */
        SimpleDoubleProperty taxPrice = new SimpleDoubleProperty();
        /**
         * 数量
         */
        SimpleDoubleProperty quantity = new SimpleDoubleProperty();

        /**
         * 含税价格合计
         */
        SimpleDoubleProperty taxAmount = new SimpleDoubleProperty();
        /**
         * 不含税价格合计
         */
        SimpleDoubleProperty exclusiveTaxAmount = new SimpleDoubleProperty();

        SimpleListProperty<ContractItem> items = new SimpleListProperty<>();

        public void add(ContractItem item) {
            taxRate.set(item.getTaxRate());
            exclusiveTaxPrice.set(item.getExclusiveTaxPrice());
            taxPrice.set(item.getTaxPrice());

            taxAmount.set(taxAmount.get() + item.getTaxPrice() * item.getQuantity());
            exclusiveTaxAmount.set(exclusiveTaxAmount.get() + item.getExclusiveTaxPrice() * item.getQuantity());

            quantity.set(quantity.get() + item.getQuantity());

            ObservableList<ContractItem> list = items.get();
            if (list == null) {
                list = FXCollections.observableArrayList();
                items.set(list);
            }
            list.add(item);
        }
    }

    SimpleIntegerProperty id = new SimpleIntegerProperty();
    SimpleObjectProperty<String> title = new SimpleObjectProperty<>();
    SimpleObjectProperty<String> specification = new SimpleObjectProperty<>();
    SimpleObjectProperty<String> unit = new SimpleObjectProperty<>();

    /**
     * 进项
     */
    Compose in = new Compose();
    /**
     * 出项
     */
    Compose out = new Compose();

    SimpleObjectProperty<String> remark = new SimpleObjectProperty<>();

}
