<template>
  <el-dialog
    v-dialogDrag
    :visible.sync="codeOpen"
    title="签收"
    width="600px"
    append-to-body
    @close="issueCodeClose"
    @open="dialogOpen"
  >
    <el-form
      ref="queryForm"
      :model="form"
      size="small"
      label-width="68px"
      @submit.native.prevent
    >
      <el-form-item label="签收人" prop="encodedEmployeeId">
        <el-input
          ref="inputRef"
          v-model="form.encodedEmployeeId"
          placeholder="请输入/扫入签收人"
          clearable
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="buttonLoading" type="primary" @click="handleEnsure">
        确定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import {
  syntheticalSign,
  revokeMasterSign
} from '@/api/wms/orders/productMaterials'

export default {
  props: {
    signType: {
      type: String,
      required: false,
      default: 'sign'
    }
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      buttonLoading: false,

      // 弹出层标题
      title: '',
      // 是否显示弹出层
      codeOpen: false,
      signId: '',
      // 表单
      form: {
        encodedEmployeeId: null
      }
    }
  },
  methods: {
    // 重置
    reset() {
      this.form = {
        encodedEmployeeId: null
      }
      this.resetForm('form')
    },
    // 取消操作
    cancel() {
      this.reset()
    },
    //
    handleEnsure() {
      if (!this.form.encodedEmployeeId) {
        this.$modal.msgWarning('请先输入/扫入签收人工号')
        return
      }
      const params = {
        id: this.signId,
        encodedEmployeeId: this.form.encodedEmployeeId
      }

      switch (this.signType) {
      case 'sign':
        this.handleSign(params)
        break
      case 'cancelSign':
        this.handleCancelSign(params)
        break

      default:
        break
      }
    },
    // 正常签收
    handleSign(params) {
      this.buttonLoading = true
      syntheticalSign(params)
        .then(() => {
          this.$modal.msgSuccess('操作成功')
          this.codeOpen = false
          this.$emit('complete')
        })
        .finally(() => {
          this.buttonLoading = false
        })
    },
    // 撤销签收
    async handleCancelSign(params) {
      await this.$confirm('您正在进行撤销签收的操作，请确认是否继续?')
      this.buttonLoading = true
      revokeMasterSign(params)
        .then(() => {
          this.$modal.msgSuccess('操作成功')
          this.codeOpen = false
          this.$emit('complete')
        })
        .finally(() => {
          this.reset()
          this.buttonLoading = false
          this.autoFocus()
        })
    },
    issueCodeClose() {
      this.form.encodedEmployeeId = undefined
    },
    autoFocus() {
      this.$nextTick(() => {
        this.$refs.inputRef?.focus()
      })
    },
    dialogOpen() {
      this.autoFocus()
    }
  }
}
</script>

<style></style>
