<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--机构数据-->
      <el-col :span="4" :xs="24">
        <org-tree v-model="queryParams.orgId" @defaultKey="getDefaultKey" @defaultOrg="getDefaultOrg" @checkChange="checkChange"
                    @click="clickTreeNode"></org-tree>
      </el-col>
      <el-col :span="20" :xs="24">
        <div class="main-right-box">
          <div class="main-search-box">
            <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">

<el-form-item label="任务名称" prop="taskName">
  <el-input
    v-model="queryParams.taskName"
    placeholder="请输入任务名称"
    clearable
    @keyup.enter.native="handleQuery"
    maxlength="50"  />
</el-form-item>
<el-form-item label="调阅人员" prop="roleId">
  <el-select v-model="queryParams.roleId" placeholder="请选择调阅人员" clearable>
    <el-option v-for="item in roleList" :key="item.id" :label="item.name" :value="item.id"></el-option>
  </el-select>
</el-form-item>
<el-form-item label="任务进度" prop="status">
  <el-select v-model="queryParams.status" placeholder="请选择进度" clearable>
    <el-option
      v-for="dict in dict.type.retrieval_task_status"
      :key="dict.value"
      :label="dict.label"
      :value="dict.value"
    />
  </el-select>
</el-form-item>
<el-form-item label="任务时间" prop="planStartTime">

  <DataRangePicker
        type="daterange"
        value-format="yyyy-MM-dd HH:mm:ss"
        :default-time="['00:00:00', '23:59:59']"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        v-model="queryParams.dateRange"
        :picker-options="pickerOptions"
      >
      </DataRangePicker>
</el-form-item>
</el-form>

<el-row :gutter="10" class="mb8">
<el-col :span="1.5">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
                   >搜索
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="primary" icon="el-icon-refresh" size="mini" @click="resetQuery"
                   >重置
          </el-button>
        </el-col>

<el-col :span="1.5">
  <el-button
  type="primary" icon="el-icon-download"
    size="mini"
    @click="handleExport"
    v-hasPermi="['core:task:export']"
  >导出</el-button>
</el-col>
<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
</el-row>
          </div>

          <el-table v-loading="loading" :data="taskList"  border
              height="600"
              size="small">

      <el-table-column label="序号" type="index" align="center" width="100">
        <template slot-scope="scope">
          <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="任务名称" align="center" prop="taskName" width="220" show-overflow-tooltip="true"/>
      <el-table-column label="调阅机构" align="center" prop="orgName"  width="220"/>
      <el-table-column label="调阅人员" align="center" prop="roleName" width="160"/>
      <el-table-column label="任务进度" align="center" prop="status">
        <template slot-scope="scope">
          <span :style="statusColor(scope.row.status)">
          <dict-tag :options="dict.type.retrieval_task_status" :value="scope.row.status"/>
          </span>
        </template>
      </el-table-column>
      <el-table-column label="任务时间" align="center" width="300">
        <template slot-scope="scope">
          {{scope.row.planStartTime}}~{{scope.row.planEndTime}}
        </template>
      </el-table-column>

      <el-table-column label="异常数量" align="center" prop="exceptionCount" />

      <el-table-column label="操作" fixed="right" style="width: 100px" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="lookView(scope.row)"
            v-hasPermi="['core:task:query']"
          >详情</el-button>

        </template>
      </el-table-column>
    </el-table>
        <dialog-info ref="infoDialog" @success="refresh(true)"></dialog-info>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
        </div>


      </el-col>
    </el-row>
  </div>
</template>

<script>
import { listTask, getTask, delTask, addTask, updateTask,roleList,exportExcel } from "@/api/core/task";
import OrgTree from "@/components/orgTree";
import DialogInfo from "./dialog.info";
import {listPlanRole} from "@/api/core/edu/plan";
import {formatTime} from "@/utils/ruoyi";
import DataRangePicker from "@/components/dateTime/daterange.picker.vue";
import dayjs from "dayjs";
export default {
  components: {OrgTree,DialogInfo, DataRangePicker,},
  name: "Task",
  dicts: ['sys_access_cycle', 'retrieval_task_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个停用
      single: true,
      // 非多个停用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 监控调阅任务表格数据
      taskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskName: null,
        cycle: null,
        dateRange:[],
        status: null,
        roleId:null,
        checkSub: true,
      },
      selectedOrgName:'',
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      roleList: [],
      orgName:'',
    };
  },
  created() {
    this.queryParams.orgId = this.$store.getters.orgId;
    this.getList();
    this.initRoleList();
  },
  methods: {
    /** 查询监控调阅任务列表 */
    getList() {
      this.loading = true;
      listTask(this.queryParams).then(response => {
        this.taskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
/*    handleExport() {
      console.log(this,'this')
      this.$http()
        .post('/core/task/export',this.queryParams)

     /!* exportExcel(this.queryParams).then(res => {
        let file = new File([res],res.getHeader())
        console.log(res,'res111')
      });*!/
    },*/
    getDefaultOrg(org){
      this.orgName = org.name;
      this.selectedOrgName=org.shortName;
    },
    initRoleList() {
      roleList({}).then(response => {
        this.roleList = response.data;
      });
    },
    getDefaultKey(key) {
      this.queryParams.orgId = key;
      this.getList();
    },
    checkChange(state) {
      this.queryParams.checkSub = state;
      this.handleQuery();
    },
    // 节点单击事件
    clickTreeNode(data) {
      this.queryParams.orgId = data.id;
      this.orgName = data.name;
      this.selectedOrgName=data.shortName;
      this.handleQuery();
    },
    /** 下穿状态改变*/
    changeCheckBox() {
      this.getList();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        planId: null,
        taskName: null,
        orgId: null,
        orgPath: null,
        orgName: null,
        ymdDate: null,
        ymdYear: null,
        ymdHalfyear: null,
        ymdQuarter: null,
        ymdMonth: null,
        ymdWeek: null,
        ymdDay: null,
        cycle: null,
        planStartTime: null,
        planEndTime: null,
        startTime: null,
        endTime: null,
        status: null,
        roleId: null,
        retrievalUser: null,
        orderNum: null,
        batchNum: null,
        submitTime: null,
        exceptionCount: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      //this.queryParams.dateRange = [];
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.dateRange = [];
      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 = "添加监控调阅任务";
    },
    /** 修改按钮操作 */
    lookView(row) {
      this.$refs["infoDialog"].show(row.id,{});

    },

    /** 导出按钮操作 */
   handleExport() {
     console.log(this);
      this.download('core/retrievalTask/export', {
        ...this.queryParams
      }, `${this.selectedOrgName}-${this.$tab.getCurrentTabName()}-${dayjs().format("YYYYMMDD")}.xlsx`)
    },
    statusColor(status) {
      let color = "";
      switch (status) {
        case "0":
          color = "#00B0F0";
          break;
        case "1":
          color = "#FFC000";
          break;
        case "2":
          color = "#00B050";
          break;
        case "3":
          color = "#FF0000";
          break;
      }
        return "color:" + color;
    },
  }
};
</script>
