
<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="setting_alert"></div>
            <div class="panel-title"> <?php print CommonLanguage::getMessage('attendance_setting'); ?></div>

            <div class="clearfix"></div>
        </div>
        <div class="panel-body">
            <form id="attendance_setting_form">
                <div class="row form-group">
                    <div class="col-md-12 m-l-5 hidden">
                        <div class="col-md-3">
                            <label><?php echo CommonLanguage::getMessage('setting') ?></label>
                            <input type="hidden" id="all_setting" name="all_setting" class="full-width" value="1"/>
                        </div>
                    </div>
                    <div class="col-md-12 hidden" >
                        <hr>
                    </div>
                    <div class="col-md-12">
                        <div class="col-md-3">
                            <label><?php echo CommonLanguage::getMessage('Name') ?><span class="red-star">*</span></label>
                            <input type="text" id="setting_name" name="setting_name" class="form-control"/>
                        </div>
                    </div>
                    <div class="col-md-12">
                        <div class="col-md-3">
                            <label><?php echo CommonLanguage::getMessage('on_duplicate_check') ?></label>
                            <select id="duplicate_check_type" name="duplicate_check_type" class="full-width" data-init-plugin="select2">
                                <option value="first"><?php echo CommonLanguage::getMessage('first') ?></option>
                                <option value="last"><?php echo CommonLanguage::getMessage('last') ?></option>
                            </select>
                        </div>
                        <div class="col-md-3">
                            <label><?php echo CommonLanguage::getMessage('duplicate_check_period_in_min') ?></label>
                            <input type="number" min="0" id="duplicate_check_period" name="duplicate_check_period" class="form-control" step="any"/>
                        </div>
                        <div class="col-md-3">
                            <label><?php echo CommonLanguage::getMessage('max_work_period_in_hr') ?></label>
                            <input type="number" min="0" id="max_work_period" name="max_work_period" class="form-control" step="any"/>
                        </div>
                    </div>
                </div>
            </form>
            <div class="row">
                <div class="col-md-12">
                    <div class="col-md-3 pull-right">
                        <button class="btn btn-success full-width" id="submit_setting"><?php echo CommonLanguage::getMessage('Save') ?></button>
                    </div>
                </div>
            </div>
        </div>



    </div>
</div>


<script>
    var title = <?php echo json_encode(CommonLanguage::getMessage('salary_periods')); ?>,
            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 = true, IDToEdit = 0;
    $(function () {
        build_select2($("#all_setting"), "xhr/CommonSearch?col_id=att_setting_id&col_name=setting_name&table=tbl_attendance_setting")

        setTimeout(function(){
            $("#all_setting").val(1).trigger("change")
        },300)
        

        $("#submit_setting").on("click", function () {
            $("#setting_alert").removeClass('hidden')

            if ($("#setting_name").val() == '') {
                $("#setting_alert").attr("class", "alert alert-danger").html(Required_Fields)
                return false;

            }
            var DataArr = new FormData(document.getElementById("attendance_setting_form"));
            DataArr.append("period_id", IDToEdit)
            DataArr.append("is_edit", (IsEdit ? 1 : 0))
            $.ajax({
                url: "HRMAjax/add_edit_attendance_setting",
                method: "POST",
                dataType: 'json',
                data: DataArr,
                processData: false,
                contentType: false,
                complete: function (v1, v2) {
                    if (v2 == "success" && v1.responseJSON == 'OK') {
                        $("#setting_alert").attr("class", "alert alert-success").html(Submitted_Successfully)
                        setTimeout(function () {
                            $("#setting_alert").addClass('hidden')
                            $("#add_attendance_setting").modal("hide")
                        }, 5000)
                    } else {
                        $("#setting_alert").attr("class", "alert alert-danger").html(v1.responseJSON)
                    }
                }
            })
        })

        $("#all_setting").on('change', function () {
//            alert()
            if ($(this).val() == '') {
                IsEdit = false;
                IDToEdit = 0;
                $("#setting_name").val('')
                $("#duplicate_check_period").val('')
                $("#max_work_period").val('')
                $("#duplicate_check_type").val('').select2()
            } else {
                IsEdit = true;
                IDToEdit = $(this).val();
                $.ajax({
                    url: "HRMAjax/get_att_setting_by_id",
                    method: "GET",
                    dataType: 'json',
                    data: {id: IDToEdit},
                    complete: function (v1, v2) {
                        if (v2 == "success") {
                            var data = v1.responseJSON;
                            $("#setting_name").val(data['setting_name'])
                            $("#duplicate_check_period").val(data['duplicate_check_period'])
                            $("#max_work_period").val(data['max_work_period'])
                            $("#duplicate_check_type").val(data['duplicate_check_type']).select2()
                        }
                    }
                })
            }
        })


    })





</script>
