<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-row>
        <el-form-item label="工号" prop="userName">
          <el-input
            v-model="queryParams.userName"
            placeholder="请输入工号"
            clearable
            @lotoup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="姓名" prop="nickName">
          <el-input
            v-model="queryParams.nickName"
            placeholder="请输入姓名"
            clearable
            @lotoup.enter.native="handleQuery"
          />
        </el-form-item>

        <el-form-item style="margin-left: 20px">
          <el-button v-no-more-click
                     type="primary"
                     icon="el-icon-search"
                     size="mini"
                     @click="handleQuery"
          >搜索
          </el-button>
          <el-button v-no-more-click icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置
          </el-button>
        </el-form-item>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-no-more-click
                   type="primary"
                   plain
                   icon="el-icon-plus"
                   size="mini"
                   @click="handleAdd"
                   v-hasPermi="['iscs:blacklist:add']"
        >新增
        </el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button v-no-more-click
                   type="danger"
                   plain
                   icon="el-icon-delete"
                   size="mini"
                   :disabled="multiple"
                   @click="handleDelete"
                   v-hasPermi="['iscs:blacklist:remove']"
        >批量删除
        </el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="repairList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column
        label="用户编号"
        width="180px"
        align="center"
        prop="userId"
      />
      <el-table-column
        label="工号"
        align="center"
        prop="userName"
      />
      <el-table-column
        label="姓名"
        align="center"
        prop="nickName"
      />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button v-no-more-click
                     size="mini"
                     type="text"
                     icon="el-icon-delete"
                     @click="handleDelete(scope.row)"
                     v-hasPermi="['iscs:blacklist:remove']"
          >删除
          </el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

    <!-- 添加或修改设备维修单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1300px" append-to-body>
      <div>
        <el-row :gutter="24">
          <el-col :span="11" style="margin-left: 20px">
            <!-- 穿梭框左边 -->
            <el-input v-model="searchQueryLeft" placeholder="请输入姓名" @input="searchQueryLeftInput" style="margin-bottom: 5px"></el-input>
            <el-table
              ref="multipleTable"
              :data="tableData1"
              height="486"
              tooltip-effect="dark"
              style="width: 100%"
              @select="checkLeft"
              @select-all="checkAll"
              class="el-table-dialog"
              row-key="id"
            >
              <el-table-column
                type="selection"
                width="55"
                align="center"
              ></el-table-column>
              <el-table-column label="用户编号" align="center">
                <template slot-scope="{ row }">
                  <span>{{ row.userId }}</span>
                </template>
              </el-table-column>
              <el-table-column label="姓名" align="center">
                <template slot-scope="{ row }">
                  <span>{{ row.nickName }}</span>
                </template>
              </el-table-column>
              <el-table-column label="登录名" align="center">
                <template slot-scope="{ row }">
                  <span>{{ row.userName }}</span>
                </template>
              </el-table-column>
            </el-table>
            <pagination v-show="totalLeft>0" :total="totalLeft" :page.sync="currentLeft" :limit.sync="sizeLeft" @pagination="getOtherList"/>
          </el-col>
          <div style="width: 56px;height:100%;float: left">
            <el-button
              style="margin-top: 25vh"
              @click="right"
              type="primary"
              :disabled="nowSelectData.length ? false : true"
              icon="el-icon-arrow-right"
            ></el-button
            >
            <br>

            <el-button
              @click="left"
              type="primary"
              :disabled="nowSelectRightData.length ? false : true"
              icon="el-icon-arrow-left"
              style="margin-left: 0;margin-top: 10px;"
            ></el-button
            >
          </div>
          <el-col :span="11">
            <!-- 穿梭框右边 -->
            <el-input v-model="searchQueryRight" placeholder="请输入姓名" @input="searchQueryRightInput" style="margin-bottom: 5px"></el-input>
            <el-table
              ref="multipleTable"
              :data="tableData2"
              height="488"
              tooltip-effect="dark"
              style="width: 100%"
              @select="checkRight"
              @select-all="checkRightAll"
              row-key="id"
              class="el-table-dialog"
            >
              <el-table-column
                type="selection"
                width="55"
                align="center"
              ></el-table-column>
              <el-table-column label="用户编号" align="center">
                <template slot-scope="{ row }">
                  <span>{{ row.userId }}</span>
                </template>
              </el-table-column>
              <el-table-column label="类型" align="center">
                <template slot-scope="{ row }">
                  <span>{{ row.nickName }}</span>
                </template>
              </el-table-column>
              <el-table-column label="登录名" align="center">
                <template slot-scope="{ row }">
                  <span>{{ row.userName }}</span>
                </template>
              </el-table-column>
            </el-table>
          </el-col>
        </el-row>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button v-no-more-click type="primary" @click="submitForm">确 定</el-button>
        <el-button v-no-more-click @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBlacklist, addBlacklist, delBlacklist,listWhitelist } from '@/api/mes/blacklist'
