// const { log } = require('async');
const m = require('gm');
const auth=MODULE('https_modules/auth/Hauth');
const TrezSmsClient = require("trez-sms-client");
const client = new TrezSmsClient(CONFIG("Rusername"),CONFIG("Rpassword"));

var ObjectId = require('mongodb').ObjectID;
 
const mong=MODULE('https_modules/HmongoDB');

const partial=require(PATH.root('/controllers/partial/panel'));
const dbase=CONFIG("dbase");
const dbaseA=CONFIG("dbaseA");

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




exports.install = function() {
    GROUP(['#contact'],function(){
        ROUTE('/', view_index);
        ROUTE('/products/*', view_products);
        ROUTE('/product/details/{id}', view_productDetails);
        ROUTE('/aboutUs', view_aboutUs);
        ROUTE('/contactUs', view_contactUs);
        
        LOCALIZE('/components/*.html', ['compress']);
    });
};



async function view_index() {
	var self = this;
    var model={};

	const Hfiles =MODULE('https_modules/Hfiles');
	var fs_name=PATH.root('/models/about.json');
	var js=await Hfiles.read_jasoFile(fs_name);
	model["aboutUs"]=js.data['us'];
  model["aboutCompany"]=js.data['company'];

    fs_name=PATH.root('/models/slider.json');
    js=await Hfiles.read_jasoFile(fs_name);
    var slider=js.data['mainPage'];
    console.log(slider);

    const mongo=MODULE('https_modules/HmongoDB');
    const con=await mongo.connfull(conURLA);
   
    if(con.status==200){
        let conn=con.data;
        const dbo = conn.db(dbase);
        var aggrigate=[
            {
              '$match': {
                'code': {
                  '$in': slider
                }
              }
            }
          ];
        var s=await dbo.collection('product').aggregate(aggrigate).toArray();
        if(s.length>0){
            model.slider=s;
        }
    }



    // if(model.list.ax)model.list.ax=model.list.ax.slice(0,model.list.ax.length-1);
  // schemaFUNC(self,"گروه صنعتی تاوطاها","","","https://www.tavtaha.ir/images/logo/SAVE_20230516_204955.jpg","","https://www.tavtaha.ir/images/logo/SAVE_20230516_204955.jpg");
   
	self.layout('layouts/layout');
	self.view('HomePage',model);
}
////////////////////////




///////////////////////
async function view_products() {
	var self = this;
    var model=[];

	var filter=decodeURI(self.url);
	filter=filter.replace("/products/","");
    (filter[filter.length-1]=="/")?filter=filter.slice(0, -1):""; 
	var lst=[];
    var t="";

    var query={};
    self.repository.brand="";
    self.repository.color="";
    self.repository.open="all";

	x=filter.indexOf('brand:');
    if(x>=0){
            y=(filter.indexOf('|',x)!=-1)?filter.indexOf('|',x):filter.length;
            t=filter.slice(x+6,y);
            filter=(y<filter.length)?filter.replace("brand:"+t+"|",""):filter.replace("brand:"+t,"");
            lst.push({brand:t});
            self.repository.brand=t;
    }
    
    x=filter.indexOf('color:');
    if(x>=0){
            y=(filter.indexOf('|',x)!=-1)?filter.indexOf('|',x):filter.length;
            t=filter.slice(x+6,y);
            filter=(y<filter.length)?filter.replace("color:"+t+"|",""):filter.replace("color:"+t,"");
            lst.push({"color.v":t});
            self.repository.color=t;
    }
        

    x=filter.indexOf('open:');
    if(x>=0){
            y=(filter.indexOf('|',x)!=-1)?filter.indexOf('|',x):filter.length;
            t=filter.slice(x+5,y);
            filter=(y<filter.length)?filter.replace("open:"+t+"|",""):filter.replace("open:"+t,"");
            lst.push({open:(t=='1')?true:false});
            self.repository.open=t;
    }

	x=filter.indexOf('hashtags:');
    if(x>=0){
            y=(filter.indexOf('|',x)!=-1)?filter.indexOf('|',x):filter.length;
            t=filter.slice(x+9,y);
            filter=(y<filter.length)?filter.replace("hashtags:"+t+"|",""):filter.replace("hashtags:"+t,"");
            lst.push({hashtags:t});
            // self.repository.open=t;
    }

	query=(lst.length>0)?{$and:lst}:{};

	var partial=require(PATH.root('/controllers/product/partial/product_view'));
	var model=await partial.AdminproductsList(self,dbaseA,query);


    const Hfiles =MODULE('https_modules/Hfiles');
    fs_name=PATH.root('/models/slider.json');
    js=await Hfiles.read_jasoFile(fs_name);
    var slider=js.data['suggestion'];
    console.log(slider);

    const mongo=MODULE('https_modules/HmongoDB');
    const con=await mongo.connfull(conURLA);
    if(con.status==200){
        let conn=con.data;
        const dbo = conn.db(dbase);
        var aggrigate=[
            {
              '$match': {
                'code': {
                  '$in': slider
                }
              }
            }
          ];
        var s=await dbo.collection('product').aggregate(aggrigate).toArray();
        if(s.length>0){
            model.slider=s;
        }
    }




	console.log(model);
	self.layout('layouts/layout');
	self.view('products',model);
}



