<template>
  <div>
    <el-dialog :title="title" :visible.sync="open" width="700px" height="700px" :close-on-click-modal="false"
               append-to-body>
      <el-form :model="form" :rules="rules" ref="form" label-width="130px">
        <el-form-item label="工具名称">
          {{toolName}}
        </el-form-item>
        <el-form-item label="任务名称" prop="projectName">
          <el-input v-model="form.projectName"></el-input>
        </el-form-item>
        <el-form-item v-if="toolId==114 " v-for="item in keySwanParams" :label="item.name" :prop="item.key">
          <param-item :ref="item.key" :param="item" v-if="item.type!='input'"></param-item>
          <el-input v-if="item.type=='input'" v-model="form[item.key]" :placeholder="item.tip"></el-input>
        </el-form-item>
        <el-form-item v-if="toolId==113" v-for="item in arvmsParams" :label="item.name" :prop="item.key">
          <param-item :ref="item.key" :param="item" v-if="item.type!='input'"></param-item>
          <el-input v-if="item.type=='input'" v-model="form[item.key]" :placeholder="item.tip"></el-input>
        </el-form-item>
        <el-form-item v-if="toolId==106 || toolId==111" v-for="item in eagleEyeParams" :label="item.name"
                      :prop="item.key">
          <param-item :ref="item.key" :param="item" v-if="item.type!='input'"></param-item>
          <el-input v-if="item.type=='input'" v-model="form[item.key]" :placeholder="item.tip"></el-input>
        </el-form-item>
        <el-form-item v-if="toolId==100|| toolId==102 || toolId==110"
                      v-for="item in majiaParams" :label="item.name" :prop="item.key">
          <param-item :ref="item.key" :param="item" v-if="item.type!='input'"></param-item>
          <el-input v-if="item.type=='input'" v-model="form[item.key]" :placeholder="item.tip"></el-input>
        </el-form-item>
        <el-form-item label="上传软件" prop="filePath" v-if="toolId!=113">
          <el-input v-model="form.filePath" v-show="false"></el-input>
          <nonimage-upload ref="upload" :limitRemark="'，文件请选用压缩包格式'" :limit="1" @setImgPath="setImgPath"
                           imageType="zip|rar"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit">提交</el-button>
          <el-button type="info" @click="cancel">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
  import {uploadTask, queryTask} from "@/api/task/task";
  import NonimageUpload from "./NonimageUpload";
  import ParamItem from "./ParamItem";
  import { checkTargets, checkPorts} from '@/utils/validate';
  export default {
    name: "TaskUpload",
    components: {ParamItem, NonimageUpload},
    data() {
      return {
        // 遮罩层
        loading: true,
        open: false,
        taskId: null,
        toolId: null,
        toolName: '',
        title: '上传测试任务',
        majiaParams: [{
          name: '文件名称',
          key: 'fileName',
          value: '',
          type: 'input',
          check: 'string',
          tip:''
        }, {
          name: '项目ID',
          key: 'projectId',
          value: '',
          type: 'input',
          check: 'number'
        }, {
          name: '用户ID',
          key: 'userId',
          value: '',
          type: 'input',
          check: 'number'
        }, {
          name: '版本',
          key: 'version',
          value: '',
          type: 'input',
          check: 'string',
          tip:''
        }, {
          name: '是否检测第三方插件',
          key: 'testPlug',
          value: 0,
          type: 'radio',
          option: [{label: '是', value: 1}, {label: '否', value: 0}]
        }, {
          name: '是否军用代码',
          key: 'testMilitary',
          value: 0,
          type: 'radio',
          option: [{label: '是', value: 1}, {label: '否', value: 0}]
        }, {
          name: '检测特征级别',
          key: 'testLevel',
          value: 0,
          type: 'radio',
          option: [{label: '函数', value: 1}, {label: '文件', value: 0}]
        }, {
          name: '是否存在分析结果',
          key: 'status',
          value: 0,
          type: 'radio',
          option: [{label: '是', value: 1}, {label: '否', value: 0}]
        }, {
          name: '灵敏度阈值',
          key: 'sensitivityThreshold',
          value: 50,
          type: 'slider',
          min: 0,
          max: 100
        }],
        keySwanParams: [{
          name: '检测特征级别',
          key: 'testLevel',
          value: 0,
          type: 'radio',
          option: [{label: '函数', value: 1}, {label: '文件', value: 0}]
        }, {
          name: '版本',
          key: 'version',
          value: '',
          type: 'input',
          check: 'string',
          tip:''
        }, {
          name: '灵敏度阈值',
          key: 'sensitivityThreshold',
          value: 50,
          type: 'slider',
          min: 0,
          max: 100
        }],
        eagleEyeParams: [{
          name: '开发语言',
          key: 'language',
          value: 'C',
          type: 'radio',
          option: [{label: 'C', value: 'C'}, {label: 'C++', value: 'C++'}, {label: 'JAVA', value: 'JAVA'}, {label: 'C#', value: 'C#'}]
        }, {
          name: '编译环境',
          key: 'build',
          value: 'makefile',
          type: 'radio',
          option: [{label: 'makefile', value: 'makefile'}, {label: 'VC', value: 'VC'}, {label: 'maven', value: 'maven'}]
        }, {
          name: '规则集',
          key: 'rule',
          value: 'GJB8114',
          type: 'select',
          option: [{label: 'GJB8114', value: 'GJB8114'},
            {
            label: 'GJB5369',
            value: 'GJB5369'
          }, {
            label: '112a34567q',
            value: '112a34567q'
          }, {
            label: 'MISRA C',
            value: 'MISRA C'
          }, {
            label: 'MISRA C 2004',
            value: 'MISRA C 2004'
          }, {
            label: 'MISRA C 2012',
            value: 'MISRA C 2012'
          }, {
            label: 'MISRA C++ 2008',
            value: 'MISRA C++ 2008'
          }]
        }],
        arvmsParams: [{
          name: '扫描目标列表',
          key: 'targets',
          value: '',
          type: 'input',
          check: 'string',
          tip: '多个IP地址或地址段使用逗号分隔，如:92.168.3.1,192.168.3.1/24 ,192.168.3.1-196.168.3.24'
        },{
          name: '基础扫描方法',
          key: 'bs_method',
          value: 'PING+SYN',
          type: 'select',
          option: [{label: 'PING+SYN', value: 'PING+SYN'}, {label: 'SYN Only', value: 'SYN Only'}, {label: 'PING+TCP', value: 'PING+TCP'}, {label: 'TCP Only', value: 'TCP Only'}, {label: 'UDP', value: 'UDP'}]
        }, {
          name: '端口名称',
          key: 'portName',
          value: 'Default',
          type: 'select',
          option: [{label: 'Default', value: 'Default'}, {label: 'Top-10', value: 'Top-10'}, {label: 'Top-100', value: 'Top-100'}, {label: 'Top-1000', value: 'Top-1000'}, {label: 'All_ports', value: 'All_ports'}]
        },{
          name: 'tcp',
          key: 'tcp',
          value: '',
          type: 'input',
          check: 'string',
          tip: '0-65535之间的数字,使用逗号分隔，如：12,13-14'
        },{
          name: 'udp',
          key: 'udp',
          value: '',
          type: 'input',
          check: 'string',
          tip: '0-65535之间的数字,使用逗号分隔，如：12,13-14'
        }, {
          name: '扫描速度',
          key: 'scanerSpeed',
          value: 'normal',
          type: 'radio',
          option: [{label: 'normal', value: 'normal'},
            {
              label: 'fast',
              value: 'fast'
            }, {
              label: 'extreme',
              value: 'extreme'
            }]
        }, {
          name: '进行OS类型探测',
          key: 'bs_detect_os',
          value: 'true',
          type: 'radio',
          option: [{label: '是', value: 'true'},
            {
              label: '否',
              value: 'false'
            }]
        }],
        form: {
          filePath: '',
          projectName: '',
          operParam: '',
          version: '',
          userId: 0,
          projectId: 0,
          fileName: '',
          tcp: '',
          udp: '',
          targets: ''
        },
        currentTool: {},
        // 表单校验
        rules: {
          typeArr: [
            {required: true, message: "服务项不能为空", trigger: "change"}
          ],
          projectName: [
            {required: true, message: "任务名称不能为空", trigger: "change"},
            {max: 500, message: '最大长度为500个字符', trigger: 'change'}
          ],
          version: [
            {required: true, message: "版本不能为空", trigger: "change"},
            {max: 100, message: '最大长度为100个字符', trigger: 'change'}
          ],
          fileName: [
            {required: true, message: "文件名称不能为空", trigger: "change"},
            {max: 100, message: '最大长度为100个字符', trigger: 'change'}
          ],
          projectId: [
            {required: true, message: "项目ID不能为空", trigger: "change"},
            {
              type: 'number',
              message: '请输入正整数',
              trigger: 'change',
              transform(value) {
                if (!/^[1-9]\d*$/.test(value)) {
                  return true
                } else {
                }
              }
            }],
          userId: [
            {required: true, message: "用户ID不能为空", trigger: "change"},
            {
              type: 'number',
              message: '请输入正整数',
              trigger: 'change',
              transform(value) {
                if (!/^[1-9]\d*$/.test(value)) {
                  return true
                } else {
                }
              }
            }],
          filePath: [
            {required: true, message: "请上传测试任务压缩包", trigger: "change"}
          ],
          targets: [
            {required: true, message: "扫描目标列表不能为空", trigger: "change"}
            // { validator: checkTargets, trigger: "change"},
            // { max: 100, message: '最大长度为100个字符', trigger: 'change'}
          ],
          tcp: [
            // { validator: checkPorts, trigger: "change"},
            { max: 100, message: '最大长度为100个字符', trigger: 'change'}
          ],
          udp: [
            // { validator: checkPorts, trigger: "change"},
            { max: 100, message: '最大长度为100个字符', trigger: 'change'}
          ]
        }
      }
    },
    methods: {
      submit() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.form.operParam = this.formatParams()
            this.form.toolId = this.toolId;
            this.form.taskId = this.taskId;
            uploadTask(this.form).then(response => {
              if (response.code == 200) {
                this.$notify({
                  title: '上传任务成功',
                  message: '请在我的任务查看测试进度',
                  align: 'center',
                  type: 'success'
                });
                this.open = false;
                this.resetForm("form");
                this.form.filePah = '';
                this.$emit("handleSuccess");
                if (this.toolId != 113)
                    this.$refs.upload.reset();
                this.toolId = null;
                this.$router.push({path: "/testmanage/myTask"});
              }
            });
          }
        });
      },
      setImgPath(value) {
        this.form.filePath = value;
      },
      openDialog(toolId, name) {
        this.toolId = toolId;
        this.toolName = name;
        this.open = true;
      },
      resubmit(taskId, toolId, toolName) {
        this.taskId = taskId;
        this.toolId = toolId;
        this.toolName = toolName;
        this.open = true;
        queryTask(this.taskId).then(response => {
          if (this.toolId != 113) {
            let testFile = [{
              name: '测试软件包',
              url: response.fileUrl,
              filePath: response.filePath,
            }];
            this.$refs.upload.setToolFileList(testFile);
          }
          let task = response.task;
          this.form.projectName = task.projectName;
          let params = this.majiaParams;
          if (this.toolId == 114) {
            params = this.keySwanParams;
          }
          if (this.toolId == 113) {
            params = this.arvmsParams;
          }
          if (this.toolId == 106 || this.toolId == 111) {
            params = this.eagleEyeParams;
          }
          let operParam = JSON.parse(task.operParam);
          for (let index in params) {
            if (operParam.hasOwnProperty(params[index].key)) {
              if (params[index].type == 'input') {
                if (params[index].check == 'number')
                  this.form[params[index].key] = parseInt(operParam[params[index].key]);
                else
                  this.form[params[index].key] = operParam[params[index].key];
              } else {
                params[index].value = operParam[params[index].key];
              }
            }
          }
        });
      },
      cancel() {
        this.resetForm("form");
        if (this.toolId != 113)
          this.$refs.upload.reset();
        this.open = false;
        this.toolId = null;
      },
      formatParams() {
        let params = this.majiaParams;
        if (this.toolId == 114) {
          params = this.keySwanParams;
        }
        if (this.toolId == 113) {
          params = this.arvmsParams;
        }
        if (this.toolId == 106 || this.toolId == 111) {
          params = this.eagleEyeParams;
        }
        let paramsJsonObject = {}
        if (this.toolId == 113) {
          if (this.form.projectName.length > 16)
             paramsJsonObject['taskName'] = this.form.projectName.substring(0,16);
          else
            paramsJsonObject['taskName'] = this.form.projectName;
            paramsJsonObject['taskDescription'] = paramsJsonObject['taskName'];
        }
        for (let index in params) {
          if (params[index].type == 'input') {
            if (params[index].check == 'number')
              paramsJsonObject[params[index].key] = parseInt(this.form[params[index].key]);
            else {
              if (params[index].key == 'targets') {//目标列表需要转成数组
                if (this.form[params[index].key].toString().endsWith(","))
                  this.form[params[index].key] = this.form[params[index].key].toString().substring(0, this.form[params[index].key].toString().length -1);
                paramsJsonObject[params[index].key] = this.form[params[index].key].toString().split(",");
              } else {
                paramsJsonObject[params[index].key] = this.form[params[index].key];
              }
            }
          } else {
            paramsJsonObject[params[index].key] = params[index].value;
          }
        }
        return JSON.stringify(paramsJsonObject);
      }
    },
    computed: {}
  }
</script>

<style scoped>
</style>
