<template>
  <div class="app-container">
    <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="$t('common.workunit')" align="center" prop="workunitName" />
      <el-table-column :label="$t('common.option')" 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']"
          >{{ $t('common.edit') }}</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mes:md:workstation:remove']"
          >{{ $t('common.del') }}</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="$t('common.workunit')" prop="nickName">
              <el-input v-model="form.workunitName" readonly="readonly" >
                <el-button
                  slot="append"
                  icon="el-icon-search"
                  @click="$refs.WorkunitSelect.showFlag = true"
                  ></el-button>
              </el-input>
              <workunitSelect
                ref="WorkunitSelect"
                :userId="userId"
                @onSelected="onworkunitSelected"
              ></workunitSelect>
            </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{ $t('common.confirm') }}</el-button>
        <el-button @click="cancel">{{ $t('common.cancel') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>


import { listWorkunituser, getWorkunituser, delWorkunituser, addWorkunituser, updateWorkunituser } from "@/api/system/user";
import workunitSelect from "./workunitSelect.vue"
// "@/views/mes/cal/team/calTeamSelect/multi.vue"
export default {
  name: "Workunitworker",
   components: {workunitSelect},
  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: this.$t('工作中心ID不能为空'), trigger: "blur" }
        ],
        postId: [
          { required: true, message: this.$t('岗位ID不能为空'), trigger: "blur" }
        ],
        quantity: [
          { required: true, message: this.$t('数量不能为空'), 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
    },
    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);
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = this.$t('common.add_workunit');
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getWorkunituser(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = this.$t('修改工作单元');
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWorkunituser(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('common.edit_success'));
              this.open = false;
              this.getList();
            });
          } else {
            addWorkunituser(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('common.add_success'));
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm(this.$t('是否确认删除此工作单元资源')).then(function() {
        return delWorkunituser(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess(this.$t('common.del_success'));
      }).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>
