export default {

  getAdmin(){
    return "admin"
  },

  isMobile() {
    let flag = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)
    return flag;
  },
  //有记录可以 用 go回退
  //  this.utils.goRouterforPath(this, {path: 'matching', query: {data: this.images[index1][index2]}})
  goRouterforPathPush(Context,path){
    Context.$router.push(path);
  },
  //没记录不能回退
  goRouterforPathReplace(Context,path){
    Context.$router.replace(path);
  },
  //正负数
  goRouterforPathGo(Context,n){
    Context.$router.go(n);
  },
  goRouterforIndex(Context,index){
    Context.$router.push(Context.$router.options.routes[index]);

  },
  //成功的提示
  openSuccess(Context,data){
    Context.$notify(data);
  },
  //失败的提示
  openError(Context,data) {
    Context.$notify.error(data);
  },
  //给item 结构 装上他应该有的数据
  setItemData(titleData,itemData){
    for (var key in itemData) {
      // console.log("内容是key",key);
      // console.log("内容是Value",this.multipleSelection[0][key]);
      var value = itemData[key]
      for (var i = 0; i < titleData.length; i++) {

        // console.log("内容是key titleData",this.titleData[i].name,this.titleData[i].name==key);
        if (titleData[i].name == key) {
          titleData[i].value = value
        }
      }
    }


    return titleData
  },


  getNewObjData(data){
    //这样可以解除 双向绑定
    var newdata = JSON.parse(JSON.stringify(data));//先将obj转换为JSON字符串，然后再转回对象
    return newdata
  },
  getJson(data){

    var newdata = JSON.stringify(data);
    return newdata
  },
  getJsonObj(data){

    var newdata = JSON.parse(data);
    return newdata
  },
  httpToHttps(str){
    // var temp = str
    var strlist = str.split('http')
    // console.log("qiegeshenme ",strlist);
    return "https"+strlist[1];
  },
  //加载游戏图片图标
  initHttpImageGame(urlImage){
    // var temp = str
    // console.log("qiegeshenme ",strlist);
    return http_.image+urlImage;
  },
  initHttpImage(urlImage){
    // var temp = str
    // console.log("qiegeshenme ",strlist);
    return http_.image+urlImage;
  },
  /**
   *
   * @param strMap
   * @returns {any}
     var jsonMap = []
    for (var i = 0; i < data.length; i++) {
      // json.data[i].name = data[i].value
      jsonMap[data[i].name] = data[i].value

    }
       var json = this.utils.mapToJson(jsonMap)

   */
  //整体步骤是 list 转 map 再转 json
  //map 转 json
  mapToJson(strMap){
    let obj= Object.create(null);
    for (var key in strMap) {
      obj[key] = strMap[key];
    }
    return obj;
  },

  //根据 父组件得到 符合条件的 子组件
  getComponents(Context,componentName){
    var refs = []
    for (var key in Context.$refs){
      // console.log("调用了吗1",key);
      // console.log("调用了吗2",this.$refs[key]);
      // console.log("调用了吗3",this.$refs[key][0]);
      for (var i = 0; i < componentName.length; i++) {
        if (key == componentName[i]) {
          refs.push(Context.$refs[key])
        }

      }

    }
    return refs
  },
  setComponentsData(Context,refs,callback){
    for (var i = 0; i <refs.length ; i++) {
      if (callback) {
        callback(i,refs[i])
      }
    }
  },
  add(Context,sendData){
    if (!sendData.url) {
      sendData.url = "/add"
    }
    if (!sendData.data) {
      sendData.data = {}
    }
    sendData.callbackSuccess = function (Success) {
      console.log("add 成功",Success);
      // Context.$store.dispatch('ping',Success)
    }.bind(this)
    sendData.callbackfail = function (fail) {
      console.log("add 失败",fail);
    }.bind(this)

    Context.$store.dispatch('httpGet',sendData)
  },
  addGameItem(Context,sendData,callbackSuccess){
    if (!sendData.url) {
      sendData.url = "/addGameItem"
    }
    if (!sendData.data) {
      sendData.data = {}
    }
    sendData.callbackSuccess = function (Success) {
      console.log("addGameItem 成功",Success);
      if (Success.data.code ==0) {
        let data = {
          title: '成功',
          message: '添加一条数据',
          type: 'success'
        }
        this.openSuccess(Context,data)
      }else{
        let data = {
          title: '错误',
          message: sendData.data.className+"添加条目失败"
        }
        this.openError(Context,data)
      }
      // Context.$store.dispatch('ping',Success)
      if (callbackSuccess) {
        callbackSuccess()
      }
    }.bind(this)
    sendData.callbackfail = function (fail) {
      console.log("addGameItem 失败",fail);
    }.bind(this)

    Context.$store.dispatch('httpGet',sendData)
  },
  upDataGameItem(Context,sendData,callbackSuccess){
    if (!sendData.url) {
      sendData.url = "/upDataGameItem"
    }
    if (!sendData.data) {
      sendData.data = {}
    }
    sendData.callbackSuccess = function (Success) {
      console.log("upDataGameItem 成功",Success);
      if (Success.data.code ==0) {
        let data = {
          title: '成功',
          message: '添加一条数据',
          type: 'success'
        }
        this.openSuccess(Context,data)
      }else{
        let data = {
          title: '错误',
          message: sendData.data.className+"添加条目失败"
        }
        this.openError(Context,data)
      }
      // Context.$store.dispatch('ping',Success)
      if (callbackSuccess) {
        callbackSuccess()
      }
    }.bind(this)
    sendData.callbackfail = function (fail) {
      console.log("upDataGameItem 失败",fail);
    }.bind(this)

    Context.$store.dispatch('httpPost',sendData)
  },
  delGameItem(Context,sendData,callbackSuccess){
    if (!sendData.url) {
      sendData.url = "/delGameItem"
    }
    if (!sendData.data) {
      sendData.data = {}
    }
    sendData.callbackSuccess = function (Success) {
      console.log("delGameItem 成功",Success);
      if (Success.data.code ==0) {
        let data = {
          title: '成功',
          message: '删除数据',
          type: 'success'
        }
        this.openSuccess(Context,data)
      }else{
        let data = {
          title: '错误',
          message: sendData.data.className+"添加条目失败"
        }
        this.openError(Context,data)
      }
      // Context.$store.dispatch('ping',Success)
      if (callbackSuccess) {
        callbackSuccess()
      }
    }.bind(this)
    sendData.callbackfail = function (fail) {
      console.log("delGameItem 失败",fail);
    }.bind(this)

    Context.$store.dispatch('httpPost',sendData)
  },
  getTable(Context,sendData){
    if (!sendData.url) {
      sendData.url = "/getTable"
    }
    if (!sendData.data) {
      sendData.data = {}
    }
    sendData.callbackSuccess = function (Success) {
      console.log("getTable 成功",Success);
      Context.$store.dispatch('table',Success)
    }.bind(this)
    sendData.callbackfail = function (fail) {
      console.log("getTable 失败",fail);
    }.bind(this)

    Context.$store.dispatch('httpGet',sendData)
  },
  getTableData(Context,sendData){
    if (!sendData.url) {
      sendData.url = "/getTableData"
    }
    if (!sendData.data) {
      sendData.data = {}
    }
    sendData.callbackSuccess = function (Success) {
      console.log("getTableData 成功",Success);
      Context.$store.dispatch('tableData',Success)
    }.bind(this)
    sendData.callbackfail = function (fail) {
      console.log("getTableData 失败",fail);
    }.bind(this)

    Context.$store.dispatch('httpGet',sendData)
  },
  getDataForList(Context,sendData,callbackSuccess,callbackFailure){
    if (!sendData.url) {
      sendData.url = "/getDataForList"
    }
    if (!sendData.data) {
      sendData.data = {}
    }
    sendData.callbackSuccess = function (Success) {
      console.log("getDataForList 成功",Success);
      Context.$store.dispatch('dataForList',Success)
      if (callbackSuccess) {
        callbackSuccess()
      }

    }.bind(this)
    sendData.callbackfail = function (fail) {
      console.log("getDataForList 失败",fail);
      if (callbackFailure) {
        callbackFailure()
      }
    }.bind(this)

    Context.$store.dispatch('httpGet',sendData)
  },
  getGameItemType(Context,sendData,callbackSuccess,callbackFailure){
    if (!sendData.url) {
      sendData.url = "/getGameItemType"
    }
    if (!sendData.data) {
      sendData.data = {}
    }
    sendData.callbackSuccess = function (Success) {
      console.log("getGameItemType 成功",Success);
      // Context.$store.dispatch('dataForList',Success)
      if (callbackSuccess) {
        callbackSuccess(Success)
      }

    }.bind(this)
    sendData.callbackfail = function (fail) {
      console.log("getGameItemType 失败",fail);
      if (callbackFailure) {
        callbackFailure()
      }
    }.bind(this)

    Context.$store.dispatch('httpGet',sendData)
  },
  //创建游戏记录
  createHistory(Context,sendData,callbackSuccess,callbackFailure){
    if (!sendData.url) {
      sendData.url = "/createHistory"
    }
    if (!sendData.data) {
      sendData.data = {}
    }
    sendData.callbackSuccess = function (Success) {
      console.log("createHistory 成功",Success);
      // Context.$store.dispatch('dataForList',Success)
      if (callbackSuccess) {
        callbackSuccess(Success)
      }

    }.bind(this)
    sendData.callbackfail = function (fail) {
      console.log("createHistory 失败",fail);
      if (callbackFailure) {
        callbackFailure()
      }
    }.bind(this)

    Context.$store.dispatch('httpGet',sendData)
  },
  //获取游戏记录
  getHistory(Context,sendData,callbackSuccess,callbackFailure){
    if (!sendData.url) {
      sendData.url = "/getHistory"
    }
    if (!sendData.data) {
      sendData.data = {}
    }
    sendData.callbackSuccess = function (Success) {
      console.log("getHistory 成功",Success);
      Context.$store.dispatch('history',Success)
      if (callbackSuccess) {
        callbackSuccess(Success)
      }

    }.bind(this)
    sendData.callbackfail = function (fail) {
      console.log("getHistory 失败",fail);
      if (callbackFailure) {
        callbackFailure()
      }
    }.bind(this)

    Context.$store.dispatch('httpGet',sendData)
  },
  getUser(Context,sendData,callbackSuccess,callbackFailure){
    if (!sendData.url) {
      sendData.url = "/getUser"
    }
    if (!sendData.data) {
      sendData.data = {}
    }
    sendData.callbackSuccess = function (Success) {
      console.log("getUser 成功",Success);
      Context.$store.dispatch('User',Success)
      if (callbackSuccess) {
        callbackSuccess(Success)
      }

    }.bind(this)
    sendData.callbackfail = function (fail) {
      console.log("getHistory 失败",fail);
      if (callbackFailure) {
        callbackFailure()
      }
    }.bind(this)

    Context.$store.dispatch('httpGet',sendData)
  },
  test(Context,sendData){
    if (!sendData.url) {
      sendData.url = "/ping"
    }
    if (!sendData.data) {
      sendData.data = {}
    }
    sendData.callbackSuccess = function (Success) {
      console.log("ping 成功",Success);
      // Context.$store.dispatch('ping',Success)
    }.bind(this)
    sendData.callbackfail = function (fail) {
      console.log("ping 失败",fail);
    }.bind(this)

    Context.$store.dispatch('httpGet',sendData)
  },
  getListData(Context,sendData){
    if (!sendData.url) {
      sendData.url = "/getListData"
    }
    if (!sendData.data) {
      sendData.data = {}
    }
    sendData.callbackSuccess = function (Success) {
      console.log("getListData 成功",Success);
      Context.$store.dispatch('setGetTableList',Success)
    }.bind(this)
    sendData.callbackfail = function (fail) {
      console.log("getListData 失败",fail);
    }.bind(this)

    Context.$store.dispatch('httpGet',sendData)
  },
  getListDataMore(Context,sendData){
    if (!sendData.url) {
      sendData.url = "/getListDataMore"
    }
    if (!sendData.data) {
      sendData.data = {}
    }
    sendData.callbackSuccess = function (Success) {
      console.log("getListDataMore 成功",Success);
      Context.$store.dispatch('setGetTableListMore',Success)
    }.bind(this)
    sendData.callbackfail = function (fail) {
      console.log("getListDataMore 失败",fail);
    }.bind(this)

    Context.$store.dispatch('httpGet',sendData)
  },
  //直接访问网址 get
  getDataForUrl(Context,sendData,callbackSuccess,callbackFailure){
    if (!sendData.data) {
      sendData.data = {}
    }
    sendData.callbackSuccess = function (Success) {
      console.log("getDataForUrl 成功"+sendData,Success);
      // Context.$store.dispatch('setGetTableList',Success)
      if (callbackSuccess) {
        callbackSuccess(Success)
      }
    }.bind(this)
    sendData.callbackfail = function (fail) {
      console.log("getDataForUrl 失败"+sendData,fail);
      if (callbackFailure) {
        callbackFailure()
      }
    }.bind(this)

    Context.$store.dispatch('httpGetUrl',sendData)
  },

}
