const { reject, compose } = require('async');
const { resolve } = require('path');

//const auth=MODULE('https_modules/Hauth');
const dbase=CONFIG("dbase");
const conURL=CONFIG("conURL");

const dbaseA=CONFIG("dbaseA");
const conURLA=CONFIG("conURLA");

exports.install = function() {

    GROUP(['#user'],function(){
        
	//-------------------------tavtaha site rout

	ROUTE('api/',view_panel,['authorize','@admin','@administrator','@user']); // 2 MB

	ROUTE('/api/contacts',view_contacts); 
	ROUTE('/api/contacts',UPDcontacts, ['post','upload','@administrator','@admin']); 

	ROUTE('/api/icon',view_UPDicon, ['authorize','@administrator']); 
	ROUTE('/api/icon',UPDicon, ['post','upload','@administrator']);

	ROUTE('api/err/',errhtml); // 3 MB
	
	
    });


};
var smsmodel={sms:"",data:{}};

async function view_panel(req, res) {
	var self = this;
	var user = self.user;
	var model=[];
	var partial=require(PATH.root('/controllers/partial/panel'));
	self.layout('layouts/admin');
	self.view('/admin/panel',model);
}

function errhtml(req, res) {
	//var sms="";
	var self = this;
	self.layout('layouts/admin');
	self.view('err_page',smsmodel);
}



async function view_contacts(req, res) {
	//var sms="";
	var self = this; 

	const Hfiles =MODULE('https_modules/Hfiles');
	var fs_name=PATH.root('/models/contact.json');
	var js=await Hfiles.read_jasoFile(fs_name);
	var model=js.data;

	self.layout('layouts/admin');
	self.view('~../views/contact/admin_contact',model);
}

async function UPDcontacts(req,res){
	var self=this;
	var result={"status":44};
	var body=self.req.body;
	if(valid(body["address"]),valid(body["phone_1"]),valid(body["phone_2"]),valid(body["telegram"]),valid(body["whatsapp"]),valid(body["instagram"]),valid(body["eeta"]),valid(body["bale"])){
		const Hfiles =MODULE('https_modules/Hfiles');
		var fs_name=PATH.root('/models/contact.json');
		var js=await Hfiles.read_jasoFile(fs_name);
		js.data['address']=body["address"];
		js.data['phone_1']=body["phone_1"];
		js.data['phone_2']=body["phone_2"];
		js.data['telegram']=body["telegram"];
		js.data['whatsapp']=body["whatsapp"];
		js.data['instagram']=body["instagram"];
		js.data['bale']=body["bale"];
		js.data['eeta']=body["eeta"];
		var res=await Hfiles.write_jasoFile(fs_name,js.data);
		if(res.status==200){
			result.status=200;
		}else{
			result.status=44;
		}
	}else{
		result.status=700;
	}
	result['data']=F.resource(result.status);
	self.json(result);
}



async function view_UPDicon(req, res) {
	//var sms="";
	var self = this; 
	var model={};
	const mongo=MODULE('https_modules/HmongoDB');
		const con=await mongo.connfull(conURLA);
		if(con.status==200){
			const file=MODULE('https_modules/Hfiles');
			var filename = PATH.public("images/logo/logo_1.jpg");
			var checkFile=await file.checkfile(filename);
			console.log(checkFile);
			if(checkFile.data){
				model['ax']=["1"];
			}
		}else{
			model["status"]=500;
			
		}
		mongo.disconn(con.data);

	self.layout('layouts/admin');
	self.view('~../views/admin/UPDicon',model);
}



async function UPDicon(req,res){
	var self=this;
	var result={"status":44};
	var body=self.req.body;
	if(valid(body["address"]),valid(body["phone_1"]),valid(body["phone_2"]),valid(body["telegram"]),valid(body["whatsapp"]),valid(body["instagram"]),valid(body["eeta"]),valid(body["bale"])){
		const Hfiles =MODULE('https_modules/Hfiles');
		var fs_name=PATH.root('/models/contact.json');
		var js=await Hfiles.read_jasoFile(fs_name);
		js.data['address']=body["address"];
		js.data['phone_1']=body["phone_1"];
		js.data['phone_2']=body["phone_2"];
		js.data['telegram']=body["telegram"];
		js.data['whatsapp']=body["whatsapp"];
		js.data['instagram']=body["instagram"];
		js.data['bale']=body["bale"];
		js.data['eeta']=body["eeta"];
		var res=await Hfiles.write_jasoFile(fs_name,js.data);
		if(res.status==200){
			result.status=200;
		}else{
			result.status=44;
		}
	}else{
		result.status=700;
	}
	result['data']=F.resource(result.status);
	self.json(result);
}




































































