import Vue from 'vue';
import Vuex from 'vuex';
// 引入 axios
import axios from 'axios'
import Qs from 'qs'

Vue.use(Vuex);

window.http_ = {
  // baseUrl : "https://www.yuyekeji.cn/hallgame/api",
  // games : "https://www.yuyekeji.cn/games/",
  // image: "https://www.yuyekeji.cn/games/",
  baseUrl: "http://127.0.0.1:55555/hallgame/api",
  games : "http://127.0.0.1:10086/",
  image: "http://127.0.0.1:10086/",

}

const store = new Vuex.Store({


  // 定义状态
  state: {
    tableList: {},
    table: {},
    tableData: {},
    //客户端游戏列表数据
    dataForList: {},
    //游戏历史
    history: {},
    //用户数据
    User: {}
  },
  //设置状态
  mutations: {
    setUser(state, User) {
      state.User = User
      // console.log("进来了吗",state,tableList);
    },
    setTableList(state, tableList) {
      state.tableList = tableList
      // console.log("进来了吗",state,tableList);
    },
    tableData(state, tableData) {
      state.tableData = tableData
      // console.log("进来了吗",state,tableList);
    },
    //设置表结构
    setTable(state, table) {
      state.table = table
      // console.log("进来了吗",state,tableList);
    },
    setTableData(state, table) {
      state.tableData = table
      // console.log("进来了吗",state,tableList);
    },
    setTableListMore(state, tableList) {
      state.tableList = tableList
      console.log("添加完的数组是", state.tableList);
    },
    setDataForList(state, tableList) {
      // 变更状态
      state.dataForList = tableList

    },
    setHistory(state, history) {
      // 变更状态
      state.history = history

    },
  },
  //获取状态
  getters: {
    getUser(state) {
      return state.User;
      // console.log("进来了吗",state,tableList);
    },
    getTableList(state) {

      // if (state.tableList.data) {
      //   return state.tableList.data
      // }else{
      //   return []
      // }
      return state.tableList
    },
    getTable(state) {

      // if (state.tableList.data) {
      //   return state.tableList.data
      // }else{
      //   return []
      // }
      return state.table
    },
    getTableData(state) {

      // if (state.tableList.data) {
      //   return state.tableList.data
      // }else{
      //   return []
      // }
      return state.tableData
    },
    getDataForList(state) {
      return state.dataForList
    },
    getTableListMore(state) {
      return state.tableList
    },
    getHistory(state) {
      // 变更状态
      return state.history

    },
  },
  actions: {
    User(state, table) {
      // 变更状态
      state.commit('setUser', table)
    },
    table(state, table) {
      // 变更状态
      state.commit('setTable', table)
    },
    setTableListMore(state, tableList) {
      // 变更状态
      state.commit('setTableListMore', tableList)

    },
    tableData(state, tableData) {
      // 变更状态
      state.commit('setTableData', tableData)

    },
    dataForList(state, tableList) {
      // 变更状态
      state.commit('setDataForList', tableList)

    },
    history(state, history) {
      // 变更状态
      state.commit('setHistory', history)

    },

    httpGet(message, sendData) {
      console.log("准备去get", sendData);
      axios.request({
        url: http_.baseUrl + sendData.url,  // 后端地址
        method: 'get', // post发送复杂请求
        // transformRequest: [function (data) {
        //   // 对 data 进行任意转换处理
        //   return Qs.stringify(data)
        // }],
        //post 用 data
        // data: sendData.data,
        contentType: "application/json",
        //get 参数 用 params
        params: sendData.data,
      })
        .then(function (response) {
          // console.log("get请求  "+sendData.url+"  ----成功", response);
          // console.log("表添加条目----成功回调",sendData.callbackSuccess);
          if (sendData.callbackSuccess != null) {
            sendData.callbackSuccess(response);
          }
          // this.$store.dispatch('tableList',sendData.title)
        }.bind(this))
        .catch(function (error) {
          // console.log("表添加条目----失败", error);
          if (sendData.callbackfail != null) {
            sendData.callbackfail(error);
          }
        }.bind(this));
    },

    httpGetUrl(message, sendData) {
      console.log("准备去get", sendData);
      axios.request({
        url: sendData.url,  // 后端地址
        method: 'get', // post发送复杂请求
        // transformRequest: [function (data) {
        //   // 对 data 进行任意转换处理
        //   return Qs.stringify(data)
        // }],
        //post 用 data
        data: sendData.data,
        contentType: "application/json",
        //get 参数 用 params
        // params: sendData.data,
      })
        .then(function (response) {
          // console.log("get请求  "+sendData.url+"  ----成功", response);
          // console.log("表添加条目----成功回调",sendData.callbackSuccess);
          if (sendData.callbackSuccess != null) {
            sendData.callbackSuccess(response);
          }
          // this.$store.dispatch('tableList',sendData.title)
        }.bind(this))
        .catch(function (error) {
          // console.log("表添加条目----失败", error);
          if (sendData.callbackfail != null) {
            sendData.callbackfail(error);
          }
        }.bind(this));
    },

    httpPost(message, sendData) {
      console.log("准备去post", sendData);
      axios.request({
        url: http_.baseUrl + sendData.url,  // 后端地址
        method: 'post', // post发送复杂请求
        transformRequest: [function (data) {
          // 对 data 进行任意转换处理
          return Qs.stringify(data)
        }],
        //post 用 data
        data: sendData.data,
        contentType: "application/json",
        //get 参数 用 params
        // params: sendData.data,
      })
        .then(function (response) {
          // console.log("get请求  "+sendData.url+"  ----成功", response);
          // console.log("表添加条目----成功回调",sendData.callbackSuccess);
          if (sendData.callbackSuccess != null) {
            sendData.callbackSuccess(response);
          }
          // this.$store.dispatch('tableList',sendData.title)
        }.bind(this))
        .catch(function (error) {
          // console.log("表添加条目----失败", error);
          if (sendData.callbackfail != null) {
            sendData.callbackfail(error);
          }
        }.bind(this));
    },

  }
});

export default store;