export default {
  name: 'lock',
  dicts: ['hardware_status'],
  data() {
    return {
      autoGenFlag: false,
      optType: undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      codes:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备维修单表格数据
      repairList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      createTime: '',
      queryParams: {
        current: 1,
        size: 10,
        nickName: undefined,
        userName: undefined,
      },
      // 表单参数
      form: {},
      // 日期选择
      tableData1: [],
      tableData2: [],
      originalTableData2:[],//右侧原始数据
      nowSelectData: [], // 左边选中列表数据
      nowSelectRightData: [], // 右边选中列表数据
      searchQueryLeft:'',//左边查询
      searchQueryRight:'',//右边查询
      currentLeft: 1,
      sizeLeft: 10,
      totalLeft:0
    }
  },

  created() {
    this.getList()
    this.getOtherList()
  },
  methods: {
    /** 查询设备维修单列表 */
    getList() {
      this.loading = true
      // 锁定站数据
      listBlacklist(this.queryParams).then((response) => {
        console.log(response, '锁定站')
        this.repairList = response.data.records
        this.total = response.data.total
        this.loading = false
      })
    },
    getOtherList() {
      const data = {
        ...this.queryParams,
        current: this.currentLeft,
        size: this.sizeLeft,
      }
      listWhitelist(data).then((respone) => {
        console.log(respone,'白名单')
        this.tableData1 = respone.data.records
        this.totalLeft = respone.data.total
      })
    },

    searchQueryLeftInput(value){
      console.log(value,'查询的接口')
      this.queryParams.nickName = value
      this.getOtherList()
    },
    searchQueryRightInput(value) {
      console.log(value, '右侧数据筛选');
      if (!this.originalTableData2.length) {
        this.originalTableData2 = [...this.tableData2]; // 保存原始数据
      }
      if (value) {
        this.tableData2 = this.originalTableData2.filter(item => {
          return item.nickName.includes(value);
        });
      } else {
        this.tableData2 = [...this.originalTableData2]; // 恢复原始数据
      }
      console.log(this.tableData2, '过滤筛选');
    },

    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        nickName: null,
        userId: null,
      }

      this.autoGenFlag = false
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.nickName = ''
      this.queryParams.userId = ''
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      console.log(selection,'selection')
      this.ids = selection.map((item) => item.recordId)
      this.codes=selection.map((item) => item.nickName)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.tableData2=[]
      this.tableData1=[]
      this.reset()
      this.getOtherList()
      this.open = true
      this.title = '新增黑名单'
    },

    /** 提交按钮 */
    submitForm() {
      const data=this.tableData2.map((item) => {
        return {
          userId: item.userId,
          module:'2'
        }
      })

      addBlacklist(data).then((response) => {
        this.$modal.msgSuccess('新增成功')
        this.open = false
        this.getList()
      })
    },
    // -------穿梭框功能的方法
    // 左边全选事件
    checkAll(row) {
      this.nowSelectData = row;
    },
    // 右边全选事件
    checkRightAll(row) {
      this.nowSelectRightData = row;
    },
    // 左边选中事件
    checkLeft(row) {
      this.nowSelectData = row;
    },
    // 右边选中事件
    checkRight(row) {
      this.nowSelectRightData = row;
    },
    // 点击去右边
    right() {
      this.tableData2 = this.tableData2.concat(this.nowSelectData);
      this.handleRemoveTabList(this.nowSelectData, this.tableData1);
      // 按钮禁用
      this.nowSelectData = [];
    },
    // 点击去左边
    left() {
      this.tableData1 = this.tableData1.concat(this.nowSelectRightData);
      this.handleRemoveTabList(this.nowSelectRightData, this.tableData2);
      // 按钮禁用
      this.nowSelectRightData = [];
    },
    // 方法
    handleRemoveTabList(isNeedArr, originalArr) {
      if (isNeedArr.length && originalArr.length) {
        for (let i = 0; i < isNeedArr.length; i++) {
          for (let k = 0; k < originalArr.length; k++) {
            // 注意，nickName为唯一值，如果不为唯一值那么会出错
            if (isNeedArr[i]["nickName"] === originalArr[k]["nickName"]) {
              console.log("-----------1111");
              originalArr.splice(k, 1);
            }
          }
        }
      }
    },
    // -------穿梭框功能的方法结束
    /** 删除按钮操作 */
    handleDelete(row) {
      const repairIds = row.recordId || this.ids
      const nickName = row.nickName ||this.codes
      this.$modal
        .confirm('是否确认删除所选数据项？')
        .then(function() {
          return delBlacklist(repairIds)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {
        })
    }
  },


}

</script>
<style scoped lang="scss">
::v-deep .el-table-dialog {
  border: 1px solid #e8e6e6;
  thead {
    // color: black;
    th {
      background-color: #f4f4f4;
      .cell {
        font-weight: bold;
      }
    }
    th:last-child {
      text-align: center;
    }
  }
}
.imgstatus {
  position: relative;
  top: 1px;
  left: 0px;
}


</style>
