import { userInfoStore } from '../stores/counter'
import axios from 'axios'
import qs from 'qs'
export const myMixin = {
  data() {
    return {
        userJson: {},
        new_url: 'https://appapi.cocorobo.cn'
    };
  },
  methods: {
    detectBrowser() {
      const ua = navigator.userAgent;

      // 按优先级顺序检测
      if (ua.includes("Edg/") || ua.includes("Edge/")) {
        return "Microsoft Edge";
      } else if (ua.includes("Firefox")) {
        return "Mozilla Firefox";
      } else if (ua.includes("Trident") || ua.includes("MSIE")) {
        return "Internet Explorer";
      } else if (ua.includes("360EE")) {
        return "360 Browser (极速模式)";
      } else if (ua.includes("360SE")) {
        return "360 Browser (安全模式)";
      } else if (ua.includes("SLBrowser")) {
        return "QQ Browser";
      } else if (ua.includes("UCBrowser")) {
        return "UC Browser";
      } else if (ua.includes("Opera") || ua.includes("OPR/")) {
        return "Opera";
      } else if (ua.includes("Chrome") && !ua.includes("Edg/")) {
        return "Google Chrome";
      } else if (ua.includes("Safari/") && !ua.includes("Chrome")) {
        return "Safari";
      } else {
        return "Other Browser";
      }
    },
    async addOp3(userTime, loadTime, object, status,api = 'selectUser') {
      const user = userInfoStore()
      if (!user.user.userid) return

      try {
        if(!this.userJson || !this.userJson.accountNumber){
            let res = await axios.get(`https://pbl.cocorobo.cn/api/pbl/${api}?userid=`+user.user.userid)
            this.userJson = res.data[0][0]
        }
      } catch (e) {
        console.log(e);
        return this.addOp3(userTime, loadTime, object, status,'selectUser2');
      }
      let _time = new Date()
        .toLocaleString("zh-CN", { hour12: false, timeZone: "Asia/Shanghai" })
        .replace(/\//g, "-");
      let browser = this.detectBrowser();
      const params = {
        userid: user.user.userid,
        username: this.userJson.username,
        accountNumber: this.userJson.accountNumber,
        org: this.userJson.orgName,
        school: this.userJson.schoolName,
        role: this.userJson.type =='1'?'老师':'学生',
        browser: browser,
        userTime: userTime == "1" ? _time : userTime, // 使用时间 1次的就1 其次传秒
        loadTime: loadTime, //load的时间没有就“”
        object: JSON.stringify(object), //执行信息传json
        status: status //成功返回success。失败返回error的信息
      };
      console.log('params',params);
     let encoded = {};

      for (const key in params) {
        if (Object.hasOwnProperty.call(params, key)) {
          encoded[key] = encodeURIComponent(params[key]);
        }
      }
      console.log('encoded',encoded);
      // params = qs.stringify([encoded]) //序列化post 参数
      axios
        .post("https://pbl.cocorobo.cn/api/mongo/updateUserData2", [encoded], {
          headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
          withCredentials: true
        })
        .then(res => {
          console.log('res',res);
          if (res.data.status == 1) {
            console.log("保存成功");
          } else {
            console.log("保存失败");
          }
        })
        .catch(e => {
          console.log("保存失败");
          console.log(e);
        });
    }

  }
};

export function addOp3(...args) {
  const ctx = {
    ...myMixin.data(),
    detectBrowser: myMixin.methods.detectBrowser,
    addOp3: myMixin.methods.addOp3,
  }
  return myMixin.methods.addOp3.call(ctx, ...args)
}
