<template>
  <div class="input-scan">
    <van-field
      v-model="field.value"
      :name="field.label"
      :required="field.required"
      :disabled="field.disabled"
      :label="field.label"
      :placeholder="`请输入${field.label}`"
      :rules="rules(field)"
      clearable
      center
    >
      <template #button>
        <van-button
          :disabled="field.disabled"
          icon="scan"
          type="primary"
          size="small"
          @click="field.disabled ? () => {}:scanCode()"
        >扫码</van-button>
      </template>
    </van-field>
  </div>
</template>
<script>
export default {
  name: 'InputScan',
  props: {
    field: {
      type: Object,
      default: () => {
        return {}
      }
    },
    // 校验规则
    rules: {
      type: Function,
      default: () => {
        return []
      }
    }
  },
  data () {
    return {
    }
  },
  methods: {
    // 扫码
    scanCode () {
      const that = this
      window.App.scanCode(function (response) {
        if (response.status === 1) {
          that.field.value = response.data.code
        }
      })
    }
  }
}
</script>
