<template>
  <div>
    <manager-ui
      :loading="loading"
      title="体检列表"
      :query-model="form"
      :query-items="queryItems"
      :static-data="staticData"
      :column-items="columnItems"
      :table-data="tableData"
      :pagination="pagination"
      show-table-tools
      :show-download-btn="false"
      :show-add-btn="false"
      @searchClick="pagination.current=1;batchRows = [];search();"
      @resetForm="pagination.current=1;batchRows = [];resetForm();"
      @search="search"
      @selection-change="doSelectHandle"
    >
      <template #status="{scope}">
        {{ scope.row.status | toCase(staticData.statusList,'value', 'label') }}
      </template>
      <template #operate="{scope}">
        <el-context-menu
          :menus="menus"
          :scope="scope"
          :default="3"
          :visibles="[scope.row.status == '4',scope.row.status == '2']"
          @oper="clickRow"
        />
      </template>
      <template slot="tableTools">
        <el-button type="text" icon="el-icon-video-pause" @click="operateActionbatch('cancle')">
          取消体检
        </el-button>
        <el-button type="text" icon="el-icon-circle-close" @click="operateActionbatch('close')">
          关闭定时
        </el-button>
        <el-button type="text" icon="el-icon-refresh" @click="operateActionbatch('resultSyn')">
          结果同步
        </el-button>
      </template>
      <template slot="tableDropdown">
        <el-button type="text" icon="el-icon-refresh" @click="operateActionbatch('argSyn')">
          参数同步
        </el-button>
      </template>
    </manager-ui>
    <!--立即体检-->
    <manager-dialog :visible.sync="nowDialog" title="立即体检" icon="el-icon-setting" width="600px">
      <el-form ref="nowForm" label-width="110px" :model="nowForm">
        <template v-if="taskType == 2">
          <el-form-item label="定时体检开关：" prop="status">
            <el-switch
              v-model="nowForm.status"
              active-color="#2EA1F8"
            />
          </el-form-item>
          <el-form-item label="定时频率设置：" prop="interval">
            <el-radio-group v-model="nowForm.interval">
              <el-row>
                <el-col :span="11">
                  <el-radio :label="1">每隔
                    <el-select
                      v-model="nowForm.days"
                      placeholder="请选择"
                      style="width:70%"
                      :disabled="nowForm.interval == 2"
                    >
                      <el-option
                        v-for="item in staticData.dayInterval"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                      />
                    </el-select>
                  </el-radio>

                </el-col>
                <el-col :span="11" :offset="2">
                  <el-radio :label="2">每周
                    <el-select
                      v-model="nowForm.dayOfWeek"
                      placeholder="请选择"
                      style="width:70%"
                      :disabled="nowForm.interval == 1"
                    >
                      <el-option
                        v-for="item in staticData.weekInterval"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                      />
                    </el-select>
                  </el-radio>
                </el-col>
              </el-row>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="体检时间段设置：" prop="time">
            <el-time-picker
              v-model="nowForm.time"
              is-range
              range-separator="至"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              placeholder="选择时间范围"
              value-format="HH-mm"
            />
          </el-form-item>

        </template>
        <el-form-item label="漏洞风险检查项：" prop="riskItems">
          <el-checkbox-group v-model="nowForm.riskItems">
            <el-checkbox label="1">系统漏洞</el-checkbox>
            <el-checkbox label="2">弱口令</el-checkbox>
            <el-checkbox label="3">风险账号</el-checkbox>
            <el-checkbox label="4">配置缺陷</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="入侵威胁检查项：" prop="scanItems">
          <el-checkbox-group v-model="nowForm.scanItems">
            <el-checkbox label="1">反弹shell</el-checkbox>
            <el-checkbox label="2">异常账号</el-checkbox>
            <el-checkbox label="3">系统命令篡改</el-checkbox>
            <el-checkbox label="4">异常进程</el-checkbox>
            <el-checkbox label="5">日志异常删除</el-checkbox>
            <el-row>
              <el-col :span="11">
                <el-checkbox label="11">病毒木马<el-alert
                  title="资源消耗高"
                  type="warning"
                  show-icon
                  :closable="false"
                  style="display: inline;background-color: unset;"
                />
                </el-checkbox>
                <div v-show="nowForm.scanItems.includes('11')" style="border:1px solid #edf0f3;padding:10px">
                  <el-radio-group v-model="nowForm.scanType">
                    <el-radio :label="1">快速扫描</el-radio>
                    <el-radio :label="2">全盘扫描</el-radio>
                    <el-radio :label="3">自定义扫描</el-radio>
                  </el-radio-group>
                  <el-input
                    v-show="nowForm.scanType == 3"
                    v-model="nowForm.virusPath"
                    type="textarea"
                    :rows="2"
                    placeholder="例如：/home/test;/home/test01/;"
                  />
                </div>
              </el-col>
              <el-col :span="11" :offset="2">
                <el-checkbox label="12">网页后门
                  <el-alert
                    title="资源消耗高"
                    type="warning"
                    show-icon
                    :closable="false"
                    style="display: inline;background-color: unset;"
                  />
                </el-checkbox>
                <div v-show="nowForm.scanItems.includes('12')" style="border:1px solid #edf0f3;padding:10px">
                  <el-input
                    v-model="nowForm.webShellPath"
                    type="textarea"
                    :rows="2"
                    placeholder="例如：/home/test/;/home/test01/;"
                  />
                </div>
              </el-col>
            </el-row>
          </el-checkbox-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="nowDialog=false">取消</el-button>
        <el-button type="primary" @click="submitnowDialog">确定</el-button>
      </span>
    </manager-dialog>
  </div>
