<?php if (empty($emp_name)) { ?>
    <div class="alert alert-danger" role="alert">
        <button class="close" data-dismiss="alert"></button>
        <strong><?php echo CommonLanguage::getMessage('danger') ?>: </strong>
        <?php echo CommonLanguage::getMessage('you_can_not_view_payrol_for_this_employee') ?>! 
        <br>
        <?php echo CommonLanguage::getMessage('this_employee_is_not_available') ?>
        .
    </div>
    <?php die();
} ?>
<style>
    .f-18{
        font-size:20px;
    }
</style>
<div class="wrapper">
    <div class="panel invoice">
        <div class="panel-heading">
            <div id="PaidSuccess"></div>
            <div class="panel-title pull-left">
                <h1><?php print CommonLanguage::getMessage('payroll') . ' : ' . $emp_name; ?>
                    <br>
                    <?php echo CommonLanguage::getMessage('Date') ?>&nbsp;:&nbsp;<span class="date-title"></span>
                    <br> 
                    <?php echo CommonLanguage::getMessage('balance') . ' : ' . CommonFunctions::formatNumber($emp_balance) . ' ' . smc_session()['local_currency_symbol']; ?>

                </h1></div>
        </div>
        <div class="panel-body">

            <div class="row">
                <div class="col-xs-3">
                    <div class="invoice-logo">
                        <img src="<?= upload_url; ?>org_logo/invoice_logo.jpg" alt=""style="max-width:250px"/>
                    </div>
                </div>
            </div>
            <div class="row no-print">
                <div class="col-md-3 month-div <?php echo $pay_period == 1 ? '' : 'hidden' ?>">
                    <label><?php echo CommonLanguage::getMessage('month') ?></label>
                    <input class="month_year form-control" type="text" data-date-format="yyyy-mm" value=""  />
                </div>
                <div class=" week-div <?php echo $pay_period == 0 ? '' : 'hidden' ?>">
                    <div class=" col-md-3">
                        <label><?php echo CommonLanguage::getMessage('start') ?></label>
                        <input class="from_date form-control" type="text"  />
                    </div>
                    <div class=" col-md-3">
                        <label><?php echo CommonLanguage::getMessage('end') ?></label>
                        <input class="to_date form-control" type="text"  readonly/>
                    </div>
                </div>


            </div>
            <div class="table-responsive">
                <input type="hidden" id="Employee_ID" value="<?php echo $empid ?>"/>
                <div id="hint_div" class="text-center alert  m-t-10" style="height:100%;background-color:#CFF5F2"> <h3 ><?php print CommonLanguage::getMessage('choose_date_first'); ?> </h3> </div>

                <table class="table  table-hover m-t-10" id="payroll_table">
                    <tbody>

                    </tbody>

                </table>
            </div>
        </div>
        <br/>
        <br/>
        <div class="row">
            <div class="col-xs-8" id="InvoiceBottomPriceInWords">
                <h4></h4>
            </div>
            <div class="col-xs-4">
                <table class="table table-hover" id="TotalTable">
                    <tbody>
                        <tr>
                            <td class="v-align-middle text-center" style="display:none"><span class="ValSpan TotalVal" style="opacity: 1;"><?php echo $Is_Paid ? $payroll[0]['other_prices']['Net_Pay'] : $payroll['Total_Net_Pay'] ?></span></td>
                            <td class='v-align-middle text-center'>
                                <?php print CommonLanguage::getMessage('signature'); ?> :
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
        <br/>
        <br/>
        <br/>
        <div class="row no-print">
            <div class="col-md-12">


                <div class="pull-right col-md-3 m-t-20">
                    <a class="btn btn-success hidden"  id="SubmitPaid"><i class="fa fa-dollar"></i> <span id="paid_span"><?php print CommonLanguage::getMessage('pay'); ?></span></a>

                    <button class="btn btn-danger" id="printBtn" ><i class="fa fa-print"></i> <?php print CommonLanguage::getMessage('print'); ?></button>


                </div>
                <div class="pull-right col-md-3">
                    <label><?php echo CommonLanguage::getMessage('paid_amount') ?></label>
                    <input id="paid_amount" type="number" class="form-control"  name="paid_amount" step="any" autocomplete="off">
                </div>
                <div class="pull-right col-md-3 paid_account_div hidden">
                    <label><?php echo CommonLanguage::getMessage('payment_account') ?></label>
                    <select id="paymentType" class="full-width AccountCode" name="paymentType" data-init-plugin="select2">
                        <?php foreach ($payment_type as $type): ?>
                            <option value="<?php echo $type['Account_ID']; ?>" ><?php echo $type['Account_Code'] . ' - ' . $type['Account_Name']; ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </div>
        </div>
        <br>
        <br>
    </div>
