<style>
    #category_data{
        padding: 4px !important;
        font-size: 12px !important;
    }
    #tableWithSearch tfoot td{
        font-weight:bold;
    }
    #tableWithSearch tbody tr td:last-child{
        font-weight:bold;
    }
    .table thead tr >th{
        background-color:#4c0870;
        color:#fff;
    }
</style>
<div class="container-fluid container-fixed-lg bg-white">
    <div class="panel panel-transparent">
        <div class="panel-heading">
            <div class="panel-title">
                <h4 class="bold"><?php echo CommonLanguage::getMessage($title); ?> </h4>
            </div>
            <div class="row form-group">
                <input type="hidden" id="project_funding_id" name="project_funding_id" value="<?php echo $project_funding_id; ?>"/>
                <input type="hidden" id="project_funding_nb" name="project_funding_nb" value="<?php echo!empty($edit_arr['main']['project_funding_nb']) ? $edit_arr['main']['project_funding_nb'] : ''; ?>"/>
                <div class="col-md-3">
                    <label><?php echo CommonLanguage::getMessage('project_period') ?></label>
                    <input id="period_id" type="hidden" class="full-width" name="period_id" value="<?php echo!empty($edit_arr['main']['period_id']) ? $edit_arr['main']['period_id'] : ''; ?>" data-select-name="<?php echo!empty($edit_arr['main']['project_period_title']) ? $edit_arr['main']['project_period_title'] : ''; ?>" <?php echo $is_edit ? 'disabled' : ''; ?> />
                </div> 
                <div class="col-md-2">
                    <label><?php echo CommonLanguage::getMessage('period') ?></label>
                    <input id="detail_id" type="hidden" class="full-width" name="detail_id" value="<?php echo!empty($edit_arr['main']['detail_id']) ? $edit_arr['main']['detail_id'] : ''; ?>" data-select-name="<?php echo!empty($edit_arr['main']['detail_title']) ? $edit_arr['main']['detail_title'] : ''; ?>" <?php echo $is_edit ? 'disabled' : ''; ?>  >
                </div> 
                <div class="col-md-2">
                    <label><?php echo CommonLanguage::getMessage('currency') ?></label>
                    <input type="hidden" name="currency_id" id="currency_id" value="<?php echo!empty($edit_arr['main']['currency_id']) ? $edit_arr['main']['currency_id'] : ''; ?>"/>
                    <input id="currency_name" type="text" readonly class="form-control" name="currency_name" value="<?php echo!empty($edit_arr['main']['currency_name']) ? $edit_arr['main']['currency_name'] : ''; ?>" >
                </div> 
                <div class="col-md-2">
                    <label><?php echo CommonLanguage::getMessage('Date') ?></label>
                    <input id="project_funding_date" type="text" class="form-control" name="project_funding_date" value="<?php echo!empty($edit_arr['main']['project_funding_date']) ? $edit_arr['main']['project_funding_date'] : date('Y-m-d'); ?>"   />
                </div> 
                <div class="col-md-3">
                    <label><?php echo CommonLanguage::getMessage('payment_account') ?><span class='red-star'>*</span></label>
                    <input type="hidden" name="payment_acc_id" id="payment_acc_id" class='full-width' value="<?php echo!empty($edit_arr['main']['payment_acc_id']) ? $edit_arr['main']['payment_acc_id'] : ''; ?>" data-select-name='<?= json_encode(!empty($edit_arr['main']['payment_acc_name']) ? $edit_arr['main']['payment_acc_name'] : '')?>'/>
                </div> 
                <?php if (!$is_edit) { ?>
                <div class="col-md-1 pull-right m-t-25">
                    <button  class="btn btn-complete full-width pull-right " id="generate" ><?php echo CommonLanguage::getMessage('generate') ?></button>
                </div>
                <?php } ?>
            </div>
        </div>
        <div class="panel-body <?php echo ($is_edit && !empty($edit_arr['details'])) ? '' : 'hidden'; ?>">
            <div class="row ">
                <div class="col-md-12 table-responsive">
                    <table class="table table-bordered" id="tableWithSearch" >
                        <thead>
                            <?php if ($is_edit) { ?>
                            <th><?php  echo CommonLanguage::getMessage('activity_code');?></th>
                            <th><?php  echo CommonLanguage::getMessage('activity');?></th>
                            <th><?php echo $edit_arr['main']['detail_title']; ?></th>
                            <?php } ?>
                        </thead>
                        <tbody>
                            <?php
                            if ($is_edit) {
                                $total_funding = 0;
                                foreach ($edit_arr['details'] as $row) {
                            ?>
                            <tr>
                                <td><?php echo $row[0]; ?></td>
                                <td><?php echo $row[1]; ?></td>
                                <td><?php echo $row[2]; ?></td>
                            </tr>
                            <?php }
                            }
                            ?>
                        </tbody>
                        <tfoot>
                            <tr>
                                <td></td>
                                <td></td>
                                <td id="total_funding"></td>
                            </tr>
                        </tfoot>
                    </table>
                </div>
            </div>
            <div class="row">
                <div class="col-md-12">
                    <button  class="btn btn-complete pull-right " id="submit_project_funding" ><?php echo CommonLanguage::getMessage('Save') ?></button>
                </div>
            </div>
        </div>
        <?php echo Forms::ModalLoader('project_funding_loader'); ?>
    </div>
