<!--
 * @Author: 曹文丽 caowenli@yuan-info.com
 * @Date: 2023-03-22 14:25:02
 * @LastEditors: 曹文丽 caowenli@yuan-info.com
 * @LastEditTime: 2023-09-20 10:46:42
 * @FilePath: \yuan-risk-management-browser\src\views\configCenter\components\weakPasswordDrawer.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div class="yaDrawer">
    <yaEditForm
      :formColumns="formColumns"
      @submit="handleToSubmit"
      :loading="subBtnLoading"
    >
      <template #yaLoginProtocol>
        <div class="ya-row">
          <template v-for="item in allData.loginProtocol" :key="item.random">
            <yaRow
              :colArr="item"
              :userList="userList"
              :passwordList="passwordList"
              :combinDictList="combinDictList"
              :options="options"
            ></yaRow>
          </template>
        </div>
      </template>
      <template #yaDataBase>
        <div class="ya-row">
          <template v-for="item in allData.database" :key="item.random">
            <yaRow
              :colArr="item"
              :userList="userList"
              :passwordList="passwordList"
              :combinDictList="combinDictList"
              :options="options"
            ></yaRow>
          </template>
        </div>
      </template>
      <template #yaOther>
        <div class="ya-row">
          <template v-for="item in allData.other" :key="item.random">
            <yaRow
              :colArr="item"
              :userList="userList"
              :passwordList="passwordList"
              :combinDictList="combinDictList"
              :options="options"
            ></yaRow>
          </template>
        </div>
      </template>
      <template #yaPassDict>
        <span class="ya-dict" @click="handleGoToDict">
          <img src="../../../../assets/image/icon-dict.png" alt="" /> 密码字典管理
        </span>
      </template>
    </yaEditForm>
  </div>
</template>
<script lang="ts" setup>
import { ref, reactive, onMounted, watch } from 'vue';
import yaRow from './components/yaRow.vue';
import { yaMessage } from 'yuan-plus';
import { getDictList } from '../../../../utils/utils';
import { handleGetIndex } from '../../../../utils/tools';
import { useRoute, useRouter } from 'vue-router';
const route = useRoute();
const router = useRouter();
//猜测频率的校验
var checkGuessFrequency = (rule, value, callback) => {
  if (value < 0) {
    callback(new Error('范围在0-600'));
  } else if (value > 600) {
    callback(new Error('范围在0-600'));
  } else {
    callback();
  }
};
//猜测时间的校验
var checkGuessTime = (rule, value, callback) => {
  if (value < 0) {
    callback(new Error('范围在0-1800'));
  } else if (value > 1800) {
    callback(new Error('范围在0-1800'));
  } else {
    callback();
  }
};
const guessNumArr = ref([]);
const maxThreadArr = ref([]);
//模板下拉框
const pwdSelected = ref([]);
const formColumns = reactive([
  {
    title: '基础信息',
    formItemClassName: 'line',
  },
  {
    key: 'weakPwd',
    type: 'select',
    label: '弱口令模版选择',
    options: pwdSelected,
    clearable: true,
    onChange() {},
  },
  {
    title: '服务类型',
    formItemClassName: 'line',
  },
  {
    label: '登录协议',
    slotName: 'yaLoginProtocol',
  },
  {
    label: '数据库',
    slotName: 'yaDataBase',
  },
  {
    label: '其他',
    slotName: 'yaOther',
  },
  {
    slotName: 'yaPassDict',
  },
  {
    title: '高级选项',
    formItemClassName: 'line',
  },
  {
    key: 'guessTime',
    type: 'input',
    label: '口令猜测时间',
    clearable: true,
    isType: 'number',
    rules: [{ validator: checkGuessTime, trigger: 'change' }],
    value: 300,
    tooltip:
      '描述一个口令猜测插件的最长运行时间，达到设定的超时时间则该插件终止运行。范围：0-1800',
  },
  {
    key: 'guessFrequency',
    type: 'input',
    label: '口令猜测频率',
    clearable: true,
    rules: [{ validator: checkGuessFrequency, trigger: 'change' }],
    value: 20,
    tooltip: '对某一个被扫描目标两次同协议口令猜测之间的时间间隔，范围0-600',
  },
  {
    key: 'guessNum',
    type: 'select',
    label: '猜测次数',
    options: guessNumArr,
    clearable: true,
    rules: true,
    value: 0,
    tooltip: '对单个主机猜测次数，0表示不限制',
  },
  {
    key: 'maxThread',
    type: 'select',
    label: '最大并发线程数',
    options: maxThreadArr,
    clearable: true,
    rules: true,
    value: 5,
    tooltip:
      '对单个主机下的单个服务进行口令探测时的并发线程数目。值越大，探测速度越快。',
  },
]);

