<template>
  <div class="root" :style="style">
    <div class="fake" style="text-align:center; margin-top:20px;">
      <img :src="comp.props.bgImg.val" alt  style="width:75%;   height:80px;"/>
    </div>
    <div style="margin-top:30px; ">
      <el-input id="AntiFakeCode" placeholder="请输入16位数字防伪码" style=" width:70%" v-model="code" maxlength="16"  @input.native="changeEvent"></el-input>

      <el-button @click="searchAntifake()">查询</el-button>
       <div v-if="!showInput" style="color:red;"><p>请输入16位数字防伪码查询</p></div>
      <p v-if="!!antifakeInfo.Content" id="PContent" v-html="antifakeInfo.Content"></p>
    </div>
  </div>
</template>

<script>
//import $ from "jquery";
import Vue from "vue";
import apiHelper from "../../assets/js/apiHelper";
import eventBus from "../../assets/js/eventBus.js";
import _stringResource from "../../assets/js/stringResource.js";
import { Toast } from "mint-ui";
import "element-ui/lib/theme-chalk/index.css";
import ElementUI from "element-ui";
Vue.use(ElementUI);


export default {
  name: "CompAntiFake02",
  props: ["comp", "cfg", "selectedPage", "sup88"],
  data() {
    return {
      style: {},
      title: "",
      code: "",
      clicksum:10,
      clickcount:0,
      SkuLogisticCode:"",
      showInput: true,
      antifakeInfo: {
        Code: "",
        Result: true,
        Content: ""
      }
    };
  },
  methods: { 
    searchAntifake() {
      this.clickcount++;
     if(this.clicksum<this.clickcount)
     {
       this.antifakeInfo.Content="<span style='color:red;'>操作过于频繁，请重新扫描二维码查询！</span>";
       return false;
     }
    if(this.code=="")
    {
     this.showInput=false;
      return false;
    }  
    else if(this.code.length!=16)
     {
       this.antifakeInfo.Content="";
       this.showInput=false;
       return false;
     }
     else
     {
        this.showInput=true;
     }
      var that = this;
      {
        function next() {
          apiHelper.getAntifakeInfo(
            that.sup8.context.eno,
            '',
            that.code,
            that.sup8.UA.geo.latitude + "," + that.sup8.UA.geo.longitude ||
              that.sup8.UA.ip.cip,
            '',
            '',
            '123',
            function(res) {
              if (res.flag === true && res.data.Result=== true) {
                if(res.data.SkuLogisticCode&&that.SkuLogisticCode==res.data.SkuLogisticCode){
                that.antifakeInfo = res.data;
                that.sup8.antifakeInfo = res.data;
                that.antifakeInfo.Content=that.antifakeInfo.Content.replace("，由发售","");
                }
                else{
                   that.antifakeInfo = {
                  Result: true,
                  Content: res.msg || "当前防伪码为正品防伪数码，但不属于当前所扫描标签上的防伪数码，请正确输入防伪数码或拨打咨询热线021-26095599。谢谢您的使用！"
                  };
                }
              } else {
                that.antifakeInfo = {
                  Result: true,
                  Content: res.msg || res.data.Content
                };
              }

             // that.antifakeInfo.Code = codeInfo.Code;
            },
            function(res) {
              that.antifakeInfo = {
                Result: true,
                Content: res.msg || res.data.Content
              };
            }
          );
        }

        if (this.sup8.context.geo) {
          next();
        } else {
          eventBus.subscribe("gpsDone", data => {
            next();
          });
        }
      }
    },
    render: function() {
      var that = this;
      switch (that.sup8.env) {
        case that.sup8._def.env.design:
          break;
        case that.sup8._def.env.preview:
          break;
        case that.sup8._def.env.run:
          that.SkuLogisticCode=that.sup8.codeInfo.SkuLogisticCode;
         // that.searchAntifake(that.sup8.codeInfo);
          break;
        default:
          break;
      }
    },
    changeEvent:function(){
      if(this.code !== null){
      this.code = this.code.replace(/[^\d]/g,'')
      }
          this.showInput=true;
    }
  },

  created: function() {
    var that = this;
    that.mergeCompProps(that.comp);
    
    switch (that.sup8.env) {
      case that.sup8._def.env.design:
        that.antifakeInfo = {
          Code: "xxxxxxxxxxxxxxx",
          Result: true,
          Content: "该商品已被查询1次，首次查询时间为2019-09-18，谨防假冒！"
        };
      case that.sup8._def.env.preview:
        that.antifakeInfo = {
          Code: "xxxxxxxxxxxxxxx",
          Result: true,
          Content: "该商品已被查询1次，首次查询时间为2019-09-18，谨防假冒！"
        };

        // that.selectedPage =
        //   that.selectedPage ||
        //   _.find(that.cfg.pages, {
        //     type: that.sup8.context.ptype || "1"
        //   });

        break;
      case that.sup8._def.env.run:
        break;
      default:
        break;
    }

    //that.initStyle();
  },

  mounted: function() {
    //console.log("CompAntiFake:mounted:this.comp", this.comp);
    var that = this;
    if (that.sup8.antifakeInfo) {
      that.antifakeInfo = that.sup8.antifakeInfo;
    } else {
      that.render();
    }
  }
};
</script>

<style scoped>
.root {
  padding: 1em;
  color: #606060;
}
</style>
