import axios from "axios";

//store/modules/user.js

// 声明变量
const state = {
  collapse: false,
  activeIndex: 0,
  username: "",
  password: "",
  // api: "/carstop/carBuy", // 线上
  // api: "/carstop/carbook", // 线上
  api: "/ride-sharing/carBuy", // 校团
  newsNum: "",
};

// 修改变量（state不能直接赋值修改，只能通过mutations）
const mutations = {
  menuClose: (state, value) => {
    state.collapse = value;
  },
  indexUp: (state, value) => {
    state.activeIndex = value;
  },
  userName: (state, value) => {
    state.username = value;
  },
  passWord: (state, value) => {
    state.password = value;
  },
  newsUp: (state, value) => {
    state.newsNum = value;
  },
};
// mutations的值由actions传入(异步)
const actions = {
  addAsync: ({ commit }) => {
    setTimeout(() => {
      commit("add");
    }, 1000);
  },
  newsAsync: async ({ commit, state }) => {
    let data = new FormData();
    let res = await axios({
      method: "post",
      url: state.api + "/carBook/usermesUnRead.action",
      headers: {
        token: sessionStorage.getItem("token"),
      },
      data: data,
    });
    console.log(res, "系统消息");

    commit("newsUp", res.data.u_count);
    sessionStorage.setItem("newsNum", res.data.u_count);
    console.log("vuex", res.data.u_count);
  },
  // 车辆管理
  busNum: async ({ commit, state }) => {
    let data = new FormData();
    // let res = await adminApi.adminLogin(data);
    let res = await axios({
      method: "post",
      url: state.api + "/carBook/cinfoqueryAll.action",
      headers: {
        token: sessionStorage.getItem("token"),
      },
      data: data,
    });
    console.log(res, "车辆管理");
    if (res.status == 200) {
      sessionStorage.setItem("busSelect", JSON.stringify(res.data.data));
    } else {
      // ElMessage({
      //   type: "error",
      //   showClose: true,
      //   message: res.data.message,
      //   center: true,
      // });
      // if (res.data.message == "token错误") {
      //   router.push({
      //     path: `/login`,
      //   });
      // }
    }

    // {
    //   let resss = await axios({
    //     method: "post",
    //     url: "https://car.meiyishuoo.com/ride-sharing/carBuy/carBook/cinfoqueryAll.action",
    //     headers: {
    //       // token: sessionStorage.getItem("token"),
    //     },
    //     // data: data,
    //   });
    //   console.log(resss, "车辆管理");
    // }
  },
  // 路线管理
  pathSelect: async ({ commit, state }) => {
    let data = new FormData();
    let res = await axios({
      method: "post",
      url: state.api + "/carBook/routequeryAll.action",
      headers: {
        token: sessionStorage.getItem("token"),
      },
      data: data,
    });
    res.data.data.forEach((item) => {
      if (item.is_repair == 0) {
        item.state = false;
      } else if (item.is_repair == 1) {
        item.state = true;
      }
      if (item.repair_end == "null") {
        item.repair_end = "";
      }
    });
    console.log(res, "路线管理");
    if (res.status == 200) {
      sessionStorage.setItem("route_endSelect", JSON.stringify(res.data.data));
      let obj2 = {};
      var reduce2 = res.data.data.reduce((curr, next) => {
        /*判断对象中是否已经有该属性  没有的话 push 到 curr数组*/
        obj2[next.route] ? "" : (obj2[next.route] = curr.push(next));
        return curr;
      }, []);
      sessionStorage.setItem("pathSelect", JSON.stringify(reduce2));
    } else {
      ElMessage({
        type: "error",
        showClose: true,
        message: res.data.message,
        center: true,
      });
    }
  },
  // 系统扫码时间
  sm_time: async ({ commit, state }) => {
    let res = await axios({
      method: "post",
      url: state.api + "/carBook/cnqueryHb.action",
      headers: {
        token: sessionStorage.getItem("token"),
      },
    });
    console.log(res.data, "系统扫码时间");

    if (res.data.code == 200) {
      sessionStorage.setItem("sm_start", res.data.data.sm_start);
      sessionStorage.setItem("sm_end", res.data.data.sm_end);
      sessionStorage.setItem("yy_end", res.data.data.yy_end);
    }
  },
};

export default {
  state,
  mutations,
  actions,
};
