<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" size="small" :inline="true" label-width="68px">
      <el-form-item label="任务名称" prop="taskName">
        <el-input
          v-model="queryParams.taskName"
          placeholder="请输入任务名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="子任务" prop="subtask">
        <el-input
          v-model="queryParams.subtask"
          placeholder="请输入子任务"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="源文件id" prop="relateSourceFileId">
        <el-input
          v-model="queryParams.relateSourceFileId"
          placeholder="请输入源文件id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处理文件id" prop="relateProcessedFileId">
        <el-input
          v-model="queryParams.relateProcessedFileId"
          placeholder="请输入处理文件id"
          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
          v-hasPermi="['graph:task:add']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['graph:task:edit']"
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['graph:task:remove']"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['graph:task:export']"
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="taskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="任务名称" align="center" prop="taskName" />
      <el-table-column label="任务类型" align="center" prop="taskType" />
      <el-table-column label="子任务" align="center" prop="subtask" />
      <el-table-column label="任务状态" align="center" prop="taskStatus" />
      <el-table-column label="源文件id" align="center" prop="relateSourceFileId" />
      <el-table-column label="处理文件id" align="center" prop="relateProcessedFileId" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['graph:task:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-hasPermi="['graph:task:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</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" :close-on-click-modal="false" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="任务名称" prop="taskName">
          <el-input v-model="form.taskName" placeholder="请输入任务名称" />
        </el-form-item>
        <el-form-item label="任务类型" prop="taskType">
          <!-- <el-input v-model="form.taskType" placeholder="请输入任务类型" /> -->
          <el-radio v-model="form.taskType" label="1" @change="taskTypeChange">数据处理</el-radio>
          <el-radio v-model="form.taskType" label="2" @change="taskTypeChange">知识抽取</el-radio>
        </el-form-item>
        <el-form-item label="子任务" prop="subtaskArray">
          <el-checkbox-group v-model="form.subtaskArray">
            <template v-if="form.taskType==='1'">
              <el-checkbox v-for="dict in dict.type.biz_data_process" :key="dict.value" :label="dict.value"> {{ dict.label }}</el-checkbox>
            </template>
            <template v-else>
              <el-checkbox v-for="dict in dict.type.biz_kg_build" :key="dict.value" :label="dict.value"> {{ dict.label }}</el-checkbox>
            </template>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="源文件" prop="relateSourceFileId">
          <el-select v-model="form.relateSourceFileId" placeholder="请选择源文件" @change="relateSourceFileIdChange">
            <el-option
              v-for="item in sourceFileOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="处理文件" prop="relateProcessedFileId">
          <el-select v-model="form.relateProcessedFileId" placeholder="请选择处理文件">
            <el-option
              v-for="item in relateProcessedFileOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </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 { listTask, getTask, delTask, addTask, updateTask } from '@/api/graph/task'
import { getSourceFileOptions } from '@/api/graph/sourceFile'
import { getOptionsBySourceFileId } from '@/api/graph/processedFile'
export default {
  name: 'Task',
  dicts: ['biz_data_process', 'biz_kg_build'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 任务管理表格数据
      taskList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskName: null,
        taskType: '1',
        subtask: null,
        subtaskArray: [],
        taskStatus: null,
        relateSourceFileId: null,
        relateProcessedFileId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      sourceFileOptions: [],
      relateProcessedFileOptions: []
    }
  },
  created() {
    this.getList()
    this.getOptions()
  },
  methods: {
    getOptions() {
      getSourceFileOptions().then(resp => {
        this.sourceFileOptions = resp.data
      })
    },
    getOptionsBySourceFileId(id) {
      getOptionsBySourceFileId(id).then(resp => {
        this.relateProcessedFileOptions = resp.data
      })
    },
    relateSourceFileIdChange() {
      this.getOptionsBySourceFileId(this.form.relateSourceFileId)
    },
    /** 查询任务管理列表 */
    getList() {
      this.loading = true
      listTask(this.queryParams).then(response => {
        this.taskList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        taskName: null,
        taskType: '1',
        subtask: null,
        subtaskArray: [],
        taskStatus: null,
        relateSourceFileId: null,
        relateProcessedFileId: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      }
      this.resetForm('form')
      this.relateProcessedFileOptions = []
    },
    /** 搜索按钮操作 */
    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.open = true
      this.title = '添加任务管理'
      this.taskTypeChange()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getTask(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改任务管理'
        this.getOptionsBySourceFileId(this.form.relateSourceFileId)
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTask(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addTask(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 delTask(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('graph/task/export', {
        ...this.queryParams
      }, `task_${new Date().getTime()}.xlsx`)
    },
    taskTypeChange() {
      this.form.subtask = ''
    }
  }
}
</script>
