"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Reseller = void 0;
const RawView_1 = require("../lib/view/RawView");
const RawNonQuery_1 = require("../lib/model/RawNonQuery");
const Session_1 = require("../lib/model/Session");
const _406_1 = require("../lib/view/406");
const AppRoute_1 = require("../lib/AppRoute");
const RawQuery_1 = require("../lib/model/RawQuery");
const bcrypt = __importStar(require("bcrypt"));
class Reseller extends AppRoute_1.AppRoute {
    constructor() {
        super();
    }
    saveReseller(req, res, next) {
        let session = new Session_1.SessionManagment(req, res, next);
        session.GetSession((error, sessdata) => {
            const ipAddress = req.socket.remoteAddress;
            let sdata = req.body.data;
            let password = bcrypt.hashSync(sdata.password, 10);
            let obj = new RawQuery_1.ModelRawQuery(req, res);
            obj.qrysql = "SELECT up.email ,u. username FROM `user` u JOIN `user_profile` up ON up.iduser=u.iduser WHERE username = '" + sdata.user_name + "' OR email = '" + sdata.email + "'";
            obj.prepare();
            obj.execute((_error, getUser) => {
                if (_error == 1) {
                    if (getUser.length > 0) {
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({
                            message: "Username or Email Already Exists!",
                            status: 404
                        });
                        objv.execute();
                    }
                    else {
                        // To Create New Reseller
                        // account_type = 4 for reseller
                        let objbl0 = new RawNonQuery_1.ModelRawNonQuery(req, res);
                        objbl0.nonqrysql = "INSERT INTO `account` SET `idtariff1`='', idtariff2='', `account_type`=4, `balance`=0, `credit` = 0";
                        objbl0.prepare();
                        objbl0.execute((__errAcc, inserAcc) => {
                            if (__errAcc == 1 && inserAcc.insertId > 0) {
                                let idaccount = inserAcc.insertId;
                                // To Insert Data in Company
                                let objb9 = new RawNonQuery_1.ModelRawNonQuery(req, res);
                                objb9.nonqrysql = "INSERT INTO `company` (`idaccount`) VALUES ('" + idaccount + "')";
                                objb9.prepare();
                                objb9.execute((__errCom, inserCom) => {
                                    if (__errCom == 1 && inserCom.length > 0) {
                                        console.log("Account Data Inserted!");
                                    }
                                });
                                // insert Address in billing address
                                let objb10 = new RawNonQuery_1.ModelRawNonQuery(req, res);
                                objb10.nonqrysql = "INSERT INTO `billing_address` SET `idaccount`='" + idaccount + "',`address1`='" + sdata.address1 + "', `address2`='" + sdata.address2 + "', `city`='" + sdata.city + "', `state`='" + sdata.state + "', `country`='" + sdata.country + "', `postal_code`='" + sdata.postal_code + "'";
                                objb10.prepare();
                                objb10.execute((__errAdd, insertAddress) => {
                                    if (__errAdd == 1 && insertAddress.length > 0) {
                                        console.log("Address Inserted!");
                                    }
                                });
                                // To Insert Data in Theme
                                // type = 4 for reseller
                                let objb8 = new RawNonQuery_1.ModelRawNonQuery(req, res);
                                objb8.nonqrysql = "INSERT INTO  `theme`  SET `idaccount`='" + idaccount + "', `brandName`='Signalmash', `domain`= 'https://portal.signalmash.com/',  `customLogo`='assets/img/logo.png', `email`='signalmash@portal.signalmash.com/', `phone`='1234567890', `support_url`='https://portal.signalmash.com/', `header_color`='blue', `navbar_color`='blue', `sidebar_color`='blue', `ssl_certificate`='test', `ssl_key`='test'";
                                objb8.prepare();
                                objb8.execute((__errTheme, inserTheme) => {
                                    if (__errTheme == 1 && inserTheme.length > 0) {
                                        console.log("Theme Data Inserted!");
                                    }
                                });
                                // To Insert Data in Theme
                                // type = 4 for reseller
                                let objb7 = new RawNonQuery_1.ModelRawNonQuery(req, res);
                                objb7.nonqrysql = "INSERT INTO `user` SET `idaccount`='" + idaccount + "', `username`='" + sdata.user_name + "', `password`='" + password + "', `idtariff`='', `idservice`='" + sdata.service + "', `type`='4', `status`= 'false',`isEmailVerify`='" + sdata.isEmailVerify + "',`isMobileVerify`='" + sdata.isMobileVerify + "',`isAddress`='" + sdata.isAddress + "',`isPlan`='false'";
                                objb7.prepare();
                                objb7.execute((__errUser, inserUser) => {
                                    if (__errUser == 1 && inserUser.insertId > 0) {
                                        let iduser = inserUser.insertId;
                                        let key = "iduser=" + iduser + "&idaccount=" + idaccount + "&tariffid=" + sdata.tariff;
                                        // To Insert Data in User Profile
                                        let objb6 = new RawNonQuery_1.ModelRawNonQuery(req, res);
                                        objb6.nonqrysql = "INSERT INTO  `user_profile` SET `iduser`='" + iduser + "',`account_id`='" + idaccount + "', `firstname`='" + sdata.first_name + "', `lastname`='" + sdata.last_name + "', `email`='" + sdata.email + "', `phone`='" + sdata.contact_number + "',`profile_pic`='https://portal.signalmash.com/assets/img/icon.png'";
                                        objb6.prepare();
                                        objb6.execute((__errUP, inserUP) => {
                                            if (__errUP == 1 && inserUP.insertId > 0) {
                                                let idUP = inserUP.insertId;
                                                //To Insert data in tariff_seller table
                                                let QI = 0;
                                                let iQry = "";
                                                for (let x of sdata.tariff) {
                                                    iQry += "INSERT INTO `tariff_seller` (`idaccount`, `iduser`, `idtariff`) VALUES(" + idaccount + ", " + iduser + ", '" + x.idtariff + "');\n";
                                                    QI++;
                                                }
                                                if (QI > 0) {
                                                    let obj5 = new RawNonQuery_1.ModelRawNonQuery(req, res);
                                                    obj5.nonqrysql = iQry;
                                                    obj5.prepare();
                                                    obj5.execute((errTrf, resultTrf) => { });
                                                }
                                                let objv = new RawView_1.RawView(res);
                                                objv.prepare({ status: 200, message: "User Profile Created Successfully" });
                                                objv.execute();
                                                console.log("User Profile Data Inserted!");
                                            }
                                            else {
                                                let objv = new RawView_1.RawView(res);
                                                objv.prepare({ status: 502, message: "Seller Profile Not Created Successfully!" });
                                                objv.execute();
                                            }
                                        });
                                        console.log("User Data Inserted!");
                                    }
                                });
                            }
                        });
                    }
                }
                else {
                    let objv = new RawView_1.RawView(res);
                    objv.prepare({ message: "Something Went Wrong With Connection!", status: 502 });
                    objv.execute();
                }
            });
        });
    }
    updateResellerbyID(req, res, next) {
        let session = new Session_1.SessionManagment(req, res, next);
        session.GetSession((error, sessdata) => {
            let sdata = req.body.data;
            let password = bcrypt.hashSync(sdata.password, 10);
            let obj = new RawNonQuery_1.ModelRawNonQuery(req, res);
            obj.nonqrysql = "DELETE FROM `tariff_seller` WHERE iduser='" + sdata.iduser + "'";
            obj.prepare();
            obj.execute((_delError, delTariffSeller) => {
                if (_delError == 1) {
                    let obj0 = new RawNonQuery_1.ModelRawNonQuery(req, res);
                    obj0.nonqrysql = "INSERT INTO `tariff_seller` (`idaccount`,`iduser`, `idtariff`) VALUES(" + sdata.idaccount + "," + sdata.iduser + ", " + sdata.tariff[0].idtariff + ")";
                    obj0.prepare();
                    obj0.execute((_inserTariffSellerError, inserTariffSeller) => {
                        if (_inserTariffSellerError == 1) {
                            let obj1 = new RawNonQuery_1.ModelRawNonQuery(req, res);
                            obj1.nonqrysql = "update user set `isEmailVerify`='" + sdata.isEmailVerify + "',`isMobileVerify`='" + sdata.isMobileVerify + "',`isAddress`='" + sdata.isAddress + "',`isPlan`='" + sdata.isPlan + "',`idservice`='" + sdata.service + "' where `iduser`='" + sdata.iduser + "'";
                            obj1.prepare();
                            obj1.execute((_userError, updateUser) => {
                                let obj2 = new RawNonQuery_1.ModelRawNonQuery(req, res);
                                obj2.nonqrysql = "update user_profile set `firstname`='" + sdata.first_name + "',`lastname`='" + sdata.last_name + "',`email`='" + sdata.email + "',`phone`='" + sdata.contact_number + "' where `iduser`='" + sdata.iduser + "'";
                                obj2.prepare();
                                obj2.execute((_upError, updateUp) => {
                                    let obj3 = new RawNonQuery_1.ModelRawNonQuery(req, res);
                                    obj3.nonqrysql = "update billing_address set `address1`='" + sdata.address1 + "',`address2`='" + sdata.address2 + "',`city`='" + sdata.city + "',`country`='" + sdata.country + "',`state`='" + sdata.state + "',`postal_code`='" + sdata.postal_code + "' where `idaccount`='" + sdata.idaccount + "'";
                                    obj3.prepare();
                                    obj3.execute((_upError, updateUp) => {
                                        if (_upError == 1) {
                                            let objv = new RawView_1.RawView(res);
                                            objv.prepare({
                                                message: "Reseller Data Update SuccessFully",
                                                status: 200
                                            });
                                            objv.execute();
                                        }
                                        else {
                                            let objv = new RawView_1.RawView(res);
                                            objv.prepare({ message: "Something Went Wrong!", status: 502 });
                                            objv.execute();
                                        }
                                    });
                                });
                            });
                        }
                    });
                }
                else {
                    let objv = new RawView_1.RawView(res);
                    objv.prepare({ message: "Something Went Wrong With Connection!", status: 502 });
                    objv.execute();
                }
            });
        });
    }
    getReseller(req, res, next) {
        let session = new Session_1.SessionManagment(req, res, next);
        session.GetSession((error, sessdata) => {
            if (error == 1) {
                let obj = new RawQuery_1.ModelRawQuery(req, res);
                // obj.qrysql = "SELECT u.* ,up.*,ts.*,t.*,s.name AS serviceName,ba.address1,ba.address2,ba.city,ba.state,ba.postal_code,ba.country FROM `user` u INNER JOIN `user_profile` up ON u.iduser=up.iduser INNER JOIN `tariff_seller` ts ON ts.iduser=up.iduser INNER JOIN `tariff` t ON ts.idtariff=t.idtariff INNER JOIN `service` s ON u.idservice=s.idservice INNER JOIN `billing_address` ba ON u.idaccount=ba.idaccount"
                obj.qrysql = "SELECT DISTINCT r.iduser,r.idaccount,r.idservice,r.username,r.password,r.type,r.isEmailVerify,r.isMobileVerify,r.isAddress,r.isPlan,ext_name,rp.firstname,rp.lastname,rp.email,rp.phone,ts.tariff_seller_id,t.name,t.description,s.name,s.description,ba.address1,ba.address2,ba.city,ba.state,ba.postal_code,ba.country,ba.idbilling_address FROM `reseller` r INNER JOIN `reseller_profile` rp ON r.iduser=rp.iduser INNER JOIN `tariff_seller`  ts ON ts.iduser=rp.iduser INNER JOIN `tariff` t ON ts.idtariff=t.idtariff INNER JOIN `service` s ON r.idservice=s.idservice INNER JOIN `billing_address` ba ON r.idaccount=ba.idaccount WHERE r.is_deleted='0' AND r.type=4 GROUP BY r.username";
                obj.prepare();
                obj.execute((_error, result) => {
                    if (_error == 1) {
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({ data: result, message: "Reseller Fetched Successfully!", status: 200 });
                        objv.execute();
                    }
                    else {
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({ message: "Something Went Wrong With Connection!", status: 502 });
                        objv.execute();
                    }
                });
            }
            else {
                let objv = new _406_1.Res406(res);
                objv.prepare("No session data there");
                objv.execute();
            }
        });
    }
    commissionTerm(req, res, next) {
        let sdata = req.query;
        let session = new Session_1.SessionManagment(req, res, next);
        session.GetSession((err, sessdata) => {
            if (err == 1) {
                let obj = new RawQuery_1.ModelRawQuery(req, res);
                obj.qrysql = "SELECT invoice_monthly_key,status FROM `commission_terms`";
                obj.prepare();
                obj.execute((_error, result) => {
                    if (result) {
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({ message: "Fetched Data Successfully!", data: result });
                        objv.execute();
                    }
                    else {
                        let objv = new RawView_1.RawView(res);
                        objv.prepare({ message: "Something Went Wrong!" });
                        objv.execute();
                    }
                });
            }
        });
    }
}
exports.Reseller = Reseller;
