/*
 * addLineItem.js
 *
 * Brings up a jQuery modal form dialog for adding a line item to a quote/job/invoice
 *
 * Also does the date picker on the commercial details. Might change the commercial details so they're all javacript sexy too.
 *   
 *
 */



$(function() {


    fetchTable();

    var quoteID = $("#QuoteId").serialize();
   

    $(".nocosting").hide();
    $(".blankItem").hide();
    $(".fromProductDB").hide();
   

    $("#addLineItem-form").dialog({
        autoOpen: false,
        height: 800,
        width: 800,
        modal: true,
        buttons: {
            'Add Product': function() {
                
                $.post("/line_items/ajaxSave", $("#LineItemAddForm").serialize(), function(data) {
                    
                    fetchTable();
                    $('#mydebug').html(data);
                }
                );

                $("#LineItemAddForm").resetForm();
                $(this).dialog('close');
            },
            Cancel: function() {
                $(this).dialog('close');
            }
        },
        close: function() {
        
        }
    });


    $('#addLineItem')
    .button()
    .click(function() {
        $('#addLineItem-form2').hide();
        
        $('#addLineItem-form').dialog('open');
    });




    $('#generatePDF')
    .button()
    .click(function() {
        $.post("/quotes/ajaxpdf",quoteID, function(data) {
            $('#mydebug').html(data);
        });
    });


    $("#LineItemPrincipleId").change(getPrincipleProducts);
    
    $('#QuoteDateIssuedDisplay').datepicker({
        showButtonPanel: true,
        dateFormat: 'DD d MM yy',
        altFormat: 'yy-mm-dd',
        altField: '#QuoteDateIssued'

    });


    $('#addBlankItem').button().click(function () {
        $("#LineItemPrincipleId").hide();
        $(".blankItem").show();
        $(".nocosting").show();
        return false;

    });

    $('#LineItemFromDB').change(function () {
        if(this.value == 'No') {
            $(".blankItem").show();
            $(".nocosting").show();
            $(".fromProductDB").hide();
        }
        else {
            $(".fromProductDB").show();
            $(".blankItem").hide();
            $(".nocosting").hide();
        }


    });
 

});


/**
 * Updates the productBox div with a Select Box of the products.
 *
 * puts the output in a div called productBox
 */
function getPrincipleProducts() {
    $.post("/products/getPrincipleProducts", $("#LineItemPrincipleId").serialize(), function(data) {

        $('#productBox').html(data);
        $("#LineItemProductId").change(getProductOptions); 
    });


}



/*
 * Ajax Request to get the Product Options (if any) for the Product selected
 */

function getProductOptions() {
    
    $.post("/products/getProductOptions", $("#LineItemProductId").serialize(), function(optionData) {
        $("#productOptionsBox").html(optionData);
        
    });

    $(".nocosting").show();
    
}



/*
 * Fetch the most recent table of Line Items. This allows calculation to be done in PHP rather than JS.
 */
function fetchTable() {
    $.post("/lineItems/viewTable", $("#LineItemQuoteId").serialize(), function (itemTable) {
        $("#productTable").html(itemTable);
    });
}



