<template>
  <div class="ya-box">
    <YaEditForm
      @submit="handlerToSubmit"
      :formColumns="formColumns"
      ref="yaEditForm"
      :hideSaveBtn="hideSaveBtn"
    >
      <template #btn_verify
        ><ya-button
          :loading="isSubLoading"
          type="primary"
          @click="handleLoginVerify"
          >登录验证</ya-button
        ></template
      >
    </YaEditForm>
  </div>
</template>
<script lang="ts" setup>
import { ref, onMounted } from 'vue';
import {
  loginTest,
  loginTestResult,
} from '../../../../api/riskDetect/riskIdentify/addTask/hostScan';
import { yaMessage } from 'yuan-plus';
import { useRouter, useRoute } from 'vue-router';
import { getDictList } from '../../../../utils/utils';
const router = useRouter();
const route = useRoute();
//保存按钮是否隐藏
const hideSaveBtn = ref(true);
//登录验证按钮和保存按钮的事件区分
const loginVerify = ref(false);
const yaEditForm = ref();
//登陆验证的转圈效果
const isSubLoading = ref(false);
//登陆协议
const protocolArr = ref([]);

const formColumns = ref([
  {
    key: 'protocol',
    type: 'select',
    label: '登录协议',
    options: protocolArr,
    onChange() {
      hideSaveBtn.value = true;
    },
    rules: true,
    value: 'ssh',
  },
  {
    key: 'port',
    type: 'input',
    label: '登录端口',
    clearable: true,
    isType: 'port',
    customType:'port',
    rules: true,
    onChange() {
      hideSaveBtn.value = true;
    },
    value: 22,
  },
  {
    key: 'ip',
    type: 'textarea',
    label: 'IP',
    clearable: true,
    tooltip: '支持单个ip，应属于目标地址中的一个',
    rules: true,
    onChange() {
      hideSaveBtn.value = true;
    },
    value: null,
  },
  {
    key: 'username',
    type: 'input',
    label: '用户名（域名/域用户）',
    clearable: true,
    rules: true,
    onChange() {
      hideSaveBtn.value = true;
    },
    maxLength: 30,
    value: null,
  },
  {
    key: 'pwd',
    type: 'password',
    label: '密码（域密码）',
    showPassword: true,
    rules: true,
    clearable: true,
    autocomplete: 'new-password',
    onChange() {
      hideSaveBtn.value = true;
    },
    maxLength: 50,
    value: null,
  },
  {
    label: ' ',
    slotName: 'btn_verify',
  },
]);

// 登录验证校验--表单校验
const handleLoginVerify = () => {
  loginVerify.value = false;
  yaEditForm.value?.confirmClick();
};

// 登录验证和保存按钮的事件 loginVerify.value--false:登陆验证事件 loginVerify.value--true：保存按钮事件
const handlerToSubmit = (data) => {
  !loginVerify.value?handleToCheck(data): handlerToSave(data)
};
//登录校验的接口
const handleToCheck = (data) => {
  isSubLoading.value = true;
  loginTest(data).then((res) => {
    const { code, data } = res || {};
    if (code == 200) {
      let timer =setInterval(() => {
        loginTestResult({ uuid: data }).then((res) => {
          const { code, data = {} } = res || {};
          if (code == 200 && data) {
            if (data.isEnd) {
              isSubLoading.value = false;
              clearInterval(timer);
              hideSaveBtn.value = !data.success;
              loginVerify.value = data.success;
              yaMessage(data.message, data.success ? 'success' : 'error');
            }
          }
        });
      }, 1000);
    } else {
      isSubLoading.value = false;
    }
  });
};
//保存按钮的事件
const handlerToSave = (data) => {
  let hostScanData = ref(JSON.parse(sessionStorage.getItem('YA-HOSTSCANDATA')));
  let id = route.query?.id;
  if (id) {
    hostScanData.value.loginCheckConfigConfigList[id] = data;
  } else {
    hostScanData.value.loginCheckConfigConfigList = hostScanData.value
      .loginCheckConfigConfigList
      ? hostScanData.value.loginCheckConfigConfigList
      : [];
    hostScanData.value.loginCheckConfigConfigList.push(data);
  }
  sessionStorage.setItem('YA-HOSTSCANDATA', JSON.stringify(hostScanData.value));
  router.go(-1);
};
onMounted(() => {
  //编辑
  if (route.query.id) {
    let id = route.query.id;
    let hostScanData = JSON.parse(sessionStorage.getItem('YA-HOSTSCANDATA'))
      .loginCheckConfigConfigList;
    hostScanData.forEach((item) => {
      if (item.id == id) {
        for (let key in item) {
          formColumns.value.find((child) => {
            if (child.key == key) {
              child.value = item[key];
            }
          });
        }
      }
    });
  }
  initDict();
});
//登陆协议
const initDict = () => {
  getDictList('loginCheckProtocol').then((res) => {
    protocolArr.value = res || [];
  });
};
</script>
<style lang="scss" scoped></style>
