<template>
  <div>
    <div class="row">
      <div class="col-xs-1 col-md-4"></div>
      <div class="col-xs-10 col-md-4">
        <h6 style="text-align: center;margin: 1rem;">节点<q-icon v-show="online" title="在线" name="check_circle_outline"
            style="color: green;" /> <q-icon v-show="!online" title="离线" name="highlight_off" style="color: red;" /></h6>
        <q-input v-model="text" filled autogrow />
      </div>
    </div>
    <div v-if="isobj">
      <div class="row" v-for="(item, index) in serve" :key="index">
        <div class="col-xs-1 col-md-4"></div>
        <div class="col-xs-10 col-md-4">
          <LinkItem :serve="item" v-model:text="text"></LinkItem>
        </div>
      </div>
    </div>


  </div>
</template>

<script lang="ts">
import { server } from 'components/models';
import { computed, defineComponent, onMounted, ref } from 'vue';
import { getdata } from 'src/api/api'
import LinkItem from 'components/LinkItem.vue';
import { decode } from 'js-base64';
export default defineComponent({
  name: 'IndexPage',
  components: { LinkItem },
  setup() {
    const serve = ref(<server[]>[])
    const api = new getdata;
    const text = ref('');
    const online = ref(false);
    onMounted(() => {
      api.get_server().then((res) => {
        for (let index = 0; index < res.data.length; index++) {
          serve.value.push(res.data[index]);
        }
      })
    })
    const isobj = computed(() => {
      try {
        if (text.value.length <= 0) {
          return false
        }
        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
        let texttmp = text.value!.replace(/\ +/g, '');
        texttmp = texttmp.replace(/[\r\n]/g, '');
        let arr = texttmp.split('vmess://');
        for (let iterator of arr) {
          // eslint-disable-next-line vue/no-side-effects-in-computed-properties
          online.value = false
          if (iterator.length > 0) {
            let obj = JSON.parse(decode(iterator));
            if (obj.id != '2ee57806-f6e4-482a-ef08-7360c04cd3e5' || obj.net != 'ws') {
              return false
            }
            let reg = new RegExp(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/);
            let ip = obj.ps.match(reg)[0];
            isonline(ip)
          }

        }
      } catch (error) {

        return false
      }

      return true
    })
    const isonline = (ip: string) => {
      api.text_server(`http://${ip}:9000/`).then(res => {
        online.value = res.data.sataus == 400
      })
    }
    return {
      serve,
      text,
      isobj,
      online
    };
  }
});
</script>