</div>
<script>
    $(function () {
        if ($('#project_funding_id').val() != 0) {
            RebuildDataTable($('#panel-heading .panel-title').html(), [], "");
            update_main_total();
        }
        build_select2($('#payment_acc_id'), "xhr/accIDSearch?type=Payment");
        $('#project_funding_date').datepicker({autoclose:true});

        build_select2($('#period_id'), 'ACCAjax/searchProjectPeriod');
        build_select2($('#detail_id'), 'ACCAjax/searchProjectPeriodDetails?period_id=' + $('#period_id').val());
        $('#period_id').on('change', function () {
            $('#detail_id').val('').trigger('change');
            build_select2($('#detail_id'), 'ACCAjax/searchProjectPeriodDetails?period_id=' + $('#period_id').val());
            if ($('#period_id').val()) {
                $('#account_id').val($('#period_id').select2('data').account_id);
                $('#currency_id').val($('#period_id').select2('data').currency_id);
                $('#currency_name').val($('#period_id').select2('data').Currency_Name);
            } else {
                $('#account_id').val('');
                $('#currency_id').val('');
                $('#currency_name').val('');
            }
        });

        $('#generate').on('click', function () {
            if ($('#period_id').val() && $('#detail_id').val()) {
                $('.panel-body').removeClass('hidden');
                ChangeTable();
            } else {
                $('.panel-body').addClass('hidden');
            }
        });
        $('#tableWithSearch').on('input','input.funding_total',function(){
            update_main_total();
        });
        $('#submit_project_funding').on('click', function () {
            var project_funding = [], row = $("#tableWithSearch").dataTable().fnGetNodes();
            if (row.length == 0) {
                return;
            }
            for (var i = 0; i < row.length; i++) {
                var parent = $(row[i]);
                if ($('input.funding_total', parent).length > 0) {
                    project_funding.push({
                        activity_id: $('input.funding_total', parent).attr('data-activity-id'),
                        total: $('input.funding_total', parent).val(),
                        account_id: $('input.funding_total', parent).attr('data-account-id')});
                }
            }
            if ($("#payment_acc_id").val() == '') {
                toastr.error('<?php echo CommonLanguage::getMessage('Required_Fields') ?>');
                return
            }
            $("#project_funding_loader").removeClass('hidden');
            $.ajax({
                url: "ACCAjax/submit_project_funding",
                method: "POST",
                data: {project_funding: project_funding, period_id: $('#period_id').val(),
                       currency_id: $('#currency_id').val(),
                       payment_acc_id: $('#payment_acc_id').val(),
                       project_funding_date: $('#project_funding_date').val(),
                       detail_id: $('#detail_id').val(), project_funding_id: $('#project_funding_id').val(),
                       project_funding_nb: $('#project_funding_nb').val()},
                dataType: "JSON",
                complete: function (jqXHR, status) {
                    if (status == 'success') {
                        if (jqXHR.responseJSON[1] == "success") {
                            toastr.success(jqXHR.responseJSON[0]);
                            $('#submit_project_funding').prop('disabled', true);
                            if ($('#project_funding_id').val() == 0)
                                ChangeTable();
                        } else {
                            toastr.error(jqXHR.responseJSON[0]);
                        }
                    }
                    $("#project_funding_loader").addClass('hidden');
                }
            });
        });
        //        ChangeTable();
    });
    function ChangeTable() {
        var Data = {
            period_id: $("#period_id").val(),
            detail_id: $("#detail_id").val()
        }
        $('#project_funding_loader').removeClass('hidden');
        $.ajax({
            url: "ACCAjax/get_project_budget",
            data: Data,
            dataType: "json",
            complete: function (v1, v2) {
                if (v2 == "success") {
                    var response = v1.responseJSON;
                    if (response['body'] == ' ') {
                        toastr.error('<?php echo CommonLanguage::getMessage('no_budget_project') ?>')
                    }
                    var header = '<tr><th><?php echo CommonLanguage::getMessage('activity_code') ?></th><th><?php echo CommonLanguage::getMessage('activity') ?></th><th>' + $("#detail_id").select2('data').text + '</th></tr>';
                    if ($.fn.DataTable.isDataTable('#tableWithSearch')) {
                        $("#tableWithSearch").DataTable().destroy();
                    }
                    $("#tableWithSearch thead").empty().html(header);
                    $("#tableWithSearch tbody").empty().html(response['body']);
                    RebuildDataTable($('#panel-heading .panel-title').html(), [], "", Data, '', '', '', false);
                    var row = $("#tableWithSearch").dataTable().fnGetNodes();
                    $('#project_funding_loader').addClass('hidden');
                    update_main_total();
                }
            }
        });
    }
    function update_main_total(){
        var total_funding = 0,row = $("#tableWithSearch").dataTable().fnGetNodes();
        for (var i = 0; i < row.length; i++) {
            var parent = $(row[i]);
            if($('input.funding_total', parent).val())
                total_funding += parseFloat($('input.funding_total', parent).val());
        }
        $('#total_funding').html(total_funding);
    }
</script>