/**
 * index
 *
 * @author: fengwang
 * @date: 2017-9-11 17:39
 * @version: 1.0
 */
$(function () {
    $(".select2").removeClass("hidden");
    $(".select2").select2();
    $(".select2").on("change", function (e) {
        search();
    });

    //搜索关键字
    var keyword = '';

    var table = $('#attendanceTable').DataTable({
        paging: true,
        lengthChange: false,
        searching: false,
        ordering: false,
        order: [[1, "desc"]],
        info: true,
        autoWidth: false,
        processing: true,
        serverSide: true,
        ajax: {
            url: gContextPath + "/a/class/" + $('#srcClassId').val() + "/attendance/list",
            type: "POST",
            data: function (d) {
                d.keyword = keyword;
                d.attendanceType = $("#attendanceType").val();
            }
        },
        columnDefs: [
            {
                render: function (data, type, row) {
                    var html = [];
                    html.push('<img src="' + gContextPath + '/res/images/' + row.studentAvatar + '" class="console-user-image" alt="User Image">');
                    html.push('<span class="attendance-student">' + row.student.studentName + '</span>');
                    return html.join('');
                },
                targets: 0
            },
            {
                render: function (data, type, row) {
                    var html = '<span class="attendance-cardNo">' + row.attendance.cardNo + '</span>';
                    return html;
                },
                targets: 1
            },
            {
                render: function (data, type, row) {
                    var html = '<span class="attendance-attendanceTime">' + row.attendance.attendanceTime + '</span>';
                    return html;
                },
                targets: 2
            },
            {
                render: function (data, type, row) {
                    var html = '';
                    var attendanceType = '';
                    if ('0' == row.attendance.attendanceType) {
                        attendanceType = '<span class="label bg-green">Go in</span>'
                    } else if ('1' == row.attendance.attendanceType) {
                        attendanceType = '<span class="label bg-yellow">Go out</span>'
                    }
                    html += attendanceType;

                    return html;
                },
                targets: 3
            },
            {
                render: function (data, type, row) {
                    var html = '<span class="attendance-deviceNo">' + row.attendance.deviceNo + '</span>';
                    return html;
                },
                targets: 4
            },
            {
                render: function (data, type, row) {
                    var html = '<span class="attendanceDevice-deviceName">' + row.attendanceDevice.deviceName + '</span>';
                    return html;
                },
                targets: 5
            }
        ]
    });

    table.on('draw', function () {
        //绑定事件
        bindEvent();
    });

    //搜索
    function search() {
        keyword = $('.search-keyword').val();
        table.search(keyword).draw();
    }

    //绑定事件
    function bindEvent() {

    }

    $(".table-search").click(function () {
        search();
    });
});