$(function () {
    getTableData();
});

var objTab = null;

function getTableData() {
    $.post("/pqs9900/sysset/getTimerManage", function (data) {
        if (data.code == 200) {
            initTableData(data.body);
        } else {
            initTableData([]);
        }
    }, "json");
}

function initTableData(data) {
    var tableData =[];
    var offset = 0;

    while (offset < data.length) {
        var temp = [];
        temp.push(data[offset].name);
        temp.push(data[offset].descriptions);
        temp.push(data[offset].strState);
        temp.push("");
        temp.push(data[offset].guid);
        tableData.push(temp);
        offset++;
    }


    console.log(tableData)

    objTab = $('#example').DataTable({
        data: tableData,
        lengthChange: false,
        info: false,
        paging: false,
        destroy: true,
        searching: true,
        ordering: false,
        language: {
            url: '/pqs9900/json/chine.json'
        },
        createdRow: function (row, data, index) {
            var $btnView = $('<button type="button" class="btn btn-danger ml10"><i class="glyphicon glyphicon-trash"></i>关闭</button><button type="button" class="btn themeBGColor updateData ml10"><i class="glyphicon glyphicon-edit"></i>启用</button>');
            $('td', row).eq(3).append($btnView);
        }
    });
}

$('#example').on("click", ".btn-danger", function () {
    var item = objTab.row($(this).closest('tr')).data();

    $.post("/pqs9900/sysset/setTimerState", {
        guid: item[4],
        state: 1
    }, function (data) {
        if (data.code == 200) {
            layer.msg("设置成功");
            location.reload();
        } else {
            layer.msg("设置失败");
        }
    }, "json");
})

$('#example').on("click", ".updateData", function () {
    var item = objTab.row($(this).closest('tr')).data();

    $.post("/pqs9900/sysset/setTimerState", {
        guid: item[4],
        state: 0
    }, function (data) {
        if (data.code == 200) {
            layer.msg("设置成功");
            location.reload();
        } else {
            layer.msg("设置失败");
        }
    }, "json");
})