<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="计划名称" prop="planName">
        <el-input v-model="queryParams.planName" placeholder="请输入计划名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="执行类型" prop="planType">
        <el-select v-model="queryParams.planType" placeholder="请选择执行类型" clearable>
          <el-option v-for="dict in dict.type.plan_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="机器人名称" prop="robotName">
        <el-input v-model="queryParams.robotName" placeholder="请输入计划执行的机器人名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="应用类型名称" prop="appTypeName">
        <el-input v-model="queryParams.appTypeName" placeholder="请输入应用类型名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="应用名称" prop="appName">
        <el-input v-model="queryParams.appName" placeholder="请输入应用名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="appid" prop="appId">
        <el-input v-model="queryParams.appId" placeholder="请输入appid" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="执行类型" prop="excType">
        <el-select v-model="queryParams.excType" placeholder="请选择执行类型" clearable>
          <el-option
            v-for="dict in dict.type.exc_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="优先级" prop="priority">
        <el-input v-model="queryParams.priority" placeholder="请输入优先级" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['business:ctApplyPlan:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['business:ctApplyPlan:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['business:ctApplyPlan:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['business:ctApplyPlan:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ctApplyPlanList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" fixed="left" />
      <el-table-column label="序号" width="55" align="center" fixed="left">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="计划名称" align="center" prop="planName" width="100" />
      <el-table-column label="执行类型" align="center" prop="planType" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.plan_type" :value="scope.row.planType" />
        </template>
      </el-table-column>
      <el-table-column label="机器人名称" align="center" prop="robotName" width="100" />
      <el-table-column label="应用类型名称" align="center" prop="appTypeName" width="100" />
      <el-table-column label="应用名称" align="center" prop="appName" width="250" />
      <el-table-column label="appid" align="center" prop="appId" width="300" />
      <el-table-column label="应用类型枚举" align="center" prop="appType" width="100" />
      <el-table-column label="预计人工耗时" align="center" prop="manualTime" width="100" />
      <el-table-column label="计划参数" align="center" prop="planParams" width="100" />
      <el-table-column label="等待超时时间" align="center" prop="waitTimeout" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wait_timeout" :value="scope.row.waitTimeout"/>
        </template>
      </el-table-column>
      <el-table-column label="优先级" align="center" prop="priority" width="100" />
      <el-table-column label="执行类型" align="center" prop="excType" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.exc_type" :value="scope.row.excType"/>
        </template>
      </el-table-column>
      <el-table-column label="执行时间" align="center" prop="excTime" width="100"/>
      <el-table-column label="执行表达式" align="center" prop="cronExpression" width="100"/>
      <el-table-column label="任务运行uuid" align="center" prop="taskUuid" width="300"/>
      <el-table-column label="创建者" align="center" fixed="right" prop="createBy" width="100" />
      <el-table-column label="创建时间" align="center" fixed="right" prop="createTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新者" align="center" fixed="right" prop="updateBy" width="100" />
      <el-table-column label="更新时间" align="center" fixed="right" prop="updateTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" width="150" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['business:ctApplyPlan:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['business:ctApplyPlan:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改应用执行计划管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="计划名称" prop="planName">
          <el-input v-model="form.planName" placeholder="请输入计划名称" />
        </el-form-item>
        <el-form-item label="执行类型" prop="planType">
          <el-select v-model="form.planType" placeholder="请选择执行类型">
            <el-option v-for="dict in dict.type.plan_type" :key="dict.value" :label="dict.label"
              :disabled="dict.value == 0" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.planType == 0" label="机器人名称" prop="robotName">
          <el-select v-model="form.robotName" multiple collapse-tags style="margin-left: 20px;" placeholder="请选择机器人">
            <el-option v-for="rebot in rebotList" :key="rebot.robotClientName" :label="rebot.robotClientName"
              :value="rebot.robotClientName">
            </el-option>
          </el-select>
          <el-input v-model="form.robotName" placeholder="请输入机器人名称" />
        </el-form-item>
        <el-form-item label="应用名称" prop="appName">
          <el-select v-model="form.appId" multiple filterable collapse-tags style="margin-left: 20px;" placeholder="请选择机器人" >
            <el-option v-for="apply in applyList" :key="apply.appId" :label="apply.appName":value="apply.appId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="计划参数" prop="planParams">
          <el-input v-model="form.planParams" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="等待超时时间" prop="waitTimeout">
          <el-select v-model="form.waitTimeout" placeholder="请选择等待超时时间">
            <el-option
              v-for="dict in dict.type.wait_timeout"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="优先级" prop="priority">
          <el-input-number v-model="form.priority" @change="handleChange" label="请输入预计人工用时"></el-input-number>
        </el-form-item>
        <el-form-item label="执行类型" prop="excType">
          <el-select v-model="form.excType" placeholder="请选择执行类型">
            <el-option
              v-for="dict in dict.type.exc_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.excType == 1" label="执行时间" prop="excTime">
          <el-date-picker clearable
            v-model="form.excTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择执行时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item v-if="form.excType == 2" label="执行表达式" prop="cronExpression">
          <el-input v-model="form.cronExpression" placeholder="请输入执行表达式" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCtApplyPlan, getCtApplyPlan, delCtApplyPlan, addCtApplyPlan, updateCtApplyPlan } from "@/api/business/ctApplyPlan";
import { listRebotAll } from "@/api/business/ctRebot";
import { listApplyAll } from "@/api/business/ctApply";

export default {
  name: "CtApplyPlan",
  dicts: ['plan_type', 'wait_timeout',, 'exc_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 应用执行计划管理表格数据
      ctApplyPlanList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        planName: null,
        planType: null,
        robotName: null,
        appTypeName: null,
        appName: null,
        appId: null,
        appType: null,
        manualTime: null,
        planParams: null,
        waitTimeout: null,
        priority: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      // 机器人列表
      rebotList: [],
      // 应用列表
      applyList: []
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询应用执行计划管理列表 */
    getList() {
      this.loading = true;
      listCtApplyPlan(this.queryParams).then(response => {
        this.ctApplyPlanList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询所有应用 */
    listAll() {
     this.listRebotAll();
     this.listApplyAll();
    },
    /** 查询所有可用机器人 */
    listRebotAll() {
      listRebotAll({}).then(res => {
        this.rebotList = res.data;
      });
     
    },
    /** 查询应用 */
    listApplyAll() {
      listApplyAll({}).then(res => {
        this.applyList = res.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        planName: null,
        planType: null,
        robotName: null,
        appTypeName: null,
        appName: null,
        appId: null,
        appType: null,
        manualTime: null,
        planParams: null,
        waitTimeout: null,
        priority: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        deleted: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.listAll();
      this.open = true;
      this.title = "添加应用执行计划";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.listAll();
      const id = row.id || this.ids
      getCtApplyPlan(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改应用执行计划";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCtApplyPlan(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCtApplyPlan(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除应用执行计划管理编号为"' + ids + '"的数据项？').then(function () {
        return delCtApplyPlan(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/ctApplyPlan/export', {
        ...this.queryParams
      }, `ctApplyPlan_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>