<template>
    <!-- pages/addDevice/addDevice.wxml -->
    <view class="container">
        <view class="add-container">
            <form @submit="bindAddSubmit">
                <view class="form-container">
                    <view class="macid-item">
                        <input type="text" name="macid" placeholder="请输入15位IMEI号" class="form-input" :value="macid" />
                        <image src="/static/resource/images/xiangji2.png" class="form-img-right" @tap="bindScanCode"></image>
                    </view>
                    <view class="form-btn">
                        <button class="login-btn" form-type="submit">添加设备</button>
                    </view>
                </view>
            </form>
            <image src="/static/resource/images/shuoming.png" class="bottom-img"></image>
        </view>
    </view>
</template>

<script>
// pages/addDevice/addDevice.js
var config = require('../../common/config.js');
var http = require('../../common/http.js');
var common = require('../../common/common.js');
var storage = require('../../common/storage.js');
export default {
    data() {
        return {
            macid: ''
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad: function (options) {},
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
    methods: {
        bindScanCode() {
            uni.scanCode({
                onlyFromCamera: true,
                scanType: [],
                success: (res) => {
                    console.log(res);
                    if (res.result) {
                        this.setData({
                            macid: res.result
                        });
                    }
                },
                fail: function (res) {},
                complete: function (res) {}
            });
        },

        bindAddSubmit(e) {
            console.log(e.detail.value);
            const macid = e.detail.value.macid;
            if (common.isEmpty(macid)) {
                common.simpleToast('请输入设备编号');
                return;
            }
            http.postApi(
                config.API_BATTERY_BIND,
                {
                    macid: macid
                },
                (resp) => {
                    if (resp.data.code === 200) {
                        common.simpleToast('添加成功');
                        storage.setRefreshDeviceoPage(true);
                        setTimeout(function () {
                            uni.navigateBack({
                                delta: 1
                            });
                        }, 1500);
                    } else {
                        common.simpleToast(resp.data.msg);
                    }
                }
            );
        }
    }
};
</script>
<style>
@import './addDevice.css';
</style>
