const Discord = require('discord.js')
const fs = require("fs");
const ms = require("ms");
const moment = require("moment");
const database = JSON.parse(fs.readFileSync("./database.json", `utf8`));
const mysql = require("mysql");

    module.exports.run = async (client, message, args) => {
        var con = mysql.createConnection({
          host: database.host,
          user: database.user,
          password: database.password,
          database: database.database,
          charset : 'utf8mb4'
      });
      con.connect(err => {
          if (err) throw err;
      });


      if(!message.member.hasPermission("KICK_MEMBERS")) return message.channel.send(`:)`);
    var cname = args[0];
    var value2change = args[0];
    var newvalue = args[0];

    var filter = m => m.author.id === message.author.id

    message.channel.send("🗑️ **Naam** | Voor welk bedrijf moet er iets worden aangepast?")
        message.channel.awaitMessages(filter, {
        max: 1,}).then(collected => {
        cname = collected.first().content;
    
    message.channel.send("🗑️ **Data** | Welke value moet er worden aangepast?\nscore | name | price | ddosprot | kvk | disk | hardware | panel | location | support | package | url | img | cpu | visible | trustpilot").then((message) => {
        message.channel.awaitMessages(filter, {
        max: 1,}).then(collected => {
        value2change = collected.first().content;

    message.channel.send("🛡️ **Waarde** | Wat is de nieuwe waarde").then((message) => {
        message.channel.awaitMessages(filter, {
        max: 1,}).then(collected => {
        newvalue = collected.first().content


    con.query(`SELECT * FROM data`, (err, rows) => {

        if (err) throw err;
  
        if (rows.length < 1) {
            con.query(`UPDATE data SET ${value2change} = '${newvalue}' WHERE name = '${cname}'`);
             setTimeout(function(){ 
                message.channel.send(`**Update** (${value2change})\n\nNieuwe waarde: ${newvalue}\nBedrijf: ${cname}`)
                         }, 10);
  
            } else {
                con.query(`UPDATE data SET ${value2change} = '${newvalue}' WHERE name = '${cname}'`);
                 setTimeout(function(){ 
                    message.channel.send(`**Update** (${value2change})\n\nNieuwe waarde: ${newvalue}\nBedrijf: ${cname}`)
                             }, 10);
  
        }
  
    })

});
});
});
});
    })


}

module.exports.help = {
  name: "update"
}