
<div class="row-fluid">
    <div class="span12">
        <h4 class="lighter" style="color: #2679B5;">
            <i class="icon-hand-right icon-animated-hand-pointer blue"></i>
            Attendance By Excel Sheet  
        </h4>
        <div class="hr hr-18 hr-double dotted"></div>
    </div>    


    <div class="span4" style="margin-left:0">
        <form enctype="multipart/form-data">
            <div class="" >
                <input id="upload" type="file" name="sheet"accept=" application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />
                <label data-title="Choose"><span data-title="No File ..."></label>

            </div>
            <i class="icon-spinner icon-spin orange bigger-125 icon-loading" style="display:none;"></i>
        </form>           
    </div>
</div>
<div class="row-fluid" id="load_attendance_sheet">



</div>
<script>

    var checkfile = function (sender) {
        var validExts = new Array(".xlsx", ".xls");
        var fileExt = sender[0].name;
        fileExt = fileExt.substring(fileExt.lastIndexOf('.'));
        if (validExts.indexOf(fileExt) < 0) {
            //  alert("Invalid file selected, valid files are of " +
            //        validExts.toString() + " types.");
            return false;
        }
        else
            return true;
    }

    $('#upload').ace_file_input({
        no_file: 'No File ...',
        btn_choose: 'Choose',
        btn_change: 'Change',
        droppable: false,
        onchange: null,
        thumbnail: false, //| true | large
        whitelist: 'xls',
        blacklist: 'gif|png|jpg|jpeg|exe|php'
                //onchange:''
                //
    });

    $('#upload').change(function () {
        //fire your ajax call  
        $('#load_attendance_sheet').empty();
		$('.icon-loading').css('display', 'block');
        file_upload();
    });
    var fileInput = $('input[type=file]'),
            button = $('#upload');
    button.on('focusout', function () {

// Access the files property, which holds
// an array with the selected files

    });
    function file_upload() {

        var files = fileInput.prop('files');
// No file was chosen!
        var file_checking = checkfile(files);

        if (files.length == 0) {
            alert('Please choose a file to upload!');
            return false;
        }
        else if (file_checking == false) {
            alert("Invalid file selected");
            return false;
        }
        else {
// Create a new FormData object
            var fd = new FormData();
            fd.append('file', files[0]);
// Upload the file to assets/php/upload.php, which only prints the filename
            $.ajax({
                url: 'module/attendance/attencance_by_sheet/upload_attendance_file.php',
                data: fd,
                contentType: false,
                processData: false,
                type: 'POST',
                success: function (data) {
                    $('.icon-loading').css('display', 'none');
                    var data_all = JSON.parse(data);
                    $('#load_attendance_sheet').html(data_all.design);
                    $('#invalid_id').html(data_all.invalid_id);
                    $('#duplicate_id').html(data_all.duplicate_id);
                    $('#all_valid_id').val(data_all.valid_id);
					

                    $('#sample-table-22').dataTable({
                        "aoColumns": [
                            {"bSortable": true},
                            null, null, null, null, null,
                            {"bSortable": true}
                        ]});

                }
            });
        }
    }

    $("#load_attendance_sheet").on("click", "#btn-save-data", function () {
        var data = {"allAtt": []};
        var allform = $(".each-row");
        var info;
        $(allform).each(function () {
            var uniq_id = $(this).find(".single_uniq_id ").val();
            var attendance_status = $(this).find(".single_attendance_status ").val();
            var late_time = $(this).find(".single_late_time ").val();
            info = {"uniq_id": uniq_id, "attendance_status": attendance_status, "late_time": late_time};
            data.allAtt.push(info);
        });
//	var p = JSON.stringify(data);


	if(!$.isEmptyObject(data.allAtt)){
        $.ajax({
            type: "POST",
            dataType: "json",
            url: "module/attendance/attencance_by_sheet/attendance_sheet_save.php",
            data: data,
            success: function (st) {
				alert(st.message);
				$('#load_attendance_sheet').empty();
            }
        });
	}else{
		alert('Error!!!');

	}

        return false;
    });
    $("#load_attendance_sheet").on("click", ".remove-this", function () {
        $(this).parent().parent().remove();
        return false;
    });
	$("#load_attendance_sheet").on("click", ".clear-all", function () {
       //  $(this).parent().prev().html();
		 $('#load_attendance_sheet').empty();
		 // $('#upload').val('');
        return false;
    });
	
</script>


