const express = require("express");
const router = express.Router();
const multer = require('multer');
const authorize = require("../../../../middlewares/authorize-sapps");
const goodsReceiptService = require("./goodsReceipt.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(), upload.fields([
    { name: 'shipper_filename', maxCount: 1 },
    { name: 'cosignee_filename', maxCount: 1 },
]), create);

router.get("/", authorize(), getAll);
router.get("/shippers", authorize(), getAllShipper);
router.get("/cosignees", authorize(), getAllCosignee);
router.get("/filter", authorize(), filter);
router.get("/option", authorize(), option);
router.get("/count", authorize(), count);

router.put("/:id", authorize(), upload.fields([
    { name: 'shipper_filename', maxCount: 1 },
    { name: 'cosignee_filename', maxCount: 1 },
]), update);

router.put("/enabled/:id", authorize(), enabled);
router.put("/completed/:id", authorize(), completed);
router.delete("/:id", authorize(), _delete);

module.exports = router;

function create(req, res, next) {
    const createData = {
        ...req.body,
        shipper_filename: req.files && req.files.shipper_filename ? req.files.shipper_filename[0].filename : null,
        cosignee_filename: req.files && req.files.cosignee_filename ? req.files.cosignee_filename[0].filename : null
    };

    goodsReceiptService
        .create(createData)
        .then((result) => res.json(result))
        .catch(next);
}

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

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

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

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

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

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

function update(req, res, next) {
    const updateData = {
        ...req.body,
        shipper_filename: req.files && req.files.shipper_filename ? req.files.shipper_filename[0].filename : req.body.shipper_filename ? req.body.shipper_filename : null,
        cosignee_filename: req.files && req.files.cosignee_filename ? req.files.cosignee_filename[0].filename : req.body.cosignee_filename ? req.body.cosignee_filename : null
    };

    goodsReceiptService
        .update(req.params.id, updateData)
        .then((result) => res.json(result))
        .catch(next);
}

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

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

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

