import { Component } from '@angular/core'; import { IonicPage, NavController, NavParams, Platform } from 'ionic-angular'; import { AppInit } from '../../providers/app-init/app-init'; import { AppUtil } from '../../providers/app-util/app-util'; import { AD_SysConfig } from '../../providers/setting/setting'; import { AppConf } from '../../providers/app-conf/app-conf'; import { DbSvc } from '../../providers/db/db'; import { DataSync } from '../../providers/data-sync/data-sync'; import { FileSvc } from '../../providers/file/file-service'; @IonicPage() @Component({ selector: 'page-setting', templateUrl: 'setting.html', }) export class SettingPage { sysConfigList: any = []; constructor(public navCtrl: NavController, public navParams: NavParams, public appInit: AppInit, public appUtil: AppUtil, public adSysConfig: AD_SysConfig, public appConf: AppConf, public dbSvc: DbSvc, public dataSync: DataSync, public platform: Platform, public fileSvc: FileSvc) { } ionViewDidLoad() { this.sysConfigList = Object.assign([], this.adSysConfig.filterByConfigType()); //Object.assign(this.adSysConfig.filterByConfigType(), this.sysConfigList); for (var i = 0; i < this.sysConfigList.length; i++) { if (this.sysConfigList[i].datatype == "number" || this.sysConfigList[i].datatype == "checkbox") { this.sysConfigList[i].value = JSON.parse(this.sysConfigList[i].value); } else if (this.sysConfigList[i].datatype == "select") { this.sysConfigList[i].description = JSON.parse(this.sysConfigList[i].description); } } } inputSubString(input){ return input.substring(input.lastIndexOf('.') + 1, input.length); } goBack() { var me = this; if (me.appConf.isLogin() == true) { me.navCtrl.setRoot('HomePage'); } else { me.navCtrl.setRoot('LoginPage'); } } setLoggingLevel(level) { console.log(level); } onSysSettingSubmit() { var me = this; me.adSysConfig.addArray(me.sysConfigList).then(function (res) { me.adSysConfig.init().then(response => { if (me.appConf.isLogin()) { me.navCtrl.setRoot('HomePage'); } else { me.navCtrl.setRoot('LoginPage'); } }, error => { console.log("SysSettingCtrl -> onSysSettingSubmit -> AD_SysConfig.init() -> " + error); }); }, err => { console.log("SysSettingCtrl -> onSysSettingSubmit -> AD_SysConfig.addArray() -> " + err); }); } dbCleanUtil() { var me = this; if (me.appConf.getLoginRequest() == null) { me.appUtil.alert("Authentication Require to clean database", "System"); } else { var tableList = []; me.appInit.clearAppSyncHistory().then(function (response) { me.dbSvc.query("SELECT name FROM sqlite_master where type = 'table'").then(function (result) { tableList = me.dbSvc.fetchAll(result); var sql = 'DROP Table '; for (var i = 0; i < tableList.length; i++) { if (tableList[i].name == 'android_metadata') { continue; } me.dbSvc.query(sql + tableList[i].name); } me.appUtil.alert('Database has been cleaned', 'Database Cleaned').then(function () { me.dbSvc.init().then(function (response) { me.adSysConfig.init(); //$state.go('appLogin'); me.navCtrl.setRoot('LoginPage'); me.appConf.clearLocalStorage(); }, function (error) { console.error("sysSettingCtrl -> $scope.dbCleanUtil -> DBSvc.init() -> " + error); }); }); }, function (error) { console.error("sysSettingCtrl -> $scope.dbCleanUtil -> Select Query" + error); me.appUtil.alert("Database not cleaned, Restart app to try", 'Database Cleaned'); }); }, function (reject) { console.error("sysSettingCtrl -> $scope.dbCleanUtil -> AppInit.clearAppSyncHistory()" + reject); }); } } exportDB() { var me = this; this.fileSvc.createSqlFile().then(function (res) { console.info("sysSettingCtrl -> $scope.exportDB -> FileSvc.createSqlFile() -> Database has Been Exported"); me.appUtil.alert("Database has Been Exported", 'File-info'); }, function (err) { console.error("sysSettingCtrl -> $scope.exportDB -> FileSvc.createSqlFile()", err); }); } /*$scope.viewLogs = function() { $fileLogger.getLogfile().then(function(writeData) { $scope.logs = ''; var lines = angular.toJson(writeData).split('\\n'); $scope.lines = []; for (var i = 0; i < lines.length; i++) { $scope.lines.push({ "lineno": i, "val": lines[i] }); } var fileName = new Date().getTime() + "logs.log"; FileSvc.writeFile(FileSvc.directory.appData(), fileName, writeData, false).then(function(res) { FileSvc.readAsDataURL(FileSvc.directory.appData(), fileName).then(function(res_read) { res_read = res_read.replace('data:null;base64,', ''); AppUtil.confirm("Do you want to export to the Server ?", "Export").then(function() { if (AppConf.isLogin()) { DataSync.attachData([{ "Table": HomeSvc.tableName, "Rows": [{ "UU": HomeSvc.list[0].x_route_uu, // TODO: change the x route uu method. "AlwaysAdd": "Y", "Attachments": [{ "Content": res_read, "FileName": AppConf.getUserName().replace(/ /g, '') + "_" + fileName }] }] }]).then(function(res_upload) { console.info(Console.Info("sysSettingCtrl -> $scope.viewLogs -> FileSvc.writeFile() -> DataSync.attachData() -> Log File Uploaded to the DataBase")); }, function(err_upload) { console.error(Console.Error("sysSettingCtrl -> $scope.viewLogs -> FileSvc.writeFile() -> DataSync.attachData() -> Log File Failed Uploaded to the DataBase", err_upload)); }); } else { AppUtil.alert("Login Required to Export Data to Server"); } }, function(err) { console.info(Console.Info("sysSettingCtrl -> $scope.viewLogs -> FileSvc.writeFile() -> Cancel by the User.")); }); }, function(err_read) { console.info(Console.Info("sysSettingCtrl -> $scope.viewLogs -> FileSvc.readAsDataURL()")); }); console.info(Console.Info("sysSettingCtrl -> $scope.viewLogs -> FileSvc.writeFile() -> Log File Exported to storage")); }, function(err) { console.error(Console.Error("SysSettingCtrl -> $scope.viewLogs -> FileSvc.writeFile() -> ", err)); }); }, function(err) { console.error(Console.Error("SysSettingCtrl -> $scope.viewLogs -> $fileLogger.getLogfile()() -> ", err)); }); }*/ }