const express = require("express");
const bodyParser = require('body-parser');
var multer = require('multer');
var upload = multer({dest:'uploads/'});
const cors = require("cors");

const db = require("./app/models");
const Client = db.client;

const app = express();

var corsOptions = {
  origin: "*"
};

app.use(cors(corsOptions));

// parse requests of content-type - application/json
app.use(bodyParser.json());

// parse requests of content-type - application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));

app.use('/images',express.static(__dirname + '/uploads'));

// app.use(express.static('public'));
// app.use(express.static('uploads'));
 

db.sequelize.sync();
// // drop the table if it already exists
// db.sequelize.sync({ force: true }).then(() => {
//   console.log("Drop and re-sync db.");
// });


// simple route
app.get("/", (req, res) => {
  res.json({ message: "Welcome to bezkoder application." });
});

app.post('/api/client/', upload.fields([{
  name: 'file', maxCount: 1
}, {
  name: 'backfile', maxCount: 1
}]), (req, res) => {
  console.log(req.files.file);
  console.log(req.files.backfile);
  let client = {
    first_name: req.body.first_name,
    last_name: req.body.last_name,
    middle_name: req.body.middle_name,
    company_name: req.body.company_name,
    address: req.body.address,
    phone_number: req.body.phone_number,
    email_address: req.body.email_address,
    occupation: req.body.occupation,
    date_of_verification: req.body.date_of_verification,
    type_of_document: req.body.type_of_document,
    document_number: req.body.document_number,
    expire_date: req.body.expire_date,
    in_person: req.body.in_person,
    file_store_path: req.body.file_store_path,
    document_upload_type: req.body.document_upload_type,
    notes: req.body.notes 
  };
  if(req.files.file)
  {
    client.front_document_pic_name = req.files.file[0].filename;
  }
  if(req.files.backfile)
  {
    client.back_document_pic_name = req.files.backfile[0].filename;
  }

  
  Client.create(client)
    .then(data => {
      res.send(data);
    })
    .catch(err => {
      res.status(500).send({
        message:
          err.message || "Some error occurred while creating the Tutorial."
      });
    });

});

app.put('/api/client/:id', upload.fields([{
  name: 'file', maxCount: 1
}, {
  name: 'backfile', maxCount: 1
}]), (req, res) => {
  console.log(req.files.file);
  console.log(req.files.backfile);
  let client = {
    first_name: req.body.first_name,
    last_name: req.body.last_name,
    middle_name: req.body.middle_name,
    company_name: req.body.company_name,
    address: req.body.address,
    phone_number: req.body.phone_number,
    email_address: req.body.email_address,
    occupation: req.body.occupation,
    date_of_verification: req.body.date_of_verification,
    type_of_document: req.body.type_of_document,
    document_number: req.body.document_number,
    expire_date: req.body.expire_date,
    in_person: req.body.in_person,
    file_store_path: req.body.file_store_path,
    document_upload_type: req.body.document_upload_type,
    notes: req.body.notes 
  };
  if(req.files.file)
  {
    client.front_document_pic_name = req.files.file[0].filename;
  }
  if(req.files.backfile)
  {
    client.back_document_pic_name = req.files.backfile[0].filename;
  }

  const id = req.params.id;

  Client.update(client, {
    where: { id: id }
  })
    .then(data => {
      res.send(data);
    })
    .catch(err => {
      res.status(500).send({
        message:
          err.message || "Some error occurred while creating the Tutorial."
      });
    });

});

require("./app/routes/turorial.routes")(app);

// set port, listen for requests
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}.`);
});
