<template>
  <div class="app-container">
    <el-row :gutter="20">
    <el-col :span="4" :xs="24">
      <org-tree v-model="queryParams.orgId" @defaultKey="getDefaultKey" @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="100px">
      
      <el-form-item label="培训主题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入培训岗位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="培训类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择执行机构类型" clearable>
            <el-option
              v-for="dict in dict.type.edu_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
      </el-form-item>
      <el-form-item label="培训状态" prop="status">
        <!-- <el-input
          v-model="queryParams.status"
          placeholder=""
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
        <el-select v-model="queryParams.status" placeholder="请选择培训状态" clearable>
            <el-option
              v-for="dict in dict.type.edu_task_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
      </el-form-item>
      <el-form-item label="任务时间" prop="rangen">
        <el-date-picker
          v-model="queryParams.range"
          type="daterange"
          value-format="yyyy-MM-dd"
          placeholder="请选择开始日期">
        </el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="截止日期" prop="endDate">
        <el-date-picker clearable
          v-model="queryParams.endDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择截止日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="实到人数" prop="actualCount">
        <el-input
          v-model="queryParams.actualCount"
          placeholder="请输入实到人数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="培训岗位名称" prop="roleName">
        <el-input
          v-model="queryParams.roleName"
          placeholder="请输入培训岗位名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="时间" prop="dateTime">
        <el-date-picker clearable
          v-model="queryParams.dateTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="应到人数" prop="dueCount">
        <el-input
          v-model="queryParams.dueCount"
          placeholder="请输入应到人数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主持人" prop="hostId">
        <el-input
          v-model="queryParams.hostId"
          placeholder="请输入主持人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="机构" prop="orgId">
        <el-input
          v-model="queryParams.orgId"
          placeholder="请输入机构"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="记录人" prop="recorderId">
        <el-input
          v-model="queryParams.recorderId"
          placeholder="请输入记录人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="机构名称" prop="orgName">
        <el-input
          v-model="queryParams.orgName"
          placeholder="请输入机构名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="机构" prop="orgPath">
        <el-input
          v-model="queryParams.orgPath"
          placeholder="请输入机构"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="pdf下载地址" prop="pdfUrl">
        <el-input
          v-model="queryParams.pdfUrl"
          placeholder="请输入pdf下载地址"
          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="['system:eduTask: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="['system:eduTask: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="['system:eduTask: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="['system:eduTask:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table height="550px" v-loading="loading" :data="eduTaskList" @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="教育培训计划Id" align="center" prop="planId" /> -->
      
      <el-table-column label="培训机构" align="center" prop="orgName" />
      <el-table-column label="培训角色" align="center" >
        <template slot-scope="scope">
            <template v-if="scope.row.taskRoleNameList  ">
              <el-tag size="mini" type="success" v-for="(item, index) in (scope.row.taskRoleNameList || '').split(',')"
                      :key="index">
                {{ item }}
              </el-tag>
            </template>          
          </template>
      </el-table-column>
      <el-table-column label="培训主题" align="center" prop="title" />
      <el-table-column label="类型" align="center"  >
        <template slot-scope="scope">
            <dict-tag :options="dict.type.edu_type" :value="scope.row.type"/>
          </template>
      </el-table-column>

      <el-table-column label="主持人" align="center" prop="hostName" />
      <el-table-column label="开始日期" align="center" prop="startDate" width="180">
        <!-- <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startDate, '{y}-{m}-{d}') }}</span>
        </template> -->
      </el-table-column>
      <el-table-column label="截止日期" align="center" prop="endDate" width="180">
        <!-- <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
        </template> -->
      </el-table-column>
      <el-table-column label="培训时间" align="center" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.trainingStartDateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="应到人数" align="center" prop="dueCount" />
      <el-table-column label="状态" align="center" prop="status" >
        <template slot-scope="scope">
            <dict-tag :options="dict.type.edu_task_status" :value="scope.row.status"/>
          </template>
      </el-table-column>
      <el-table-column label="操作" width="120px" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(scope.row.id)"
            v-hasPermi="['system:eduTask:detail']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleRecorded(scope.row.id)"
            v-hasPermi="['system:eduTask:remove']"
          >培训登记</el-button>
          <!-- <el-button type="text" @click="onEdit(scope.row)">查看</el-button>            
          <el-button
              type="text"
              v-if="
              r.row.auditFlag == 'PASS' &&
                (isExecute || loginOrgId == r.row.orgId) && canPerform(r.row.eduJobIds)&&isOverOrUnStart(r.row)
            "
              @click="onPerform(r.row.id, r.row.eduJobIds, r.row.endDate)"
          >执行
          </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-col>
  </el-row>
    <!-- 添加或修改教育任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="教育培训计划Id" prop="planId">
          <el-input v-model="form.planId" placeholder="请输入教育培训计划Id" />
        </el-form-item>
        <el-form-item label="培训岗位" prop="roleId">
          <el-input v-model="form.roleId" placeholder="请输入培训岗位" />
        </el-form-item>
        <el-form-item label="开始日期" prop="startDate">
          <el-date-picker clearable
            v-model="form.startDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择开始日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="截止日期" prop="endDate">
          <el-date-picker clearable
            v-model="form.endDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择截止日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="实到人数" prop="actualCount">
          <el-input v-model="form.actualCount" placeholder="请输入实到人数" />
        </el-form-item>
        <el-form-item label="内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="培训岗位名称" prop="roleName">
          <el-input v-model="form.roleName" placeholder="请输入培训岗位名称" />
        </el-form-item>
        <el-form-item label="岗位id集合" prop="roleIds">
          <el-input v-model="form.roleIds" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="岗位名集合" prop="roleNames">
          <el-input v-model="form.roleNames" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="学习主题" prop="title">
          <el-input v-model="form.title" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="总结" prop="note">
          <el-input v-model="form.note" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="时间" prop="dateTime">
          <el-date-picker clearable
            v-model="form.dateTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="应到人数" prop="dueCount">
          <el-input v-model="form.dueCount" placeholder="请输入应到人数" />
        </el-form-item>
        <el-form-item label="主持人" prop="hostId">
          <el-input v-model="form.hostId" placeholder="请输入主持人" />
        </el-form-item>
        <el-form-item label="附件" prop="fileList">
          <file-upload v-model="form.fileList"/>
        </el-form-item>
        <el-form-item label="图片" prop="imageList">
          <image-upload v-model="form.imageList"/>
        </el-form-item>
        <el-form-item label="机构" prop="orgId">
          <el-input v-model="form.orgId" placeholder="请输入机构" />
        </el-form-item>
        <el-form-item label="记录人" prop="recorderId">
          <el-input v-model="form.recorderId" placeholder="请输入记录人" />
        </el-form-item>
        <el-form-item label="机构名称" prop="orgName">
          <el-input v-model="form.orgName" placeholder="请输入机构名称" />
        </el-form-item>
        <el-form-item label="机构" prop="orgPath">
          <el-input v-model="form.orgPath" placeholder="请输入机构" />
        </el-form-item>
        <el-form-item label="pdf下载地址" prop="pdfUrl">
          <el-input v-model="form.pdfUrl" placeholder="请输入pdf下载地址" />
        </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>

    <dialog-info ref="infoDialog" @success="refresh(true)"></dialog-info>
    <dialog-perform ref="performDialog" @success="refresh(true)"></dialog-perform>
  </div>
