<template>
  <el-dialog
    class="scan-task-bind-user-dialog"
    :visible.sync="modalIsShow"
    title="绑定扫描员"
    top="10vh"
    width="460px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    @open="visibleChange"
  >
    <el-form
      ref="modalFormComp"
      :model="modalForm"
      :rules="rules"
      label-width="100px"
    >
      <el-form-item prop="scanUserId" label="扫描员：">
        <el-select v-model="modalForm.scanUserId" placeholder="请选择扫描员">
          <el-option
            v-for="item in scanUserList"
            :key="item.id"
            :value="item.id"
            :label="item.name"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button type="primary" :disabled="isSubmit" @click="submit"
        >确认</el-button
      >
      <el-button @click="cancel">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { scanTaskBindUser } from "../api";
import { roleUserListQuery } from "../../base/api";

const initModalForm = {
  id: null,
  scanUserId: ""
};

export default {
  name: "scan-task-bind-user-dialog",
  props: {
    instance: {
      type: Object,
      default() {
        return {};
      }
    }
  },
  data() {
    return {
      modalIsShow: false,
      isSubmit: false,
      modalForm: { ...initModalForm },
      scanUserList: [],
      rules: {
        scanUserId: [
          {
            required: true,
            message: "请选择扫描员",
            trigger: "change"
          }
        ]
      }
    };
  },
  mounted() {
    // this.getScanUsers();
  },
  methods: {
    visibleChange() {
      this.modalForm = this.$objAssign(initModalForm, this.instance);
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    async getScanUsers() {
      const res = await roleUserListQuery({ roleType: "Scan" });
      this.scanUserList = res || [];
    },
    async submit() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;

      if (this.isSubmit) return;
      this.isSubmit = true;
      let datas = { ...this.modalForm };
      const data = await scanTaskBindUser(datas).catch(() => {});
      this.isSubmit = false;

      if (!data) return;

      this.$message.success("绑定成功！");
      this.$emit("modified");
      this.cancel();
    }
  }
};
</script>
