function initVue() {
    window.vue = new Vue({
        el: "#vue",
        data: {
            templates: [
                {
                    fields: [
                        {
                            label: "MODEL",
                            value: "E715HCBG132-T12",
                        },
                        {
                            label: "Corporate name",
                            value: "Emmvee Energy Private Limited",
                        },
                        {
                            label: "Company address",
                            value: "SY NO.66-70/3, SOMPURA INDUSTRIAL AREA, PEMMANAHALLI VILLAGE, SOMPURA HOBLI, NELAMANGALA TALUKU,BANGALORE(RURAL),562111DISTRICT",
                        },
                        {
                            label: "Tel",
                            value: "+91-80-22174511",
                        },
                        {
                            label: "Email",
                            value: "info@emmvee.in",
                        },
                        {
                            label: "Website",
                            value: "www.emmvee.com",
                        }
                    ],
                    declare: "MADE IN INDIA"
                },
                {
                    fields: [
                        {
                            label: "MODEL",
                            value: "E625HCBG132-T12R",
                        },
                        {
                            label: "Corporate name",
                            value: "Emmvee Energy Private Limited",
                        },
                        {
                            label: "Company address",
                            value: "SY NO.66-70/3, SOMPURA INDUSTRIAL AREA, PEMMANAHALLI VILLAGE, SOMPURA HOBLI, NELAMANGALA TALUKU,BANGALORE(RURAL),562111DISTRICT",
                        },
                        {
                            label: "Tel",
                            value: "+91-80-22174511",
                        },
                        {
                            label: "Email",
                            value: "info@emmvee.in",
                        },
                        {
                            label: "Website",
                            value: "www.emmvee.com",
                        }
                    ],
                    declare: "MADE IN INDIA"
                }
            ],

            template: null,
            loading: false,
        },
        computed: {},
        watch: {},
        methods: {

            readTemplate: function () {
                if (this.loading) return;
                this.template = null;
                
                this.loading = true;
                api.post("/tag/readModuleTag", null, (r) => {
                    this.loading = false;
                    if (r.data && r.data.productType) {
                        console.log("readTemplate", r.data);
                        var model = r.data.productType;
                        for (var t = 0; t < this.templates.length; t++) {
                            var template = this.templates[t];
                            for (var f = 0; f < template.fields.length; f++) {
                                var field = this.templates[t].fields[f];
                                if (field.label == "MODEL" && field.value == model) {
                                    this.template = template;
                                    toast("读取成功。");
                                    return;
                                }
                            }
                        }
                    }
                    toast("标签内容无效，请重新写入标签。");
                }, (e) => {
                    this.loading = false;
                    toast(e.message);
                });
            },

            writeTemplate: function (template) {
                if (this.loading) return;
                console.log("writeTemplate", template);
                this.template = null;
                var model = template.fields.filter(x => x.label == "MODEL");
                var productType = (model.length > 0) ? model[0].value : "-";

                this.loading = true;
                api.post("/tag/writeModuleTag", {
                    tag: {
                        moduleId: "-",
                        productType: productType,
                        moduleDate: "2016-01-01",
                        cellDate: "2016-01-01"
                    }
                }, (r) => {
                    this.loading = false;
                    setTimeout(() => {
                        this.readTemplate();
                    }, 100);
                }, (e) => {
                    this.loading = false;
                    toast(e.message);
                });
            },

            run: function () {

            }
        },
        created() { }
    });
    vue.run();
}

window.addEventListener("load", initVue);