</template>

<script>
import { query, cancleOperate, operateSyn, nowAssetTask, cycleAssetTask,
  closeOperate, argSynOperate, resultSynOperate, sdAgentTaskRiskitems } from './api'
export default {
  data() {
    return {
      loading: false,
      nowDialog: false,
      taskType: 1, // 1立即体检 2定时体检
      form: {
        status: null,
        score: null,
        type: null,
        serverIp: null
      },
      queryItems: [
        {
          type: 'select',
          span: 12,
          key: 'status',
          clearable: true,
          multiple: true,
          placeholder: '请选择体检状态',
          label: '体检状态',
          option: 'statusList',
          optionLabel: 'label',
          optionValue: 'value'
        }, {
          type: 'select',
          span: 12,
          key: 'score',
          clearable: true,
          multiple: true,
          placeholder: '请选择体检分数',
          label: '体检分数',
          option: 'scoreList',
          optionLabel: 'label',
          optionValue: 'value'
        }, {
          type: 'select',
          span: 12,
          key: 'type',
          clearable: true,
          multiple: true,
          placeholder: '请选择体检类型',
          label: '体检类型',
          option: 'typeList',
          optionLabel: 'label',
          optionValue: 'value'
        }, {
          type: 'input',
          span: 12,
          key: 'serverIp',
          label: '主机IP'
        }
      ],
      columnItems: [
        {
          type: 'selection',
          width: 60
        },
        {
          _key: 'serverIp',
          prop: 'serverIp',
          label: '主机'
        },
        {
          _key: 'progress',
          prop: 'progress',
          label: '体检进度'
        },
        {
          _key: 'status',
          prop: 'status',
          label: '状态',
          render: true
        },
        {
          _key: 'score',
          prop: 'score',
          label: '体检得分'
        },
        {
          _key: 'stime',
          prop: 'stime',
          label: '开始时间'
        },
        {
          _key: 'etime',
          prop: 'etime',
          label: '完成时间'
        }, {
          _key: 'operate',
          prop: 'operate',
          width: '115px',
          label: '操作',
          render: true
        }
      ],
      pagination: {
        current: 1,
        size: 10,
        total: 0
      },
      tableData: [],
      menus: [
        {
          text: '同步',
          command: 'syn',
          icon: 'el-icon-refresh'
        }, {
          text: '立即体检',
          command: 'now',
          icon: 'el-icon-video-play'
        }, {
          text: '取消体检',
          command: 'cancle',
          icon: 'el-icon-video-pause'
        }, {
          text: '定时体检',
          command: 'cycle',
          icon: 'el-icon-timer'
        }
      ],
      batchRows: [],
      // 弹窗表单
      nowForm: {
        interval: 1,
        status: 1,
        riskItems: [],
        scanItems: [],
        scanType: null,
        virusPath: null, // 多个路径通过@_@拼接
        webShellPath: null, // 多个路径通过@_@拼接
        time: ['00-00', '00-03']
      }
    }
  },
  computed: {
    staticData() {
      return {
        statusList: [
          { value: '0', label: '未体检' },
          { value: '1', label: '待体检' },
          { value: '2', label: '体检中' },
          { value: '3', label: '已完成' },
          { value: '4', label: '已取消' }
        ],
        scoreList: [
          { value: '0', label: '0-59（不健康）' },
          { value: '1', label: '60-89（亚健康）' },
          { value: '2', label: '90-100（健康）' }
        ],
        typeList: [
          { value: '1', label: '非定时体检' },
          { value: '2', label: '定时体检' }
        ],
        dayInterval: [
          { value: '1', label: '1天' },
          { value: '3', label: '3天' },
          { value: '5', label: '5天' },
          { value: '7', label: '1周' },
          { value: '30', label: '1个月' }
        ],
        weekInterval: [
          { value: '1', label: '周一' },
          { value: '2', label: '周二' },
          { value: '3', label: '周三' },
          { value: '4', label: '周四' },
          { value: '5', label: '周五' },
          { value: '6', label: '周六' },
          { value: '7', label: '周日' }
        ]
      }
    }
  },
  created() {
    this.search()
  },
  methods: {
    // 按钮操作-开始
    clickRow(command, scope) {
      if (command === 'syn') {
        this.operateSyn(scope.row.macCode)
      }
      if (command === 'now') {
        this.taskType = 1
        this.nowDialog = true
      }
      if (command === 'cancle') {
        this.operateTooltip('cancle', [scope.row.macCode])
      }
      if (command === 'cycle') {
        this.taskType = 2
        this.sdAgentTaskRiskitems(scope.row.macCode)
        this.nowDialog = true
      }
    },
    operateActionbatch(action) {
      this.operateTooltip(action, this.batchRows.map(item => item.macCode))
    },
    operateTooltip(action, macCodes) {
      this.$confirm('确认执行此次操作？').then(() => {
        this.operateAction(action, macCodes)
      }).catch(() => {})
    },
    getActionPromise(acType) {
      return {
        cancle: cancleOperate,
        close: closeOperate,
        argSyn: argSynOperate,
        resultSyn: resultSynOperate
      }
    },
    operateAction(action, macCodes) {
      const promise = this.getActionPromise(action)[action]
      var params = {
        'map': {
          'macCodes': macCodes
        }
      }
      promise(params).then(resp => {
        var d = resp.data
        if (d.returnCode == '0') {
          this.$message({
            message: d.returnMsg,
            type: 'success'
          })
          this.search()
        } else {
          this.$message({
            message: d.returnMsg,
            type: 'error'
          })
        }
      }, (error) => {
        console.log(error)
      })
    },
    operateSyn(macCode) {
      operateSyn(
        { 'map': {
          'macCode': macCode
        }}
      ).then(resp => {
        var d = resp.data
        if (d.returnCode == '0') {
          this.$message({
            message: d.returnMsg,
            type: 'success'
          })
          this.search()
        } else {
          this.$message({
            message: d.returnMsg,
            type: 'error'
          })
        }
      })
    },
    sdAgentTaskRiskitems(macCode) {
      sdAgentTaskRiskitems(macCode).then(resp => {
        this.nowForm = resp.data.data
      })
    },
    submitnowDialog() {
      const promise = this.taskType == 1 ? nowAssetTask : cycleAssetTask
      var params = {
        map: this.nowForm
      }
      promise(params).then(resp => {
        var d = resp.data
        if (d.returnCode == '0') {
          this.$message({
            message: d.returnMsg,
            type: 'success'
          })
          this.nowDialog = false
          this.search()
        } else {
          this.$message({
            message: d.returnMsg,
            type: 'error'
          })
        }
      })
    },
    // 按钮操作-结束
    resetForm() {
      this.search()
    },
    doSelectHandle: function(selection, row) {
      this.batchRows = selection
    },
    search() {
      this.loading = true
      var params = {
        map: this.form,
        page: this.pagination
      }
      query(params).then((rep) => {
        this.tableData = rep.data.data.list
        this.pagination.total = rep.data.data.page.total
        this.loading = false
      }, (error) => {
        this.loading = false
        console.log(error)
      })
    }
  }
}
</script>
