<template>
    <customer-navBar-layout>
        <template #navBar>
            <van-nav-bar title="扫码"
                         left-arrow
                         @click-left="_cancelScan"></van-nav-bar>
        </template>
        <template #content>
            <div id="bcid">
                <div style="height:40%"></div>
            </div>
            <div class="bottom-btn">
                <van-button block type="default" @click="_cancelScan">取消</van-button>
            </div>
        </template>
    </customer-navBar-layout>
</template>
<style scoped>
    #bcid {
        width: 100%;
        height: calc(100% - 42px);
        text-align: center;
    }

</style>

<script>
    import {samplingSample} from '../../api'

    var barcode = null;//扫码对象
    export default {
        data() {
            return {
                num: '',
            }
        },
        mounted() {
            this._open()
            //返回键操作
            mui.init({
                beforeback: function () {
                    barcode.close();
                    return true;
                }
            });
        },
        methods: {
            _open() {
                barcode = null;
                let that = this;
                if (!barcode) {
                    barcode = new plus.barcode.Barcode('bcid', [plus.barcode.EAN13, plus.barcode.EAN8], {
                        frameColor: '#00FF00',
                        scanbarColor: '#00FF00'
                    });
                    barcode.onmarked = function (type, result) {
                        if (type === plus.barcode.EAN13 || type === plus.barcode.EAN8) {
                            that._getSample(result)
                        } else {
                            that.$toast('扫描失败！')
                            barcode.close();
                            that.$router.go(-1);
                        }
                    };
                    plus.webview.currentWebview().append(barcode);
                }
                barcode.start();
            },
            _getSample: async function (qrcode) {
                let res = await samplingSample.scanSample({qrcode: qrcode})
                if (res) {
                    localStorage.setItem('sample', JSON.stringify(res))
                    barcode.close();
                    this.$router.go(-1)
                } else {
                    localStorage.setItem('sample', JSON.stringify({qrcode: qrcode}))
                    barcode.close();
                    this.$router.go(-1)
                }
            },
            _cancelScan() {
                barcode.close();
                this.$router.go(-1)
            },
            // _scanPicture() {
            //     let that = this;
            //     plus.gallery.pick(function (path) {
            //         plus.barcode.scan(path, function (type, result) {
            //             var text = '未知: ';
            //             switch (type) {
            //                 case plus.barcode.CODE128:
            //                     text = 'CODE128: ';
            //                     break;
            //             }
            //             that.$store.dispatch('EnvSample/scanReceive', {num: result}).then(() => {
            //                 if (that.$store.state.EnvSample.success) {
            //                     barcode.close();
            //                     that.$router.go(-1);
            //                 }
            //             });
            //         }, function (error) {
            //             plus.nativeUI.alert('无法识别此图片');
            //         }, [plus.barcode.CODE128]);
            //     }, function (err) {
            //         console.log('Failed: ' + err.message);
            //     });
            // },
        }
    }
</script>
