<template>
  <el-dialog title="工作单元选择" v-if="showFlag" :visible.sync="showFlag" :modal="false" width="80%">
    <el-form :model="queryParams" @submit.native.prevent ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="工作单元编码" prop="workunitCode">
        <el-input v-model="queryParams.workunitCode" placeholder="请输入工单编码" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="工作单元名称" prop="workunitName">
        <el-input v-model="queryParams.workunitName" placeholder="请输入工作单元名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item v-if="!workstationId" label="工作中心" prop="workstationName">
        <el-input v-model="queryParams.workstationName" placeholder="请输入工作中心名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      :data="workunitList"
      @selection-change="handleSelectionChange"
    >
    <el-table-column type="selection" width="55" align="center" />
    <el-table-column label="工作单元名称" align="center" prop="workunitName" />
     <el-table-column  label="工作单元编码" align="center" prop="workunitCode" />
      <el-table-column label="工作中心名称" align="center" prop="workstationName" />
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <div slot="footer" class="dialog-footer">
      <el-button @click="showFlag = false">取 消</el-button>
      <el-button type="primary" @click="confirmSelect">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { listWorkunit } from "@/api/mes/md/workunit";
import {updateWorkunituserList } from "@/api/system/user";
import Workstationworker from "./worker";
export default {
  name: "WorkunitSelectSingle",
  components: {Workstationworker},
  dicts: [],
  props: {
    userId: undefined,
    workstationName: {
      type: String,
      default: "",
    },
    workstationId: {
      type: String | Number,
      default: "",
    },
    workunitId: {
      type: Number | undefined,
      default: undefined,
    }, //外部传入的工单过滤信息
  },
  data() {
    return {
      showFlag: false,
      // 遮罩层
      loading: true,

      // 选中数组
      selectedWorkunitId: undefined,
      selectedRows: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产工单表格数据
      workunitList: [],

      workunitData: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        workunitCode: null,
        pageNum: 1,
        pageSize: 10,
        workunitName: null,
        workstationId: null,
        workstationName: this.workstationName,
        workstationId: this.workstationId,
        workunitId: null,
        enableFlag: "Y",
      },
    };
  },
  watch: {
    showFlag: {
      handler(newName) {
        this.selectedWorkunitId = null
        this.selectedRows = []
        this.queryParams.workstationName = this.workstationName;
        this.queryParams.workstationId = this.workstationId;
        this.getList();
      },
      immediate: true
    }
  },
  created() {

  },
  methods: {
    /** 查询生产工单列表 */
    getList() {
      this.loading = true;
      listWorkunit(this.queryParams).then((response) => {
        this.workunitList = response.rows;
        this.loading = false;
        this.total = response.total
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleCurrent(row) {
      console.log(row);
      if (row) {
        this.selectedRows = row;
      }
    },
    // 单选选中数据
    handleRowChange(row) {
      if (row) {
        this.selectedRows = row;
      }
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      console.log(this.ids);
      this.ids = selection.map(item => item.workunitId);
      this.workunitData = selection;
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    //双击选中
    handleRowDbClick(row) {
      if (row) {
        this.selectedRows = row;
        this.$emit("onSelected", this.selectedRows);
        this.showFlag = false;
      }
    },
    //确定选中
    confirmSelect() {
      console.log(this.ids.length);
      if (this.ids.length == 0) {
        this.$notify({
          title: "提示",
          type: "warning",
          message: "请至少选择一条数据!",
        });
        return;
      }
      console.log(this.workunitData);
      const a= this.workunitData.map((s)=>{
        let data={
          userId: this.userId,
          workunitId: s.workunitId,
          workunitName: s.workunitName
        }
        return data;
      });
      console.log(a);
      this.$emit("onSelected", this.workunitData);
      this.showFlag = false;
      updateWorkunituserList(a).then((response) => {
        this.$emit('reflash')
       this.$modal.msgSuccess(response.msg);
      });
      


    }

  },
};
</script>