<div class="container-fluid container-fixed-lg bg-white">
    <div class="panel panel-transparent">
        <div class="panel-heading">
            <div class="panel-title"> <?php print CommonLanguage::getMessage($is_actual == 1 ? 'multi_advance_payment_act' :'multi_advance_payment'); ?></div>
        </div>
        <div class="panel-body">
            <form id="multi-advance-payment-form" action="javascript:void(0);">
                <div class="row form-group">
                    <div  class="col-md-3">
                        <label><?php print CommonLanguage::getMessage('Date'); ?></label>
                        <span class="red-star">*</span>
                        <input id="Advance_Payment_Date" name="Advance_Payment_Date" class="form-control" autocomplete="off" value="<?php echo ($today); ?>" required />
                    </div>
                    <div  class="col-md-3">
                        <label><?php print CommonLanguage::getMessage('currency'); ?></label>
                        <span class="red-star">*</span>
                        <input id="Currency_ID" name="Currency_ID" class="full-width" type="hidden" required/>
                    </div>
                    <div  class="col-md-3">
                        <label><?php print CommonLanguage::getMessage('payment_type'); ?></label>
                        <span class="red-star">*</span>
                        <select id="paymentType" name="Payment_ID" class="full-width PaymentType" data-init-plugin="select2" required>
                            <?php foreach ($payment_type as $type): ?>
                            <option data-code="<?php echo $type['Account_ID'] ?>" value="<?php echo $type['Account_ID']; ?>"><?php echo $type['Account_Code'] . ' - ' . $type['Account_Name']; ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                </div>
            </form>
            <div class="row">
                <div class="col-md-12 table-responsive">
                    <table class="table table-bordered hrm" id="tableWithSearch" >
                        <thead>
                            <tr>
                                <th class="text-center no-print"  ><input type="checkbox" class="select_all_emp"/></th>
                                <th class="text-center" style="width:15%" ><?php print CommonLanguage::getMessage('work_id'); ?></th>
                                <th class="text-center" style="width:15%" ><?php print CommonLanguage::getMessage('Name'); ?></th>
                                <th class="text-center no-print" style="width:15%"  ><input type="number" class="total_amount form-control" name="total_amount" min="0" id="total_amount" onkeyup="applyMainAmount(this)" onchange="applyMainAmount(this)"/></th>
                            </tr>
                        </thead>
                        <tbody>
                        </tbody>
                        <tfoot>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th><button class="btn btn-success full-width" id="submit_advance_payment"><?php echo CommonLanguage::getMessage('Save');?></button></th>
                        </tfoot>
                    </table>
                </div>
            </div>
            <?php echo Forms::ModalLoader('advancePaymnetLoader'); ?>
        </div>
    </div>
</div>
<script>
    var is_actual = <?php echo json_encode($is_actual);?>;
    $(function () {
        build_select2($("#Currency_ID"), "xhr/CommonSearch?col_name=concat_ws(' - ',Currency_Name,Currency_Symbol)&col_id=Currency_ID&table=tbl_common_currency");
        $("#Advance_Payment_Date").datepicker({autoclose: true})
        buildCurrentTable();
        $("#tableWithSearch").on("click", "input.select_all_emp", function (e) {
            var is_checked = $(this).is(":checked");
            var row = $("#tableWithSearch").dataTable().fnGetNodes();
            for (var i = 0; i < row.length; i++) {
                $(row[i]).find("input.advance_payment_check").prop("checked", is_checked);
            }
        });
        $("#submit_advance_payment").on("click", function () {
            if(!$("#multi-advance-payment-form").valid()){
                $(document.body).animate({scrollTop: 0}, 800)
                return;
            }
            var employee_advance = [],row = $("#tableWithSearch").dataTable().fnGetNodes();
            if($('input.advance_payment_check:checked').length == 0){
                toastr.error('<?php echo CommonLanguage::getMessage('choose_entry');?>')
                $(document.body).animate({scrollTop: 0}, 800)
                return;
            }
            for (var i = 0; i < row.length; i++) {
                if ($(row[i]).find("input.advance_payment_check").is(":checked")) {
                    if(!$(row[i]).find('input.amount').val()){
                        toastr.error('<?php echo CommonLanguage::getMessage('amount_for_all_entry');?>')
                        return;
                    }
                    employee_advance.push({Employee_ID : $(row[i]).find('input.amount').attr("data-id"),
                                           amount:$(row[i]).find('input.amount').val(),
                                           Employee_Name:$('td:eq(2)',row[i]).html()});
                }
            }
            var Data = new FormData(document.getElementById('multi-advance-payment-form'));
            Data.append('employee_advance',JSON.stringify(employee_advance));
            Data.append('is_actual',0)
            $('#advancePaymnetLoader').removeClass('hidden');
            $.ajax({
                url: "HRMAjax/addMultiAdvancePayment",
                data: Data,
                method: "POST",
                processData: false,
                contentType: false,
                complete: function (v1, v2) {
                    if (v2 == "success") {
                        if (v1.responseText === 'OK') {
                            toastr.success('<?php echo CommonLanguage::getMessage('Submitted_Successfully');?>')
                            buildCurrentTable();
                        }
                        $('#advancePaymnetLoader').addClass('hidden');
                    }
                }
            })
        })
    });
    function buildCurrentTable(){
        $.ajax({
            url: "HRMAjax/multi_advance_payment?is_actual="+is_actual,
            complete: function (v1, v2) {
                if (v2 == "success") {
                    $("#tableWithSearch").DataTable().destroy();
                    $("#tableWithSearch tbody").empty().html(v1.responseText)
                    RebuildDataTable($('.panel-heading .panel-title').html(), [], "", {}, "#tableWithSearch");
                }
            }
        });
    }
    function applyMainAmount(input){
        var current_input =$(input).val()
        var row = $("#tableWithSearch").dataTable().fnGetNodes();
        for (var i = 0; i < row.length; i++) {
            $(row[i]).find("input.amount").val(current_input? current_input : '');
        }

    }
</script>
