
const db = require('../../db');
const Op = db.Sequelize.Op;

const Shop = require("../../models/shop");

exports.addShop = async (req, res) => {
  try {
    const addshopData = req.body;
    console.log(addshopData);
    await Shop.create(addshopData);
    res.send("create");
  } catch (err) {
    console.log(err);
    res.sendStatus(401);
  }
};

exports.getShop = async (req, res) => {
  try {
    let shop = await Shop.findAll();
    res.status(200).send(shop);
  } catch (err) {
    console.log(err);
    res.sendStatus(401);
  }
};

exports.deleteshop = async (req, res) => {
  try {
    const shopId = req.params.shopId;
    await Shop.destroy({
      where: {
        id: shopId,
      },
    });
    res.status(200).send("success");
  } catch (err) {
    console.log(err);
    res.sendStatus(401);
  }
};

exports.getShopId = async (req, res) => {
  try {
    const shopId = req.params.id;
    if(Shop){
      let shop = await Shop.findOne({
        where: {
          id: shopId,
        },
      });
      res.status(200).send(shop);
    }else{
      res.status(200).send(); 
    }
   
  } catch (err) {
    console.log(err);
    res.sendStatus(401);
  }
};
exports.getShopBytype = async (req, res) => {
  try {
    const type = req.params.type;
    let shop = await Shop.findAll({
      where: {
        type: type,
      },
    });
    res.status(200).send(shop);
  } catch (err) {
    console.log(err);
    res.sendStatus(401);
  }
};
exports.updateShop = async (req, res) => {
  try {
    const shopData = req.body;
    const id = req.params.id;
    await Shop.update(shopData, { where: { id: id } });
    res.status(200).send({ status: "done" });
  } catch (err) {
    console.log(err);
    res.sendStatus(401);
  }
};
exports.getUserId = async (req, res) => {
  try {
    const userId = req.params.userId;
    let response = await Shop.findOne({
      where: {
        userId: userId,
      },
    });
    res.status(200).send(response);
  } catch (err) {
    console.log(err);
    res.sendStatus(401);
  }
};
exports.getType = async (req, res) => {

  try {
    const type = req.params.type;
    let response = await Shop.findOne({
      where: {
        type: type
      },
    });
    res.status(200).send(response);
  } catch (err) {
    console.log(err);
    res.sendStatus(401);
  }
};

exports.updateshopId = async (req, res) => {
  try {
    const shopData = req.body;
    const id = req.params.id;
    await Shop.update(shopData, { where: { id: id } });
    res.status(200).send({ status: "done" });
    console.log(shopData);
  } catch (err) {
    console.log(err);
    res.sendStatus(401);
  }
};
exports.searchShop = async (req, res) => {
  const name = req.body.name;
  console.log("name", name);

  if (name !== undefined) {
    console.log("called1");
    var condition = name ? { name: { [Op.like]: `%${name}%` } } : null;
    const datasearch = await Shop.findAll({
      where: condition,
    });
    res.status(200).send(datasearch);
  } else {
    console.log("called2");

    const datasearch = await  Shop.findAll({
     
    });
    res.status(200).send(datasearch);
  }
};
// exports.searchType = async (req, res) => {
//   const type = req.body.type;
//   console.log("name", type);

//   if (type !== undefined) {
//     console.log("called1");
//     var condition = type ? { name: { [Op.like]: `%${name}%` } } : null;
//     const datasearch = await Shop.findAll({
//       where: condition,
//     });
//     res.status(200).send(datasearch);
//   } else {
//     console.log("called2");

//     const datasearch = await  Shop.findAll({
     
//     });
//     res.status(200).send(datasearch);
//   }
// };
