<template>
  <div class="app-container">
    <div style="margin-bottom:10px">
      <el-button type="success" @click="addBtn">新增</el-button>
      <el-button type="info" @click="goSearchLogs">日志</el-button>
    </div>
    <!--搜索-->
    <el-form ref="form" class="mt_15" inline size="small">
      <el-form-item label="任务名称">
        <el-input v-model="searchParm.jobName" clearable placeholder="任务名称" />
      </el-form-item>
      <el-date-picker
        v-model="searchTime"
        type="daterange"
        range-separator=":"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        style="width:270px"
      />
      <el-form-item label="状态">
        <el-select v-model="searchParm.isPause" placeholder="请选择">
          <el-option
            v-for="item in isPauseList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="goSearchByCondition">搜索</el-button>
        <el-button type="warning" @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
    <!--表格渲染-->
    <el-table
      ref="table"
      border
      size="small"
      :data="list"
      :header-cell-style="{textAlign:'center'}"
      :cell-style="{textAlign:'center'}"
    >
      <el-table-column label="序号" type="index" width="50" />
      <el-table-column label="任务ID" prop="jobId" />
      <el-table-column label="任务名称" prop="jobName" />
      <el-table-column label="SpringBean名称" prop="beanName" />
      <el-table-column label="方法名称" prop="methodName" />
      <el-table-column label="参数" prop="params">
        <template slot-scope="scope">
          <el-button type="text" @click="showParams(scope.row)">查看参数</el-button>
        </template>
      </el-table-column>
      <el-table-column label="cron表达式" prop="cronExpression" />
      <el-table-column label="运行状态" prop="isPause">
        <template slot-scope="scope">{{ scope.row.isPause | getStatus }}</template>
      </el-table-column>
      <el-table-column label="描述" prop="description" />
      <el-table-column label="创建时间" prop="createTime" />
      <el-table-column label="操作" width="310">
        <template slot-scope="scope">
          <!-- <el-button type="info" @click="modifyStatus(scope.row.jobId)">恢复</el-button> -->
          <el-button
            type="info"
            @click="modifyStatus(scope.row.jobId)"
          >{{ scope.row.isPause ?'恢复':'暂停' }}</el-button>
          <el-button type="primary" @click="executionById(scope.row.jobId)">执行</el-button>
          <el-button type="warning" @click="editById(scope.row)">修改</el-button>
          <el-button type="danger" @click="delById(scope.row.jobId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <div class="mt_15">
      <el-pagination
        :current-page="searchParm.page"
        :page-sizes="[10, 20, 30, 40, 50]"
        :page-size="searchParm.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <!--新增弹窗-->
    <el-dialog :visible.sync="dialogAddVisible" :title="dialogAddTitle" center width="45%">
      <div class="dialog-content">
        <el-form ref="ruleForm" size="small" :model="addParm" :rules="rules" label-width="130px">
          <el-form-item label="任务名称" prop="jobName">
            <el-input v-model="addParm.jobName" clearable />
          </el-form-item>
          <el-form-item label="SpringBean名称" hidden prop="beanName">
            <el-input v-model="editParm.beanName" clearable />
          </el-form-item>
          <el-form-item label="方法名称" hidden prop="methodName">
            <el-input v-model="editParm.methodName" clearable />
          </el-form-item>
          <el-form-item label="描述" prop="description">
            <el-input v-model="addParm.description" clearable />
          </el-form-item>
          <el-form-item label="cron表达式" prop="cronExpression">
            <el-input v-model="addParm.cronExpression" clearable />
          </el-form-item>
          <el-form-item label="子任务ID" prop="subTask">
            <el-input v-model="addParm.subTask" clearable />
          </el-form-item>
          <el-form-item label="负责人" prop="personInCharge">
            <el-input v-model="addParm.personInCharge" clearable />
          </el-form-item>
          <el-form-item label="报警邮箱" prop="email">
            <el-input v-model="addParm.email" clearable />
          </el-form-item>
          <el-form-item label="失败后暂停" prop="pauseAfterFailure">
            <el-select v-model="addParm.pauseAfterFailure" placeholder="请选择">
              <el-option
                v-for="item in pauseAfterFailureList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>`
          </el-form-item>
          <el-form-item label="运行状态" prop="isPause">
            <el-select v-model="addParm.isPause" placeholder="请选择">
              <el-option
                v-for="item in isPauseList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="设备名称" prop="rebotName">
            <el-input v-model="addParm.rebotName" disabled="true" style="width:200px" />
            <el-button
              type="primary"
              icon="el-icon-search"
              style="width:130px"
              @click="goSearchRebot"
            >请选择设备</el-button>
          </el-form-item>
          <el-form-item label="应用名称" prop="applyId">
            <el-select v-model="addParm.applyId" placeholder="请选择" @change="applyClick">
              <el-option
                v-for="item in applys"
                :key="item.name"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-form>
        <div id="app">
          <el-form ref="baseForm" class="base-form" :model="baseForm">
            <el-table
              ref="table-input"
              class="table"
              highlight-current-row
              :data="baseForm.demoList"
              @row-click="selectItem"
            >
              <el-table-column label="参数名称">
                <template slot-scope="scope">
                  <el-form-item :prop="'demoList.'+scope.$index+'.name'" class="all">
                    <el-input v-model="scope.row.name" placeholder="请输入" />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="参数值">
                <template slot-scope="scope">
                  <el-form-item :prop="'demoList.'+scope.$index+'.value'" class="all">
                    <el-input v-model="scope.row.value" placeholder="请输入" />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="参数类型">
                <template slot-scope="scope">
                  <el-form-item :prop="'demoList.'+scope.$index+'.type'" class="all">
                    <el-input v-model="scope.row.type" placeholder="请输入" />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column prop label="操作">
                <template>
                  <div class="flex-c-a">
                    <el-button type="primary" @click="addLine()">增加</el-button>
                    <el-button type="danger">删除</el-button>
                  </div>
                </template>
              </el-table-column>
            </el-table>
          </el-form>
        </div>
      </div>
      <span slot="footer">
        <el-button type="info" @click="dialogAddVisible=flase">取消</el-button>
        <el-button type="primary" @click="sureAdd">确认</el-button>
      </span>
    </el-dialog>

    <!--修改弹窗-->
    <el-dialog :visible.sync="dialogEditVisible" :title="dialogEditTitle" center width="45%">
      <div class="dialog-content">
        <el-form ref="ruleForm" size="small" :model="editParm" :rules="rules" label-width="130px">
          <el-form-item label="任务名称" prop="jobName">
            <el-input v-model="editParm.jobName" clearable />
          </el-form-item>
          <el-form-item label="描述" prop="description">
            <el-input v-model="editParm.description" clearable />
          </el-form-item>
          <el-form-item label="SpringBean名称" hidden prop="beanName">
            <el-input v-model="editParm.beanName" clearable />
          </el-form-item>
          <el-form-item label="方法名称" hidden prop="methodName">
            <el-input v-model="editParm.methodName" hidden clearable />
          </el-form-item>
          <el-form-item label="cron表达式" prop="cronExpression">
            <el-input v-model="editParm.cronExpression" clearable />
          </el-form-item>
          <el-form-item label="子任务ID" prop="subTask">
            <el-input v-model="editParm.subTask" clearable />
          </el-form-item>
          <el-form-item label="负责人" prop="personInCharge">
            <el-input v-model="editParm.personInCharge" clearable />
          </el-form-item>
          <el-form-item label="报警邮箱" prop="email">
            <el-input v-model="editParm.email" clearable />
          </el-form-item>
          <el-form-item label="失败后暂停" prop="pauseAfterFailure">
            <el-select v-model="editParm.pauseAfterFailure" placeholder="请选择">
              <el-option
                v-for="item in pauseAfterFailureList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>`
          </el-form-item>
          <el-form-item label="运行状态" prop="isPause">
            <el-select v-model="editParm.isPause" placeholder="请选择">
              <el-option
                v-for="item in isPauseList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="设备名称" prop="rebotName">
            <el-input v-model="editParm.rebotName" disabled="true" style="width:200px" />
            <el-button
              type="primary"
              icon="el-icon-search"
              style="width:130px"
              @click="goSearchRebot"
            >请选择设备</el-button>
          </el-form-item>
          <el-form-item label="应用名称" prop="applyId">
            <el-select v-model="editParm.applyId" placeholder="请选择" @change="applyClick">
              <el-option
                v-for="item in applys"
                :key="item.name"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-form>
        <div id="app">
          <el-form ref="baseForm" class="base-form" :model="baseForm">
            <el-table
              ref="table-input"
              class="table"
              highlight-current-row
              :data="baseForm.demoList"
              @row-click="selectItem"
            >
              <el-table-column label="参数名称">
                <template slot-scope="scope">
                  <el-form-item :prop="'demoList.'+scope.$index+'.name'" class="all">
                    <el-input v-model="scope.row.name" placeholder="请输入" />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="参数值">
                <template slot-scope="scope">
                  <el-form-item :prop="'demoList.'+scope.$index+'.value'" class="all">
                    <el-input v-model="scope.row.value" placeholder="请输入" />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="参数类型">
                <template slot-scope="scope">
                  <el-form-item :prop="'demoList.'+scope.$index+'.type'" class="all">
                    <el-input v-model="scope.row.type" placeholder="请输入" />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column prop label="操作">
                <template>
                  <div class="flex-c-a">
                    <el-button type="primary" @click="addLine()">增加</el-button>
                    <el-button type="danger">删除</el-button>
                  </div>
                </template>
              </el-table-column>
            </el-table>
          </el-form>
        </div>
      </div>
      <span slot="footer">
        <el-button type="info" @click="dialogEditVisible=false">取消</el-button>
        <el-button type="primary" @click="sureEdit">确认</el-button>
      </span>
    </el-dialog>

    <!--日志弹窗-->
    <el-dialog
      width="75%"
      :visible.sync="dialogLogsListVisible"
      :title="dialogLogsListTitle"
      center
      append-to-body
    >
      <!-- 搜索日志 -->
      <el-form ref="form" class="mt_15" inline size="small">
        <el-form-item label="任务名称">
          <el-input v-model="searchLogsParm.jobName" clearable placeholder="任务名称" />
        </el-form-item>
        <el-date-picker
          v-model="searchLogsTime"
          type="daterange"
          range-separator=":"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          style="width:270px"
        />
        <el-form-item label="状态">
          <el-input v-model="searchLogsParm.isSuccess" clearable placeholder="状态" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="goSearchLogsByCondition">搜索</el-button>
          <el-button type="warning" @click="resetLogsForm">重置</el-button>
        </el-form-item>
      </el-form>
      <!--表格渲染-->
      <el-table
        ref="table"
        border
        size="small"
        :data="logsList"
        :header-cell-style="{textAlign:'center'}"
        :cell-style="{textAlign:'center'}"
      >
        <el-table-column label="任务名称" prop="jobName" />
        <el-table-column label="Bean名称" prop="beanName" />
        <el-table-column label="执行方法" prop="methodName" />
        <el-table-column label="参数">
          <template slot-scope="scope">
            <el-button type="text" @click="showParams(scope.row)">查看参数</el-button>
          </template>
        </el-table-column>
        <el-table-column label="cron表达式" prop="cronExpression" />
        <el-table-column label="异常详情" prop="exceptionDetail">
          <template slot-scope="scope">
            <el-button type="text" @click="showDetail(scope.row)">查看异常详情</el-button>
          </template>
        </el-table-column>
        <el-table-column label="耗时(毫秒)" prop="time" />
        <el-table-column label="状态" prop="isSuccess">
          <template slot-scope="scope">{{ scope.row.isSuccess | getLogIsSuccess }}</template>
        </el-table-column>
        <el-table-column label="创建日期" prop="createTime" />
      </el-table>
      <!--分页-->
      <div class="mt_15">
        <el-pagination
          :current-page="searchLogsParm.page"
          :page-sizes="[10, 20, 30, 40, 50]"
          :page-size="searchLogsParm.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="logsTotal"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-dialog>

    <!--设备选择弹窗-->
    <el-dialog
      width="75%"
      :visible.sync="dialogRebotListVisible"
      :title="dialogRebotListTitle"
      center
      append-to-body
    >
      <!--搜索-->
      <el-form ref="form" class="mt_15" inline size="small">
        <el-form-item label="机器人账号">
          <el-input v-model="searchRebotParm.accountName" clearable placeholder="机器人账号" />
        </el-form-item>
        <el-form-item label="uuid">
          <el-input v-model="searchRebotParm.robotClientUuid" clearable placeholder="uuid" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="goSearchRebot">搜索</el-button>
          <el-button type="warning" @click="resetRebotForm">重置</el-button>
        </el-form-item>
      </el-form>
      <!--表格渲染-->
      <el-table
        ref="table"
        border
        size="small"
        :data="rebotList"
        :header-cell-style="{textAlign:'center'}"
        :cell-style="{textAlign:'center'}"
      >
        <el-table-column label="序号" type="index" width="50" />
        <el-table-column label="机器人账号" prop="accountName" />
        <el-table-column label="uuid信息" prop="robotClientUuid" />
        <el-table-column label="状态" prop="status">
          <template slot-scope="scope">{{ scope.row.status | getRebotStatus }}</template>
        </el-table-column>
        <el-table-column label="创建时间" prop="createdAt" />
        <el-table-column label="修改时间" prop="updatedAt" />
        <el-table-column label="操作" width="220">
          <template slot-scope="scope">
            <el-button type="success" @click="pickRebot(scope.row)">选中</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <div class="mt_15">
        <el-pagination
          :current-page="searchRebotParm.page"
          :page-sizes="[10, 20, 30, 40, 50]"
          :page-size="searchRebotParm.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="this.rebotTotal"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-dialog>

    <!--參數詳情-->
    <el-dialog
      :title="dialogParamTitle"
      :visible.sync="dialogParamVisible"
      width="30%"
      style="overflow: hidden"
    >
      <div style="overflow-y: auto; height: 310px">
        <json-viewer :value="paramJsonData" :expand-depth="5" copyable boxed sort />
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogParamVisible = false">确定</el-button>
      </span>
    </el-dialog>

    <!--异常详情-->
    <el-dialog :title="dialogExceTitle" :visible.sync="dialogExceVisible" width="30%">
      <div v-text="this.exceptionDetail" />
    </el-dialog>
  </div>
