$(function () { common.checkboxInit("device"); $("#device-list").on('pjax:success', function () { common.checkboxInit("device"); if ($("#checkbox-device-all:checked").length > 0) $('.checkbox-device').iCheck('check'); }); $('#checkbox-device-all').iCheck({ checkboxClass: 'icheckbox_flat-red' }); $('#checkbox-device-all').on('ifChanged', function (event) { if (event.target.checked) { $('.checkbox-device').iCheck('check'); } else { $('.checkbox-device').iCheck('uncheck'); } }); }); function validate() { var error = 0; var Name = $("input[name='Name']").val(); if (Name === "") { common.error("name", "Tên thiết bị không được để trống"); error++; } else { common.success("name"); } var Ip = $("input[name='Ip']").val(); if (Ip === "") { common.error("ip", "Địa chỉ IP không được để trống"); error++; } else if (!common.validateIp(Ip)) { common.error("ip", "Định dạng địa chỉ IP không đúng"); error++; } else { common.success("ip"); } return error == 0 ? true : false; } function checkConnection(e) { var Ip = $("input[name='Ip']").val(); if (Ip === "") { common.error("ip", "Địa chỉ IP không được để trống"); } else if (!common.validateIp(Ip)) { common.error("ip", "Định dạng địa chỉ IP không đúng"); } else { common.success("ip"); common.modalBlock(true); $.ajax({ url: $(e).attr('href'), type: 'POST', data: { Ip: $("input[name='Ip']").val() }, success: function (data) { common.modalBlock(false); $(".alert").addClass("hidden"); if (data.Status === "SUCCESS") { $("#success").removeClass("hidden"); } else { $("#error").removeClass("hidden"); } }, error: function (jqXHR, textStatus, errorThrown) { common.modalBlock(false); alert("Có lỗi xảy ra! hãy kiểm tra lại service!"); } }); } } function save(e) { if (validate()) { common.modalBlock(true); $.ajax({ url: $(e).attr('data-href'), type: 'POST', data: { Name: $("input[name='Name']").val(), Area: $("select[name='Area']").val(), Ip: $("input[name='Ip']").val() }, success: function (data) { common.modalBlock(false); if (data.status) { notification.success("Đã lưu thông tin", 1000); $.pjax.reload({container: '#device-list'}); $("#device-list").on('pjax:success', function () { common.checkboxInit("device"); if ($("#checkbox-device-all:checked").length > 0) $('.checkbox-device').iCheck('check'); }); $("#myModal").modal("hide"); } else { if (data.type === "ip") { common.error("ip", "Địa chỉ ip đã tồn tại"); } else if (data.type === "connect") { $(".alert").addClass("hidden"); $("#error").removeClass("hidden"); } else { notification.danger("Có lỗi xảy ra, không lưu được dữ liệu!", 1000); } } }, error: function (jqXHR, textStatus, errorThrown) { common.modalBlock(false); alert("Có lỗi xảy ra! hãy kiểm tra lại service!"); } }); } } function _form(e) { var lists = []; $.each($("input[name='checkbox-device']:checked"), function () { lists.push($(this).val()); }); if (lists.length == 0) { alert("Vui lòng lựa chọn đối tượng để thay đổi!"); return; } if (lists.length > 1) { alert("Tác vụ này không thể lựa chọn nhiều hơn một đối tượng!"); return; } common.modalBlock(true); $.ajax({ url: $(e).attr('data-href') + "?id=" + lists[0], type: 'POST', success: function (data) { common.modalBlock(false); common.modalOpen(data.form, false, data.title); $('#Area').select2(); }, error: function (jqXHR, textStatus, errorThrown) { common.modalBlock(false); common.ajaxError(); } }); } function _delete(e) { var lists = []; $.each($("input[name='checkbox-device']:checked"), function () { lists.push($(this).val()); }); if (lists.length == 0) { alert("Vui lòng lựa chọn đối tượng để xóa!"); return; } if (lists.length == 1 && lists[0] === "1") { alert("Danh mục gốc không thể xóa!"); return; } if (confirm("Bạn có chắc chắn muốn xóa các đối tượng đã lựa chọn không?")) { common.modalBlock(true); $.ajax({ url: $(e).attr('data-href'), type: 'POST', data: { lists: lists }, success: function (data) { common.modalBlock(false); notification.danger("Đã xóa dữ liệu", 1000); $.pjax.reload({container: '#device-list'}); $("#device-list").on('pjax:success', function () { common.checkboxInit("device"); if ($("#checkbox-device-all:checked").length > 0) $('.checkbox-device').iCheck('check'); }); }, error: function (jqXHR, textStatus, errorThrown) { common.modalBlock(false); common.ajaxError(); } }); } } function _export(e) { window.location = $(e).attr("data-href"); } function _logs(e) { common.modalBlock(true); $.ajax({ url: $(e).attr('data-href'), type: 'POST', success: function (data) { common.modalBlock(false); common.modalOpen(data.form, true, data.title); }, error: function (jqXHR, textStatus, errorThrown) { common.modalBlock(false); common.ajaxError(); } }); } function validateChangeIP() { var error = 0; var Ip = $("input[name='IpNew']").val(); if (Ip === "") { common.error("ip", "Địa chỉ IP mới không được để trống"); error++; } else if (!common.validateIp(Ip)) { common.error("ip", "Định dạng địa chỉ IP không đúng"); error++; } else { common.success("ip"); } var SubnetMask = $("input[name='SubnetMask']").val(); if (SubnetMask === "") { common.error("subnet_mask", "Subnet mask không được để trống"); error++; } else if (!common.validateIp(SubnetMask)) { common.error("subnet_mask", "Định dạng Subnet mask không đúng"); error++; } else { common.success("subnet_mask"); } var Gateway = $("input[name='Gateway']").val(); if (Gateway === "") { common.error("gateway", "Gateway không được để trống"); error++; } else if (!common.validateIp(Gateway)) { common.error("gateway", "Định dạng Gateway không đúng"); error++; } else { common.success("gateway"); } return error == 0 ? true : false; } function changeIP(e) { if (validateChangeIP()) { common.modalBlock(true); $.ajax({ url: $(e).attr('data-href'), type: 'POST', data: { Ip: $("input[name='IpNew']").val(), SubnetMask: $("input[name='SubnetMask']").val(), Gateway: $("input[name='Gateway']").val() }, success: function (data) { common.modalBlock(false); if (data.status) { notification.success("Đổi IP thiết bị thành công", 1000); $.pjax.reload({container: '#device-list'}); $("#device-list").on('pjax:success', function () { common.checkboxInit("device"); if ($("#checkbox-device-all:checked").length > 0) $('.checkbox-device').iCheck('check'); }); $("#myModal").modal("hide"); } else { if (data.type === "ip") { common.error("ip", "Địa chỉ ip đã tồn tại"); } else { notification.danger("Có lỗi xảy ra, không lưu được dữ liệu!", 1000); } } }, error: function (jqXHR, textStatus, errorThrown) { common.modalBlock(false); alert("Có lỗi xảy ra! hãy kiểm tra lại service!"); } }); } } var progress = 0; var totals = 0; var index = 0; var totalPage = 0; var currentPage = 1; var secondIncrement = 0; async function _sync(e) { var all = false; if ($("#checkbox-device-all:checked").length > 0) all = true; var lists = []; $.each($("input[name='checkbox-device']:checked"), function () { lists.push({ id: $(this).val(), ip: $(this).attr("data") }); }); if (lists.length == 0) { alert("Vui lòng lựa chọn đối tượng!"); return; } common.modalBlock(true); $.ajax({ url: $(e).attr('data-href'), type: 'POST', data: { lists: lists, all: all }, success: function (data) { common.modalBlock(false); common.modalOpen(data.form, false, data.title); $("#close-modal").attr("disabled", true); $("#modalHeader").find("button").remove(); totals = data.lists.length; syncSchedule(data.lists[index].id, data.lists[index].ip, data); }, error: function (jqXHR, textStatus, errorThrown) { common.modalBlock(false); common.ajaxError(); } }); } function syncSchedule(device_id, device_ip, fullData) { appendLogs("[" + device_ip + "] Bắt đầu đồng bộ dữ liệu tới thiết bị", "blue", "minus"); $.ajax({ url: $("input[name='URL_sync_schedule']").val(), type: 'POST', data: { data: device_id }, success: function (data) { var totalsStaff = data.staffs; totalPage = parseInt(totalsStaff / parseInt($("input[name='pageSize']").val())); if (totalsStaff % parseInt($("input[name='pageSize']").val()) > 0) totalPage++; secondIncrement = 1 / (totalPage + 1); if (data.res.ErrorCode == "-1") { appendLogs("[" + data.IP + "] Đồng bộ lịch trình tới thiết bị thất bại", "red", "remove"); processProgress(1); index++; if (index < totals) syncSchedule(fullData.lists[index].id, fullData.lists[index].ip, fullData); else { index = 0; } } else { appendLogs("[" + data.IP + "] Đồng bộ lịch trình tới thiết bị thành công", "green", "check"); syncStaffs(device_id, device_ip, currentPage, fullData); processProgress(secondIncrement); } }, error: function (jqXHR, textStatus, errorThrown) { common.ajaxError(); } }); } function syncStaffs(device_id, device_ip, page, fullData) { appendLogs("[" + device_ip + "] Đồng bộ dữ liệu nhân viên tới thiết bị", "", "minus"); $.ajax({ url: $("input[name='URL_sync_staff']").val() + "?page=" + page, type: 'POST', data: { data: device_id }, success: function (data) { if (totalPage == 0) appendLogs("[" + data.IP + "] Không có dữ liệu nhân viên đồng bộ tới thiết bị", "orange", "info-circle"); else { appendLogs("[" + data.IP + "] Đồng bộ dữ liệu " + data.staff + " nhân viên từ " + data.from + " đến " + data.to + " tới thiết bị", "green", "check"); processProgress(secondIncrement); currentPage++; } if (totalPage > 0 && currentPage <= totalPage) syncStaffs(device_id, device_ip, currentPage, fullData); else { currentPage = 1; index++; if (index < totals) syncSchedule(fullData.lists[index].id, fullData.lists[index].ip, fullData); else { index = 0; } } }, error: function (jqXHR, textStatus, errorThrown) { common.ajaxError(); } }); } function getDataSync(data) { $.ajax({ url: $("input[name='get_data_sync_url']").val(), type: 'POST', data: { data: data }, success: function (data) { var html = ""; html = " Bắt đầu đồng bộ dữ liệu tới thiết bị " + data.IP + ".
"; $("#logs-response").append(html); if (data.schedule.length == 0 && data.staffs.length == 0) { var html = ""; html = " Không có dữ liệu cần đồng bộ tới thiết bị " + data.IP + ".
"; $("#logs-response").append(html); progress += 2; var percent = parseInt(progress / totals * 100); $("#progress").attr("aria-valuenow", percent); $("#progress").attr("style", "width: " + percent + "%"); $("#progress").html(percent + "%"); if (percent >= 100) { progress = 0; $("#close-modal").attr("disabled", false); } return; } syncSchedule(data.schedule, data.IP); }, error: function (jqXHR, textStatus, errorThrown) { common.ajaxError(); } }); } var progressLogs = 0; var totalsLogs = 0; function _getLogs(e) { var all = false; if ($("#checkbox-device-all:checked").length > 0) all = true; var lists = []; $.each($("input[name='checkbox-device']:checked"), function () { lists.push($(this).val()); }); if (lists.length == 0) { alert("Vui lòng lựa chọn đối tượng!"); return; } common.modalBlock(true); $.ajax({ url: $(e).attr('data-href'), type: 'POST', data: { lists: lists, all: all }, success: function (data) { common.modalBlock(false); common.modalOpen(data.form, false, data.title); $("#close-modal").attr("disabled", true); $("#modalHeader").find("button").remove(); totalsLogs = data.lists.length; for (var i = 0; i < data.lists.length; i++) { syncLogs(data.lists[i]); } }, error: function (jqXHR, textStatus, errorThrown) { common.modalBlock(false); common.ajaxError(); } }); } function syncLogs(data) { $.ajax({ url: $("input[name='get_logs_sync_url']").val(), type: 'POST', data: { data: data }, success: function (data) { var html = ""; html = " Đồng bộ thành công " + data.totals + " sự kiện từ thiết bị " + data.IP + ".
"; $("#logs-response").prepend(html); progressLogs++; var percent = parseInt(progressLogs / totalsLogs * 100); $("#progress").attr("aria-valuenow", percent); $("#progress").attr("style", "width: " + percent + "%"); $("#progress").html(percent + "%"); if (percent >= 100) { progressLogs = 0; $("#close-modal").attr("disabled", false); } }, error: function (jqXHR, textStatus, errorThrown) { common.ajaxError(); } }); } function appendLogs(text, cls, icon) { var html = " " + text + "
"; $("#logs-response").prepend(html); } function processProgress(ai) { progress = progress + ai; var percent = parseInt(progress / totals * 100); if (percent >= 98) percent = 100; $("#progress").attr("aria-valuenow", percent); $("#progress").attr("style", "width: " + percent + "%"); $("#progress").html(percent + "%"); if (percent >= 100) { progress = 0; $("#close-modal").attr("disabled", false); } } function openDoor(e) { common.modalBlock(true); $.ajax({ url: $(e).attr('data-href'), type: 'POST', success: function (data) { common.modalBlock(false); if (data.ErrorCode === "0") notification.success("Mở cửa thành công", 2000); else notification.error("Mở cửa thất bại, không có kết nối tới thiết bị", 2000); }, error: function (jqXHR, textStatus, errorThrown) { common.modalBlock(false); common.ajaxError(); } }); } function openFormEditDoor(e, stt) { var id = $(e).attr("data"); if (stt) { $(".real-" + id).addClass("hidden"); $(".edit-" + id).removeClass("hidden"); } else { $(".real-" + id).removeClass("hidden"); $(".edit-" + id).addClass("hidden"); } } function editDoor(e) { var id = $(e).attr("data"); var name = $("#name-input-" + id).val(); if (name === "") { alert("Tên thiết bị không được để trống"); return; } var duration = $("#duration-input-" + id).val(); if (parseInt(duration) > 0 && parseInt(duration) < 255) { common.modalBlock(true); $.ajax({ url: $(e).attr('data-href'), type: 'POST', data: { duration: duration, name: name }, success: function (data) { common.modalBlock(false); var id = parseInt(data.id); $("#name-text-" + id).html(name); $("#duration-text-" + id).html(duration); $(".real-" + id).removeClass("hidden"); $(".edit-" + id).addClass("hidden"); $("#device-ls-" + data.device_id).html(data.form); notification.success("Đã lưu thông tin", 2000); }, error: function (jqXHR, textStatus, errorThrown) { common.modalBlock(false); common.ajaxError(); } }); } else { alert("Thời gian mở cửa phải từ 1-254 giây"); return; } } function changeDuration(e) { common.modalBlock(true); $.ajax({ url: $(e).attr('data-href'), type: 'POST', success: function (data) { common.modalBlock(false); var id = parseInt(data.id); $(".change-duration-" + id).removeClass("btn-warning").addClass("btn-default"); if (data.always_close || data.always_open) { $(e).removeClass("btn-default").addClass("btn-warning"); if (data.always_close) { $("#always-open-" + id).attr("disabled", true); } if (data.always_open) { $("#always-close-" + id).attr("disabled", true); } } else { if (!data.always_close) { $("#always-open-" + id).attr("disabled", false); } if (!data.always_open) { $("#always-close-" + id).attr("disabled", false); } } notification.success("Đã lưu thông tin", 2000); }, error: function (jqXHR, textStatus, errorThrown) { common.modalBlock(false); common.ajaxError(); } }); } var progressOpenDoor = 0; var totalsDoor = 0; function openAllDoors(e) { var all = false; if ($("#checkbox-device-all:checked").length > 0) all = true; var lists = []; $.each($("input[name='checkbox-device']:checked"), function () { lists.push($(this).val()); }); if (lists.length == 0) { alert("Vui lòng lựa chọn đối tượng!"); return; } common.modalBlock(true); $.ajax({ url: $(e).attr('data-href'), type: 'POST', data: { lists: lists, all: all }, success: function (data) { common.modalBlock(false); common.modalOpen(data.form, false, data.title); $("#close-modal").attr("disabled", true); $("#modalHeader").find("button").remove(); totalsDoor = data.lists.length; for (var i = 0; i < data.lists.length; i++) { openAllDoorOfDevice(data.lists[i]); } }, error: function (jqXHR, textStatus, errorThrown) { common.modalBlock(false); common.ajaxError(); } }); } function openAllDoorOfDevice(data) { $.ajax({ url: $("input[name='URL_open_door']").val(), type: 'POST', data: { data: data }, success: function (data) { console.log(data.response); var html = ""; if (data.response.ErrorCode === "999") html = " Mở " + data.totals + " cửa từ thiết bị " + data.IP + ".
"; else html = " Kết nối tới thiết bị " + data.IP + " thất bại.
"; $("#logs-response").prepend(html); progressOpenDoor++; var percent = parseInt(progressOpenDoor / totalsDoor * 100); $("#progress").attr("aria-valuenow", percent); $("#progress").attr("style", "width: " + percent + "%"); $("#progress").html(percent + "%"); if (percent >= 100) { progressOpenDoor = 0; $("#close-modal").attr("disabled", false); } }, error: function (jqXHR, textStatus, errorThrown) { common.ajaxError(); } }); } var progressCloseDoor = 0; var totalsDoorToClose = 0; function closeAllDoors(e) { var all = false; if ($("#checkbox-device-all:checked").length > 0) all = true; var lists = []; $.each($("input[name='checkbox-device']:checked"), function () { lists.push($(this).val()); }); if (lists.length == 0) { alert("Vui lòng lựa chọn đối tượng!"); return; } common.modalBlock(true); $.ajax({ url: $(e).attr('data-href'), type: 'POST', data: { lists: lists, all: all }, success: function (data) { common.modalBlock(false); common.modalOpen(data.form, false, data.title); $("#close-modal").attr("disabled", true); $("#modalHeader").find("button").remove(); totalsDoorToClose = data.lists.length; for (var i = 0; i < data.lists.length; i++) { closeAllDoorOfDevice(data.lists[i]); } }, error: function (jqXHR, textStatus, errorThrown) { common.modalBlock(false); common.ajaxError(); } }); } function closeAllDoorOfDevice(data) { $.ajax({ url: $("input[name='URL_close_door']").val(), type: 'POST', data: { data: data }, success: function (data) { console.log(data.response); var html = ""; if (data.response.ErrorCode === "999") html = " Đóng " + data.totals + " cửa từ thiết bị " + data.IP + ".
"; else html = " Kết nối tới thiết bị " + data.IP + " thất bại.
"; $("#logs-response").prepend(html); progressCloseDoor++; var percent = parseInt(progressCloseDoor / totalsDoorToClose * 100); $("#progress").attr("aria-valuenow", percent); $("#progress").attr("style", "width: " + percent + "%"); $("#progress").html(percent + "%"); if (percent >= 100) { progressCloseDoor = 0; $("#close-modal").attr("disabled", false); } }, error: function (jqXHR, textStatus, errorThrown) { common.ajaxError(); } }); }