</div>
<div class="modal fade" id="confirm_submit" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title"><?php print CommonLanguage::getMessage('confirm_action') ?></h4>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-md-12 m-t-10">
                        <p><?php print CommonLanguage::getMessage('submit_payroll_confirm') ?></p>
                        <p><?php print CommonLanguage::getMessage('Proceed') ?></p>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal"><?php print CommonLanguage::getMessage('Close') ?></button>
                <button type="button" class="btn btn-danger width-auto" id="proceed_submit"><?php print CommonLanguage::getMessage('Save') ?></button>
            </div>
        </div>
    </div>
</div>
<script>
    var Submitted_Successfully = <?php echo json_encode(CommonLanguage::getMessage('Submitted_Successfully')); ?>,
            ZeroLang = <?php echo json_encode(CommonLanguage::getMessage('zero')); ?>,
            SayLang = <?php echo json_encode(CommonLanguage::getMessage('say')); ?>,
            pay_lang = <?php print json_encode(CommonLanguage::getMessage('pay')); ?>,
            repay_lang = <?php print json_encode(CommonLanguage::getMessage('repay')); ?>,
            edit_pay_perm = <?php echo json_encode($permissions->hasPermission('edit_payroll')); ?>,
            Failed_Submit = <?php echo json_encode(CommonLanguage::getMessage('Failed_Submit')); ?>;
    var actual = <?php echo json_encode($is_actual) ?>;
    var emp_id = <?php echo $empid ?>;
    var payroll_data = [];
    $(document).ready( function() {
       
 });
    $(function () {
        
//        console.log(emp_id)
        $(".month_year").datepicker({autoclose: true,
            viewMode: "months",
            minViewMode: "months"})
//        build_select2($('.Payment_Method'), 'xhr/accSearch?type=Payroll')
        $(".month_year").on("changeDate", function () {
            var date = new Date($(this).val() + '-01T12:00:00');
            var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate();
            window.startDate = $(this).val() + '-01';
            window.endDate = $(this).val() + '-' + lastDay;
            build_payroll()
              
             
//            to_date: endDate,
//            console.log(startDate + '==' + endDate)
        })
        $(".from_date,.to_date").datepicker({autoclose: true})
        $(".from_date").on("changeDate", function () {
            var date2 = $(this).datepicker('getDate', '+6d');
            date2.setDate(date2.getDate() + 6);
            $('.to_date').datepicker('setDate', date2);
            window.startDate = $(this).val()
            window.endDate = $('.to_date').val()
//            console.log("hello")
            build_payroll()

        })
        $("#printBtn").on("click", function () {
            var myurl = "HRM/print_payroll?emp_id=" + emp_id + '&from_date=' + startDate + '&to_date=' + endDate;
            printExternal(myurl)
        })
        $("#SubmitPaid").on("click", function () {
            $("#confirm_submit").modal("show")
        })
        $("#proceed_submit").on("click", function () {
            $("#PaidSuccess").attr("class", "hidden");
            var Data = payroll_data;
            Data['main']['paid_amount'] = $("#paid_amount").val();
            Data['main']['Payment_Method'] = $("#paymentType").val();
            Data['main']['from_date'] = window.startDate;
            Data['main']['to_date'] = window.endDate;
            Data['main']['Actual'] = actual;
//                Employee_ID: $("#Employee_ID").val(),
//                Employee_Name: $("#Name").attr("data-value"),
//                Basic_Salary: $("#basic_salary").attr("data-value"),
//                Net_Pay: $("#Total_Net_Pay").attr("data-value"),
//                Lunch: $("#Lunch").attr("data-value"),
//                Transportation: $("#Transportation").attr("data-value"),
//                total_other_charges: $("#total_other_charges").attr("data-value"),
//                Total_Allowance_New: $("#Family").attr("data-value"),
//                Fam_Allow: $("#Allowance").attr("data-value"),
//                Familly_Allow: $("#Fam_Tax").attr("data-value"),
//                Phone: $("#Phone").attr("data-value"),
//                SSNF: $("#SSNF").attr("data-value"),
//                Gross: $("#Gross").attr("data-value"),
//                Med_Ins: $("#MedIns").attr("data-value"),
//                Tax: $("#Tax").attr("data-value"),
//                Leav_Ind: $("#Leave_Ind").attr("data-value"),
//                Total_Comp: $("#Total").val(),
//                Advance_Payment: $("#advance_payment").attr("data-value"),
//                Loan_Payment: $("#loan_payment").attr("data-value"),
//                Absent_Amount: $("#Absent").attr("data-value"),
//                Deduction: $("#Deduc").attr("data-value"),
//                Bonus: $("#Bonus").attr("data-value"),
//                OverTime: $("#OverTime").attr("data-value"),
//                Currency_ID: $("#currency_id").val(),
//                Payroll_Paid_Date: $("#Payroll_Paid_Date").val(),
//                Payment_Method: $("#Payment_Method").val(),
//                Account_Name: $.trim($(".Payment_Method").text()),
//                Total_Deduction: $("#Total_Deduction").attr("data-value"),
//                Taxable: $("#Taxable").attr("data-value"),

//console.log(Data)
//return
            $.ajax({
                url: "HRMAjax/PaidPayrollForEmployee",
                data: Data,
                method: "POST",
                complete: function (v1, v2) {
                    if (v2 == "success" && v1.responseText == 'OK') {
                        $("#PaidSuccess").attr("class", "alert alert-success").html(Submitted_Successfully)
                        $('#SubmitPaid').attr('disabled', 'disabled')
                        setTimeout(function () {
                            $("#PaidSuccess").attr("class", "hidden")

                        }, 5000)
                    } else {
                        $("#PaidSuccess").attr("class", "alert alert-danger").html(v1.responseText)
                    }
                    $("#confirm_submit").modal("hide")
                    $(document.body).animate({scrollTop: 0}, 800)

                }
            });
        })
    })
     function build_payroll() {
//        var emp_id = payroll_data['main']['Employee_ID'];

        $("#PaidSuccess").addClass("hidden")
        var p_data = {
            emp_id: emp_id,
            from_date: startDate,
            to_date: endDate,
        };
        $.ajax({
            url: "HRMAjax/get_payroll_per_emp",
            data: p_data,
            dataType: 'json',
            complete: function (v1, v2) {
                if (v2 == "success") {
                    payroll_data = v1.responseJSON['data'];
                    if ("error" in payroll_data) {
                        $("#PaidSuccess").attr("class", "alert alert-danger").html(payroll_data['error'])
                        $(document.body).animate({scrollTop: 0}, 800)
                    } else {
                        if (payroll_data['main'].length == 0)
                            return false;
                        $(".date-title").html(startDate + ' - ' + endDate)
                        $("#hint_div").addClass("hidden")
                        $("#payroll_table tbody").empty().html(v1.responseJSON['html'])
                        console.log(v1.responseJSON['html']);
                        $("#paid_amount").val(payroll_data['Amount_Paid'])
                        if (payroll_data['payment_account_id'] != 0)
                            $("#paymentType").val(payroll_data['payment_account_id']).select2()
                        if (payroll_data['is_paid'] == 1 && !edit_pay_perm) {
                            $("#SubmitPaid,.paid_account_div").addClass("hidden")
                            $("#paid_amount").attr("readonly", true)
                        } else {
                            $("#SubmitPaid,.paid_account_div").removeClass("hidden")
                            $("#paid_amount").removeAttr("readonly")
                            if (payroll_data['is_paid'] == 1)
                                $("#paid_span").text(repay_lang)
                            else
                                $("#paid_span").text(pay_lang)
                        }
                    }

                }
            }
        })
       

    }
</script>