<style>
    .table thead tr >th{
        background-color: #4c0870;
        color:#fff;
    }
    .table thead tr th[class*='sorting_'] {
        color: #FFFFFF;
    }
    .bg-master-lighter {
        border:none;
        background-color:#fff;
        border-top:1px solid #ddd; 
    }
    .btn-default{
        border: none;
        background-color: #eee;
    }
    .btn-default:hover{
        background-color:#4c0870 !important;
        color:#fff;
        border: none;
    }
    .p-0{
        padding:0!important;
    }
</style>
<div class="container-fluid container-fixed-lg bg-white">
    <div class="panel panel-transparent">
        <div class="panel-heading">
            <div id="FinancialYearAlert"></div>
            <div class="panel-title"> <?php print CommonLanguage::getMessage('salary_unit_formulas'); ?></div>
            <div class="clearfix"></div>
        </div>
        <div class="panel-body">
            <?php
            if ($permissions->hasPermission('add_salary_unit_formulas')) {
                echo Forms::FloatingAddButton('4c0870', '#add_formula', CommonLanguage::getMessage('add'));
            }
            ?>
            <table class="table table-bordered table-hover demo-table-search" id="tableWithSearch" >
                <thead>
                    <tr>
                        <th class="text-center" style="width:15%" ><?php print CommonLanguage::getMessage('Name'); ?></th>
                        <th class="text-center" style="width:15%" ><?php print CommonLanguage::getMessage('formula'); ?></th>
                        <th class="text-center" style="width:15%" ><?php print CommonLanguage::getMessage('deduction_rate'); ?></th>
                        <th class="text-center" style="width:15%" ><?php print CommonLanguage::getMessage('overtime_rate'); ?></th>
                        <th class="text-center" style="width:15%" ><?php print CommonLanguage::getMessage('Action'); ?></th>
                    </tr>
                </thead>
                <tbody>
                </tbody>
            </table>
        </div>
    </div>
</div>
<div class="modal fade bs-example-modal-md " id="add_formula" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-sm" >
        <div class="modal-content">
            <div class="modal-header clearfix text-left">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true"><i class="pg-close fa-22"></i></span></button>
                <h4 class="modal-title"><?php print CommonLanguage::getMessage('salary_unit_formulas') ?></h4>
                <div id="assignformulaAlert"></div>
            </div>
            <div class="modal-body">
                <form id="assignformulaForm">
                    <div class="row">
                        <div class=" col-md-12 m-t-10 " >
                            <div class=" col-md-6 m-t-10 " >
                                <label class="labelText"><?php print CommonLanguage::getMessage('Name'); ?><span class="red-star">*</span></label>
                                <input type="text" class="form-control" id="formula_name" name="formula_name" required/>
                            </div>
                        </div>
                        <div class=" col-md-12 m-t-10 p-0 " >
                            <div class="alert alert-success" style="margin:0;">
                                <?php print CommonLanguage::getMessage('basic_salary'); ?>&nbsp;<strong>(bs)</strong>:<?php echo CommonLanguage::getMessage('formula_bs_desc')?><br>
                                <?php print CommonLanguage::getMessage('nb_of_hrs'); ?>&nbsp;<strong>(hrs)</strong>:<?php echo CommonLanguage::getMessage('formula_hrs_desc')?>
                            </div>
                        </div>
                        <div class=" col-md-12 m-t-10 p-0" >
                            <label class="labelText"><?php print CommonLanguage::getMessage('hr_price_formula'); ?><span class="red-star">*</span></label>
                            <textarea id="hr_price_formula" class="form-control" name="hr_price_formula" style="resize:vertical;"></textarea>
                        </div>
                        <div class=" col-md-12 m-t-10 " >
                            <div class=" col-md-6 m-t-10 " >
                                <label class="labelText"><?php print CommonLanguage::getMessage('deduction_rate'); ?><span class="red-star">*</span>&nbsp;<i class="fa fa-info-circle" title="<?php echo CommonLanguage::getMessage('deduction_rate_hint') ?>"></i></label>
                                <input type="text" class="form-control" id="deduction_rate" name="deduction_rate" required/>
                            </div>
                            <div class=" col-md-6 m-t-10 " >
                                <label class="labelText"><?php print CommonLanguage::getMessage('overtime_rate'); ?><span class="red-star">*</span>&nbsp;<i class="fa fa-info-circle" title="<?php echo CommonLanguage::getMessage('overtime_rate_hint') ?>"></i></label>
                                <input type="text" class="form-control" id="overtime_rate" name="overtime_rate" required/>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal"><?php print CommonLanguage::getMessage('Close') ?></button>
                <button style="color:white !important;" type="button" class="btn bg-success white" id="assignBtn" style="width:auto"><?php print CommonLanguage::getMessage('Save') ?></button>
            </div>
        </div>
    </div>
</div>
<script>
    var title = <?php echo json_encode(CommonLanguage::getMessage('salary_unit_formulas')); ?>,
        Submitted_Successfully = <?php echo json_encode(CommonLanguage::getMessage('Submitted_Successfully')); ?>,
            Required_Fields = <?php echo json_encode(CommonLanguage::getMessage('Required_Fields')); ?>,
                Failed_Submit = <?php echo json_encode(CommonLanguage::getMessage('Failed_Submit')); ?>;
                var IsEdit = false, IDToEdit = 0;
    $(function () {
        $(".floatingContainer").on("click", function () {
            IsEdit = false;
            IDToEdit = 0;
        })
        RebuildDataTable(title, [0], "HRMAjax/salary_unit_formulas_ajax")
        $("#assignBtn").on("click", function () {
            $("#assignformulaAlert").removeClass('hidden')
            if ($("#formula_name").val() == '' || $("#overtime_rate").val() == '' || $("#deduction_rate").val() == '' || $("#hr_price_formula").val()=='') {
                $("#assignformulaAlert").attr("class", "alert alert-danger").html(Required_Fields)
                return false;
            }
            var DataArr = new FormData(document.getElementById("assignformulaForm"));
            DataArr.append("formula_id", IDToEdit)
            DataArr.append("is_edit", (IsEdit ? 1 : 0))
            $.ajax({
                url: "HRMAjax/add_edit_salary_formula",
                method: "POST",
                dataType: 'json',
                data: DataArr,
                processData: false,
                contentType: false,
                complete: function (v1, v2) {
                    if (v2 == "success" && v1.responseJSON == 'OK') {
                        $("#assignformulaAlert").attr("class", "alert alert-success").html(Submitted_Successfully)
                        RebuildDataTable(title, [0], "HRMAjax/salary_unit_formulas_ajax")
                        setTimeout(function () {
                            $("#assignformulaAlert").addClass('hidden')
                            $("#add_formula").modal("hide")
                        }, 5000)
                    } else {
                        $("#assignformulaAlert").attr("class", "alert alert-danger").html(v1.responseJSON)
                    }
                }
            })
        })
        $("#tableWithSearch tbody").on('click', '.Edit', function () {
            IsEdit = true;
            IDToEdit = $(this).attr('data-id');
            $("#formula_name").val($(this).parents("tr").find("td:eq(0)").text())
            $("#hr_price_formula").val($(this).parents("tr").find("td:eq(1)").text())
            $("#deduction_rate").val($(this).parents("tr").find("td:eq(2)").text())
            $("#overtime_rate").val($(this).parents("tr").find("td:eq(3)").text())
            $("#add_formula").modal("show")
        })
    })
</script>
