<template>
  <el-dialog :close-on-click-modal="false" custom-class="edit-dialog" append-to-body width="60%" :close-on-press-escape="false"
    :visible.sync="dialogVisible" :title="`给车辆${vehicle.vehicleNo} 绑定RFID标签`">
    <el-input ref="rfidInput" class="rfidinput" v-model="rfidNo" prefix-icon="el-icon-full-screen" placeholder="请输入编码" >
    </el-input>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button :disabled="!rfidNo || !vehicle.id" type="primary" @click="confirm">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { rfidCheckBind, addRfid } from "@/api/gangkou/device/rfid";

export default {
  name: 'InputDialog',
  data() {
    return {
      dialogVisible: false,
      rfidNo: '',
      vehicle: {},
    }
  },
  methods: {
    open(vehicle) {
      this.rfidNo = '';
      this.vehicle = vehicle || {};
      this.dialogVisible = true;

      this.$nextTick(() => {
        this.$refs.rfidInput.focus();
      })
    },
    confirm() {
      
      rfidCheckBind({
        rfidNo: this.rfidNo,
        vehicleId: this.vehicle.id
      }).then(res => {
        if (!res.data?.id) {
          this.addRfid();
        } else {
          this.$confirm(res.msg || `该标签当前已绑定了车辆${res.data?.vehicleNo || ''}，是否换绑到当前车辆？`, '温馨提示', {
            type: 'warning'
          }).then(() => {
            this.addRfid();
          });
        }
      });
    },
    didBind() {
      addRfid({
        vehicleId: this.vehicle.id,
        rfidNo: this.rfidNo,
        isValid: 'Y',
      }).then(resp => {
        this.$message.success('绑定成功');
        this.rfidNo = '';
        this.$refs.rfidInput.focus();
        this.$emit('refresh');
      });
    }
  }
}
</script>

<style scoped lang="scss">
.rfidinput {
  width: 100%;
  height: 60px;
  font-size: 30px;
  font-weight: bold;
  margin: 20px 0;

  ::v-deep .el-input__inner {
    height: 100%;
    padding-left: 50px;
  }
  ::v-deep .el-input__icon {
    line-height: 60px;
    margin-left: 10px;
  }
}
</style>