/**
 * Created by Huzaifa on 9/18/15.
 */

$(document).on('click','#btnAddGrid', function() {
    $html = '';
    $html += '<tr id="grid_row_'+$grid_row+'" data-row_id="'+$grid_row+'">';
    $html += '<td><a onclick="removeRow(this);" title="Remove" class="btn btn-sm btn-danger" href="javascript:void(0);"><i class="fa fa-times"></i></a></td>';
    $html += '<td>';
    $html += '<select onchange="getPartners('+$grid_row+');" class="form-control select2" id="journal_voucher_detail_partner_type_id_'+$grid_row+'" name="journal_voucher_details['+$grid_row+'][partner_type_id]">';
    $html += '<option value="">&nbsp;</option>';
    $partner_types.forEach(function($partner_type) {
        $html += '<option value="'+$partner_type['partner_type_id']+'">'+$partner_type['name']+'</option>';
    })
    $html += '</select>';
    $html += '</td>'
    $html += '<td>';
    $html += '<select onchange="getDocuments('+$grid_row+');" class="form-control select2" id="journal_voucher_detail_partner_id_'+$grid_row+'" name="journal_voucher_details['+$grid_row+'][partner_id]">';
    $html += '<option value="">&nbsp;</option>';
    $html += '</select>';
    $html += '</td>'
    $html += '<td>';
    $html += '<input type="hidden" class="form-control" name="journal_voucher_details['+$grid_row+'][ref_document_type_id]" id="journal_voucher_detail_ref_document_type_id_'+$grid_row+'" value="" />';
    $html += '<select onchange="setDocumentType('+$grid_row+');" class="form-control select2" id="journal_voucher_detail_ref_document_identity_'+$grid_row+'" name="journal_voucher_details['+$grid_row+'][ref_document_identity]">';
    $html += '<option data-document_type_id="0" value="">&nbsp;</option>';
    $html += '</select>';
    $html += '</td>'
    $html += '<td>';
    $html += '<select class="form-control select2" id="journal_voucher_detail_coa_id_'+$grid_row+'" name="journal_voucher_details['+$grid_row+'][coa_id]" >';
    $html += '<option value="">&nbsp;</option>';
    $coas.forEach(function($coa) {
        $html += '<option value="'+$coa.coa_id+'">'+$coa.display_name+'</option>';
    })
    $html += '</select>';
    $html += '</td>'
    $html += '<td>';
    $html += '<input type="text" class="form-control" name="journal_voucher_details['+$grid_row+'][remarks]" id="journal_voucher_detail_remarks_'+$grid_row+'" value="" />';
    $html += '</td>'
//    $html += '<td>';
//    $html += '<input type="text" class="form-control dtpDate" name="journal_voucher_details['+$grid_row+'][cheque_date]" id="journal_voucher_detail_cheque_date_'+$grid_row+'" value="" />';
//    $html += '</td>'
//    $html += '<td>';
//    $html += '<input type="text" class="form-control" name="journal_voucher_details['+$grid_row+'][cheque_no]" id="journal_voucher_detail_cheque_no_'+$grid_row+'" value="" />';
//    $html += '</td>'
    $html += '<td>';
    $html += '<input onchange="calculateTotal();" type="text" class="form-control fPDecimal" name="journal_voucher_details['+$grid_row+'][document_debit]" id="journal_voucher_detail_document_debit_'+$grid_row+'" value="" />';
    $html += '</td>'
    $html += '<td>';
    $html += '<input onchange="calculateTotal();" type="text" class="form-control fPDecimal" name="journal_voucher_details['+$grid_row+'][document_credit]" id="journal_voucher_detail_document_credit_'+$grid_row+'" value="" />';
    $html += '</td>'
    $html += '<td><a onclick="removeRow(this);" title="Remove" class="btn btn-sm btn-danger" href="javascript:void(0);"><i class="fa fa-times"></i></a></td>';
    $html += '</tr>';


    $('#tblJournalVoucherDetail tbody').append($html);
    setFieldFormat();
    //$('#journal_voucher_detail_ref_document_type_id_'+$grid_row).select2({width: '100%'});
    //$('#journal_voucher_detail_coa_id_'+$grid_row).select2({width: '100%'});
    $grid_row++;
});

