<?php session_start(); 
include("include.php"); ?>
<style>
  html {

    font-family: 'arial';

  }
</style>
<html>

<head>
  <title>WEBAPP - Installation</title>

   <style>
    html, body {
      margin: 0;
      padding: 0;
      height: 100%;
      width: 100%;
       overflow-y: hidden;
    }
    #main-content {
      display: block;
      width: 100%;
      height: 100%;
      background: linear-gradient(#ba0b0b 0%, #d62929 60%, white 60%, #ebebeb 100%);
    }
    #main-content-content {
      width: 100%;
      height: 100%;
    }
  </style>
</head>

<body style='margin: 0; padding: 0; background-color: #dedede;ove'>

   <div id="main-content">
    <div id="main-content-content"></div>
  </div>

</body>

</html>

<script>
  $(document).ready(function() {

    installationStep(0)

  });


  function installationStep(step) {

    if (step > 0)
      $('#installation-content-hide').fadeOut(200);

    $.post("get_content.php?InstallationStep", {

      'step': step,

    }).done(function(data) {

      if (step == '0') {

        document.getElementById('main-content-content').innerHTML = data;
        $('#main-content').fadeIn(1000);

      }

      if (step > 0) {

        setTimeout(function() {
          document.getElementById('installation-content').innerHTML = data;
          $('#installation-content-hide').fadeIn(200);
        }, 210);

      }

    });

  }


  function saveDBInformation() {

    var ip = document.getElementById('db_ip').value;
    var user = document.getElementById('db_user').value;
    var password = document.getElementById('db_pwd').value;
    var schema = document.getElementById('db_schema').value;
    var prefix = document.getElementById('db_prefix').value;

    document.getElementById("saveDBInformation").disabled = true;
    document.getElementById("saveDBInformation").innerHTML = 'Verify...';

    $.post("get_content.php?saveDBInformation", {

      'ip': ip,
      'user': user,
      'password': password,
      'schema': schema,
      'prefix': prefix,

    }).done(function(data) {
      document.getElementById("saveDBInformation").disabled = false;
      document.getElementById("saveDBInformation").innerHTML = 'Continue';

      if (data == 'error_ip') {

        $('#db_ip').animate({
          'color': 'red'
        }, {
          duration: 250
        }).delay(3000).animate({
          'color': '#525252'
        }, {
          duration: 250
        });
        $('#label_ip').animate({
          'color': 'red'
        }, {
          duration: 250
        }).delay(3000).animate({
          'color': '#525252'
        }, {
          duration: 250
        });
        return false;

      }

      if (data == 'error_login') {
        $('#db_user').animate({
          'color': 'red'
        }, {
          duration: 250
        }).delay(3000).animate({
          'color': '#525252'
        }, {
          duration: 250
        });
        $('#db_pwd').animate({
          'color': 'red'
        }, {
          duration: 250
        }).delay(3000).animate({
          'color': '#525252'
        }, {
          duration: 250
        });
        $('#label_user').animate({
          'color': 'red'
        }, {
          duration: 250
        }).delay(3000).animate({
          'color': '#525252'
        }, {
          duration: 250
        });
        $('#label_pass').animate({
          'color': 'red'
        }, {
          duration: 250
        }).delay(3000).animate({
          'color': '#525252'
        }, {
          duration: 250
        });
        return false;
      }


      if (data == 'fine') {

        $('#db_ip').animate({
          'color': '#61d43b'
        }, {
          duration: 250
        }).delay(3000).animate({
          'color': '#525252'
        }, {
          duration: 250
        });
        $('#db_user').animate({
          'color': '#61d43b'
        }, {
          duration: 250
        }).delay(3000).animate({
          'color': '#525252'
        }, {
          duration: 250
        });
        $('#db_pwd').animate({
          'color': '#61d43b'
        }, {
          duration: 250
        }).delay(3000).animate({
          'color': '#525252'
        }, {
          duration: 250
        });
        $('#db_schema').animate({
          'color': '#61d43b'
        }, {
          duration: 250
        }).delay(3000).animate({
          'color': '#525252'
        }, {
          duration: 250
        });
        $('#db_prefix').animate({
          'color': '#61d43b'
        }, {
          duration: 250
        }).delay(3000).animate({
          'color': '#525252'
        }, {
          duration: 250
        });

        setTimeout(function() {

          installationStep('2');

        }, 1500);


      }

    });


  }



  function saveSharedPath() {


    var sharedPath = document.getElementById('shared_folder').value;

    document.getElementById("saveSharedPath").disabled = true;
    document.getElementById("saveSharedPath").innerHTML = 'Verify...';

    $.post("get_content.php?saveSharedPath", {

      'sharedPath': sharedPath,

    }).done(function(data) {

      document.getElementById("saveSharedPath").disabled = false;
      document.getElementById("saveSharedPath").innerHTML = 'Continue';

      if (data == 'error') {

        $('#shared_folder').animate({
          'border': '1px solid red'
        }, {
          duration: 250
        }).delay(2000).animate({
          'border': 'none'
        }, {
          duration: 250
        });
        $('#shared_folder_label').animate({
          'color': 'red'
        }, {
          duration: 250
        }).delay(2000).animate({
          'color': '#525252'
        }, {
          duration: 250
        });

        return false;

      } else {

        $('#shared_folder').animate({
          'color': '#61d43b'
        }, {
          duration: 250
        }).delay(2000).animate({
          'color': '#525252'
        }, {
          duration: 250
        });
        $('#shared_folder_label').animate({
          'color': '#61d43b'
        }, {
          duration: 250
        }).delay(2000).animate({
          'color': '#525252'
        }, {
          duration: 250
        });

        setTimeout(function() {

          installationStep('3');

        }, 1500);


      }

    });


  }


  function executeInstallationWebApp() {

    var username = document.getElementById('username_login').value;
    var password = document.getElementById('password1_login').value;
    var checkpwd = document.getElementById('password2_login').value;

    if (password != checkpwd) {

      $('#password1_label').animate({
        'color': 'red'
      }, {
        duration: 250
      }).delay(2000).animate({
        'color': '#525252'
      }, {
        duration: 250
      });
      $('#password1_login').animate({
        'color': 'red'
      }, {
        duration: 250
      }).delay(2000).animate({
        'color': '#525252'
      }, {
        duration: 250
      });

      $('#password2_label').animate({
        'color': 'red'
      }, {
        duration: 250
      }).delay(2000).animate({
        'color': '#525252'
      }, {
        duration: 250
      });
      $('#password2_login').animate({
        'color': 'red'
      }, {
        duration: 250
      }).delay(2000).animate({
        'color': '#525252'
      }, {
        duration: 250
      });

      return false;

    }

    if (username == '' || username == null || username == undefined) {

      $('#username_login').animate({
        'color': 'red'
      }, {
        duration: 250
      }).delay(2000).animate({
        'color': '#525252'
      }, {
        duration: 250
      });
      $('#username_label').animate({
        'color': 'red'
      }, {
        duration: 250
      }).delay(2000).animate({
        'color': '#525252'
      }, {
        duration: 250
      });

      return false;

    }

    $('#password1_login').animate({
      'color': '#61d43b'
    }, {
      duration: 250
    }).delay(2000).animate({
      'color': '#525252'
    }, {
      duration: 250
    });
    $('#password2_login').animate({
      'color': '#61d43b'
    }, {
      duration: 250
    }).delay(2000).animate({
      'color': '#525252'
    }, {
      duration: 250
    });
    $('#username_login').animate({
      'color': '#61d43b'
    }, {
      duration: 250
    }).delay(2000).animate({
      'color': '#525252'
    }, {
      duration: 250
    });

    $('#installation-content-hide').fadeOut(200);
    setTimeout(function() {

      document.getElementById('installation-content-hide').innerHTML = "<div style='width: 100%; float: left; font-size: 2vw; height: 4vw; font-weight: bold; color: #525252; margin-top: 13vw; text-align: center;'>INSTALLING...</div>";
      $('#installation-content-hide').fadeIn(1500);

    }, 210)


    $.post("get_content.php?executeInstallationWebApp", {

      'username': username,
      'password': password,

    }).done(function(data) {

      if (data == 'installation_complete') {

        console.log("correct " + data);
        installationStep(4);

      } else {
        console.log("error " + data);
        //header("Location: ../login.php");
        //window.location.href = 'login.php';
        // errore installazione...

      }

    });






  }
</script>