import Vue from 'vue'
import Vuex from 'vuex'
import user from './modules/user.js'
import ElementUI from 'element-ui'
import {doLocalPrint} from "@/utils";
import {
  Notification
} from 'element-ui'
// 应用插件
Vue.prototype.$notify = Notification

Vue.use(ElementUI)
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    websock: null,
    eventlist: {}
  },
  mutations: {
    WEBSOCKET_INIT(state, url) {
      // var path = url + '?loginName=' + user.state.user.loginName
      // console.log(path)
      state.websock = new WebSocket(url + '?loginId=' + user.state.user.id + '&supplierCode=' + user.state.indexInfo.supplierCode + '&dataSourceId=' + user.state.indexInfo.dataSourceId)
      // console.log(state.websock)
      state.websock.onopen = function() {
        // console.log('socket连接成功')
        var data = {
          restPing: {
            ping: 'ping'
          }
        }
        setInterval(function() {
          state.websock.send(JSON.stringify(data))
        }, 30000)
      }
      state.websock.onmessage = function(msg) {
        // console.log(msg);
        // console.log(JSON.stringify(msg));
        // alert(JSON.stringify(msg))
        const msgData = msg.data
        const msgContent = JSON.parse(msgData)
        // this.code1 = JSON.stringify(msgContent)
        // state.eventlist = msgContent
        // console.log("msgContent",msgContent)
        if (msgContent.message != 'PONG') {
          if(msgContent.messageKind==8){
            var printContentData = JSON.parse(msgContent.message);
            var PrintTemplate = "";
            // console.log("printContentData",printContentData);
            if(printContentData.printTemplate=="OutSourceBill"){
              PrintTemplate = 'OutSourceBill_XQD.fr3';
            }else {
              PrintTemplate = ""
            }
            var rootData = JSON.parse(printContentData.printContent);
            // console.log("rootData",rootData);
            var PrintData = {
              '_rootData': rootData
            }

            // console.log(PrintData);
            var data = {
              PrintTemplate: PrintTemplate,
              NeedPreview: 1,
              PrintData: JSON.stringify(PrintData)
            }
            doLocalPrint(data)

          }else {
            var userAgent = navigator.userAgent
            if (userAgent.indexOf('DnyERPBrowser') > 0) {
              dyERP.sendAlertMessage(msgContent.messageKind, msgContent.message)
            }

            Notification({
              title: '提示',
              message: msgContent.message + '-' + msgContent.fromUserName,
              position: 'top-right',
              duration: 10000
            })
          }

        }
        // state.eventlist.push(msgContent)
        // if (msgContent.messageKind == '1') {
        //   this.active = 3;
        //   this.keyWord = msgContent.message.slice(1);
        //   this.$forceUpdate(); //重绘
        //   // this.code1 = this.keyWord
        //   // this.keyWord = msg.data.messageBody.value.slice(1);
        //   // this.dataSearch();
        // }
        // this.send();
      }
      state.websock.onerror = function(e) { // 错误
        console.log('ws错误!')
        // console.log(e)
      }
      state.websock.onclose = function(e) { // 关闭
        console.log('ws关闭！')
        // console.log(e)
      }
    },
    WEBSOCKET_SEND(state, p) {
      console.log('ws发送！')
    }
  },
  actions: {
    WEBSOCKET_INIT({ commit }, url) {
      commit('WEBSOCKET_INIT', url)
    },
    WEBSOCKET_SEND({ commit }, p) {
      p.type = 3
      commit('WEBSOCKET_SEND', p)
    }
  }
})
