<script>
import bridge, { isInApp } from '@/utils/bridge'

export default {
  name: 'CompScanCode',
  props: {
    disabled: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      isInApp
    }
  },
  methods: {
    /**
     * 开始扫码
     * @param isBarcode 是否是条形码
     */
    handleTakeScan(isBarcode) {
      bridge.scanCode(isBarcode, code => {
        this.$emit('change', code)
      })
    }
  },
  render() {
    const defaultSlot = this.$scopedSlots.default()
    // 如果当前是app内部
    if (this.isInApp) {
      return (
        <el-popover
          disabled={this.disabled}
          class='component-scan-code'
          placement='bottom'
          width='280'
          trigger='click'
          scopedSlots={{
            reference: () => defaultSlot
          }}
        >
          <div class='operation-wrapper'>
            <el-button disabled={this.disabled} icon='el-icon-camera' onClick={this.handleTakeScan.bind(this, false)}>扫描二维码</el-button>
            <el-button disabled={this.disabled} icon='el-icon-document' type='primary' onClick={this.handleTakeScan.bind(this, true)}>扫描条形码</el-button>
          </div>
        </el-popover>
      )
    }
    return defaultSlot
  }
}
</script>

<style lang="scss">
.component-scan-code {
  .operation-wrapper {
    align-items: center;
    display: flex;
    justify-content: space-between;
  }
}
</style>
