<template>
  <div>
    <el-card>
      <el-row type="flex">
        <el-col style="flex: 1" v-show="Dictionary">
          <el-col v-if="showSearch" style="min-width: 300px; max-width: 100%">
            <el-tree
              :data="treeList"
              class="tree"
              :default-expand-all="true"
              @node-click="handleNodeClick"
              :expand-on-click-node="false"
              highlight-current
            >
              <span class="custom-tree-node" slot-scope="{ node, data }">
                <span style="font-size: 14px; font-weight: 500">{{
                    node.label
                  }}</span>
                  <el-button
                    type="text"
                    size="mini"
                    style="margin-left: 10px"
                    v-if="data.parentCode == null"
                    @click="() => append(data,'add')"
                  >
                  新增
                </el-button>
                <el-button
                  type="text"
                  size="mini"
                  style="margin-left: 10px"
                  :disabled="data.parentCode == null"
                  @click="() => append(data, 'edit')"
                >
            编辑
          </el-button>
          <el-button
            type="text"
            size="mini"
            :disabled="data.parentCode == null"
            @click="delTree(data)"
          >
            删除
          </el-button>

              </span
              >
            </el-tree>
          </el-col>
        </el-col>

        <el-button
          type="success"
          @click="Dictionary = !Dictionary"
          icon="el-icon-arrow-left"
          v-show="Dictionary"
          class="iconButton"
        >
        </el-button>
        <el-button
          type="success"
          @click="Dictionary = !Dictionary"
          icon="el-icon-arrow-right"
          v-show="!Dictionary"
          class="iconButton"
        >
        </el-button>


        <el-col style="border-left: 3px solid #dddddd; overflow-y: hidden">
          <el-card>
            <el-form :model="query" label-width="auto" v-show="showSearch" size="mini" class="order-el-form">
              <el-row :gutter="20">
                <el-col :span="8">
                  <el-form-item label="当前部门:" class="query-form-item">
                    <deptSelect  style="width: 68%" :value.sync="query.deptCode"/>
                    <!--<deptSelect :value.sync="query.invCode" :params.sync="filterBadInv" :changeValue.sync="invChange"/>-->
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="编码/名称:" class="query-form-item">
                    <el-input v-model="query.key" placeholder="请输入编码/工位名称" clearable>
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="工位状态:" class="query-form-item">
                    <el-select v-model="query.workplaceStatus" placeholder="工位状态" clearable>
                      <el-option label="停用" :value="0"></el-option>
                      <el-option label="在用" :value="1"></el-option>
                    </el-select>
                  </el-form-item>

                </el-col>
              </el-row>
            </el-form>
            <div class="top-right-btn">
              <el-button-group>
                <el-button icon="el-icon-view" type="primary" @click="hideSearch">显示/隐藏搜索栏</el-button>
                <el-button type="primary" icon="el-icon-refresh" @click="onReset">重置</el-button>
                <el-button type="primary" icon="el-icon-search" @click="onSubmit">查询</el-button>
                <el-button type="primary" icon="el-icon-plus" @click="addWorkplace">新增工位</el-button>
              </el-button-group>
            </div>
            <el-table v-loading="loading" :data="list" style="width: 100%" :row-style="{height: '32px' }"
                      highlight-current-row @current-change="handCurrentChange"
            >
              <el-table-column type="index" label="序号"></el-table-column>
              <el-table-column label="工位编码" prop="workplaceId" width="90"></el-table-column>
              <el-table-column label="工位名称" prop="workplaceName" width="120"></el-table-column>
              <!--<el-table-column label="所属组别" prop="categoryName" width="90"></el-table-column>-->
              <!--<el-table-column label="单据类型编号" prop="busTypeCode" width="100"></el-table-column>-->
              <!--<el-table-column label="单据类型名称" prop="busTypeName" width="100"></el-table-column>-->
              <!--<el-table-column label="单据来源系统" prop="fromTypeName" width="100"></el-table-column>-->
              <el-table-column label="所属部门" prop="deptCodeName" width="90"></el-table-column>
              <!--<el-table-column label="默认仓库" prop="invName" width="90"></el-table-column>-->
              <el-table-column label="工位类型" prop="workplaceType" width="90">
                <template slot-scope="scope">
                  <span>{{ workplaceTypeMao[scope.row.workplaceType] }}</span>
                </template>
              </el-table-column>
              <el-table-column label="工位状态" prop="workplaceStatus" width="90">
                <template slot-scope="scope">
                  <span>{{ workplaceStatusMap[scope.row.workplaceStatus] }}</span>
                </template>
              </el-table-column>
              <el-table-column label="工位负责人" prop="employeeName" width="90"></el-table-column>
              <el-table-column label="库存预警数" prop="invRemindNumber" width="90"></el-table-column>
              <el-table-column label="创建时间" prop="createTime" width="140"></el-table-column>
              <el-table-column label="备注" prop="remake" width="140"></el-table-column>
              <el-table-column label="操作" fixed="right" width="120">
                <template slot-scope="scope">
                  <el-button
                    type="text"
                    size="small"
                    @click.native.stop="printLabel(scope.row)"
                  >打印
                  </el-button>

                  <el-button
                    type="text"
                    size="small"
                    @click.native.stop="edit(scope.row)"
                  >编辑
                  </el-button>
                  <el-button
                    type="text"
                    size="small"
                    @click.native.stop="deleteWorkplace(scope.row)"
                  >删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
            <pagination
              :total="total"
              :limit.sync="query.limit"
              :page.sync="query.page"
              @pagination="getList"
            >
            </pagination>
          </el-card>
          <!--<el-card>-->
          <el-tabs type="border-card" style="margin: 15px">
            <el-tab-pane>
              <span slot="label">{{ workplaceName }}-用户列表</span>
              <el-form :inline="true" :model="userQuery" class="query-form" size="mini"
                       style="margin-bottom: 10px"
              >
                <el-form-item class="query-form-item" label="用户账号/用户名称:">
                  <el-input
                    v-model="userQuery.userKey"
                    placeholder="请输入用户账号/名称"
                    clearable
                  ></el-input>
                </el-form-item>
                <el-form-item>
                  <el-button-group>
                    <el-button
                      type="primary"
                      icon="el-icon-refresh"
                      @click="onUserReset"
                    >重置
                    </el-button>
                    <el-button type="primary" @click="loadUserList"
                               icon="el-icon-search"
                    >查询
                    </el-button
                    >
                  </el-button-group>
                </el-form-item>
              </el-form>

              <el-table v-loading="userLoading" :data="userList" style="width: 100%;" :row-style="{height: '32px' }"
                        highlight-current-row
              >
                <el-table-column type="index" label="序号"></el-table-column>
                <el-table-column label="用户账号" prop="userName"></el-table-column>
                <el-table-column label="用户名称" prop="employeeName"></el-table-column>
                <el-table-column label="备注" prop="remark"></el-table-column>
                <el-table-column label="操作" fixed="right" width="50">
                  <template slot-scope="scope">
                    <el-button
                      type="text"
                      size="small"
                      @click.native.stop="removeUser(scope.row)"
                    >移除
                    </el-button>
                  </template>
                </el-table-column>
              </el-table>
              <pagination
                :total="userTotal"
                :limit.sync="userQuery.limit"
                :page.sync="userQuery.page"
                @pagination="getUserBindWork(userQuery)"
              >
              </pagination>
            </el-tab-pane>
            <el-tab-pane>
              <span slot="label">{{ workplaceName }}-业务类型</span>
              <el-form :inline="true" :model="busQuery" class="query-form" size="mini"
                       style="margin-bottom: 10px"
              >
                <el-form-item class="query-form-item" label="业务类型编号/业务名称:">
                  <el-input
                    v-model="busQuery.busKey"
                    placeholder="请输入业务类型编号/业务名称"
                    clearable
                  ></el-input>
                </el-form-item>
                <el-form-item>
                  <el-button-group>
                    <el-button
                      type="primary"
                      icon="el-icon-refresh"
                      @click="onBusTypeReset"
                    >重置
                    </el-button>
                    <el-button type="primary" @click="loadBusList"
                               icon="el-icon-search"
                    >查询
                    </el-button
                    >
                    <el-button type="primary" @click="chooseBusType"
                               icon="el-icon-plus"
                    >新增业务类型
                    </el-button
                    >
                  </el-button-group>
                </el-form-item>
              </el-form>
              <el-table :data="busTypeList" style="width: 100%;" :row-style="{height: '32px' }"
                        highlight-current-row
              >
                <el-table-column type="index" label="序号"></el-table-column>
                <el-table-column label="业务类型编码 " prop="documentTypeCode"></el-table-column>
                <el-table-column label="业务名称 " prop="busName"></el-table-column>
                <el-table-column label="仓库名称 " prop="invName"></el-table-column>
                <el-table-column label="是否配套系统" prop="isMatching">
                  <template slot-scope="scope">
                    <span>{{ isMatchings[scope.row.isMatching] }}</span>
                  </template>
                </el-table-column>
                <el-table-column label="扫码方式" prop="scanType">
                    <template slot-scope="scope">
                      <span>{{ operationTypes[scope.row.scanType] }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="处理结果 " prop="checkInsert">
                  <template slot-scope="scope">
                    <span>{{ checkInserts[scope.row.checkInsert] }}</span>
                  </template>
                </el-table-column>
                <el-table-column label="异常方式提醒" prop="warnType">
                  <template slot-scope="scope">
                    <span>{{ warnTypeMap[scope.row.warnType] }}</span>
                  </template>
                </el-table-column>
                <el-table-column label="往来单位类型" prop="corpType">
                    <template slot-scope="scope">
                      <span>{{ corpTypeMap[scope.row.corpType] }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="往来类型标题" prop="unitTittle"></el-table-column>
                <el-table-column label="操作" fixed="right" width="120">
                  <template slot-scope="scope">
                    <el-button
                      type="text"
                      size="small"
                      @click.native.stop="updateBusType(scope.row)"
                    >编辑
                    </el-button>
                    <el-button
                      type="text"
                      size="small"
                      @click.native.stop="removeBusType(scope.row)"
                    >移除
                    </el-button>
                  </template>
                </el-table-column>
              </el-table>
              <pagination
                :total="busTypeTotal"
                :limit.sync="busQuery.limit"
                :page.sync="busQuery.page"
                @pagination="getWorkBindBusTypes()"
              >
              </pagination>
            </el-tab-pane>
          </el-tabs>

        </el-col>

      </el-row>

    </el-card>
    <el-dialog
      :title="fromMap[fromName]"
      :visible.sync="addWorkplaceDialogVisible"
      width="60%"
      v-if="addWorkplaceDialogVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <el-form :model="formData" style="width: 100%;" ref="dataForm" :rules="formRules"
               label-width="auto"
      >
        <el-row :gutter="24" class="el-row">
          <el-col :span="12" class="el-col">
            <el-form-item label="工位编码:" prop="workplaceId" class="query-form-item">
              <el-input
                disabled
                v-model="formData.workplaceId" style="width: 80%"
                auto-complete="off"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="el-col">
            <el-form-item label="工位名称:" prop="workplaceName" class="query-form-item">
              <el-input
                v-model="formData.workplaceName" style="width: 80%"
                auto-complete="off"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="24" class="el-row">
          <el-col :span="12" class="el-col">
            <el-form-item label="作业方式:" prop="operationType" class="query-form-item">
              <el-select v-model="formData.operationType" style="width: 80%" placeholder="选择扫码方式" clearable
                         @change="changeCheckInsert"
              >
                <el-option label="扫码生成业务单" :value="1"></el-option>
                <el-option label="按单校验三期" :value="2"></el-option>
                <el-option label="按单不校验三期" :value="3"></el-option>
              </el-select>
            </el-form-item>
          </el-col>


          <el-col :span="12" class="el-col">
            <el-form-item label="结果处理:" prop="checkInsert" class="query-form-item">
              <el-select v-model="formData.checkInsert" style="width: 80%" placeholder="选择扫码方式" clearable
                         :disabled="formData.operationType == 1"
              >
                <el-option label="自动添加" :value="1"></el-option>
                <el-option label="自动不添加" :value="2"></el-option>
                <el-option label="根据提醒决定" :value="3"></el-option>
              </el-select>
            </el-form-item>
          </el-col>


          <el-col :span="12" class="el-col">
            <el-form-item label="异常提醒方式:" prop="warnType" class="query-form-item">
              <el-select v-model="formData.warnType" style="width: 80%" placeholder="选择扫码方式" clearable>
                <el-option label="信息提醒" :value="1"></el-option>
                <el-option label="信息弹框提醒" :value="2"></el-option>
                <el-option label="不提醒" :value="3"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item prop="deptCode" label="所属部门:">
              <el-select v-model="formData.deptCode" placeholder="请选择部门" clearable
                         style="width: 80%"
                         @change="handleInvChange"
              >
                <el-option
                  v-for="item in deptList"
                  :key="item.name"
                  :label="item.name"
                  :value="item.code"
                >
                  <span style="float: left">{{ item.name }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <!--<el-col :span="12" class="el-col">-->
          <!--  <el-form-item label="默认仓库:" prop="invCode" class="query-form-item">-->
          <!--    <el-select v-model="formData.invCode" placeholder="请选择仓库"-->
          <!--               style="width: 80%"-->
          <!--               clearable-->
          <!--               @change="handleInvChange"-->
          <!--    >-->
          <!--      <el-option-->
          <!--        v-for="item in invList"-->
          <!--        :key="item.name"-->
          <!--        :label="item.name"-->
          <!--        :value="item.code"-->
          <!--      >-->
          <!--        <span style="float: left">{{ item.name }}</span>-->
          <!--      </el-option>-->
          <!--    </el-select>-->
          <!--  </el-form-item>-->
          <!--</el-col>-->
          <el-col :span="12" class="el-col">
            <el-form-item label="工位负责人:" prop="chargeUser" class="query-form-item">
              <el-select v-model="formData.chargeUser" placeholder="请选择负责人"
                         style="width: 80%"
                         clearable
              >
                <el-option
                  v-for="item in users"
                  :key="item.id"
                  :label="item.employeeName"
                  :value="item.id"
                >
                  <span style="float: left">{{ item.employeeName }}</span>
                </el-option>
              </el-select>
              <!--<el-input-->
              <!--  v-model="formData.chargeUser" style="width: 80%"-->
              <!--  auto-complete="off"-->
              <!--&gt;</el-input>-->
            </el-form-item>
          </el-col>
          <!--<el-col :span="12" class="el-col">-->
          <!--  <el-form-item label="单据类型:" prop="orderId" class="query-form-item">-->
          <!--    <el-select v-model="formData.orderId" placeholder="请选择单据类型"-->
          <!--               style="width: 80%"-->
          <!--               clearable-->
          <!--    >-->
          <!--      <el-option-->
          <!--        v-for="item in busTypes"-->
          <!--        :key="item.name"-->
          <!--        :label="item.name"-->
          <!--        :value="item.code"-->
          <!--      >-->
          <!--        <span style="float: left">{{ item.name }}</span>-->
          <!--      </el-option>-->
          <!--    </el-select>-->
          <!--  </el-form-item>-->
          <!--</el-col>-->
          <el-col :span="12" class="el-col">
            <el-form-item label="所属组别:" prop="constituencies" class="query-form-item">
              <el-select v-model="formData.constituencies" placeholder="请选择所属组别"
                         style="width: 80%"
                         clearable
              >
                <el-option
                  v-for="item in fromDeptOptions"
                  :key="item.code"
                  :label="item.label"
                  :value="item.code"
                >
                  <span style="float: left">{{ item.label }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <!--<el-col :span="12" class="el-col">-->
          <!--  <el-form-item label="往来单位类型:" prop="corpType" class="query-form-item">-->
          <!--    <el-select v-model="formData.corpType" style="width: 80%" placeholder="选择往来单位类型" clearable>-->
          <!--      <el-option label="客户" :value="1"></el-option>-->
          <!--      <el-option label="供应商" :value="2"></el-option>-->
          <!--      <el-option label="内部科室" :value="3"></el-option>-->
          <!--      <el-option label="特殊往来" :value="4"></el-option>-->
          <!--    </el-select>-->
          <!--  </el-form-item>-->
          <!--</el-col>-->


          <!--<el-col :span="12" class="el-col">-->
          <!--  <el-form-item label="往来类型标题:" prop="unitTittle" class="query-form-item">-->
          <!--    <el-input-->
          <!--      v-model="formData.unitTittle" style="width: 80%"-->
          <!--      auto-complete="off"-->
          <!--    ></el-input>-->
          <!--  </el-form-item>-->
          <!--</el-col>-->
          <el-col :span="12" class="el-col">
            <el-form-item label="库存预警数:" prop="invRemindNumber" class="query-form-item">
              <el-input-number
                :min="0"
                :precision="0" :step="1"
                placeholder="请输入库存预警数"
                v-model="formData.invRemindNumber" style="width: 80%"
                auto-complete="off"
              ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="el-col">
            <el-form-item label="工位状态:" prop="workplaceStatus" class="query-form-item">
              <el-select v-model="formData.workplaceStatus" style="width: 80%" placeholder="工位状态" clearable>
                <el-option label="暂停" :value="0"></el-option>
                <el-option label="启用" :value="1"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="el-col">
            <el-form-item label="工位类型:" prop="workplaceType" class="query-form-item">
              <el-select v-model="formData.workplaceType" style="width: 80%" placeholder="工位状态" clearable>
                <el-option label="拆零" :value="1"></el-option>
                <el-option label="取整" :value="2"></el-option>
                <el-option label="全部" :value="3"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <!--<el-col :span="12" class="el-col">-->
        <!--  <el-form-item label="往来单位类型:" prop="corpType" class="query-form-item">-->
        <!--    <el-select v-model="formData.corpType" style="width: 80%" placeholder="选择往来单位类型" clearable>-->
        <!--      <el-option label="客户" :value="1"></el-option>-->
        <!--      <el-option label="供应商" :value="2"></el-option>-->
        <!--      <el-option label="内部科室" :value="3"></el-option>-->
        <!--      <el-option label="特殊往来" :value="4"></el-option>-->
        <!--    </el-select>-->
        <!--  </el-form-item>-->
        <!--</el-col>-->
        <el-row :gutter="24" class="el-row">
          <el-col :span="22" class="el-col">
            <el-form-item label="备注:" prop="remake" class="query-form-item">
              <el-input
                v-model="formData.remake" style="width: 100%"
                auto-complete="off"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="hideForm">取消</el-button>
        <el-button
          type="primary"
          @click.native="formSubmit()"
        >提交
        </el-button
        >
      </div>
    </el-dialog>


    <el-dialog
      :title="formMap[formName]"
      append-to-body
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="subFormVisible"
      width="60%"
      top="5vh"
    >
      <el-form :model="subData" ref="dataForm" label-width="80px">
        <el-row :gutter="20" class="el-row" type="flex">
          <el-col :span="12" class="el-col">
            <el-form-item label="上级菜单" prop="parentCode">
              <el-select v-model="subData.parentCode" placeholder="选择上级菜单"
                         style="width: 80%"
                         clearable
                         disabled
              >
                <el-option
                  v-for="item in fromDeptOptions"
                  :key="item.parentCode"
                  :label="item.parentName"
                  :value="item.parentCode"
                >
                  <span style="float: left">{{ item.parentName }}</span>
                </el-option>
              </el-select>

            </el-form-item>
          </el-col>
          <el-col :span="12" class="el-col">
            <el-form-item label="名称" prop="name" class="query-form-item">
              <el-input
                v-model="subData.name" style="width: 80%"
                placeholder="请输入名称"
                auto-complete="off"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row class="el-row" type="flex">
          <el-col class="el-col">
            <el-form-item label="备注:" prop="remark" class="query-form-item">
              <el-input
                v-model="subData.remark"
                type="textarea"
                style="width: 90%"
                auto-complete="off"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="subFormVisible = false">取消</el-button>
        <el-button
          type="primary"
          @click.native="forInvSubmit()"
          :loading="subLoading"
        >提交
        </el-button
        >
      </div>
    </el-dialog>

    <el-dialog
      :title="busTypeMap[busName]"
      :visible.sync="chooseBusTypeDialogVisible"
      width="60%"
      v-if="chooseBusTypeDialogVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <!--<selectBusType-->
      <!--  :closeDialog="closeDialog"-->
      <!--  :workplaceId="workplaceId"-->
      <!--  :busTypeCodes="busTypeCodes"-->
      <!--&gt;-->

      <!--</selectBusType>-->
      <addBusTypeDialog
        :workplaceId="workplaceId"
        :busData="busData"
        :busName="busName"
        :upData="upData"
        :closeDialog="closeDialog"
      >

      </addBusTypeDialog>

    </el-dialog>
  </div>
</template>

<script>
import {
  listPage,
  addWorkplace,
  updateWorkplace,
  deleteWorkplace,
  createWorkplaceId, printWorkLabel, getUsers
} from '@/api/basic/workPlace/sysWorkplaceManage'
import { filterAllByUser, getInvListByUser } from '@/api/system/invWarehouse'
import { addTree, deleteTree, getTree, updateTree } from '@/api/basic/collectPoint/collectPointManage'
import { getCollectBusType } from '@/api/basic/collectPoint/gatherOrderType'
import panelGroup from '@/views/dashboard/PanelGroup'
import { delUserBindWork, getUserBindWork } from '@/api/basic/collectPoint/userWorkplace'
import selectBusType from './selectBusType'
import addBusTypeDialog from './addBusTypeDialog'
import { getWorkBindBusTypes,removeBusTypeById } from '@/api/basic/workPlace/sysWorkplaceDocuments'

export default {
  components: { selectBusType,addBusTypeDialog },
  data() {
    return {
      query: {
        page: 1,
        limit: 10,
        code: null,
        workplaceStatus: 1,
        deptCode: ''
      },
      showSearch: true,
      Dictionary: false,
      loading: false,
      list: [],
      treeList: [
        {
          code: 1001,
          parentCode: null,
          label: '全部'
        }
      ],
      addWorkplaceDialogVisible: false,
      chooseBusTypeDialogVisible: false,
      fromName: 'add',
      fromMap: {
        'add': '新增工位',
        'edit': '编辑工位'
      },
      busName: '',
      busTypeMap: {
        'add': '新增业务类型',
        'edit': '编辑业务类型'
      },
      formData: {
        operationType: 1,
        deptCode: null
      },
      workplaceCode: '6001',
      unitStorageList: [],
      invList: [],
      fromDeptOptions: [],
      filterBadInv: true,
      formRules: {
        workplaceName: [
          { required: true, message: '请输入工位名称', trigger: 'blur' }
        ],
        operationType: [
          { required: true, message: '请选择作业方式', trigger: 'blur' }
        ],
        checkInsert: [
          { required: true, message: '处理结果不能为空', trigger: 'blur' }
        ],
        invRemindNumber: [
          { required: true, message: '请输入工位存量提醒数', trigger: 'blur' }
        ],
        warnType: [
          { required: true, message: '请选择提醒方式', trigger: 'blur' }
        ],
        corpType: [
          { required: true, message: '请选择往来单位类型', trigger: 'blur' }
        ],
        orderId: [
          { required: true, message: '请选择单据类型', trigger: 'blur' }
        ],
        unitTittle: [
          { required: true, message: '请输入往来类型标题', trigger: 'blur' }
        ],
        workplaceStatus: [
          { required: true, message: '请选择工位状态', trigger: 'blur' }
        ],
        workplaceType: [
          { required: true, message: '请选择工位类型', trigger: 'blur' }
        ]
      },
      invQuery: {
        invCode: '',
        deptCode: ''
      },
      users: [],
      busTypes: [],
      isMatchings: {
        0: '否',
        1: '是'
      },
      operationTypes: {
        1: '扫码生成业务单',
        2: '按单校验三期',
        3: '按单不校验三期'
      },
      corpTypeMap: {
        1: '客户',
        2: '供应商',
        3: '内部科室',
        4: '特殊往来'
      },
      checkInserts: {
        1: '自动添加',
        2: '自动不添加',
        3: '根据提醒决定'
      },
      workplaceStatusMap: {
        0: '暂停',
        1: '启用'
      },
      workplaceTypeMao: {
        1: '拆零',
        2: '取整',
        3: '全部'
      },
      warnTypeMap: {
        1: '信息提醒',
        2: '信息弹框提醒',
        3: '不提醒'
      },
      userList: [],
      userLoading: false,
      rowData: {
        workplaceName: '工位'
      },
      userQuery: {
        page: 1,
        limit: 10,
        userKey: null,
        workplaceCode: null
      },
      formName: 'add',
      formMap: {
        add: '新增',
        edit: '编辑'
      },
      subFormVisible: false,
      subData: {},
      groupData: null,
      total: 0,
      userTotal: 0,
      workplaceName: '用户',
      workplaceBusType: '工位',
      deptList: [],
      //单据类型数据
      workplaceId: null,
      busQuery: {
        busKey: '',
        workplaceCode: null,
        page: 1,
        limit: 10
      },
      busTypeList: [],
      busTypeTotal: 0,
      busTypeCodes: [],
      busData: null,
      upData: null
    }
  },
  computed: {},
  methods: {
    hideSearch() {
      this.showSearch = !this.showSearch
    },
    onSubmit() {
      this.userList = []
      this.workplaceName = '工位'
      this.query.page = 1
      this.getList()
    },
    onReset() {
      this.$router.push({
        path: ''
      })
      this.query = {
        page: 1,
        limit: 20,
        workplaceStatus: null
      }
      this.getList()
    },
    getList() {
      listPage(this.query).then(res => {
        if (res.code != 20000) {
          return this.$message.error(res.message)
        }
        this.list = res.data.list || []
        this.total = res.data.total || 0
      })
    },
    addWorkplace() {
      this.fromName = 'add'
      this.getDeptList()
      createWorkplaceId().then(res => {
        if (res.code == 20000) {
          if (res.data == null) {
            this.formData.workplaceId = this.workplaceCode + 1001
            this.formData.operationType = 1
            this.formData.checkInsert = 1
            this.formData.workplaceStatus = 1
            this.addWorkplaceDialogVisible = true
            this.getTree()
          } else {
            this.formData = {
              operationType: 1,
              checkInsert: 1,
              workplaceStatus: 1
            }
            this.getTree()
            this.formData.workplaceId = res.data
            this.addWorkplaceDialogVisible = true
          }
        } else {
          this.$message.error('获取错误')
        }
      }).catch(() => {
        this.$message.error('获取错误')
      })
    },
    hideForm() {
      this.addWorkplaceDialogVisible = false
    },
    invChange(invCode) {
      this.filterQuery.invCode = invCode
      this.getFromStorage()
    },
    getFromStorage() {
      let query = {}
      filterFromAllByUser(query)
        .then((response) => {
          this.unitStorageList = response.data || []
          console.log(this.filterQuery.invCode)
          if (this.filterQuery.invCode) {
            this.unitStorageList = this.unitStorageList.filter(item => item.code != this.filterQuery.invCode) || []
          }
          console.log(this.unitStorageList)
        })
        .catch(() => {
        })
    },
    formSubmit() {
      // this.formData.constituencies = this.groupData.code
      if (this.fromName == 'add') {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            addWorkplace(this.formData).then(res => {
              if (res.code != 20000) {
                return this.$message.error('新增错误')
              }
              this.addWorkplaceDialogVisible = false
              this.$message.success('新增成功')
              // this.query.code = this.groupData.code
              // if (this.query.code == 1001) {
              //   this.query.code = null
              // }
              this.getList()
            }).catch(() => {

            })
          } else {
            // this.$message.error('请完善必填信息')
          }
        })
      } else {
        updateWorkplace(this.formData).then(res => {
          if (res.code != 20000) {
            return this.$message.error('更新错误')
          }
          this.addWorkplaceDialogVisible = false
          this.$message.success(res.data)
          this.getList()
        }).catch(() => {
          this.addWorkplaceDialogVisible = false
          this.$message.error('更新错误')
        })
      }

    },
    edit(row) {
      this.formData = row
      // this.formData.invCode = String(row.invCode)
      if (row.chargeUser == 0){
        this.formData.chargeUser = null
      }else if(row.chargeUser == 1){
        this.formData.chargeUser = Number(row.chargeUser)
      }else {
        this.formData.chargeUser = row.chargeUser
      }
      this.fromName = 'edit'
      this.invQuery.deptCode = row.deptCode
      getUsers(this.invQuery).then(res => {
        if (res.code == 20000) {
          this.users = res.data.list || []
        } else {
          this.$message.error('错误')
        }
      })
      this.addWorkplaceDialogVisible = true
    },
    deleteWorkplace(row) {
      this.$confirm('此操作将永久删除该工位, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteWorkplace(row).then(res => {
          if (res.code != 20000) {
            this.$message.error(res.message)
          } else {
            this.$message.success(res.data)
            this.getList()
          }
        })
      }).catch(() => {

      })
    },
    getInvList() {
      let query = {
        advanceType: 1,
        deptCode: this.formData.deptCode
      }
      getInvListByUser(query)
        .then((response) => {
          this.invList = response.data || []
        })
        .catch(() => {
        })
    },
    handleInvChange(deptCode) {
      this.invQuery.deptCode = deptCode
      this.formData.chargeUser  = null
      getUsers(this.invQuery).then(res => {
        if (res.code == 20000) {
          this.users = res.data.list || []
        } else {
          this.$message.error('错误')
        }
      })
    },
    printLabel(row) {
      row.labelId = 6
      printWorkLabel(row).then((response) => {
        const binaryData = []
        binaryData.push(response)
        let url = window.URL.createObjectURL(
          new Blob(binaryData, { type: 'application/pdf' })
        )
        this.loading = false
        window.open(url)
      }).catch(() => {
        this.loading = false
      })
    },
    getLstTree() {
      getTree(this.query)
        .then((response) => {
          var invlist = response.data || []
          if (invlist.length == 0) {
            this.treeList = this.handleTree(this.treeList, 'code', 'parentCode')
          } else {
            this.treeList = this.handleTree(invlist, 'code', 'parentCode')
          }
        })
        .catch(() => {
        })
    },
    getBusType() {
      let query = {
        page: 1,
        limit: 10
      }
      getCollectBusType(query)
        .then((response) => {
          this.busTypes = response.data.list || []
          // this.filterQuery.action = this.busTypes[0].action;
          // this.getList();
        })
        .catch(() => {
        })
    },
    getTree() {
      getTree(this.query)
        .then((response) => {
          var treeList = response.data || []
          const filteredList = treeList.filter(item => item.parentCode != null)
          this.fromDeptOptions = filteredList
        })
        .catch(() => {
        })
    },
    handCurrentChange(row) {
      this.rowData = row
      this.workplaceName = row.workplaceName
      this.userQuery.workplaceCode = row.workplaceId
      this.getUserBindWork(this.userQuery)
      this.getWorkBindBusTypes()
    },
    loadUserList() {
      this.userQuery.workplaceCode = this.rowData.workplaceId
      this.getUserBindWork(this.userQuery)
    },
    onBusTypeReset(){
      this.busQuery= {
        busKey: '',
        workplaceCode: null,
        page: 1,
        limit: 10
      },
      this.getWorkBindBusTypes()
    },
    loadBusList() {
      this.getWorkBindBusTypes()
    },
    chooseBusType() {
      if (this.rowData.workplaceId == null) {
        return this.$message.error('请先选择工位')
      }
      this.busName = 'add'
      this.workplaceId = this.rowData.workplaceId
      this.busData = this.rowData
      this.userId = this.rowData.id
      this.busTypeCodes = this.busTypeList.map(item => item.code)
      this.upData = null
      this.chooseBusTypeDialogVisible = true
    },
    onUserReset() {
      this.userQuery = {
        page: 1,
        limit: 10,
        userKey: null,
        workplaceCode: this.rowData.workplaceId
      }
      this.getUserBindWork(this.userQuery)
    },
    getUserBindWork(query) {
      getUserBindWork(query).then(res => {
        if (res.code == 20000) {
          this.userList = res.data.list || []
          this.userTotal = res.data.total || 0
        } else {
          this.$message.error('获取失败')
        }
      })
    },
    removeUser(row) {
      this.$confirm('确定移除该用户？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let query = {
          id: row.id
        }
        delUserBindWork(query).then(res => {
          if (res.code == 20000) {
            this.userQuery.workplaceCode = this.rowData.workplaceId
            this.getUserBindWork(this.userQuery)
            this.$message.success('移除成功')
          } else {
            this.$message.error(res.message)
          }
        })
      }).catch(() => {

      })
    },
    handleNodeClick(row) {
      this.groupData = row
      this.query.code = row.code
      if (this.query.code == 1001) {
        this.query.code = null
      }
      this.getList()
    },
    append(data, formName) {//新增 编辑
      this.formName = formName
      if (formName === 'edit') {
        this.subData = JSON.parse(JSON.stringify(data))
        this.subData.name = JSON.parse(JSON.stringify(data.label))
      } else if (formName === 'add') {
        this.subData = {
          parentCode: JSON.parse(JSON.stringify(data)).code
        }
        let parentData = {
          parentCode: JSON.parse(JSON.stringify(data)).code,
          parentName: JSON.parse(JSON.stringify(data.label))
        }
        this.fromDeptOptions.push(parentData)
      }
      this.subFormVisible = true
    },
    delTree(row) {
      deleteTree(row).then(res => {
        if (res.code == 20000) {
          this.$message.success('删除成功')
          this.getLstTree()
        } else {
          this.$message.error('删除失败')
        }
      })
    },
    forInvSubmit() {
      if (this.formName === 'edit') {
        updateTree(this.subData).then(res => {
          if (res.code == 20000) {
            this.subFormVisible = false
            this.getLstTree()
            this.$message.success('成功')
          } else {
            this.$message.error(res.message)
          }
        }).catch(() => {

        })
      } else {
        addTree(this.subData).then(res => {
          if (res.code == 20000) {
            this.subFormVisible = false
            this.getLstTree()
            this.$message.success('成功')
          } else {
            this.$message.error(res.message)
          }
        }).catch(() => {

        })
      }

    },
    getDeptList() {
      this.deptList = []
      filterAllByUser()
        .then((response) => {
          this.deptList = response.data || []
          this.getInvList()
        })
        .catch(() => {
        })
    },
    deptChange() {
      this.getInvList()
      this.formData.invCode = ''
    },
    changeCheckInsert() {
      if (this.formData.operationType == 1) {
        this.formData.checkInsert = 1
      }
    },
    getWorkBindBusTypes() {
      this.busQuery.workplaceCode = this.rowData.workplaceId
      getWorkBindBusTypes(this.busQuery).then(res => {
        if (res.code == 20000) {
          this.busTypeList = res.data.list
          this.busTypeTotal = res.data.total
        } else {
          return this.$message.error('获取错误')
        }
      })
    },
    removeBusType(row) {
      this.$confirm('此操作将永久移除该单据类型, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        removeBusTypeById(row).then(res => {
          if (res.code == 20000) {
            this.$message.success('移除成功')
            this.getWorkBindBusTypes()
          }else {
            this.$message.error("移除失败")
          }
        })
      }).catch(() => {
      })

    },
    updateBusType(row){
      this.upData = row
      this.busData = this.rowData
      this.busName = 'edit'
      this.chooseBusTypeDialogVisible = true

    },
    closeDialog() {
      this.chooseBusTypeDialogVisible = false
      this.getWorkBindBusTypes()
    }
  },
  created() {
    this.getList()
    // this.getInvList()
    this.getBusType()
    this.getTree()
    this.getLstTree()
    this.getDeptList()
  }
}
</script>

<style scoped>
.iconButton {
  margin-left: 0px;
  height: 30px;
}
</style>
