360 lines
12 KiB
JavaScript
360 lines
12 KiB
JavaScript
$(function () {
|
|
common.checkboxInit("device");
|
|
});
|
|
|
|
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");
|
|
});
|
|
$("#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");
|
|
});
|
|
},
|
|
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");
|
|
}
|
|
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()
|
|
},
|
|
success: function (data) {
|
|
common.modalBlock(false);
|
|
console.log(data);
|
|
return;
|
|
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");
|
|
});
|
|
$("#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;
|
|
function _sync(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!");
|
|
return;
|
|
}
|
|
common.modalBlock(true);
|
|
$.ajax({
|
|
url: $(e).attr('data-href'),
|
|
type: 'POST',
|
|
data: {
|
|
lists: lists
|
|
},
|
|
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 * 2;
|
|
for (var i = 0; i < data.lists.length; i++) {
|
|
getDataSync(data.lists[i]);
|
|
}
|
|
},
|
|
error: function (jqXHR, textStatus, errorThrown) {
|
|
common.modalBlock(false);
|
|
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 = "<span class='text-blue'><i class='fa fa-minus'></i> Bắt đầu đồng bộ dữ liệu tới thiết bị <b>" + data.IP + "</b>.</span><br>";
|
|
$("#logs-response").append(html);
|
|
syncSchedule(data.schedule, data.staffs, data.IP);
|
|
},
|
|
error: function (jqXHR, textStatus, errorThrown) {
|
|
common.ajaxError();
|
|
}
|
|
});
|
|
}
|
|
|
|
function syncSchedule(schedule, staffs, ip) {
|
|
$.ajax({
|
|
url: $("input[name='sync_schedule_url']").val(),
|
|
type: 'POST',
|
|
data: {
|
|
data: schedule,
|
|
ip: ip
|
|
},
|
|
success: function (data) {
|
|
console.log("sync schedule: ", data.res);
|
|
var html = "";
|
|
html = "<span class='text-green'><i class='fa fa-check'></i> Đồng bộ lịch trình tới thiết bị <b>" + data.IP + "</b>.</span><br>";
|
|
$("#logs-response").append(html);
|
|
progress++;
|
|
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);
|
|
}
|
|
syncStaffs(staffs, ip);
|
|
},
|
|
error: function (jqXHR, textStatus, errorThrown) {
|
|
common.ajaxError();
|
|
}
|
|
});
|
|
}
|
|
|
|
function syncStaffs(data, ip) {
|
|
$.ajax({
|
|
url: $("input[name='sync_staffs_url']").val(),
|
|
type: 'POST',
|
|
data: {
|
|
data: data,
|
|
ip: ip
|
|
},
|
|
success: function (data) {
|
|
console.log("sync user: ", data.resUser);
|
|
var html = "";
|
|
html = "<span class='text-green'><i class='fa fa-check'></i> Đồng bộ nhân viên tới thiết bị <b>" + data.IP + "</b>.</span><br>";
|
|
$("#logs-response").append(html);
|
|
progress++;
|
|
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);
|
|
}
|
|
},
|
|
error: function (jqXHR, textStatus, errorThrown) {
|
|
common.ajaxError();
|
|
}
|
|
});
|
|
}
|