<template>
  <div class="app-container">
    <el-col></el-col>
    <!-- <el-button
      type="primary"
      plain
      icon="el-icon-plus"
      size="mini"
      @click="onworkunitSelectRef()"
    >新增</el-button> -->
    <!-- <workunitSelect   :userId="userId" :id="id" ref="workunitSelectRef" @onSelected="onworkunitSelecteds" /> -->
    <el-table v-loading="loading" :data="workstationworkerList" @selection-change="handleSelectionChange" :max-height="tableHeight">
      <!-- <el-table-column label="岗位名称" align="center" prop="postName" /> -->
      <!-- <el-table-column label="数量" align="center" prop="quantity" /> -->
      <el-table-column label="工作单元" align="center" prop="workunitName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mes:md:workstation:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mes:md:workstation:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="工作单元" prop="nickName">
              <el-input v-model="form.workunitName" placeholder="请选择工作单元" readonly="readonly" >
                <el-button
                  slot="append"
                  icon="el-icon-search"
                  @click="$refs.WorkunitSelect.showFlag = true"
                  ></el-button>
              </el-input>
              <workunitSelects
                ref="WorkunitSelect"
                @onSelected="onworkunitSelected"
              ></workunitSelects>
            </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWorkunituser, getWorkunituser, delWorkunituser, addWorkunituser, updateWorkunituser } from "@/api/system/user";
//import workunitSelect from "./workunitSelect.vue"
import workunitSelects from "./workunitSelect1.vue"
export default {
  name: "Workunitworker",
   components: {workunitSelects},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      tableHeight: 0,
      // 总条数
      total: 0,
      // 工作单元表格数据
      workstationworkerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: this.userId
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        workstationId: [
          { required: true, message: "工作中心ID不能为空", trigger: "blur" }
        ],
        postId: [
          { required: true, message: "岗位ID不能为空", trigger: "blur" }
        ],
        quantity: [
          { required: true, message: "数量不能为空", trigger: "blur" }
        ],
      }
    };
  },
  props: {
    userId: undefined,
  },
  created() {
    this.getList();
  },
  watch: {
    userId() {
      this.getList()
    },
  },
  mounted(){
    this.$nextTick(()=>{
      this.tableHeight = window.innerHeight - 550
    })
  },
  methods: {
    /** 查询工作单元列表 */
    getList() {
      this.loading = true;
      this.queryParams.userId = this.userId
      listWorkunituser(this.queryParams).then(response => {
        this.workstationworkerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: this.userId,
        workunitId: null,
        workunitName: null,
        userName: null,
        nickName: null,
        postName: null,
        quantity: null,
        remark: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    
    handleWorkunitSelect(){
      this.$refs.WorkunitSelect.showFlag = true
    },
    onworkunitSelected(row){
      console.log(45456, row);
      if (row != undefined && row != null) {
        this.form.workunitId = row.workunitId;
        this.form.workunitName = row.workunitName;
        console.log(111111, this.form);
      }
    },


    selectworkunit(){
      this.getList();
    },

    // onworkunitSelectRef(){
    //   this.$refs['workunitSelectRef'].showFlag = true
    // },
    // onworkunitSelecteds(list){
    //   console.log(45456, row);
    //   this.workstationworkerList = list.map(item =>{
    //     return{
    //     workunitId:item.workunitId,
    //     workunitName:item.workunitName,
    //     }
    //   })
    // },


    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工作单元";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getWorkunituser(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改工作单元";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWorkunituser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWorkunituser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除此工作单元资源').then(function() {
        return delWorkunituser(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
<style lang="scss" >
.el-divider--horizontal.userdivider {
  margin-top: 10px;
}
.usercard {
  .el-card__header {
    padding: 14px 20px;
  }
  .el-card__body {
    padding-top: 0;
    padding-bottom: 5px;
  }
  .app-container {
    padding: 10px;
  }
}
</style>
