<template>
  <div>
    <span v-for="(l, i) in line" :key="`l-${i}`">
      <span class="regText">
        <span class="regBold">
          {{ l.username }}@{{ l.hostname }}:<span class="pathText">{{ l.path }}</span>
        </span>$
        <span v-if="l.active">
          <input
            :key="i"
            spellcheck="false"
            type="text"
            class="inputField"
            v-model="l.command"
            @keyup.enter="exec(l.command, i)"
          />
        </span>
        <span v-else>
          <input
            :key="i"
            disabled
            spellcheck="false"
            type="text"
            class="inputField"
            v-model="l.command"
            @keyup.enter="exec(l.command, i)"
          />
        </span>
        <span v-for="(res, d) in result" :key="`res-${d}`">
          <br />
          {{ res.text }}
          <span v-if="res.field">
            <span v-if="res.password">
              <span v-if="res.active">
                <input
                  :key="i"
                  password="password"
                  spellcheck="false"
                  type="text"
                  class="inputField"
                  v-model="res.command"
                  @keyup.enter="execContinue(res.command, i)"
                />
              </span>
              <span v-else>
                <input
                  :key="i"
                  password="password"
                  disabled
                  spellcheck="false"
                  type="text"
                  class="inputField"
                  v-model="res.command"
                  @keyup.enter="execContinue(res.command, i)"
                />
              </span>
            </span>
            <span v-else>
              <span v-if="res.active">
                <input
                  :key="i"
                  spellcheck="false"
                  type="text"
                  class="inputField"
                  v-model="res.command"
                  @keyup.enter="execContinue(res.command, i)"
                />
              </span>
              <span v-else>
                <input
                  :key="i"
                  disabled
                  spellcheck="false"
                  type="text"
                  class="inputField"
                  v-model="res.command"
                  @keyup.enter="execContinue(res.command, i)"
                />
              </span>
            </span>
          </span>
        <br />
        </span>
      </span>
    </span>
  </div>
</template>

<script>
export default {
  name: 'cli',
  data () {
    return {
      user: 'user',
      host: '076server',
      pwd: '/',
      line: [{
        active: true,
        username: this.user,
        hostname: this.host,
        path: this.pwd,
        command: ''
      }],
      result: []
    }
  },
  created: function () {
    this.line = [];
    this.line.push({
      active: true,
      username: this.user,
      hostname: this.host,
      path: this.pwd,
      command: ''
    });
  },
  methods: {
    exec(text, key) {
      var arg = text.split(' ');

      this.line[key].active = false;
      if (arg[0] === 'sudo') this.isSudo();
      else this.execContinue(text, key);
    },
    isSudo() {
      this.result.push({
        active: true,
        text: 'パスワード: ',
        field: true,
        password: true,
        command: ''
      });
    },
    execContinue(text, key) {
      // this.result[key].active = false;
      var arg = text.split(' ');

      if (arg[0] === 'ls') this.ls(this.line[key].path);
      else this.result.push({
        active: true,
        text: arg[0] + ': コマンドは見つかれません。',
        field: false,
        password: false,
        command: ''
      })

      this.line.push({
        active: true,
        username: this.user,
        hostname: this.host,
        path: this.pwd,
        command: ''
      });
    },
    ls(pwd) {
      var neopwd = pwd.replace('/', 'sl');
      axios.get('/api/rpc/bash/ls/' + neopwd).then(res => {
        console.log(res.data);
        this.result.push({
          active: true,
          text: res.data,
          field: false,
          password: false,
          command: ''
        })
      })
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.inputField {
  background-color: #000000;
  color: #73D216;
  border-width: 0px;
  font-family: monospace;
  font-size: large;
  width: 500px;
  outline: none;

}
.regBold {
  font-weight: bold;
}
.regText {
  color: #73D216;
}
.pathText {
  color: #5454FF;
}
h1, h2 {
  font-weight: normal;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>