let resetAllData = ref({});
//获取初始数据
const initData = () => {
  // getServeData().then((res) => {
  //   const { code, data = {} } = res || {};
  //   if (code == 200 && data) {
  //     allData.value = data || {};
  //     resetAllData.value = JSON.parse(JSON.stringify(allData.value));
  //     if (sessionStorage.getItem('YA-HOSTSCANDATA')) {
  //       let selectItem = JSON.parse(sessionStorage.getItem('YA-HOSTSCANDATA'));
  //       let params = {
  //         guessFrequency: '',
  //         guessNum: '',
  //         guessTime: '',
  //         maxThread: '',
  //         weakPassConfigList: [],
  //       };
  //       for (let key in params) {
  //         params[key] = selectItem[key];
  //       }
  //       getSelectedItem(params);
  //     }
  //   }
  // });
};
let subBtnLoading = ref(false);
//确定按钮的事件
const handleToSubmit = (data) => {
  let params = JSON.parse(JSON.stringify(data));
  params.weakPassConfigList = [];
  allData.value.database?.find((item) => {
    item.isSelected && params.weakPassConfigList.push(item);
  });
  allData.value.loginProtocol?.find((item) => {
    item.isSelected && params.weakPassConfigList.push(item);
  });
  allData.value.other?.find((item) => {
    item.isSelected && params.weakPassConfigList.push(item);
  });
  let detailConfig = ref(JSON.parse(sessionStorage.getItem('YA-HOSTSCANDATA')));
  for (let key in params) {
    key != 'weakPwd' && (detailConfig.value[key] = params[key]);
  }
  sessionStorage.setItem('YA-HOSTSCANDATA', JSON.stringify(detailConfig.value));
  router.push({
    path: '/taskManagement/taskList/taskDelivery',
    query: route.query,
  });
};

//跳转到密码字典页面
const handleGoToDict = () => {
  router.push('/system/dictMangment');
};