</template>
<script>
import {
  getList,
  add,
  editById,
  delById,
  getById,
  modifyStatus,
  executionById,
  getLogsList
} from '@/api/rpa/quartzJob'
import { getAll } from '@/api/rpa/apply'
import { getFreeList } from '@/api/rpa/rebot'
import Vue from 'vue'
import JsonViewer from 'vue-json-viewer'
Vue.use(JsonViewer)
export default {
  filters: {
    getStatus: function(value) {
      if (value == 1) {
        return '已暂停'
      } else if (value == 0) {
        return '运行中'
      }
    },
    getLogIsSuccess: function(value) {
      if (value == 1) {
        return '成功'
      } else if (value == 0) {
        return '失败'
      }
    },
    getRebotStatus: function(value) {
      if (value == 0) {
        return '未占用'
      } else if (value == 1) {
        return '已占用'
      }
    }
  },
  //	数据
  data() {
    return {
      searchParm: {
        page: 1,
        pageSize: 10,
        sort: 'job_id',
        order: 'desc',
        jobName: null,
        isPause: null,
        sTime: null,
        eTime: null
      },
      searchLogsParm: {
        page: 1,
        pageSize: 10,
        sort: 'log_id',
        order: 'desc',
        jobName: null,
        isSuccess: null,
        sTime: null,
        eTime: null
      },
      searchRebotParm: {
        page: 1,
        pageSize: 10,
        sort: 'id',
        order: 'desc',
        accountName: null,
        robotClientUuid: null
      },
      logsTotal: 0,
      rebotTotal: 0,
      total: 0,
      logsList: [],
      list: [],
      baseForm: {
        demoList: [
          {
            name: null,
            value: null,
            type: null
          }
        ]
      },
      newParams: {
        name: '',
        value: '',
        type: ''
      },
      index: 0,
      // 新增、修改弹窗判断：1新增、2修改
      isEdit: 0,
      rebotList: [],
      applys: [],
      searchTime: '',
      searchLogsTime: '',
      paramJsonData: '',
      params: [],
      taskParam: {
        name: null,
        value: null,
        type: null
      },
      requestParams: {
        accountName: null,
        applyId: null,
        params: null
      },
      exceptionDetail: '',
      dialogAddVisible: false,
      dialogAddTitle: '新增',
      dialogEditVisible: false,
      dialogEditTitle: '修改',
      dialogImportVisible: false,
      dialogLogsListTitle: '操作日志',
      dialogLogsListVisible: false,
      dialogRebotListTitle: '设备日志',
      dialogRebotListVisible: false,
      dialogParamVisible: false,
      dialogParamTitle: '参数详情',
      dialogExceVisible: false,
      dialogExceTitle: '异常详情',
      pauseAfterFailureList: [
        {
          value: 1,
          label: '是'
        },
        {
          value: 0,
          label: '否'
        }
      ],
      isPauseList: [
        {
          value: 1,
          label: '已暂停'
        },
        {
          value: 0,
          label: '运行中'
        }
      ],
      logIsSuccess: [
        {
          value: 0,
          label: '失败'
        },
        {
          value: 1,
          label: '成功'
        }
      ],
      addListDemo: [],
      addParm: {
        jobId: null,
        jobName: null,
        description: null,
        beanName: null,
        methodName: null,
        cronExpression: null,
        subTask: null,
        personInCharge: null,
        email: null,
        pauseAfterFailure: null,
        isPause: null,
        rebotName: null,
        applyId: null,
        params: null
      },
      editParm: {
        jobId: null,
        jobName: null,
        description: null,
        beanName: null,
        methodName: null,
        cronExpression: null,
        subTask: null,
        personInCharge: null,
        email: null,
        pauseAfterFailure: null,
        isPause: null,
        params: null,
        applyId: null,
        rebotName: null
      },
      testTest: null,
      testTestStr: '',
      ruleForm: {
        contntryShort: null,
        ipAddress: null,
        parentIpAddress: null,
        port: null,
        name: null,
        pwd: null,
        vpsType: null,
        dealer: null,
        mac: null,
        status: null
      },
      rules: {
        jobName: [
          { required: true, message: '请输入任务名称', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入任务描述', trigger: 'blur' }
        ],
        // beanName: [
        //     { required: true, message: '请输入SpringBean名称', trigger: 'blur' }
        //   ],
        // methodName: [
        //     { required: true, message: '请输入方法名称', trigger: 'blur' }
        //   ],
        cronExpression: [
          { required: true, message: '请输入cron表达式', trigger: 'blur' }
        ],
        personInCharge: [
          { required: true, message: '请输入负责人', trigger: 'blur' }
        ],
        pauseAfterFailure: [
          { required: true, message: '请选择失败后是否暂停', trigger: 'blur' }
        ],
        isPause: [
          { required: true, message: '请选择运行状态', trigger: 'blur' }
        ],
        rebotName: [{ required: true, message: '请选择设备', trigger: 'blur' }],
        applyId: [{ required: true, message: '请选择应用', trigger: 'blur' }]
      },
      paramsRules: {
        name: [
          {
            required: true,
            message: '请输入参数名称',
            trigger: 'blur'
          }
        ],
        value: [
          {
            required: true,
            message: '请输入参数值',
            trigger: 'blur'
          }
        ],
        type: [
          {
            required: true,
            message: '请输入参数类型',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  // 调用
  created() {
    this.goSearch()
  },
  // 方法
  methods: {
    showDetail(data) {
      if (data.exceptionDetail == null || data.exceptionDetail == '') {
        this.$alert('无异常', '异常详情', {
          confirmButtonText: '确定',
          closeOnClickModal: true,
          closeOnPressEscape: true
        })
      } else {
        this.dialogExceVisible = true
        this.exceptionDetail = data.exceptionDetail
      }
    },
    showParams(data) {
      console.log(data)
      if (data.params == null || data.params == '') {
        this.$alert('无参数', '参数详情', {
          confirmButtonText: '确定',
          closeOnClickModal: true,
          closeOnPressEscape: true
        })
      } else {
        this.paramJsonData = JSON.parse(data.params)
        this.dialogParamVisible = true
      }
    },
    applyClick(value) {
      this.requestParams.applyId = value
      console.log(this.requestParams)
    },
    // 重置
    resetForm() {
      this.searchParm.jobName = null
      this.searchParm.isPause = null
      this.searchParm.sTime = null
      this.searchParm.eTime = null
      this.searchTime = null
      this.goSearch()
    },
    resetLogsForm() {
      this.searchLogsParm.jobName = null
      this.searchLogsParm.isSuccess = null
      this.searchLogsParm.sTime = null
      this.searchLogsParm.eTime = null
      this.searchLogsTime = null
      this.goSearchLogs()
    },
    resetRebotForm() {
      this.searchRebotParm.accountName = null
      this.searchRebotParm.robotClientUuid = null
      this.goSearchRebot()
    },
    //	查询
    goSearchByCondition() {
      this.searchParm.sTime = this.searchTime[0]
      this.searchParm.eTime = this.searchTime[1]
      console.log(this.searchParm)
      this.goSearch()
    },
    goSearch() {
      this.searchParm.page = 1
      this.getAllApply()
      this.getDataList()
    },
    goSearchLogsByCondition() {
      this.searchLogsParm.sTime = this.searchLogsTime[0]
      this.searchLogsParm.eTime = this.searchLogsTime[1]
      this.goSearchLogs()
    },
    goSearchLogs() {
      this.dialogLogsListVisible = true
      this.searchLogsParm.page = 1
      this.getLogsDataList()
    },
    goSearchRebot() {
      this.dialogRebotListVisible = true
      this.searchRebotParm.page = 1
      this.getRebotDataList()
    },
    // 分页获取表格数据
    getDataList() {
      getList(this.searchParm)
        .then(res => {
          if (res.code == 200) {
            this.list = res.data.list
            this.total = res.data.total
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    getLogsDataList() {
      getLogsList(this.searchLogsParm)
        .then(res => {
          console.log(res)
          if (res.code == 200) {
            this.logsList = res.data.list
            this.logsTotal = res.data.total
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    getRebotDataList() {
      getFreeList(this.searchRebotParm)
        .then(res => {
          console.log(res)
          if (res.code == 200) {
            this.rebotList = res.data.list
            this.rebotTotal = res.data.total
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    getAllApply() {
      getAll().then(res => {
        if (res.code == 200) {
          this.applys = res.data
        }
      })
    },
    // 新增
    addBtn() {
      this.addParm = {
        id: null,
        jobId: null,
        jobName: null,
        description: null,
        beanName: null,
        methodName: null,
        cronExpression: null,
        subTask: null,
        personInCharge: null,
        email: null,
        pauseAfterFailure: null,
        isPause: null,
        params: null,
        applyId: null,
        rebotName: null
      }

      this.baseForm.demoList = [
        {
          name: '',
          value: '',
          type: ''
        }
      ]
      this.requestParams.applyId = ''
      this.requestParams.accountName = ''
      this.requestParams.params = ''
      this.addParm.params = JSON.stringify(this.requestParams)
      this.isEdit = 1
      this.dialogAddVisible = true
    },
    // 确认新增
    sureAdd() {
      this.$refs.baseForm.validate(valid => {
        if (valid) {
          this.requestParams.params = this.baseForm.demoList
          this.addParm.params = JSON.stringify(this.requestParams)
          add(this.addParm)
            .then(res => {
              if (res.code == 200) {
                this.dialogAddVisible = false
                this.$message.success('新增成功!')
                this.getDataList()
              } else if (res.code == -1) {
                this.$message.warning(res.message)
              }
            })
            .catch(err => {
              console.log(err)
            })
        } else {
          this.$message.warning('请输入所有必填项')
        }
      })
    },
    //	根据id删除
    delById(id) {
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          delById({ id })
            .then(res => {
              if (res.code == 200) {
                this.$message.success('删除成功!')
                this.getDataList()
              }
            })
            .catch(err => {
              console.log(err)
            })
        })
        .catch(() => {
          this.$message.info('取消删除')
        })
    },
    executionById(id) {
      executionById({ id })
        .then(res => {
          if (res.code == 200) {
            this.$message.success('执行成功')
            this.getDataList
          } else {
            this.$message.error('执行失败')
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    //	修改
    editById(data) {
      getById({ id: data.jobId })
        .then(res => {
          if (res.code == 200) {
            this.editParm.jobId = res.data.jobId
            this.editParm.jobName = res.data.jobName
            this.editParm.description = res.data.description
            this.editParm.beanName = res.data.beanName
            this.editParm.methodName = res.data.methodName
            this.editParm.cronExpression = res.data.cronExpression
            this.editParm.subTask = res.data.subTask
            this.editParm.personInCharge = res.data.personInCharge
            this.editParm.email = res.data.email
            this.editParm.pauseAfterFailure = res.data.pauseAfterFailure
            this.editParm.isPause = res.data.isPause
            this.editParm.params = res.data.params
            this.requestParams = JSON.parse(this.editParm.params)
            this.editParm.rebotName = this.requestParams.accountName
            this.editParm.applyId = this.requestParams.applyId
            console.log(this.requestParams.params)
            if (this.requestParams.params.length != 0) {
              this.baseForm.demoList = this.requestParams.params
            } else {
              this.baseForm.demoList = [
                {
                  name: '',
                  value: '',
                  type: ''
                }
              ]
            }

            this.isEdit = 2
            this.dialogEditVisible = true
            this.$refs.ruleForm.clearValidate()
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    // 确认修改
    sureEdit() {
      this.$refs.baseForm.validate(valid => {
        if (valid) {
          this.requestParams.params = this.baseForm.demoList
          this.editParm.params = JSON.stringify(this.requestParams)
          editById(this.editParm)
            .then(res => {
              if (res.code == 200) {
                this.dialogEditVisible = false
                this.$message.success('修改成功!')
                this.getDataList()
              }
            })
            .catch(err => {
              console.log(err)
            })
        } else {
          this.$message.warning('请输入所有必填项')
        }
      })
    },
    modifyStatus(id) {
      modifyStatus({ id })
        .then(res => {
          if (res.code == 200) {
            this.$message.success('更改成功!')
            this.getDataList()
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    pickRebot(data) {
      if (this.isEdit == 1) {
        this.addParm.rebotName = data.accountName
      } else if (this.isEdit == 2) {
        this.editParm.rebotName = data.accountName
      }
      this.requestParams.accountName = data.accountName
      this.dialogRebotListVisible = false
      this.$message.success('选择成功')
    },
    //	分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.searchParm.pageSize = val
      this.getDataList()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.searchParm.page = val
      this.getDataList()
    },
    // 选中某一行修改或移除
    selectItem(row, column, event) {
      this.selectedFundRow = row
      if (event.target.innerText == '删除') {
        this.removeFundBtn(this.selectedFundRow)
      }
    },
    // 删除指定行
    removeFundBtn(params) {
      this.baseForm.demoList = this.baseForm.demoList.filter(ele => {
        var flag = false
        // 如果不一致，则保留该行
        for (const key in params) {
          if (ele[key] != params[key]) {
            flag = true
            break
          }
        }
        return flag
      })
      // 如果全部删除后没有可以点击的一行了，需要加一行空行
      if (!this.baseForm.demoList.length) {
        this.addLine()
      }
    },
    // 增加一个空行, 用于录入或显示第一行
    addLine() {
      const newParams = {
        name: '',
        value: '',
        type: ''
      }
      this.index++
      this.baseForm.demoList.push(newParams)
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
</style>
