//var socket = io("http://165.73.80.85:12055");
var socketURL = "http://165.73.80.85:12075"; 
socket = io(socketURL);

var util = {
    getreglocationsperdate: function (registration, startdate, callback) {
        var inData = {};
        inData.registration = registration;
        inData.startdate = startdate;
        socket.emit("getreglocationsperdate", inData);
        socket.on("getreglocationsperdate", function (data) {
            callback(data);
        });
    },
    getofficialreport: function (registration, startdate, callback) {
        var inData = {};
        inData.registration = registration;
        inData.startdate = startdate;
        socket.emit("getofficialreportdata", inData);
        socket.on("getofficialreportdata", function (data) {
            callback(data);
            socket.disconnect();
        });
    },
    getInstaller: function (callback) {
        socket.emit("getinstaller", "");
        socket.on("getinstaller", function (data) {
            callback(data);
            socket.disconnect();
        });
    },

    getTodaysInformation: function (callback) {

        socket.emit("gettodayinformation", "");
        socket.on("gettodayinformation", function (data) {
            callback(data);
            //socket.disconnect();
        });
    },

    getAnalyticsdata: function (callback) {

        socket.emit("getanalytics", "");
        socket.on("getanalytics", function (data) {
            callback(data);
            //socket.disconnect();
        });

    },

    getClientsdata: function (callback) {
        //alert("Get Clients");

        socket.emit("getallclients", "");
        socket.on("getallclients", function (data) {
            callback(data);
            socket.disconnect();
        });

    },

    getMonthlyInstalls: function (callback) {

        socket.emit("getmonthlyinstalls", "");
        socket.on("getmonthlyinstalls", function (data) {
            callback(data);
            //socket.disconnect();
        });

    },

    getMonthInstalls: function (label, callback) {

        socket.emit("getmonthinstalls", "");
        socket.on("getmonthinstalls", function (data) {
            callback(data);
            socket.disconnect();
        });

    },

    checkSerialNumber: function (serial, callback) {

        socket.emit("checkserial", serial);
        socket.on("checkserial", function (data) {
            callback(data);
            socket.disconnect();
        });

    },

    saveClientData: function (obj, callback) {

        socket.emit("saveclient", obj);
        socket.on("saveclient", function (data) {
            callback(data);
            socket.disconnect();
        });

    },

    login: function (username, password) {

        var loginInfo = {};
        loginInfo.username = username;
        loginInfo.password = password;

        console.log(loginInfo);

        //alert("login 2");
        socket.emit("login", loginInfo);
        socket.on("login", function (userInfo) {
            console.log(userInfo);
            socket.disconnect();
            if (userInfo.length > 0) {
                console.log(userInfo[0]);
                sessionStorage.setItem("user", JSON.stringify(userInfo[0]));
                window.location = "home.html";

            } else {
                var div = document.getElementsByClassName("card-body");
                div[0].innerHTML +=
                    '<div class="alert alert-danger" role="alert">The email and password did not match our records.</div>';

            }
        });        

    },

    unlinkdevice: function (serialnumber, reason, callback) {

        var devicedata = {};
        devicedata.action = "UNLINKDEVICE";
        devicedata.serialnumber = serialnumber;
        devicedata.reason = reason;
        socket.emit("unlinkdevice", devicedata);
        socket.on("unlinkdevice", function (data) {
            callback(data);
            socket.disconnect();
        });

    },

    deactivate: function (cellphone, callback) {      

        let devicedata = {};
        devicedata.action = "DEACTIVATE";
        devicedata.cellphone = cellphone;

        socket.emit("deactivate", devicedata);
        socket.on("deactivate", function (data) {
            callback(data);
            socket.disconnect();
        });

    },

    activate: function (cellphone, callback) {

        let devicedata = {};
        devicedata.action = "ACTIVATE";
        devicedata.cellphone = cellphone;
        socket.emit("activate", devicedata);
        socket.on("activate", function (data) {
            callback(data);
            socket.disconnect();
        });

    },

    replacedevice: function (oldserial, newserial, callback) {

        let devicedata = {};
        devicedata.action = "REPLACEDEVICE";
        devicedata.oldserial = oldserial;
        devicedata.newserial = newserial;
        socket.emit("replacedevice", devicedata);
        socket.on("replacedevice", function (data) {
            callback(data);
            socket.disconnect();
        });

    },

    getreglocationsperdate: function (registration, date, callback) {

        let inData = {};
        inData.action = "OFFICIALREPORT";
        inData.date = date;
        inData.registrationnumber = registration;

        socket.emit("getreglocationsperdate", inData);
        socket.on("getreglocationsperdate", function (data) {
            callback(data);
            socket.disconnect();
        });

    },

    reinstateContract: function (cellphone, callback) {

        let inData = {};
        inData.action = "REINSTATECONTRACT";
        inData.cellphone = cellphone;

        socket.emit("reinstateContract", inData);
        socket.on("reinstateContract", function (data) {
            callback(data);
            socket.disconnect();
        });

    },

    cancellContract: function (cellphone, callback) {

        let inData = {};
        inData.action = "CANCELLCONTRACT";
        inData.cellphone = cellphone;

        socket.emit("cancellContract", inData);
        socket.on("cancellContract", function (data) {
            callback(data);
            socket.disconnect();
        });
    },
    getnews:function(callback)
    {
        socket.emit("getnewsweb", "");
        socket.on("getnewsweb", function (data) {
            callback(data);
            socket.disconnect();
        });
    },
    savenews:function(news,callback)
    {
        socket = io(socketURL);
        console.log("SAVE NEWS");
        console.log(news);

        socket.emit("savenews", news);
        socket.on("savenews", function (data) {
           callback(data);
        });
    },
    savenewsimage:function(inData)
    {        
        socket = io(socketURL);
        console.log("SAVE NEWS IMAGE");
        console.log(inData);

        socket.emit("savenewsimage", inData);
        socket.disconnect();
    }
}