import { mapState } from 'vuex'
import store from "@/store"
import io from '@hyoga/uni-socket.io';

// 尝试将用户在根目录中的store/index.js的vuex的state变量，全部加载到全局变量中
let $uStoreKey = [];
try{
	$uStoreKey = store.state ? Object.keys(store.state) : [];
}catch(e){
	
}

module.exports = {
	beforeCreate() {
		// 将vuex方法挂在到$u中
		// 使用方法为：如果要修改vuex的state中的user.name变量为"史诗" => this.$u.vuex('user.name', '史诗')
		// 如果要修改vuex的state的version变量为1.0.1 => this.$u.vuex('version', '1.0.1')
		this.$u.vuex = (name, value) => {
			this.$store.commit('$uStore', {
				name,value
			})
		}
	},
  data() {
    return {
      pdfSocket: null
    }
  },
	computed: {
		// 将vuex的state中的所有变量，解构到全局混入的mixin中
		...mapState($uStoreKey)
	},
	methods: {
    createSocket() {
      this.pdfSocket = io(this.vuex_printerHostUrl, {
        transports: ['websocket'],
        auth: {
          token: 'Bearer ' + this.vuex_token, // 在此处填入你 client 设置的 token，缺省可留空
        },
      });
      this.pdfSocket.on('connect', () => {
        globalThis.connect = true;
        // console.log('连接成功444');
        // TODO: Do something for your project
      });
      this.pdfSocket.on('clients', (clients) => {
        globalThis.clients = clients;
        console.log(clients, 'clients');
      });
      this.pdfSocket.on('printerList', (printerList) => {
        globalThis.printerList = printerList;
        console.log(printerList, 'printerList');
      });
    },
    printPdfSocket(params) {
      // console.log(this.pdfSocket, 'pdfSocket')
			if (!this.pdfSocket) {
        this.createSocket()
      }
      this.$u.api
        .getPrintDown(params)
        .then((res) => {
          if (res.code === 200) {
            this.pdfSocket.emit('news', {
              client: null,
              printer: this.vuex_printerName,
              type: 'url_pdf',
              // pdf_path: 'http://192.168.3.91:8082/static/123.pdf',
              pdf_path: res.fileUrl,
            });
          }
        });
		}
	}
}