const homepage = require('../../models/homepage');
const helper = require('../../helper');
const express = require("express");
//const bodyParser = require("body-parser");
const router = express.Router();
const config = require("../../config")
//var jsonParser = bodyParser.json()
//var urlencodedParser = bodyParser.urlencoded({ extended: false })
const app = express();
app.use(express.json());

router.post("/dashboard", async (req, res, next) => {
     if(helper.check_headers(req) == "True"){
        var section1 = await  homepage.get_homepage_section1()
        section1.image = config.baxter_url+"uploads/homepage/"+section1.image
        section1.play_store_image = config.baxter_url+"uploads/homepage/"+section1.play_store_image
        section1.apple_store_image = config.baxter_url+"uploads/homepage/"+section1.apple_store_image

        //await homepage.sleep()
        var section2 = await  homepage.get_homepage_section2()
        var section2_head = await  homepage.get_homepage_section2_heading()

        var section3 = await  homepage.get_homepage_section3()
        var section3_head = await  homepage.get_homepage_section3_heading()
        section3_head.image = config.baxter_url+"uploads/homepage/"+section3_head.image

        var section4 = await  homepage.get_homepage_section4()
        var section4_head = await  homepage.get_homepage_section4_heading()

        var section5_data = await  homepage.get_homepage_section5()
        var section5 = [];
        section5_data.map(function(val, index) {
            temp = config.baxter_url+"uploads/homepage/"+val.image
            section5.push(temp)
        });
        var section5_head = await  homepage.get_homepage_section5_heading()

        var section6_data = await  homepage.get_homepage_section6()
        var section6 = [];
        section6_data.map(function(val, index) {
            temp = {
                "image":config.baxter_url+"uploads/homepage/"+val.image,
                "name":val.name,
                "position":val.position,
                "review":val.review
            }
            section6.push(temp)
        });
        var section6_head = await  homepage.get_homepage_section6_heading()

        var section7 = await  homepage.get_homepage_section7()
        section7.left_image = config.baxter_url+"uploads/homepage/"+section7.left_image
        section7.playstoreimage = config.baxter_url+"uploads/homepage/"+section7.playstoreimage
        section7.applestoreimage = config.baxter_url+"uploads/homepage/"+section7.applestoreimage
        //section1.id = helper.encrypt(section1.id.toString());
        res.status(200).json({
            message: "Successfull",
            status: "True",
            data:{
                section1: section1,
                section2: section2,
                section2_head: section2_head,

                section3: section3,
                section3_head: section3_head,
                section4: section4,
                section4_head: section4_head,
                section5: section5,
                section5_head: section5_head,
                section6: section6,
                section6_head: section6_head,
                section7: section7,
            }
        });
     }else{
        res.status(200).json({
            message: "Unauthorised request",
            status: "Fail"
        });
     }
});

module.exports = router;