<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">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
<!--
      <el-form-item label="调阅周期" prop="cycle">
        <el-select v-model="queryParams.cycle" placeholder="请选择周期" clearable>
          <el-option
            v-for="dict in dict.type.sys_access_cycle"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="调阅角色" prop="roleId">
        <el-select v-model="queryParams.roleId" placeholder="请选择">
          <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="planStartTime">
        <el-date-picker
          v-model="queryParams.dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
        ></el-date-picker>
      </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="taskName">
        <el-input
          v-model="queryParams.taskName"
          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-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>

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

      <el-table-column label="序号" type="index" align="center">
        <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"/>
<!--      <el-table-column label="调阅周期" align="center" prop="cycle" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_access_cycle" :value="scope.row.cycle" />
        </template>
      </el-table-column>-->
      <el-table-column label="开始时间" align="center" prop="planStartTime" width="160">
<!--        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.planStartTime, '{y}-{m}-{d}') }}</span>
        </template>-->
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="planEndTime" width="160">
<!--        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.planEndTime, '{y}-{m}-{d}') }}</span>
        </template>-->
      </el-table-column>
      <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" prop="orgName"  width="220"/>

<!--      <el-table-column label="调阅角色" align="center" prop="roleName" width="160"/>

      <el-table-column label="调阅开始时间" align="center" prop="startTime" width="160">

      </el-table-column>
      <el-table-column label="调阅结束时间" align="center" prop="endTime" width="160">

      </el-table-column>-->
      <el-table-column label="调阅人" align="center" prop="retrievalUserName" width="140"/>
      <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-if="scope.row.status == 2"
            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"
    />
      </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";
export default {
  components: {OrgTree,DialogInfo},
  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,
      },
      // 表单参数
      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;
    },
    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.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() {
      this.download('core/retrievalTask/export', {
        ...this.queryParams
      }, `${this.orgName+'-监控调阅任务记录-'+this.formatTime(new Date(),'YYYYMMhh')}.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>