function getPartners($row_id) {
    var $selected_partners = [];
    $partner_type_id = $('#journal_voucher_detail_partner_type_id_' + $row_id).val();
    $options = '<option value="">&nbsp;</option>';
    if($partner_type_id != '') {
        $selected_partners = $partners[$partner_type_id];
        $.each($selected_partners, function($key, $partner) {
            $options += '<option value="'+$partner['partner_id']+'">'+$partner['name']+'</option>';
        });
    }

    $('#journal_voucher_detail_partner_id_' + $row_id).select2('destroy');
    $('#journal_voucher_detail_partner_id_' + $row_id).html($options);
    $('#journal_voucher_detail_partner_id_' + $row_id).select2({width:'100%'});
}

function getDocuments($row_id) {
    $partner_type_id = $('#journal_voucher_detail_partner_type_id_' + $row_id).val();
    $partner_id = $('#journal_voucher_detail_partner_id_' + $row_id).val();
    $options = '<option data-debit_amount="0" data-credit_amount="0" data-document_type_id="0" value="">&nbsp;</option>';
    if($partner_id != '') {
        $documents = $partners[$partner_type_id][$partner_id]['documents'];
        $.each($documents, function($key, $document) {
            if($document['outstanding_amount'] != 0) {
                $options += '<option data-debit_amount="'+$document['debit_amount']+'" data-credit_amount="'+$document['credit_amount']+'" data-document_type_id="'+$document['ref_document_type_id']+'" value="'+$document['ref_document_identity']+'">'+$document['ref_document_identity']+'</option>';
            }
        });
    }

    $('#journal_voucher_detail_ref_document_identity_' + $row_id).select2('destroy');
    $('#journal_voucher_detail_ref_document_identity_' + $row_id).html($options);
    $('#journal_voucher_detail_ref_document_identity_' + $row_id).select2({width:'100%'});
}

function setDocumentType($row_id) {
    var $document_data = $('#journal_voucher_detail_ref_document_identity_' + $row_id + ' option:selected').data();
    var $document_type_id = $document_data.document_type_id;
    var $debit_amount = $document_data.debit_amount;
    var $credit_amount = $document_data.credit_amount;

    $('#journal_voucher_detail_ref_document_type_id_' + $row_id).val($document_type_id);
    $('#journal_voucher_detail_document_debit_' + $row_id).val($credit_amount);
    $('#journal_voucher_detail_document_credit_' + $row_id).val($debit_amount);

    calculateTotal();
}

function removeRow($obj) {
    var $row_id = $($obj).parent().parent().data('row_id');
    $('#grid_row_'+$row_id).remove();
    calculateTotal();
}

function calculateTotal() {
    var $document_debit = 0;
    var $document_credit = 0;
    $('#tblJournalVoucherDetail tbody tr').each(function() {
        $row_id = $(this).data('row_id');
        var $debit_amount = $('#journal_voucher_detail_document_debit_' + $row_id).val() || 0;
        var $credit_amount = $('#journal_voucher_detail_document_credit_' + $row_id).val() || 0;

        $document_debit += parseFloat($debit_amount);
        $document_credit += parseFloat($credit_amount);
    })

    $('#document_debit').val($document_debit.toFixed(2));
    $('#document_credit').val($document_credit.toFixed(2));

    calculateBaseAmount();
}
$(document).on('change','#conversion_rate', function() {
    calculateBaseAmount();
})

function calculateBaseAmount() {
    var $document_debit = parseFloat($('#document_debit').val()) || 0.00;
    var $document_credit = parseFloat($('#document_credit').val()) || 0.00;
    var $conversion_rate = parseFloat($('#conversion_rate').val()) || 0.00;

    var $base_debit = $document_debit * $conversion_rate;
    var $base_credit = $document_credit * $conversion_rate;
    $('#base_debit').val($base_debit);
    $('#base_credit').val($base_credit);
}