</template>

<script>
import { listEduTask, getEduTask, delEduTask, addEduTask, updateEduTask } from "@/api/core/edu/eduTask";
import OrgTree from "@/components/orgTree";
import DialogInfo from "./dialog.info";
import DialogPerform from "./dialog.perform";
export default {
  name: "EduTask",
  dicts: ['edu_task_status', 'edu_type'],
  components: {
    DialogInfo,
    DialogPerform,
    OrgTree
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 教育任务表格数据
      eduTaskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        checkSub:false,
        title: null,
        orgId: null,
        type: null,
        status: null,
        range: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orgId: [
          { required: true, message: "机构不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  
  methods: {
    /** 查询教育任务列表 */
    getList() {
      this.loading = true;
      listEduTask(this.queryParams).then(response => {
        this.eduTaskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        range:[],
        title: null,       
        type: null,
        status : null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    //单选框状态改变
    checkChange(state) {
      this.queryParams.checkSub = state;
      this.handleQuery();
    },
    getDefaultKey(key) {
      this.queryParams.orgId = key;
      this.getList();
    },
    // 节点单击事件
    clickTreeNode(data) {
      this.queryParams.orgId = data.id;
      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 = "添加教育任务";
    },
    /** 修改按钮操作 */
    handleDetail(id) {
      console.log("this.$refs",this.$refs["infoDialog"])
      this.$refs["infoDialog"].show(id,{});
      // this.reset();
      // const id = row.id || this.ids
      // getEduTask(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) {
            updateEduTask(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEduTask(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 培训登记按钮操作 */
    handleRecorded(id) {
      this.$refs["performDialog"].show(id,{});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/eduTask/export', {
        ...this.queryParams
      }, `eduTask_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
