
<!-- 人员排班页面 -->
<template>
  <div class="examination">
    <el-form :inline="true" class="common-head-search">
      <el-form-item label="排班车间">
        <el-select
          v-model="search.deptNo"
          filterable
          @change="selectClass"
          @clear="clearData"
          clearable
          style="width: 150px"
          collapse-tags
          size="small"
        >
          <el-option
            v-for="item in sectionNameType"
            :key="item.orgCode"
            :label="item.orgName"
            :value="item.orgCode"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="排班组">
        <el-select
          v-model="search.sectionNo"
          clearable
          style="width: 150px"
          filterable
          collapse-tags
          size="small"
        >
          <el-option
            v-for="item in sectionClass"
            :key="item.orgCode"
            :label="item.orgName"
            :value="item.orgCode"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="班值">
        <el-select
          v-model="search.wrkGrpCode"
          clearable
          style="width: 150px"
          filterable
          collapse-tags
          size="small"
        >
          <el-option
            v-for="item in wrkGrpClass"
            :key="item.baseCode"
            :label="item.baseName"
            :value="item.baseCode"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          icon="el-icon-search"
          type="goon"
          size="mini"
          @click="searchData"
          >查询</el-button
        >
        <el-button
          icon="el-icon-refresh"
          type="info"
          plain
          size="mini"
          @click="reset"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row>
      <el-col :span="10" id="getHeight">
        <el-row>
          <el-col :span="24">
            <div class="common-title-div" style="height: auto">
              <div class="common-title-name" style="margin-right: 10px">
                <img
                  style="width: 25px; height: 25px"
                  src="../../../../assets/img/imgScreen/logo.png"
                />
                <span>人员信息</span>
              </div>
              <div>
                <el-form :model="form" ref="form" :inline="true" size="mini">
                  <el-form-item style="margin-bottom: 0">
                    <el-button
                      size="mini"
                      type="goon"
                      icon="el-icon-check"
                      :loading="allotBLoading"
                      v-privilege="activeMenu + 'auto'"
                      @click="allot"
                      >分配</el-button
                    >
                    <el-button
                      size="mini"
                      type="goon"
                      icon="el-icon-check"
                      :loading="modeAllotBLoading"
                      v-privilege="activeMenu + 'manual'"
                      @click="handWork"
                      >手动分配</el-button
                    >
                  </el-form-item>
                  <!-- <el-form-item style="margin-bottom: 0" label="班次">
                    <el-select
                      v-model="classesCode"
                      style="width: 120px"
                      placeholder="请选择班次"
                      filterable
                      collapse-tags
                      size="small"
                    >
                      <el-option
                        v-for="item in classesType"
                        :key="item.baseCode"
                        :label="item.baseName"
                        :value="item.baseCode"
                      >
                      </el-option>
                    </el-select>
                  </el-form-item> -->
                  <el-form-item style="margin-bottom: 0">
                    <!-- <el-date-picker
                      v-model="startDate"
                      type="date"
                      style="width: 140px"
                      size="small"
                      placeholder="请选择排班日期"
                      value-format="yyyy-MM-dd"
                    ></el-date-picker>
                  </el-form-item>
                  <el-form-item style="margin-bottom: 0">
                    <el-date-picker
                      v-model="wgoDate"
                      type="date"
                      style="width: 140px"
                      size="small"
                      placeholder="请选择排班日期"
                      value-format="yyyy-MM-dd"
                    ></el-date-picker> -->
                    <el-date-picker
                      v-model="startDate"
                      type="datetime"
                      style="width: 173px"
                      size="small"
                      placeholder="请选择排班日期"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      default-time="08:00:00"
                    ></el-date-picker>
                    <el-date-picker
                      v-model="wgoDate"
                      type="datetime"
                      style="width: 173px"
                      size="small"
                      placeholder="请选择排班日期"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      default-time="18:00:00"
                    ></el-date-picker>
                  </el-form-item>
                </el-form>
              </div>
            </div>
            <div class="common-table-div">
              <el-table
                v-loading="dataLoading"
                ref="dataTable"
                border
                :height="topheight"
                highlight-current-row
                @selection-change="handleSelectionChange1"
                :data="tableData1"
                :icore-filter-flag="icoreFilterFlag"
                :header-cell-style="tableHeaderCellStyle"
                :summary-method="getSummaries"
                show-summary
              >
                <el-table-column
                  type="index"
                  label="NO"
                  fixed="left"
                  align="center"
                ></el-table-column>
                <el-table-column
                  type="selection"
                  width="55"
                  fixed="left"
                  align="center"
                ></el-table-column>
                <el-table-column
                  sortable
                  min-width="100px"
                  prop="empNo"
                  label="职工编号"
                  align="center"
                ></el-table-column>
                <el-table-column
                  sortable
                  min-width="100px"
                  prop="empName"
                  label="职工姓名"
                  align="center"
                ></el-table-column>
                <el-table-column
                  sortable
                  min-width="100px"
                  prop="deptName"
                  label="车间"
                  align="center"
                ></el-table-column>
                <el-table-column
                  sortable
                  min-width="110px"
                  prop="sectionName"
                  label="班组"
                  align="center"
                ></el-table-column>
                <el-table-column
                  sortable
                  min-width="100px"
                  prop="wrkGrpName"
                  label="班值"
                  align="center"
                ></el-table-column>
                <!-- <el-table-column sortable min-width="100px" prop="wrkOrdName" label="班次" align="center"></el-table-column> -->
              </el-table>
            </div>
            <!-- <div class="common-foot-style">
              <el-pagination
              @size-change="pageSizeChange"
              @current-change="pageCurrentChange"
              :current-page="page.pageNum"
              :page-sizes="[100, 200,500,800]"
              :page-size="page.pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="page.totalPage" background>
              </el-pagination>
            </div> -->
          </el-col>
          <el-col :span="24">
            <div class="common-title-div">
              <div class="common-title-name" style="margin-right: 10px">
                <img
                  style="width: 25px; height: 25px"
                  src="../../../../assets/img/imgScreen/logo.png"
                />
                <span>任务信息</span>
              </div>
              <div>
                <el-form
                  :model="form"
                  ref="form"
                  :rules="rules"
                  :inline="true"
                  size="mini"
                >
                  <el-form-item label="排班名称">
                    <el-select
                      v-model="form.ruleNo"
                      filterable
                      placeholder="请选择排班名称"
                      style="width: 150px"
                      @change="firstData"
                      collapse-tags
                      size="mini"
                    >
                      <el-option
                        v-for="item in ruleTypeName"
                        :key="item.ruleNo"
                        :label="item.ruleName"
                        :value="item.ruleNo"
                      >
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-form>
              </div>
            </div>
            <div class="common-table-div">
              <el-table
                v-loading="tableLoading2"
                ref="dataTable2"
                border
                :height="topheight"
                highlight-current-row
                @selection-change="handleSelectionChange2"
                :data="tableData2"
                :icore-filter-flag="icoreFilterFlag"
                @row-dblclick="showCell"
                @row-click="filterMat"
                :row-class-name="tableRowName"
                :header-cell-style="tableHeaderCellStyle"
                :summary-method="getSummaries"
                show-summary
              >
                <el-table-column
                  type="index"
                  label="NO"
                  fixed="left"
                  align="center"
                ></el-table-column>
                <el-table-column
                  type="selection"
                  width="55"
                  fixed="left"
                  align="center"
                ></el-table-column>
                <el-table-column
                  sortable
                  :show-overflow-tooltip="true"
                  min-width="160px"
                  prop="matName"
                  label="物料"
                  align="center"
                >
                  <template slot-scope="{ row }">
                    <el-select
                      v-model="row.matNameNo"
                      clearable
                      multiple
                      style="width: 100%"
                      filterable
                      :filter-method="
                        (val) => {
                          return matFilter(val, row);
                        }
                      "
                      collapse-tags
                      size="small"
                      @change="(val) => matChange(val, row)"
                    >
                      <el-option
                        v-for="(item, index) in matNameType"
                        :key="index"
                        :label="item.matName"
                        :value="item.matNo"
                      >
                      </el-option>
                    </el-select>
                  </template>
                </el-table-column>
                <el-table-column
                  sortable
                  :show-overflow-tooltip="true"
                  min-width="100px"
                  prop="ruleItemName"
                  label="任务名称"
                  align="center"
                >
                  <template slot-scope="scope">
                    <span>{{ scope.row.ruleItemName }}</span>
                  </template>
                </el-table-column>
                <el-table-column
                  sortable
                  :show-overflow-tooltip="true"
                  min-width="100px"
                  prop="ruleItemValue"
                  label="人员数量"
                  align="center"
                >
                  <template slot-scope="{ row }">
                    <el-input
                      oninput="value=value.replace(/[^0-9]/g,'')"
                      v-if="row.index === tabClickIndex"
                      v-model="row.ruleItemValue"
                      @keyup.enter.native="saveCell"
                      size="small"
                      style="width: 100%"
                    ></el-input>
                    <span v-else>{{ row.ruleItemValue }}</span>
                  </template>
                </el-table-column>
                <!-- <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" label="任务数量" align="center">
                  <template slot-scope="{row}">
                    <el-input oninput ="value=value.replace(/[^0-9]/g,'')" v-if="row.index === tabClickIndex" v-model="row.num"  @keyup.enter.native="saveCell" size="small" style="width: 100%;"></el-input>
                    <span v-else>{{row.num}}</span>
                  </template>
                </el-table-column> -->
                <!-- <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="isAutoName" label="自动分配" align="center"></el-table-column> -->
              </el-table>
            </div>
            <!-- <div class="common-foot-style">
              <el-pagination
                @size-change="pageSizeChange2"
                @current-change="pageCurrentChange2"
                :current-page="page2.pageNum"
                :page-sizes="[10, 20, 50, 100, 200]"
                :page-size="page2.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="page2.totalPage" background>
              </el-pagination>
            </div> -->
          </el-col>
        </el-row>
      </el-col>
      <el-col :span="14">
        <div class="common-title-div" style="height: auto">
          <div class="common-title-name" style="margin-right: 10px">
            <img
              style="width: 25px; height: 25px"
              src="../../../../assets/img/imgScreen/logo.png"
            />
            排班信息
          </div>
          <div>
            <el-button
              size="mini"
              type="goon"
              icon="el-icon-check"
              @click="editMatName"
              >修改物料</el-button
            >
            <el-button
              size="mini"
              type="goon"
              icon="el-icon-check"
              :loading="addPlanLaoding"
              @click="addPlan"
              >追加</el-button
            >
            <el-button
              icon="el-icon-circle-close"
              type="danger"
              size="mini"
              :loading="cancelLaoding"
              v-privilege="activeMenu + 'cancel'"
              @click="cancel"
              >取消</el-button
            >
            <el-date-picker
              v-model="createTime"
              @change="timeChange"
              type="date"
              style="width: 140px"
              size="small"
              placeholder="请选择排班日期"
              value-format="yyyy-MM-dd"
            ></el-date-picker>
          </div>
        </div>
        <div class="common-table-div">
          <el-table
            v-loading="tableLoading3"
            ref="dataTable3"
            border
            :height="bHeight"
            highlight-current-row
            @selection-change="selectData"
            :data="tableData3"
            :icore-filter-flag="icoreFilterFlag"
            :header-cell-style="tableHeaderCellStyle"
            :cell-class-name="tableRowClassName3"
            :summary-method="getSummaries"
            show-summary
          >
            <el-table-column
              type="index"
              label="NO"
              fixed="left"
              align="center"
            ></el-table-column>
            <el-table-column
              type="selection"
              align="center"
              width="55"
            ></el-table-column>
            <el-table-column
              sortable
              :show-overflow-tooltip="true"
              min-width="100px"
              prop="empNo"
              label="职工编号"
              align="center"
            ></el-table-column>
            <el-table-column
              sortable
              :show-overflow-tooltip="true"
              min-width="100px"
              prop="empName"
              label="职工姓名"
              align="center"
            ></el-table-column>
            <el-table-column
              sortable
              :show-overflow-tooltip="true"
              min-width="100px"
              prop="matName"
              label="物料"
              align="center"
            ></el-table-column>
            <el-table-column
              sortable
              :show-overflow-tooltip="true"
              min-width="100px"
              prop="testItemName"
              label="任务名称"
              align="center"
            ></el-table-column>
            <el-table-column
              sortable
              :show-overflow-tooltip="true"
              min-width="100px"
              prop="wrkOrdName"
              label="班次"
              align="center"
            ></el-table-column>
            <el-table-column
              sortable
              :show-overflow-tooltip="true"
              min-width="140px"
              prop="wrkGrpName"
              label="班值"
              align="center"
              style="color: blue"
            ></el-table-column>
            <!-- <el-table-column sortable :show-overflow-tooltip="true" min-width="120px" prop="wgoDate" label="值班日期" align="center"></el-table-column> -->
            <el-table-column
              sortable
              :show-overflow-tooltip="true"
              min-width="160px"
              prop="startTime"
              label="值班开始时间"
              align="center"
            ></el-table-column>
            <el-table-column
              sortable
              :show-overflow-tooltip="true"
              min-width="160px"
              prop="endTime"
              label="值班结束时间"
              align="center"
            ></el-table-column>
            <el-table-column
              sortable
              :show-overflow-tooltip="true"
              min-width="100px"
              prop="createMan"
              label="排班人"
              align="center"
            ></el-table-column>
            <el-table-column
              sortable
              :show-overflow-tooltip="true"
              min-width="160px"
              prop="createTime"
              label="排班时间"
              align="center"
            ></el-table-column>
            <!-- <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="deptName" label="部门"  align="center"></el-table-column>
            <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="sectionName" label="任务班" align="center" style="color:blue;"></el-table-column> -->
          </el-table>
        </div>
        <!-- <div class="common-foot-style">
          <el-pagination
          @size-change="pageSizeChange3"
          @current-change="pageCurrentChange3"
          :current-page="page3.pageNum"
          :page-sizes="[10, 20, 50, 100, 200]"
          :page-size="page3.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page3.totalPage" background>
          </el-pagination>
        </div> -->
      </el-col>
    </el-row>
    <el-dialog
      @close="closeDialog"
      :visible.sync="dialogTableVisible"
      width="70%"
    >
      <div slot="title">
        <i class="el-icon-document" style="font-size: 20px"></i>
        <span style="margin-left: 10px; font-size: 18px">选择人员</span>
      </div>
      <el-form :inline="true" @submit.native.prevent>
        <el-form-item label="人员编号">
          <el-input
            v-model="form.empNo"
            @keyup.enter.native="searchAllot()"
            size="small"
            placeholder="请输入搜索条件"
            style="width: 200px"
          ></el-input>
        </el-form-item>
        <el-form-item label="人员名称">
          <el-input
            v-model="form.empName"
            @keyup.enter.native="searchAllot()"
            size="small"
            placeholder="请输入搜索条件"
            style="width: 200px"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            icon="el-icon-search"
            type="goon"
            @click="searchAllot"
            size="mini"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
      <el-table
        v-loading="allotLoading"
        ref="dataTable"
        border
        height="300"
        highlight-current-row
        @selection-change="handleAllot"
        :data="allotData"
        :icore-filter-flag="icoreFilterFlag"
        :header-cell-style="tableHeaderCellStyle"
        :summary-method="getSummaries"
        show-summary
      >
        <el-table-column
          type="index"
          label="NO"
          align="center"
        ></el-table-column>
        <el-table-column
          type="selection"
          width="55"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="100px"
          prop="empNo"
          label="人员编号"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="80px"
          prop="empName"
          label="人员姓名"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="120px"
          prop="deptName"
          label="部门"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="120px"
          prop="wrkGrpName"
          label="班组"
          align="center"
        ></el-table-column>
      </el-table>
      <div>
        <el-pagination
          @size-change="pageSizeAllot"
          @current-change="pageCurrentAllot"
          :current-page="allotIndex"
          :page-sizes="[100, 200, 500]"
          :page-size="allotPage"
          layout="total, sizes, prev, pager, next, jumper"
          :total="allotTotal"
          background
        >
        </el-pagination>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button
          type="goon"
          size="mini"
          icon="el-icon-check"
          :loading="allocationLoading"
          @click="allocation()"
          >确定</el-button
        >
        <el-button
          size="mini"
          type="goon"
          icon="el-icon-remove-outline"
          @click="cancelData"
          >取消</el-button
        >
      </span>
    </el-dialog>
    <!-- 修改物料 -->
    <el-dialog @close="closeEditDialog" :visible.sync="dialogTableVisible3">
      <div slot="title" class="titleBox" style="display: flex">
        <i class="el-icon-edit-outline" style="font-size: 20px"></i>
        <span style="margin-left: 10px; font-size: 18px">修改物料</span>
      </div>
      <el-form
        :model="editData"
        ref="editData"
        label-width="80px"
        :inline="false"
        size="normal"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item label="物料">
              <el-select
                v-model="editData.matNameNo"
                clearable
                multiple
                style="width: 100%"
                filterable
                collapse-tags
                size="small"
                :filter-method="matFilter1"
                @change="matChange"
              >
                <el-option
                  v-for="(item, index) in matNameType"
                  :key="index"
                  :label="item.matName"
                  :value="item.matNo"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button
          type="goon"
          size="mini"
          icon="el-icon-check"
          :loading="addLoading2"
          @click="addData2()"
          >确定</el-button
        >
        <el-button
          size="mini"
          type="goon"
          icon="el-icon-remove-outline"
          @click="closeEditDialog"
          >取消</el-button
        >
      </span>
    </el-dialog>
    <el-dialog
      @close="cancelData2"
      :visible.sync="dialogTableVisible2"
      width="70%"
    >
      <div slot="title">
        <i class="el-icon-document" style="font-size: 20px"></i>
        <span style="margin-left: 10px; font-size: 18px">选择人员</span>
      </div>
      <el-table
        v-loading="allotLoading"
        ref="dataTable"
        border
        height="300"
        highlight-current-row
        :data="allotData"
        @selection-change="handleSelectionChange5"
        :icore-filter-flag="icoreFilterFlag"
        :header-cell-style="tableHeaderCellStyle"
        :summary-method="getSummaries"
        show-summary
      >
        <el-table-column
          type="index"
          label="NO"
          align="center"
        ></el-table-column>
        <el-table-column
          type="selection"
          width="55"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="100px"
          prop="empNo"
          label="职工编号"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="100px"
          prop="empName"
          label="职工姓名"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="100px"
          prop="sectionName"
          label="班组"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="100px"
          prop="postName"
          label="岗位"
          align="center"
        ></el-table-column>
        <el-table-column
          sortable
          :show-overflow-tooltip="true"
          min-width="100px"
          prop="wrkGrpName"
          label="班值"
          align="center"
        ></el-table-column>
      </el-table>
      <div>
        <el-pagination
          @size-change="pageSizeAllot"
          @current-change="pageCurrentAllot"
          :current-page="allotIndex"
          :page-sizes="[100, 200, 500]"
          :page-size="allotPage"
          layout="total, sizes, prev, pager, next, jumper"
          :total="allotTotal"
          background
        >
        </el-pagination>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button
          type="goon"
          size="mini"
          icon="el-icon-check"
          :loading="modeAllotBLoading"
          @click="modeAllot()"
          >确定</el-button
        >
        <el-button
          size="mini"
          type="goon"
          icon="el-icon-remove-outline"
          @click="cancelData2"
          >取消</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  getCookie, formatDate
} from '@/utils/util.js';
import {
  cookieUserId,
  cookieUserName
} from '@/config/config.js';
let userName = getCookie(cookieUserName);
let userId = getCookie(cookieUserId);
export default {
  data () {
    return {
      table: {
        list: [],
        list2: [],
        list3: []
      },
      addLoading2: false,
      createTime: '',
      ruleTypeNameType: [],
      matNameType: [],
      ruleTypeName: [],
      tableLoading2: false,
      tableLoading3: false,
      allotLoading: false,
      cancelLaoding: false,
      allotData: [],
      tableData1: [],
      tableData2: [],
      tableData3: [],
      selection1: [],
      selection5: [],
      selection2: [],
      classesCode: '483501',
      dataLoading: false,
      icoreFilterFlag: true,
      dialogTableVisible: false,
      dialogTableVisible2: false,
      form: { ruleNo: '' },
      classesType: [],
      startDate: '',
      wgoDate: '',
      search: {
        startDate: [],
        deptNo: '',
        wgoDate: [],
        sectionNo: '',
        wrkGrpCode: '',
      },
      wrkGrpClass: [],
      rules: {
        ruleTypeNo: [{ required: true, message: '不能为空', trigger: 'change' }],
        ruleNo: [{ required: true, message: '不能为空', trigger: 'change' }],
      },
      clickAllot: [],
      allotBLoading: false,
      modeAllotBLoading: false,
      allocationLoading: false,
      allotIndex: 1,
      allotPage: 100,
      allotTotal: 0,
      wgoData: {},
      clickData: [],
      itemType: [],
      savaSampleValue: 'R0009',
      topheight: 0,
      bHeight: 0,
      tabClickIndex: null,
      page: {
        pageSize: 500,
        pageNum: 1,
        totalPage: 0
      },
      dialogTableVisible3: false,
      editData: { matNameNo: [] },
      page2: {
        pageSize: 500,
        pageNum: 1,
        totalPage: 0
      },
      page3: {
        pageSize: 500,
        pageNum: 1,
        totalPage: 0
      },
      addPlanLaoding: false,
      clearFlag: true,
      companyNameType: [],
      sectionNameType: [],
      sectionClass: [],
      multipleSelection3: [],
      multipleSelection4: [],
      multipleSelection5: [],
      activeMenu: '',
      allList: [],
      nowMatList: [], // 已分配的物资编码list
      currentRow: null, // 当前行
      searchFlag: true,
      matDataList: [], // 已分配的物资数据列表
      editNameList: [], // 修改物料窗口已经分配的物资数据列表
      selectMatList: []
    }
  },
  created () {
    this.createTime = this.wgoDate = this.getYNM(new Date())
    this.topheight = window.innerHeight / 2 < 249 ? 249 : window.innerHeight / 2 - 115;
    this.topheight = this.topheight < 249 ? 249 : this.topheight
    this.bHeight = this.topheight * 2 + 80
    console.log(this.topheight, this.topheight, 'height');
    this.search.wgoDate = [this.getYNM(new Date(new Date().setHours(-24))), this.getYNM(new Date())]
    this.startDate = formatDate(new Date(), 'yyyy-MM-dd') + ' 08:00:00'
    this.wgoDate = formatDate(new Date(), 'yyyy-MM-dd') + ' 18:00:00'
    this.activeMenu = window.top.localStorage.getItem('activeMenu')
  },
  async mounted () {
    //获取公司
    this.axios.post("pass/baseManagement/v1/sysorgs/querySysOrgCompanyAll", {}, { individualType: 'json' }).then(response => {
      this.companyNameType = response.data
      this.selectDepartment('F31300000')
      this.clearFlag = false
      this.selectClass('P31302800')
      this.clearFlag = true
    });
    this.axios.post("pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode", { validFlag: 1, baseCode: 4838 }, { individualType: 'json' }).then(res => {
      this.ruleTypeNameType = res.data;
      this.filterData('483805')
    })
    this.axios.post("pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode", { validFlag: 1, baseCode: 4836 }, { individualType: 'json' }).then(res => {
      this.wrkGrpClass = res.data;
    })
    this.axios.post("pass/baseManagement/v1/limsbaseinfos/queryBaseInfoByBaseCode", { validFlag: 1, baseCode: 4835 }, { individualType: 'json' }).then(res => {
      this.classesType = res.data;
    })
    // 物料信息
    await this.axios.post("pass/baseManagement/v1/limsbasematerialss/queryLimsBaseMaterialsPage", { object: { validFlag: 1, matClassNo: "" }, pageIndex: 1, pageSize: 50 }, { individualType: 'json' }).then(res => {
      this.matNameType = [...res.data.list];
    })
    this.searchAllot()
    this.searchData()
    this.addScreen();
    this.addScreen2();
    this.addScreen3();
  },
  methods: {
    getCode (code, arr, codeKey, nameKey) {
      if (!code) {
        return '';
      }
      let targetRow = arr.find(item => {
        return item[codeKey] === code;
      });
      return targetRow ? targetRow[nameKey] : '';
    },
    selectDepartment (val) {
      this.companyNameType.find(item => {
        if (val === item.orgCode) {
          this.sectionNameType = item.sysOrgList
        }
      })
    },
    //根据选中的部门名称，查询班组名称
    selectClass (val) {
      this.sectionClass = []
      this.sectionNameType.find(item => {
        if (val === item.orgCode) {
          this.sectionClass = item.sysOrgList
          this.clearFlag ? this.search.sectionNo = '' : ''
        }
      })
    },
    clearData () {
      this.search.sectionNo = ''
    },
    getYNM (date) {
      let year = date.getFullYear().toString();
      let month = date.getMonth() + 1;
      let day = date.getDate();
      month = month < 10 ? "0" + month : month;
      day = day < 10 ? "0" + day : day;
      return `${year}-${month}-${day}`;
    },
    getSummaries (param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
        }
        if (index === 1) {
          sums[index] = data.length;
        }
      });
      return sums;
    },
    //筛选出规则名称
    filterData (val) {
      this.ruleTypeName = []
      if (!val) {
        this.ruleTypeName = []
        return
      }
      let obj = {
        object: {
          ruleTypeCode: val,
          // ruleVer: '外检车间',
          validFlag: 1
        },
        pageIndex: 1,
        pageSize: 9999
      }
      this.axios.post('pass/baseManagement/v1/limsbaserules/queryLimsBaseRuleInfoPage', obj, { individualType: 'json' }).then(res => {
        if (res.code == 0 || res.code == 200) {
          this.ruleTypeName = res.data.list
        } else {
          this.$message.error(res.message);
        }
      }).catch(err => {
        this.$message.error(err.message);
      })
    },
    closeEditDialog () {
      // 关闭修改物料弹窗的时候清空已分配物料数据
      this.editNameList = []
      this.editData = { matNameNo: [] }
      this.addLoading2 = false
      this.dialogTableVisible3 = false
    },
    closeDialog () {
      this.form = {
        empNo: '',
        empName: ''
      },
        this.allocationLoading = false
      this.searchAllot()
    },
    cancelData () {
      this.form = {
        empNo: '',
        empName: ''
      },
        this.searchAllot()
      this.dialogTableVisible = false
      this.allocationLoading = false
    }, cancelData2 () {
      this.form = {
        empNo: '',
        empName: ''
      },
        this.dialogTableVisible2 = false
    },
    //勾选人员表格数据
    handleAllot (val) {
      this.clickAllot = val;
    },
    allocation () {
      if (this.clickAllot.length < 1) {
        this.$message.warning('请勾选人员数据')
        return
      }
      this.allocationLoading = true
      this.axios.post('pass/testManagement/v1/limsempwrkords/composeType', this.clickAllot, { individualType: 'json' }).then(res => {
        if (res.code == 0 || res.code == 200) {
          this.$message.success(res.message);
          this.dialogTableVisible = false
          this.searchData()
        } else {
          this.$message.error(res.message);
        }
        this.allocationLoading = false
      }).catch(err => {
        this.allocationLoading = false
        this.$message.error(err.message);
      })
    },
    tableRowName ({ row, rowIndex }) {
      row.index = rowIndex
    },
    showCell (row) {
      this.tabClickIndex = row.index
    },
    saveCell (row) {
      row.ruleItemValue = row.ruleItemValue ? row.ruleItemValue : '0'
      this.tabClickIndex = null
    },
    // 查询
    searchData () {
      this.page.pageNum = 1;
      this.page.pageSize = 500;
      this.page2.pageNum = 1
      this.page2.pageSize = 500
      this.page3.pageNum = 1
      this.page3.pageSize = 500
      this.mainData()
      this.firstData(this.savaSampleValue)
      this.getAllotData()
    },
    timeChange () {
      this.getAllotData()
    },
    firstData (val) {
      this.tableData2 = []
      this.savaSampleValue = val
      if (!val) {
        this.tableData2 = []
        return
      }
      let obj = {
        'mode': this.savaSampleValue,
        'pageIndex': this.page2.pageNum,
        'pageSize': this.page2.pageSize
      }
      this.tableLoading2 = true
      this.axios.post('pass/testManagement/v1/limsstdtestitems/querylimstdtest', obj, { individualType: 'json' }).then(res => {
        if (res.code == 0 || res.code == 200) {
          this.tableData2 = res.data.list;
          // 为每一项添加默认物资下拉框
          this.tableData2.forEach(item => {
            item.matNameNo
            item.maxValue = item.ruleItemValue
            item.isAutoName = item.isAuto == 0 ? '否' : item.isAuto == 1 ? '是' : ''
            // item.matList = [...this.matNameType]
            // item.useIndex = 1
          })
          this.page2.totalPage = res.data.total;
        } else {
          this.$message.error(res.message)
        }
        this.tableLoading2 = false;
      }).catch(e => {
        this.tableLoading2 = false;
        this.$message.error(e.message)
      });
    },
    mainData () {
      this.dataLoading = true;
      let obj = {
        'object': {
          'deptNo': this.search.deptNo,
          'sectionNo': this.search.sectionNo,
          'wrkGrpCode': this.search.wrkGrpCode,
          // 'wrkOrdCode': this.classesCode
        },
        'startTime': this.startDate ? this.startDate : '',
        'endTime': this.wgoDate ? this.wgoDate : '',
        'pageIndex': this.page.pageNum,
        'pageSize': this.page.pageSize
      }
      this.axios.post('pass/baseManagement/v1/limslrcemps/queryEmpPages', obj, { individualType: 'json' }).then(res => {
        if (res.code == 0 || res.code == 200) {
          this.tableData1 = res.data.list;
          this.page.totalPage = res.data.total;
        } else {
          this.$message.error(res.message)
        }
        this.dataLoading = false;
      }).catch(e => {
        this.dataLoading = false;
        this.$message.error(e.message)
      });
    },
    //查询任务分配信息表
    getAllotData () {
      let obj = {
        'object': {
          'deptNo': this.search.deptNo,
          'sectionNo': this.search.sectionNo,
          'wrkGrpCode': this.search.wrkGrpCode,
        },
        'startTime2': this.createTime ? this.createTime + ' 00:00:00' : '',
        'endTime2': this.createTime ? this.createTime + ' 23:59:59' : '',
        'pageIndex': this.page3.pageNum,
        'pageSize': this.page3.pageSize
      }
      this.tableLoading3 = true;
      this.axios.post('pass/testManagement/v1/limsempwrkords/queryLimsEmpWrkordPage', obj, { individualType: 'json' }).then(res => {
        if (res.code == 0 || res.code == 200) {
          this.tableData3 = res.data.list
          this.page3.totalPage = res.data.total
          this.tableData3.forEach(item => {
            item.validFlagName = item.validFlag == 1 ? '有效' : item.validFlag == 0 ? '无效' : ''
            item.wrkOrdName = this.getCode(item.wrkOrdCode, this.classesType, 'baseCode', 'baseName')
            // let NameType = []
            // let arr = item.matNo ? item.matNo.split(',') : []
            // NameType = [...arr.map(item=> this.getCode(item,this.matNameType,'matNo','matName'))]
            // item.matName = NameType.join()
          })
        } else {
          this.$message.error(res.message)
        }
        this.tableLoading3 = false;
      }).catch(e => {
        this.tableLoading3 = false;
      });
    },
    editMatName () {
      if (this.clickData.length != 1) {
        this.$message.warning('请先勾选一条且只能勾选一条要修改的数据')
        return
      }
      let dataList = []
      this.editData = Object.assign(this.editData, this.clickData[0])
      let noList = this.clickData[0].matNo ? this.clickData[0].matNo.split(',') : []
      let nameList = this.clickData[0].matName ? this.clickData[0].matName.split(',') : []
      for (let y = 0; y < nameList.length; y++) {
        dataList.push({ matName: nameList[y], matNo: noList[y] })
      }
      this.editData.matNameNo = noList;
      this.dialogTableVisible3 = true;
      this.editNameList = [...new Set(dataList)]
      this.editNameList.forEach(item => {
        let index = this.matNameType.findIndex(i => i.matNo === item.matNo)
        if (index === -1) {
          this.matNameType.push(item)
        }
      })
    },
    addData2 () {
      this.editData.matNo = this.editData.matNameNo.length > 0 ? this.editData.matNameNo.join() : ''
      let NameType = []
      NameType = [...this.editData.matNameNo.map(items => this.getCode(items, this.matNameType, 'matNo', 'matName'))]
      this.editData.matName = NameType.join()
      this.addLoading2 = true
      this.axios.post('pass/testManagement/v1/limsempwrkords/update', this.editData, { individualType: 'json' }).then(res => {
        if (res.code == 0 || res.code == 200) {
          this.$message.success(res.message)
          this.getAllotData()
          this.dialogTableVisible3 = false
        } else {
          this.$message.error(res.message)
        }
        this.addLoading2 = false
      }).catch(e => {
        this.addLoading2 = false
        this.$message.error(e.message)
      });
    },
    addPlan () {
      if (this.clickData.length < 1) {
        this.$message.warning('请先勾选要取消的数据')
        return
      }
      this.addPlanLaoding = true
      this.axios.post('pass/testManagement/v1/limsempwrkords/addOptNoToTaskTest', this.clickData, { individualType: 'json' }).then(res => {
        if (res.code == 0 || res.code == 200) {
          this.$message.success(res.message)
          this.searchData()
        } else {
          this.$message.error(res.message)
        }
        this.addPlanLaoding = false
      }).catch(e => {
        this.addPlanLaoding = false
        this.$message.error(e.message)
      });
    },
    //取消
    cancel () {
      if (this.clickData.length < 1) {
        this.$message.warning('请先勾选要取消的数据')
        return
      }
      this.cancelLaoding = true
      this.axios.post('pass/testManagement/v1/limsempwrkords/delete', this.clickData, { individualType: 'json' }).then(res => {
        if (res.code == 0 || res.code == 200) {
          this.$message.success(res.message)
          this.searchData()
        } else {
          this.$message.error(res.message)
        }
        this.cancelLaoding = false
      }).catch(e => {
        this.cancelLaoding = false
        this.$message.error(e.message)
      });
    },
    allot () {
      if (this.selection1.length < 1) {
        this.$message.warning("请先勾选排班信息数据");
        return;
      }
      if (this.selection2.length <= 0) {
        this.$message.warning("请先勾选任务信息数据");
        return;
      }
      let num = 0
      this.selection2.map(item => {
        num += Number(item.ruleItemValue)
      })
      let time = this.getCode(this.classesCode, this.classesType, 'baseCode', 'memo').split('-') || []
      this.selection2.forEach(item => {
        item.startTime = this.startDate
        item.endTime = this.wgoDate
        item.wgoDate = this.wgoDate ? this.wgoDate : ''
        item.matNo = item.matNameNo.join()
        let NameType = []
        NameType = [...item.matNameNo.map(items => this.getCode(items, this.matNameType, 'matNo', 'matName'))]
        item.matName = NameType.join()
      })
      if (this.selection1.length < num) {
        this.$message.warning('人员数量不能小于任务所需人数！')
        return
      }
      this.allotBLoading = true
      this.axios.post('pass/testManagement/v1/limsempwrkords/allocation/', { llp: this.selection1, llo: this.selection2 }, { individualType: 'json' }).then(res => {
        if (res.code == 0 || res.code == 200) {
          this.$message.success(res.message)
          this.searchData()
        } else {
          this.$message.error(res.message)
        }
        this.allotBLoading = false
      }).catch(e => {
        this.allotBLoading = false
        this.$message.error(e.message)
      });
    },
    handWork () {
      if (this.selection2.length != 1) {
        this.$message.warning("请先勾选一条且只能勾选一条任务信息数据");
        return;
      }
      this.searchAllot2()
      this.dialogTableVisible2 = true
    },
    //手动分配
    modeAllot () {
      if (this.selection5.length < 1) {
        this.$message.warning("请先勾选排班信息数据");
        return;
      }
      if (this.selection2.length != 1) {
        this.$message.warning("请先勾选任务信息");
        return;
      }
      let num = 0
      this.selection2.map(item => {
        num += Number(item.ruleItemValue)
      })
      if (this.selection5.length != num) {
        this.$message.warning('任务数量与人员数量不相等，请选择对应的人数')
        return
      }
      let time = this.getCode(this.classesCode, this.classesType, 'baseCode', 'memo').split('-') || []
      this.selection2[0].startTime = this.startDate
      this.selection2[0].endTime = this.wgoDate
      this.selection2[0].wgoDate = this.wgoDate ? this.wgoDate : ''
      // this.selection2[0].wrkOrdCode = this.classesCode
      this.selection2[0].matNo = this.selection2[0].matNameNo.join()
      let NameType = []
      NameType = [...this.selection2[0].matNameNo.map(items => this.getCode(items, this.matNameType, 'matNo', 'matName'))]
      this.selection2[0].matName = NameType.join()
      this.modeAllotBLoading = true
      this.axios.post('pass/testManagement/v1/limsempwrkords/allocation/', { llp: this.selection5, llo: this.selection2 }, { individualType: 'json' }).then(res => {
        if (res.code == 0 || res.code == 200) {
          this.$message.success(res.message)
          this.cancelData2()
          this.searchData()
        } else {
          this.$message.error(res.message)
        }
        this.modeAllotBLoading = false
      }).catch(e => {
        this.modeAllotBLoading = false
        this.$message.error(e.message)
      });
    },
    //点击选中任务分配信息表
    selectData (row) {
      this.clickData = row
    },
    // 重置
    reset () {
      this.search.deptNo = ''
      this.search.sectionNo = ''
      this.search.wrkGrpCode = ''
      this.searchData();
    },
    dataAllot () {
      this.clickAllot = {}
      this.allotLoading = true
      let obj = {
        object: {
          empNo: this.form.empNo,
          empName: this.form.empName,
          validFlag: 1
        },
        pageIndex: this.allotIndex,
        pageSize: this.allotPage
      }
      this.axios.post('pass/baseManagement/v1/limslrcempwrkgrps/queryLimsLrcEmpWrkgrpPage', obj, { individualType: 'json' }).then(res => {
        if (res.code == 0 || res.code == 200) {
          this.allotData = res.data.list;
          this.allotTotal = res.data.total
        } else {
          this.$message.error(res.message);
        }
        this.allotLoading = false
      }).catch(err => {
        this.allotLoading = false
        this.$message.error(err.message);
      })
    },
    dataAllot2 () {
      this.clickAllot = {}
      this.allotLoading = true
      let obj = {
        'object': {
          'deptNo': this.search.deptNo,
          'sectionNo': this.search.sectionNo,
          'wrkGrpCode': this.search.wrkGrpCode
        },
        // 'endTime':this.wgoDate,
        'pageIndex': this.allotIndex,
        'pageSize': this.allotPage
      }
      this.axios.post('pass/baseManagement/v1/limslrcemps/queryEmpPagesDS', obj, { individualType: 'json' }).then(res => {
        if (res.code == 0 || res.code == 200) {
          this.allotData = res.data.list;
          this.allotTotal = res.data.total
        } else {
          this.$message.error(res.message);
        }
        this.allotLoading = false
      }).catch(err => {
        this.allotLoading = false
        this.$message.error(err.message);
      })
    },
    //查人员信息
    searchAllot () {
      this.allotIndex = 1
      this.allotPage = 100
      this.dataAllot()
    },//查人员信息 2
    searchAllot2 () {
      this.allotIndex = 1
      this.allotPage = 100
      this.dataAllot2()
    },
    pageSizeAllot (size) {
      this.allotIndex = 1
      this.allotPage = size;
      this.dataAllot()
    },
    pageCurrentAllot (index) {
      this.allotIndex = index;
      this.dataAllot()
    },
    pageSizeChange (size) {
      this.page.pageNum = 1
      this.page.pageSize = size;
      this.mainData();
    },
    pageCurrentChange (index) {
      this.page.pageNum = index;
      this.mainData();
    },
    pageSizeChange2 (size) {
      this.page2.pageNum = 1
      this.page2.pageSize = size;
      this.firstData(this.savaSampleValue)
    },
    pageCurrentChange2 (index) {
      this.page2.pageNum = index;
      this.firstData(this.savaSampleValue)
    },
    pageSizeChange3 (size) {
      this.page3.pageNum = 1
      this.page3.pageSize = size;
      this.getAllotData();
    },
    pageCurrentChange3 (index) {
      this.page3.pageNum = index;
      this.getAllotData();
    },
    handleSelectionChange1 (val) {
      this.selection1 = val;
    },
    handleSelectionChange2 (val) {
      this.selection2 = val;
    }, handleSelectionChange5 (val) {
      this.selection5 = val;
    },
    addScreen () {
      this.$nextTick(() => {
        let ss = document.createElement('div');
        ss.innerHTML = '筛选';
        ss.style = 'text-align: center;'
        this.$refs.dataTable.$el.getElementsByClassName('el-table__fixed')[0].getElementsByTagName('td')[0].appendChild(ss);
      });
    },
    addScreen2 () {
      this.$nextTick(() => {
        let ss = document.createElement('div');
        ss.innerHTML = '筛选';
        ss.style = 'text-align: center;'
        this.$refs.dataTable2.$el.getElementsByClassName('el-table__fixed')[0].getElementsByTagName('td')[0].appendChild(ss);
      });
    },
    tableRowClassName3 ({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 4) {
        if (row.validFlag === '0') {
          return 'error-row';
        }
        return '';
      }
    },
    addScreen3 () {
      this.$nextTick(() => {
        let ss = document.createElement('div');
        ss.innerHTML = '筛选';
        ss.style = 'text-align: center;'
        this.$refs.dataTable3.$el.getElementsByClassName('el-table__fixed')[0].getElementsByTagName('td')[0].appendChild(ss);
      });
    },
    // 表格上横线
    tableHeaderCellStyle () {
      return 'color: rgb(255,255,255);border-bottom: 1px solid #DCDFE7;'
    },
    // 物料改变
    matChange (val, row) {
      try {
        if (val) {
          val.forEach(i => {
            let item = this.matNameType.find(item => item.matNo === i)
            if (item) {
              let index = this.selectMatList.findIndex(iten => iten.matNo === i)
              if (index === -1) {
                this.selectMatList.push({ matNo: i, matName: item.matName })
              }
            }
          })
        }
      } catch (e) {
        console.log(e)
      }
    },
    // 过滤物料选择项
    async filterMat (row) {
      // 将物料列表设置为前200条
      if (!row || !row.ruleNoD) {
        return true;
      }
      // 获取该任务已分配的物资
      await this.axios.post('pass/testManagement/v1/limsempwrkords/queryWrkordByRuleNoD', { testItemNo: row.ruleNoD, sectionNo: this.search.sectionNo }, { individualType: 'json' }).then(res => {
        if (res.code == 0 || res.code == 200) {
          if (res.data.length > 0 && res.data[0].matNo) {
            // 获取已分配物资编码list
            let matList = res.data[0].matNo.split(',');
            this.nowMatList = [...matList]
            // 获取已分配物资名称list
            let nameList = res.data[0].matName.split(',');
            let dataList = []
            // 获取已分配物料list
            for (let y = 0; y < matList.length; y++) {
              dataList.push({ matName: nameList[y], matNo: matList[y] })
            }
            console.log(dataList)
            this.matDataList = [...dataList];
            this.editNameList = [...dataList];
            // 查询出的已分配物料是否存在于查询出的物料名称列表，没有则添加进去
            this.editNameList.forEach(item => {
              let index = this.matNameType.findIndex(i => i.matNo === item.matNo)
              if (index === -1) {
                this.matNameType.push(item)
              }
            })
            row.matNameNo = matList
            // 记住当前下拉框所在行
            this.currentRow = row.index
          }
        } else {
          this.$message.error(res.message)
        }
      }).catch(err => {
        this.$message.error(err.message)
      })
    },
    // 物料名称筛选
    async matFilter (val, row) {
      // 如果当前下拉框所在行和之前记录的不是同一行，则清空已分配物资编码列表和物资列表
      if (row.index !== this.currentRow) {
        this.nowMatList = [];
        this.matDataList = [];
      }
      let searchStr = ''
      if (val) {
        searchStr = val
      }
      // 输入值不为空则查询
      this.searchFlag = false
      // 根据输入结果进行远端搜索
      const { data: res } = await this.axios.post("pass/baseManagement/v1/limsbasematerialss/queryLimsBaseMaterialsPage", { object: { validFlag: 1, matName: searchStr, matClassNo: '' }, pageIndex: 1, pageSize: 50 }, { individualType: 'json' })
      this.matNameType = [...res.list]
      // 拼接已经分配的几种物资
      this.matDataList.forEach(item => {
        let index = this.matNameType.findIndex(i => i.matNo === item.matNo)
        if (index === -1) {
          this.matNameType.push(item)
        }
      })
      // 加入已经选中的几种物料
      this.selectMatList.forEach(item => {
        let index = this.matNameType.findIndex(i => i.matNo === item.matNo)
        if (index === -1) {
          this.matNameType.push(item)
        }
      })
    },
    // 修改窗口物料名称筛选
    async matFilter1 (val, row) {
      let searchStr = ''
      if (val) {
        searchStr = val
      }
      // 根据输入内容查询物资数据列表
      const { data: res } = await this.axios.post("pass/baseManagement/v1/limsbasematerialss/queryLimsBaseMaterialsPage", { object: { validFlag: 1, matName: searchStr, matClassNo: '' }, pageIndex: 1, pageSize: 50 }, { individualType: 'json' })
      this.matNameType = [...res.list]
      // 拼接已经分配的几种物资(去重)
      this.editNameList.forEach(item => {
        let index = this.matNameType.findIndex(i => i.matNo === item.matNo)
        if (index === -1) {
          this.matNameType.push(item)
        }
      })
      // 加入已经选中的几种物料
      this.selectMatList.forEach(item => {
        let index = this.matNameType.findIndex(i => i.matNo === item.matNo)
        if (index === -1) {
          this.matNameType.push(item)
        }
      })
    },
    matAdd (nameList, matList, index) {
      nameList.forEach(item => {
        let iItem = this.allList.find(u => u.matNo.trim() === item)
        if (iItem) {
          if (matList.findIndex(i => i.matNo === item) === -1) {
            matList.push(iItem)
          }
        }
      })
      this.tableData2[index]
    }
  }
}
</script>
<style scoped>
</style>
