const express = require("express");
const router = express.Router();
const multer = require('multer');
const authorize = require("../../../../middlewares/authorize-sapps");
const surveyServices = require("./survey.service");

const storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, './public/data/images/');
    },
    filename: function (req, file, cb) {
        cb(null, Date.now() + '-' + file.originalname);
    }
});

const upload = multer({ storage: storage });


router.post("/create", authorize(), create);
router.post("/save-sellingin", authorize(), saveSellingIn);
router.post("/save-expense", authorize(), saveExpense);
router.post("/submit-expense", authorize(), submitExpense);
router.post("/save-treatment", authorize(), saveTreatment);
router.post("/bulk-update-details", authorize(), bulkUpdateSubmit);
router.get("/", authorize(), getAll);
router.get("/filter", authorize(), filter);
router.get("/sellingin-by-id", authorize(), sellingBySurveyId);
router.get("/expense-survey-id/:survey_id", authorize(), expenseBySurveyId);
router.get("/treatment-by-id", authorize(), treatmentBySurveyId);
router.get("/option", authorize(), option);
router.get("/count", authorize(), count);
router.put("/:id", authorize(), update);
router.put("/enabled/:id", authorize(), enabled);
router.delete("/:id", authorize(), _delete);

module.exports = router;

function sellingBySurveyId(req, res, next) {
    surveyServices
        .sellingBySurveyId(req.query)
        .then((result) => res.json(result))
        .catch(next);
}

function bulkUpdateSubmit(req, res, next) {
    surveyServices
    .bulkUpdateSubmit(req.body)
    .then((result) => res.json(result))
    .catch(next);
}

function expenseBySurveyId(req, res, next) {
    surveyServices
        .expenseBySurveyId(req.params.survey_id)
        .then((result) => res.json(result))
        .catch(next);
}

function treatmentBySurveyId(req, res, next) {
    surveyServices
        .treatmentBySurveyId(req.query)
        .then((result) => res.json(result))
        .catch(next);
}

function create(req, res, next) {
    surveyServices
        .create(req.body)
        .then((result) => res.json(result))
        .catch(next);
}
function saveSellingIn(req, res, next) {
    surveyServices
        .saveSellingIn(req.body)
        .then((result) => res.json(result))
        .catch(next);
}
function saveExpense(req, res, next) {
    surveyServices
        .saveExpense(req.body)
        .then((result) => res.json(result))
        .catch(next);
}

function submitExpense(req, res, next) {
    surveyServices
        .submitExpense(req.body)
        .then((result) => res.json(result))
        .catch(next);
}

function saveTreatment(req, res, next) {
    surveyServices
        .saveTreatment(req.body)
        .then((result) => res.json(result))
        .catch(next);
}

function getAll(req, res, next) {
    surveyServices
    .getAll(req.query)
    .then((result) => res.json(result))
    .catch(next);
}

function filter(req, res, next) {
    surveyServices
        .filter(req.query)
        .then((result) => res.json(result))
        .catch(next);
}


function option(req, res, next) {
    surveyServices
        .option()
        .then((result) => res.json(result))
        .catch(next);
}

function count(req, res, next) {
    surveyServices
        .count()
        .then((result) => res.json(result))
        .catch(next);
}

function update(req, res, next) {
    surveyServices
        .update(req.params.id, req.body)
        .then((result) => res.json(result))
        .catch(next);
}

function enabled(req, res, next) {
    surveyServices
        .enabled(req.params.id, req.body)
        .then((result) => res.json(result))
        .catch(next);
}

function _delete(req, res, next) {
    surveyServices
        .delete(req.params.id)
        .then((result) => res.json(result))
        .catch(next);
}
