<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="pcTitle">
        <el-input
          v-model="queryParams.pcTitle"
          placeholder="请输入考核任务名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="模板名称" prop="templateName">
        <el-input
          v-model="queryParams.templateName"
          placeholder="请输入模板名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="考核任务状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择考核任务状态" clearable>
          <el-option
            v-for="dict in dict.type.kh_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </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="['kaohe:pingce: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="['kaohe:pingce: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="['kaohe:pingce: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="['kaohe:pingce:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pingceList" @selection-change="handleSelectionChange">
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="考核任务名称" align="center" prop="pcTitle" />
      <el-table-column label="考核任务描述" align="center" prop="pcDescription" />
      <el-table-column label="模板名称" align="center" prop="templateName" />
      <el-table-column label="考核任务状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.kh_state" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="考核任务备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="240px" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-tools"
            @click="handleSetUp(scope.row, scope.$index)"
          >发布考核
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['kaohe:pingce:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-grid"
            @click="goTotalScore(scope.row)"
          >考核得分
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-user-solid"
            @click="handleAddsUser(scope.row)"
          >生成评分账号
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="viewUser(scope.row)"
          >查看评分账号
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['kaohe:pingce: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="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="考核任务名称" prop="pcTitle">
          <el-input v-model="form.pcTitle" placeholder="请输入考核任务名称" :disabled="form.state==='2'"/>
        </el-form-item>
        <el-form-item label="模板名称" prop="templateName">
          <el-input placeholder="请选择模板名称" v-if="form.state==='0'" readonly v-model="form.templateName" @focus="changeTemp">
            <i slot="suffix" class="el-input__icon el-icon-search" style="color: #1890ff; font-weight: bolder"></i>
          </el-input>
          <el-input v-else v-model="form.templateName" placeholder="请选择模板" disabled/>
        </el-form-item>
        <el-form-item label="考核任务描述" prop="pcDescription" :disabled="form.state!=='0'">
          <el-input v-model="form.pcDescription" type="textarea" placeholder="请输入考核任务描述" :disabled="form.state==='2'"/>
        </el-form-item>
        <el-form-item label="考核任务状态" prop="state">
          <el-select v-model="form.state" placeholder="请选择考核任务状态" clearable style="width: 100%" :disabled="form.state!=='0'">
            <el-option
              v-for="dict in dict.type.kh_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="被考核对象类型" prop="bkhdxType">
          <el-radio-group v-model="form.bkhdxType" :disabled="form.state!=='0'">
            <el-radio v-for="dict in khdxRadios" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="被考核对象" v-if="form.bkhdxType==='0'">
          <el-button type="primary" @click="selectEmp" size="mini">选择职工</el-button>
          <el-table :data="form_bkh_zg" style="width: 100%; margin-top: 10px" border max-height="500">
            <el-table-column label="操作" width="100" fixed align="center">
              <template slot-scope="scope">
                <i class="el-icon-remove-outline" style="font-size: 20px;color:#F56C6C;" @click="delEmpList(scope.row,scope.$index)"></i>
              </template>
            </el-table-column>
            <el-table-column label="主键" align="center" prop="id" />
            <el-table-column label="部门名称" align="center" prop="deptName" :show-overflow-tooltip="true" min-width="120"/>
            <el-table-column label="职工姓名" align="center" prop="empName" :show-overflow-tooltip="true" min-width="120"/>
          </el-table>
        </el-form-item>
        <el-form-item label="被考核对象" v-if="form.bkhdxType==='1'">
          <el-checkbox v-model="deptExpand" :disabled="form.state!=='0'" @change="handleCheckedTreeExpand($event, 'dept')">展开/折叠</el-checkbox>
          <el-checkbox v-model="deptNodeAll" :disabled="form.state!=='0'" @change="handleCheckedTreeNodeAll($event, 'dept')">全选/全不选</el-checkbox>
          <el-checkbox v-model="form.deptCheckStrictly" :disabled="form.state!=='0'" @change="handleCheckedTreeConnect($event, 'dept')">父子联动</el-checkbox>
          <el-tree
            :disabled="form.state!=='0'"
            class="tree-border"
            :data="deptOptions"
            show-checkbox
            default-expand-all
            ref="dept"
            node-key="id"
            :check-strictly="!form.deptCheckStrictly"
            empty-text="加载中，请稍候"
            :props="defaultProps"
          ></el-tree>
        </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>

    <!-- 选择模板信息 -->
    <el-dialog title="模板选择" :visible.sync="temp.open" width="500px">
      <div>
        <el-form :model="temp.queryParams" :inline="true" label-width="68px">
          <el-form-item label="模板名称" prop="userName" style="margin-bottom: 10px">
            <el-input
              v-model="temp.queryParams.temName"
              placeholder="请输入模板名称"
              clearable
              @keyup.enter.native="tempQuery"
            />
          </el-form-item>
          <el-form-item style="margin-bottom: 10px">
            <el-button type="primary" icon="el-icon-search" size="small" @click="tempQuery">搜索</el-button>
          </el-form-item>
        </el-form>

        <el-table border v-loading="temp.loading" :data="temp.list" @row-dblclick="tempRowChange">
          <el-table-column label="主键ID" align="center" prop="id" />
          <el-table-column label="模板名称" align="center" prop="temName" />
        </el-table>

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

        <div slot="footer" class="dialog-footer">
          <el-button @click="tempCancel">取 消</el-button>
        </div>
      </div>

    </el-dialog>

    <!-- 选择职工 -->
    <el-dialog title="选择职工" :visible.sync="emp.open" width="1200px" append-to-body>
      <div style="display:flex;width: 100%;justify-content: space-between">
        <div style="width: 30%; border-right: 1px solid #eeeeee; padding-right: 10px;">
          <div style="height: 45px;line-height: 45px;font-size: 14px;font-weight: bolder">
            选中职工 <span style="color: #39CCCC">{{ emp.selectList.length }}</span>
          </div>
          <el-table :data="emp.selectList" style="width: 100%" border max-height="500">
            <el-table-column label="操作" width="50" fixed align="center">
              <template slot-scope="scope">
                <i class="el-icon-remove-outline" style="font-size: 20px;color:#F56C6C;" @click="selectListDel(scope.row,scope.$index)"></i>
              </template>
            </el-table-column>
            <el-table-column label="主键" align="center" prop="id" />
            <el-table-column label="部门名称" align="center" prop="deptName" :show-overflow-tooltip="true" min-width="100"/>
            <el-table-column label="职工姓名" align="center" prop="empName" :show-overflow-tooltip="true" min-width="100"/>
          </el-table>
        </div>
        <div style="width: 69%;">
          <el-row :gutter="20">
            <!--部门数据-->
            <el-col :span="6" :xs="24">
              <div class="head-container">
                <el-input
                  v-model="deptName"
                  placeholder="请输入部门名称"
                  clearable
                  size="small"
                  prefix-icon="el-icon-search"
                  style="margin-bottom: 20px"
                />
              </div>
              <div class="head-container">
                <el-tree
                  :data="deptOptions"
                  :props="defaultProps"
                  :expand-on-click-node="false"
                  :filter-node-method="empfilterNode"
                  ref="tree"
                  node-key="id"
                  default-expand-all
                  highlight-current
                  @node-click="empNodeClick"
                />
              </div>
            </el-col>
            <el-col :span="18" :xs="24">
              <el-form :model="emp.queryParams" :inline="true" label-width="70px">
                <el-form-item label="职工名称" prop="empName" style="margin-bottom: 10px">
                  <el-input
                    v-model="emp.queryParams.empName"
                    placeholder="请输入职工名称"
                    clearable
                    @keyup.enter.native="empQuery"
                  />
                </el-form-item>
                <el-form-item style="margin-bottom: 10px">
                  <el-button type="primary" icon="el-icon-search" size="small" @click="empQuery">搜索</el-button>
                </el-form-item>
              </el-form>
              <el-table border ref="empTable" v-loading="emp.loading" :data="emp.list" @select="empSelect" @select-all="empSelectAll">
                <el-table-column type="selection" width="50" align="center" />
                <el-table-column label="主键" align="center" prop="id" fixed width="50"/>
                <el-table-column label="部门名称" align="center" prop="deptName" :show-overflow-tooltip="true" min-width="120"/>
                <el-table-column label="职工姓名" align="center" prop="empName" :show-overflow-tooltip="true" min-width="120"/>
                <el-table-column label="年龄" align="center" prop="age" min-width="50"/>
              </el-table>

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

            </el-col>

          </el-row>

        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="empConfirm">确 认</el-button>
      </div>
    </el-dialog>

    <!--选择发布考核信息-->
    <el-dialog title="发布考核" :visible.sync="pcSetUp.open" width="99%">
      <el-table border v-loading="pcSetUp.loading" :data="pcSetUp.list">
        <el-table-column label="考核项主键" align="center" prop="id" width="90" fixed/>
        <el-table-column label="考核项名称" align="center" prop="itemName" min-width="120" :show-overflow-tooltip="true" fixed/>
        <el-table-column label="考核项类型" align="center" prop="type" fixed/>
        <el-table-column label="评分主题" align="center" prop="vote" min-width="220" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <div v-if="scope.$index=== pcSetUp.xzhIndex">
              <el-input v-model.trim="scope.row.vote.voteTitle" placeholder="请输入评分主题" :disabled="scope.row.vote.id && !(new Date(scope.row.vote.sTime) > (new Date()))"/>
            </div>
            <div v-else>
              <span>{{ scope.row.vote.voteTitle }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="评分选项" align="center" prop="options" min-width="220" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <div v-if="scope.$index=== pcSetUp.xzhIndex && scope.row.typeId===1">
              <el-select :disabled="scope.row.vote.id && !(new Date(scope.row.vote.sTime) > (new Date()))"
                         v-model="scope.row.vote.voteItems" multiple placeholder="请选择评分选项">
                <el-option
                  v-for="item in vote_options"
                  :key="item.id"
                  :label="item.vitemName"
                  :value="item.id">
                </el-option>
              </el-select>
            </div>
            <div v-else>
              <div v-if=" scope.row.vote.voteItems">
                <div>{{ scope.row.vote.voteItemsName }}</div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="最大值" align="center" prop="vote" min-width="220" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <div v-if="scope.$index=== pcSetUp.xzhIndex && scope.row.typeId!==1">
              <el-input-number :disabled="scope.row.vote.id && !(new Date(scope.row.vote.sTime) > (new Date()))"
                v-model="scope.row.vote.maxNum" :min="0" style="width: 150px;"></el-input-number>
            </div>
            <div v-else>
              <span>{{ scope.row.vote.maxNum }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="评分占比" align="center" prop="vote"  min-width="220" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <div v-if="scope.$index=== pcSetUp.xzhIndex && scope.row.typeId!==5">
              <el-input-number :disabled="scope.row.vote.id && !(new Date(scope.row.vote.sTime) > (new Date()))"
                               v-model="scope.row.vote.percentage" :precision="2" :step="0.01" :max="1" style="width: 150px;"></el-input-number>
            </div>
            <div v-else>
              <span>{{ scope.row.vote.percentage }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="vote"  min-width="220" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.kh_state" :value="scope.row.vote.state"/>
          </template>
        </el-table-column>
        <el-table-column label="开始时间" align="center" prop="vote"  min-width="220" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <div v-if="scope.$index=== pcSetUp.xzhIndex">
              <el-date-picker clearable style="width: 100%;" v-model="scope.row.vote.sTime"
                              :disabled="scope.row.vote.id && !(new Date(scope.row.vote.sTime) > (new Date()))"
                              type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择开始时间">
              </el-date-picker>
            </div>
            <div v-else>
              <span>{{ scope.row.vote.sTime }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="结束时间" align="center" prop="vote"  min-width="220" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <div v-if="scope.$index=== pcSetUp.xzhIndex">
              <el-date-picker :disabled="scope.row.vote.id && !(new Date(scope.row.vote.eTime) > (new Date()))"
                              clearable style="width: 100%;"   v-model="scope.row.vote.eTime"
                              type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择结束时间">
              </el-date-picker>
            </div>
            <div v-else>
              <span>{{ scope.row.vote.eTime }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="评分详情描述" align="center" prop="vote" min-width="220" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <div v-if="scope.$index=== pcSetUp.xzhIndex">
              <el-input :disabled="scope.row.vote.id && !(new Date(scope.row.vote.eTime) > (new Date()))"
                        v-model="scope.row.vote.vDescription" type="textarea" placeholder="请输入评分详情描述"/>
            </div>
            <div v-else>
              <span>{{ scope.row.vote.vDescription }}</span>
            </div>

          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="vote" min-width="220" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <div v-if="scope.$index=== pcSetUp.xzhIndex">
              <el-input :disabled="scope.row.vote.id && !(new Date(scope.row.vote.eTime) > (new Date()))"
                        v-model="scope.row.vote.remark" type="textarea" placeholder="请输入备注"/>
            </div>
            <div v-else>
              <span>{{ scope.row.vote.remark }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="180">
          <template slot-scope="scope">
            <div>
              <el-button v-if="!(scope.$index=== pcSetUp.xzhIndex)"
                size="mini"
                type="text"
                icon="el-icon-s-tools"
                @click="setUpTouPiao(scope.row,scope.$index)"
              >编辑评分规则</el-button>

              <el-button v-if="scope.$index=== pcSetUp.xzhIndex"
                size="mini"
                type="text"
                @click="voteSubmitForm(scope.row)"
              >确认</el-button>

              <el-button v-if="scope.$index=== pcSetUp.xzhIndex"
                         size="mini"
                         type="text"
                         @click="cancelVote"
              >取消</el-button>

              <el-button
                v-if="scope.row.vote.id && (new Date(scope.row.vote.sTime) < (new Date())) && (new Date(scope.row.vote.eTime) > (new Date()))"
                size="mini"
                type="text"
                icon="el-icon-thumb"
                @click="goVotepage(scope.row)"
              >去评分</el-button>

            </div>

          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="pcSetUp.open=false">取 消</el-button>
      </div>
    </el-dialog>

    <!--     考核得分-->
    <el-dialog title="考核得分" :visible.sync="khdf.open" width="90%">
      <div v-show="khdf.idRw===1">
        <el-table border :data="khdf.list1" >
          <el-table-column label="党建与行政目标任务" header-align="center">
            <el-table-column header-align="center" prop="bumen" label="部门" width="150px"></el-table-column>
            <el-table-column align="center" prop="khx1" label="政治建设"></el-table-column>
            <el-table-column align="center" prop="zb1" label="占比" width="55px"></el-table-column>
            <el-table-column align="center" prop="khx2" label="宣传与思想政治工作" width="140px"></el-table-column>
            <el-table-column align="center" prop="zb2" label="占比" width="55px"></el-table-column>
            <el-table-column align="center" prop="khx3" label="作风纪律"></el-table-column>
            <el-table-column align="center" prop="zb3" label="占比" width="55px"></el-table-column>
            <el-table-column align="center" prop="khx4" label="安全稳定"></el-table-column>
            <el-table-column align="center" prop="zb4" label="占比" width="55px"></el-table-column>
            <el-table-column align="center" prop="khx5" label="师资队伍与师德师风" width="140px"></el-table-column>
            <el-table-column align="center" prop="zb5" label="占比" width="55px"></el-table-column>
            <el-table-column align="center" prop="khx6" label="财务工作"></el-table-column>
            <el-table-column align="center" prop="zb6" label="占比" width="55px"></el-table-column>
            <el-table-column align="center" prop="khx7" label="依法治校与综合治理" width="140px"></el-table-column>
            <el-table-column align="center" prop="zb7" label="占比" width="55px"></el-table-column>
            <el-table-column align="center" prop="khx8" label="国有资产管理" width="120px"></el-table-column>
            <el-table-column align="center" prop="zb8" label="占比" width="55px"></el-table-column>
            <el-table-column align="center" prop="khx9" label="实验室管理" width="120px"></el-table-column>
            <el-table-column align="center" prop="zb9" label="占比" width="55px"></el-table-column>
            <el-table-column align="center" prop="khx10" label="招生就业与创新创业工作" width="170px"></el-table-column>
            <el-table-column align="center" prop="zb10" label="占比" width="55px"></el-table-column>
            <el-table-column align="center" prop="khx11" label="对外交流与合作" width="120px"></el-table-column>
            <el-table-column align="center" prop="zb11" label="占比" width="55px"></el-table-column>
          </el-table-column>
          <el-table-column align="center" prop="zongfen" label="总分" width="70px"></el-table-column>
        </el-table>
      </div>
      <div v-show="khdf.idRw===2">
        <el-table border :data="khdf.list2">
          <el-table-column label="事业发展" header-align="center">
            <el-table-column header-align="center" prop="bumen" label="部门"></el-table-column>
            <el-table-column align="center" prop="khx1" label="教学工作"></el-table-column>
            <el-table-column align="center" prop="zb1" label="占比"></el-table-column>
            <el-table-column align="center" prop="khx2" label="科研工作"></el-table-column>
            <el-table-column align="center" prop="zb2" label="占比"></el-table-column>
            <el-table-column align="center" prop="khx3" label="学生工作"></el-table-column>
            <el-table-column align="center" prop="zb3" label="占比"></el-table-column>
          </el-table-column>
          <el-table-column align="center" prop="zongfen" label="总分"></el-table-column>
        </el-table>
      </div>
      <div v-show="khdf.idRw===3">
        <el-table border :data="khdf.list3">
          <el-table-column label="民主测评" header-align="center">
            <el-table-column header-align="center" prop="bumen" label="部门"></el-table-column>
            <el-table-column header-align="center" label="师院领导班子">
              <el-table-column align="center" prop="yx1" label="优秀"></el-table-column>
              <el-table-column align="center" prop="hg1" label="合格"></el-table-column>
              <el-table-column align="center" prop="jbhg1" label="基本合格"></el-table-column>
              <el-table-column align="center" prop="bhg1" label="不合格"></el-table-column>
            </el-table-column>
            <el-table-column align="center" prop="khx1" label="得分"></el-table-column>
            <el-table-column align="center" prop="zb1" label="占比"></el-table-column>
            <el-table-column header-align="center" label="师院处级领导干部">
              <el-table-column align="center" prop="yx2" label="优秀"></el-table-column>
              <el-table-column align="center" prop="hg2" label="合格"></el-table-column>
              <el-table-column align="center" prop="jbhg2" label="基本合格"></el-table-column>
              <el-table-column align="center" prop="bhg2" label="不合格"></el-table-column>
            </el-table-column>
            <el-table-column align="center" prop="khx2" label="得分"></el-table-column>
            <el-table-column align="center" prop="zb2" label="占比"></el-table-column>
            <el-table-column header-align="center" label="服务对象代表">
              <el-table-column align="center" prop="yx3" label="优秀"></el-table-column>
              <el-table-column align="center" prop="hg3" label="合格"></el-table-column>
              <el-table-column align="center" prop="jbhg3" label="基本合格"></el-table-column>
              <el-table-column align="center" prop="bhg3" label="不合格"></el-table-column>
            </el-table-column>
            <el-table-column align="center" prop="khx3" label="得分"></el-table-column>
            <el-table-column align="center" prop="zb3" label="占比"></el-table-column>
          </el-table-column>
          <el-table-column align="center" prop="zongfen" label="总分"></el-table-column>
        </el-table>
      </div>
      <div v-show="khdf.idRw===4">
        <el-table border :data="khdf.list4">
          <el-table-column label="特色创新" header-align="center">
            <el-table-column header-align="center" prop="bumen" label="部门"></el-table-column>
            <el-table-column align="center" prop="df" label="分数（满分10分）"></el-table-column>
          </el-table-column>
        </el-table>
      </div>
      <div v-show="khdf.idRw===5">
        <el-table border :data="khdf.list5">
          <el-table-column label="综合评价" header-align="center">
            <el-table-column header-align="center" prop="bumen" label="部门"></el-table-column>
            <el-table-column align="center" prop="df" label="分数（加减分项，值域可以为负）"></el-table-column>
          </el-table-column>
        </el-table>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button @click="khdf.open=false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listPingce, getPingce, delPingce, addPingce, updatePingce, getItemVote, addsUser } from "@/api/kaohe/pingce"
import { deptTreeSelect } from "@/api/system/user";
import { listTemplate } from "@/api/kaohe/template"
import { listEmployee } from "@/api/kaohe/employee"
import { addVote, updateVote } from "@/api/kaohe/vote"
import { listVote_items } from "@/api/kaohe/vote_items"
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Pingce",
  dicts: ['kh_state', 'sys_user_sex'],
  components: { Treeselect },
  data() {
    return {
      khdf:{
        open: false,
        idRw: null,
        list1: [
          {
            bumen: '生命科学系',
            khx1: '90',
            zb1: '0.2',
            khx2: '95',
            zb2: '0.1',
            khx3: '90',
            zb3: '0.1',
            khx4: '85',
            zb4: '0.1',
            khx5: '98',
            zb5: '0.1',
            khx6: '95',
            zb6: '0.1',
            khx7: '97',
            zb7: '0.05',
            khx8: '98',
            zb8: '0.02',
            khx9: '95',
            zb9: '0.03',
            khx10: '96',
            zb10: '0.1',
            khx11: '95',
            zb11: '0.1',
            zongfen: '93.06'
          },
        ],
        list2: [
          {
            bumen: '继续教育学院',
            khx1: '98',
            zb1: '0.5',
            khx2: '95',
            zb2: '0.28',
            khx3: '98',
            zb3: '0.22',
            zongfen: '97.16'
          }
        ],
        list3: [
          {
            bumen: '生命科学系',
            yx1: 5,
            hg1: 1,
            jbhg1: 0,
            bhg1: 0,
            khx1: '96.66666667',
            zb1: '0.3',
            yx2: 110,
            hg2: 2,
            jbhg2: 1,
            bhg2: 1,
            khx2: '98.77192982',
            zb2: '0.3',
            yx3: 30,
            hg3: 5,
            jbhg3: 5,
            bhg3: 1,
            khx3: '91.2195122',
            zb3: '0.4',
            zongfen: '95.12'
          }
        ],
        list4: [
          {
            bumen: '生命科学系',
            df: '8',
          }
        ],
        list5: [
          {
            bumen: '生命科学系',
            df: '-3',
          }
        ]
      },
      khdxRadios:[
        {
          value: '0',
          label: '职工',
        },
        {
          value: '1',
          label: '部门',
        },
      ],
      deptExpand: true,
      deptNodeAll: false,
      deptOptions: [],
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 考核评测表格数据
      pingceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        pcTitle: null,
        pcDescription: null,
        templateId: null,
        templateName: null,
        pcResultId: null,
        state: null,
      },
      // 表单参数
      form: {},
      //被考核职工
      form_bkh_zg: [],
      // 表单校验
      rules: {
        pcTitle: [
          { required: true, message: "考核名称不能为空", trigger: "change" }
        ],
        templateName: [
          { required: true, message: "模板名称不能为空", trigger: "change" }
        ],
      },
      /** 选择模板*/
      temp: {
        type: 1,
        // 是否显示弹出层
        open: false,
        // 列表遮罩层
        loading: false,
        // 列表查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10
        },
        // 表格数据
        list: [],
        // 总条数
        total: 0,
      },
      // 部门名称
      deptName: undefined,
      /** 选择职工*/
      emp: {
        // 是否显示弹出层
        open: false,
        // 列表遮罩层
        loading: false,
        // 列表查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          empName: undefined,
          deptId: undefined,
        },
        // 表格数据
        list: [],
        // 选中表格数据
        selectList: [],
        // 总条数
        total: 0,
      },
      // 考核发布
      pcSetUp: {
        // 选中行Index
        xzhIndex: null,
        isRowEdit: false,
        open: false,
        pingceId:undefined,
        pingceName:undefined,
        templateId:undefined,
        list: []
      },
      // 评分规则初始数据
      voteInitData:{
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        id: null,
        pingceId: null,
        pingceName: null,
        khitemId: null,
        khitemName: null,
        khitemTypeid: null,
        voteItems: null,
        voteTitle: null,
        vDescription: null,
        sTime: null,
        eTime: null,
        maxNum: null,
        percentage: null,
        state: null,
        itemIds: null
      },
      // 评分选项类型数据
      vote_options: []
    }
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getList();
    this.getDeptTree();
    this.getVoteItemsList();
  },
  methods: {
    /** 查询考核评测列表 */
    getList() {
      this.loading = true
      listPingce(this.queryParams).then(response => {
        this.pingceList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.deptExpand = true;
      this.deptNodeAll = false;
      this.form_bkh_zg = [];
      this.form = {
        deptCheckStrictly: false,
        id: null,
        pcTitle: null,
        pcDescription: null,
        templateId: null,
        templateName: null,
        pcBkhdxs:[],
        pcResultId: null,
        state: '0',
        bkhdxType: '0',
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: 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.open = true
      this.title = "添加考核评测"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getPingce(id).then(response => {
        this.form = response.data;
        if(this.form.bkhdxType==='0'){
          this.form.pcBkhdxs.forEach(item=>{
            this.form_bkh_zg.push({
              id: item.bkhdxId,
              empName: item.bkhdxName,
              deptId: item.deptId,
              deptName: item.deptName,
            })
          })
        }

        if(this.form.bkhdxType==='1'){
          let Keys = [];
          this.form.pcBkhdxs.forEach(item=>{
            Keys.push(item.bkhdxId);
          })
          this.$nextTick(() => {
            this.$refs.dept.setCheckedKeys(Keys)
          })
        }
        this.form.pcBkhdxs = [];
        this.open = true;
        this.title = "修改考核评测";
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let flag = true;
          // 被考核对象 - 职工
          if(this.form.bkhdxType==='0'){
            if( this.form_bkh_zg && this.form_bkh_zg.length <1 ){
              flag = false;
              this.$alert(`还没有选择被考核对象，请选择被考核对象！`, `提示`, {
                type: 'warning'
              })
            }else {
              this.form_bkh_zg.forEach(item=>{
                this.form.pcBkhdxs.push({
                  bkhdxId: item.id,
                  bkhdxName: item.empName,
                  deptId: item.deptId,
                  deptName: item.deptName,
                })
              })
            }
          }
          // 被考核对象 - 部门
          if(this.form.bkhdxType==='1'){
            // 被选中的节点所组成的数组
            let xzNodes = this.$refs.dept.getCheckedNodes();
            if(xzNodes && xzNodes.length<1){
              flag = false;
              this.$alert(`还没有选择被考核对象，请选择被考核对象！`, `提示`, {
                type: 'warning'
              })
            }else {
              xzNodes.forEach(item=>{
                this.form.pcBkhdxs.push({
                  bkhdxId: item.id,
                  bkhdxName: item.label,
                  deptId: item.id,
                  deptName: item.label,
                })
              })
            }
          }
          if(flag){
            if (this.form.id != null) {
              updatePingce(this.form).then(response => {
                this.$modal.msgSuccess("修改成功")
                this.open = false
                this.getList()
              })
            } else {
              addPingce(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 delPingce(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('kaohe/pingce/export', {
        ...this.queryParams
      }, `pingce_${new Date().getTime()}.xlsx`)
    },
    /** 选择模板信息*/
    changeTemp(val) {
      this.temp.type = val;
      this.temp.open = true;
      this.temp.queryParams.pageNum = 1;
      this.tempQuery();
    },
    /** 模板信息列表查询 */
    tempQuery() {
      this.temp.loading = true;
      listTemplate(this.temp.queryParams).then(response => {
        this.temp.list = response.rows;
        this.temp.total = response.total;
        this.temp.loading = false;
      });
    },
    /** 模板信息选择弹出框关闭*/
    tempCancel() {
      this.temp.open = false
    },
    // 模板选择 选中数据
    tempRowChange(val) {
      this.form.templateId = val.id;
      this.form.templateName = val.temName;
      this.temp.open = false;
    },
    // 选择职工
    selectEmp(){
      this.emp.open = true;
      this.emp.selectList = [...this.form_bkh_zg]
      this.emp.queryParams.pageNum = 1;
      this.empQuery();
    },
    /** 职工列表查询 */
    empQuery() {
      this.emp.loading = true;
      listEmployee(this.emp.queryParams).then(response => {
        this.emp.list = response.rows;
        this.emp.total = response.total;
        this.emp.loading = false;
        this.$nextTick(()=> {
          this.emp.selectList.forEach(item =>{
            this.emp.list.forEach(row => {
              if(row.id == item.id){
                this.$refs.empTable.toggleRowSelection(row,true);
              }
            });
          })
        })
      });
    },
    /** 职工选择弹出框 确认*/
    empConfirm() {
      this.form_bkh_zg = this.emp.selectList;
      this.emp.open = false
    },
    // 当用户手动勾选数据行的 Checkbox 时触发的事件	    参数 selection, row
    empSelect(val,row) {
      let spliceIndex = undefined;
      this.emp.selectList.forEach((item,index)=>{
        if(item.id === row.id){
          spliceIndex = index
        }
      })
      if(spliceIndex===undefined){
        this.emp.selectList.push(row)
      }else {
        this.emp.selectList.splice(spliceIndex,1)
      }
    },
    // 当用户手动勾选全选 Checkbox 时触发的事件	      参数 selection
    empSelectAll(val){
      this.$nextTick(()=> {
        if(val.length > 0){
          val.forEach(item=>{
            if(this.emp.selectList.filter(item1=>{ return item1.id===item.id}).length<1){
              this.emp.selectList.push(item)
            }
          })
        }else {
          this.emp.list.forEach(item=>{
            this.emp.selectList.forEach((item1,index)=>{
              if(item1.id===item.id){
                this.emp.selectList.splice(index,1)
              }
            })
          })
        }
      })
    },
    // 职工 移除选中
    selectListDel(row,index){
      this.emp.selectList.splice(index,1)
      this.emp.list.forEach(item=>{
        if(row.id===item.id){
          this.$refs.empTable.toggleRowSelection(item,false);
        }
      })
    },
    delEmpList(row,index){
      this.form_bkh_zg.splice(index,1)
    },
    // 发布考核
    handleSetUp(row){
      this.pcSetUp.pingceId = row.id;
      this.pcSetUp.pingceName = row.pcTitle;
      this.pcSetUp.templateId = row.templateId;
      this.pcSetUp.open = true;
      this.getItemVote();
    },
    // 获取评测规则数据
    getItemVote(){
      getItemVote({
        id: this.pcSetUp.templateId,
        pingceId: this.pcSetUp.pingceId,
      }).then(response => {
        let pcSetUpList = [];
        response.data.items.forEach(item=>{
          if (item.vote===null){
            item.vote={...this.voteInitData}
            item.vote.khitemId = item.id;
            item.vote.khitemName = item.itemName;
            item.vote.khitemTypeid = item.typeId;
            item.vote.pingceId = this.pcSetUp.pingceId;
            item.vote.pingceName = this.pcSetUp.pingceName;
          }
          if(item.vote.voteItems){
            item.vote.voteItems = item.vote.voteItems.split(',');
            let VoteItemsName = []
            item.vote.voteItems.forEach(e => {
              this.vote_options.forEach(e1 => {
                if(e === e1.id){
                  VoteItemsName.push(e1.vitemName)
                }
              })
            })
            item.vote['voteItemsName'] = VoteItemsName.join(',');
          }
          pcSetUpList.push(item);
        })
        this.pcSetUp.list = pcSetUpList;
      })
    },
    // 评测规则设置
    setUpTouPiao(row,index){
      this.pcSetUp.xzhIndex = index;
      this.getItemVote();
    },
    voteSubmitForm(row){
      let flag = true;
      let Vote = {...row.vote}
      if(!Vote.voteTitle){
        flag = false;
        this.$alert(`评分主题不能为空！`, `提示`, {
          type: 'Danger'
        })
      }
      if(!Vote.state){
        flag = false;
        this.$alert(`状态不能为空！`, `提示`, {
          type: 'Danger'
        })
      }
      if(!Vote.sTime){
        flag = false;
        this.$alert(`开始时间不能为空！`, `提示`, {
          type: 'Danger'
        })
      }
      if(!Vote.eTime){
        flag = false;
        this.$alert(`结束时间不能为空！`, `提示`, {
          type: 'Danger'
        })
      }
      if(!Vote.id && !(new Date(Vote.sTime) > (new Date()))){
        flag = false;
        this.$alert(`开始时间需大于当前时间！`, `提示`, {
          type: 'Danger'
        })
      }
      if(!(new Date(Vote.eTime) > (new Date()))){
        flag = false;
        this.$alert(`结束时间需大于当前时间！`, `提示`, {
          type: 'Danger'
        })
      }
      if(!(new Date(Vote.eTime) > (new Date(Vote.sTime)))){
        flag = false;
        this.$alert(`结束时间需大于开始时间！`, `提示`, {
          type: 'Danger'
        })
      }
      if(row.typeId===1 && (!Vote.voteItems)){
        flag = false;
        this.$alert(`评分选项不能为空！`, `提示`, {
          type: 'Danger'
        })
      }
      if(row.typeId!==1 && (!Vote.maxNum)){
        flag = false;
        this.$alert(`最大值不能为空！`, `提示`, {
          type: 'Danger'
        })
      }
      if(row.typeId!==5 && (!Vote.percentage)){
        flag = false;
        this.$alert(`评分占比不能为空！`, `提示`, {
          type: 'Danger'
        })
      }
      if(!Vote.voteTitle){
        flag = false;
        this.$alert(`评分主题不能为空！`, `提示`, {
          type: 'Danger'
        })
      }
      if(!Vote.vDescription){
        flag = false;
        this.$alert(`评分详情描述不能为空！`, `提示`, {
          type: 'Danger'
        })
      }
      if (Vote.id != null && flag ) {
        if(Vote.voteItems){
          Vote.voteItems = Vote.voteItems.join(',')
        }
        updateVote(Vote).then(response => {
          this.$modal.msgSuccess("修改成功")
          this.cancelVote()
        })
      } else if(Vote.id === null && flag){
        if(Vote.voteItems){
          Vote.voteItems = Vote.voteItems.join(',')
        }
        addVote(Vote).then(response => {
          this.$modal.msgSuccess("新增成功")
          this.cancelVote()
        })
      }
    },
    cancelVote(){
      this.pcSetUp.xzhIndex = null;
      this.getItemVote();
    },
    goVotepage(row){
      this.$router.push({ path: '/votepage', query: { id: row.vote.id }});
    },
    goTotalScore(row){
      this.khdf.open = true;
      this.khdf.idRw = row.id;
    },
    // 生成评分账号
    handleAddsUser(row){
      this.$prompt('请输入生成数量', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /^([1-9][0-9]){1,3}$/,
        inputErrorMessage: '生成数量格式不正确'
      }).then(({ value }) => {
        addsUser({
          pcId: 1,
          num: value
        }).then(response => {
          this.$modal.msgSuccess("生成成功")
        })
      }).catch(() => {

      });
    },
    viewUser(){
      this.$router.push({ path: '/system/user', query: { pcId: 1}});
    },
    getVoteItemsList() {
      listVote_items({
        pageSize: 1000000,
        pageNum: 1
      }).then(response => {
        this.vote_options = response.rows;
        this.vote_options.forEach(e1 => {
          e1.id = String(e1.id)
        })
      })
    },
    // 树权限（展开/折叠）
    handleCheckedTreeExpand(value, type) {
      if (type == 'dept') {
        let treeList = this.deptOptions
        for (let i = 0; i < treeList.length; i++) {
          this.$refs.dept.store.nodesMap[treeList[i].id].expanded = value
        }
      }
    },
    // 树权限（全选/全不选）
    handleCheckedTreeNodeAll(value, type) {
      if (type == 'dept') {
        this.$refs.dept.setCheckedNodes(value ? this.deptOptions: [])
      }
    },
    // 树权限（父子联动）
    handleCheckedTreeConnect(value, type) {
      if (type == 'dept') {
        this.form.deptCheckStrictly = value ? true: false
      }
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    },
    // 筛选节点
    empfilterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    empNodeClick(data) {
      this.emp.queryParams.deptId = data.id;
      this.empQuery();
    },
  }
}
</script>