async function view_productDetails() {
	var self = this;
    var model={};
	var id=new ObjectId(self.params.id);
	var partial=require(PATH.root('/controllers/product/partial/product_view'));
	var res=await partial.view_UPDadminProduct(dbaseA,{_id:id});
    model=res.data;
    model.colors=res.colors;
    model.brands=res.brands;


    
    const Hfiles =MODULE('https_modules/Hfiles');
    fs_name=PATH.root('/models/slider.json');
    js=await Hfiles.read_jasoFile(fs_name);
    var slider=js.data['product_details_slider'];
    console.log(slider);

    const mongo=MODULE('https_modules/HmongoDB');
    const con=await mongo.connfull(conURLA);
    if(con.status==200){
        let conn=con.data;
        const dbo = conn.db(dbase);
        var aggrigate=[
            {
              '$match': {
                'code': {
                  '$in': slider
                }
              }
            }
          ];
        var s=await dbo.collection('product').aggregate(aggrigate).toArray();
        if(s.length>0){
            model.slider=s;
        }
    }





    console.log(model);

	self.layout('layouts/layout');
	self.view('product_details',model);
}

async function view_aboutUs() {
	//var sms="";
	var self = this;
	const Hfiles =MODULE('https_modules/Hfiles');
	var fs_name=PATH.root('/models/about.json');
	var js=await Hfiles.read_jasoFile(fs_name);
	var model=js.data['us'];
	self.layout('layouts/layout');
	self.view('About',model);
}

async function view_contactUs(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;
    console.log(model);
	self.layout('layouts/layout');
	self.view('contactUs',model);
}


// function schemaFUNC(controller,title,keywords,description,url,twitsite,ogIMGurl){
//   console.log(title+"55555555555554444444");
//   var self=controller;
//   self.repository.seo.title="تاوطاها|"+title;
//   self.repository.seo.keywords=keywords;
//   self.repository.seo.description=description;
//   self.repository.seo.url=url;
//   self.repository.seo.twitsite =twitsite;
//   self.repository.seo.ogtype ="website";
//   self.repository.seo.domain ="tavtaha.ir";
//   self.repository.seo.ogIMGurl=ogIMGurl;
//   self.repository.seo.ogIMGw ="500";
//   self.repository.seo.ogIMGh ="500";
//   self.repository.seo.ogIMGalt ="گروه صنعتی تاوطاها";
//   self.repository.seo.ogIMGtype ="image/jpg";
//   self.repository.seo.twitcreator ="@tavtaha";

// }