const allData = ref({});
onMounted(() => {
  const i = handleGetIndex(formColumns, 'key', 'weakPwd');
  formColumns[i].onChange = (data) => {
    formColumns[i].value = data;
    const selectItem = pwdSelected.value?.find((item) => {
      return item.name == data;
    });
    selectItem.weakPassConfigList = selectItem?.config||[];
    getSelectedItem(selectItem);
  };
  initData();
  getList();
});
//赋值
const getSelectedItem = (selectItem) => {
  allData.value = JSON.parse(JSON.stringify(resetAllData.value));
  allData.value.loginProtocol?.forEach((item) => {
    item.random = Math.random();
  });
  allData.value.database?.forEach((item) => {
    item.random = Math.random();
  });
  allData.value.other?.forEach((item) => {
    item.random = Math.random();
  });
  for (let key in selectItem) {
    formColumns.find((child) => {
      if (child.key == key && selectItem[key]) {
        child.value = selectItem[key];
      }
    });
  }

  selectItem?.weakPassConfigList?.forEach((item) => {
    //数据库赋值
    let dataBaseItem =
      allData.value.database?.find((child) => {
        return item.name == child.name;
      }) || {};
    if (dataBaseItem) {
      dataBaseItem.mixDictId = item.mixDictId;
      dataBaseItem.ports = item.ports;
      dataBaseItem.pwdDictId = item.pwdDictId;
      dataBaseItem.type = item.type;
      dataBaseItem.userDictId = item.userDictId;
      dataBaseItem.isSelected = true;
    }
    // 登录协议赋值
    let loginProtocolItem =
      allData.value.loginProtocol?.find((child) => {
        return item.name == child.name;
      }) || {};
    if (loginProtocolItem) {
      loginProtocolItem.mixDictId = item.mixDictId;
      loginProtocolItem.ports = item.ports;
      loginProtocolItem.pwdDictId = item.pwdDictId;
      loginProtocolItem.type = item.type;
      loginProtocolItem.userDictId = item.userDictId;
      loginProtocolItem.isSelected = true;
    }
    //其他赋值
    let otherItem =
      allData.value.other?.find((child) => {
        return item.name == child.name;
      }) || {};
    if (otherItem) {
      otherItem.mixDictId = item.mixDictId;
      otherItem.ports = item.ports;
      otherItem.pwdDictId = item.pwdDictId;
      otherItem.type = item.type;
      otherItem.userDictId = item.userDictId;
      otherItem.isSelected = true;
    }
  });
};
//用户
const userList = ref([]);
//密码
const passwordList = ref([]);
//用户和密码
const combinDictList = ref([]);
//模式的数据
const options = ref([]);
//获取下拉框的数据的事件
const getList = () => {
  //获取模板的下拉框选项
  // queryTableList('').then((res) => {
  //   const { code, data = {} } = res || {};
  //   if (code == 200 && data && data.records) {
  //     data.records.forEach((item) => {
  //       item.label = item.name;
  //       item.value = item.name;
  //     });
  //     pwdSelected.value = data.records || [];
  //   }
  // });
  //模式下拉框
  getDictList('dictServiceType').then((res) => {
    options.value = res || {};
  });
  //用户
  // let params = { type: 'user' };
  // getDictData(params).then((res) => {
  //   const { code, data = {} } = res || {};
  //   if (code == 200 && data) {
  //     data.forEach((item) => {
  //       item.label = item.name;
  //       item.value = item.id;
  //       userList.value = data || [];
  //     });
  //   }
  // });
  // //密码
  // let password = { type: 'pwd' };
  // getDictData(password).then((res) => {
  //   const { code, data = {} } = res || {};
  //   if (code == 200 && data) {
  //     data.forEach((item) => {
  //       item.label = item.name;
  //       item.value = item.id;
  //       passwordList.value = data || [];
  //     });
  //   }
  // });
  // //组合字典
  // let combinDict = { type: 'mix' };
  // getDictData(combinDict).then((res) => {
  //   const { code, data = {} } = res || {};
  //   if (code == 200 && data) {
  //     data.forEach((item) => {
  //       item.label = item.name;
  //       item.value = item.id;
  //       combinDictList.value = data || [];
  //     });
  //   }
  // });
  //猜测次数
  getDictList('guessNum').then((res) => {
    guessNumArr.value = res;
  });
  //最大进程
  getDictList('maxThread').then((res) => {
    maxThreadArr.value = res;
  });
};
</script>
<style lang="scss" scoped>
.yaDrawer {
  .ya-dict {
    font-size: 14px;
    color: #0095ff;
    line-height: 20px;
    display: flex;
    align-items: center;
    padding-bottom: 16px;
    cursor: pointer;
    position: relative;
    left: -120px;
    img {
      width: 14px;
      height: 14px;
      margin-right: 8px;
    }
  }
  ::v-deep .el-form-item__content {
    display: block;
    .yaBox:last-child {
      .el-row {
        margin-bottom: 0px;
      }
    }
  }
}
</style>
