import Vue from "vue";
import Vuex from "vuex";

import loginModule from "./modules/loginStore"; //登陆数据状态管理

import applyModule from "./modules/applyStore/applyStore"; //申请房源相关数据状态管理

import repairModule from "./modules/repairStore/repairStore.js"; //维保相关数据状态管理

import onlineHouseModule from "./modules/houseStore/onlineHouseStore"; //在线选房相关数据状态管理

import feedbackModule from "./modules/feedbackStore/feedbackStore"; //投诉建议相关数据状态管理

import activityModule from "./modules/activityStore"; //活动的状态管理模块

import bindHouseModule from "./modules/bindStore/bindHouseStore"; //绑定房源的状态管理模块

import subscribeModule from "./modules/subscribeStore/subscribeStore"; //预约相关的状态管理模块


import findMoreRegionsModule from "./modules/homeStore"; //首页热门项目管理模块

import finddetailOnCAppModule from "./modules/houseStore/finddetailOnCAppStore"; //项目户型详情管理模块

import layoutListModule from "./modules/houseStore/layoutListStore"; //户型列表管理模块

import sampleRoomModule from "./modules/houseStore/sampleRoomStore"; //样板间列表管理模块

import addCollectionModule from "./modules/houseStore/addCollectionStore"; //用户收藏管理模块

import cleanSearchModule from "./modules/searchStore/cleanSearchStore"; //简洁搜索管理模块

import allActivityModule from "./modules/activityStore/allActivityStore"; //查看所有活动管理模块

import serviceModule from "./modules/serviceStore/serviceStore"; //查看所有活动管理模块
import messageModule from "./modules/serviceStore/messageStore"; //查看所有消息管理模块

import couponModule from "./modules/couponStore/couponStore"; //优惠券管理模块

import upadtePassModule from "./modules/personalInformationStore/updatePassStore"; //修改密码
import updatePasswordModule from "./modules/personalInformationStore/updatePassStore"; //修改密码
import updatePhoneModule from "./modules/personalInformationStore/updatePhoneStore"; //修改手机号
import updateUserModule from "./modules/personalInformationStore/updateUserStore"; //修改用户昵称/头像/实名认证


import commonproblemModule from "./modules/commonProblemStore/findpageStore"; //修改用户昵称/头像/实名认证


import deviceModule from "./modules/deviceControllerStore/remoteOpenStore"; //智能门锁
 
import myBillModule from "./modules/billStore/myBillStore"; //我的账单



import api from "@/api/index.js";

import weixin from "@/utils/wx.config.js";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    loading: false,
    telephone: "22222",
    columsOptions: {
      Occupation: []
    },
    modalData: {
      apply: {
        visible: false
      },
      subscribe: {
        visible: false
      },
      activity: {
        visible: false
      },
      maintenance: {
        visible: false
      }
    },
    regionName: null,
    buildNumName: null,
    unitName: null,
    floorName: null,
    houseCode: null,

    regionId: null,
    buildingNum:null,
    unit: null,
    floor: null,
    filterOption: {
      region: {
        visible: false,
        selected: "",
        options: []
      },
      room: {
        visible: false,
        selected: "",
        options: []
      },
      unit: {
        visible: false,
        selected: "",
        options: []
      },
      floor: {
        visible: false,
        selected: "",
        options: []
      },
      roomNumber: {
        visible: false,
        selected: "",
        options: []
      },
      relation: {
        visible: false,
        selected: "parent",
        options: [
          {
            label: "父母",
            value: "em_contact_re_parent"
          },
          {
            label: "子女",
            value: "em_contact_re_children"
          },
          {
            label: "配偶",
            value: "em_contact_re_spouse"
          },
          {
            label: "朋友",
            value: "em_contact_re_firend"
          },
          {
            label: "同事",
            value: "em_contact_re_workmate"
          },
          {
            label: "其他",
            value: "em_contact_re_other"
          }
        ]
      }
    }
  },
  actions: {
    initWxShare({commit, state}) {
      let url = location.href.split("#")[0];
      //
      return api
        .request("get",api.getURL("WxShare/sign"), {
          url: url
        }).then(res =>{
          //
          //commit('SET_GLOBAL_PHONE', res.data.data.phone)
          weixin.init(res.data);
          return res;
        });
    },
    selectServiceTel({commit,state}){
      return api
        .request("get",api.getURL("parameterSetting/selectServiceTel")).then(res =>{
          //
          commit("SET_GLOBAL_PHONE", res.data.data.phone);
          return res;
        });
    },/*
     delete({ commit, state }, info) {
       return api.request("get", api.getURL("systemMsg/delete"), {
        ids: info.ids,
       }).then(res => {
         
         return res;
       });
     },*/
    fetchSelectDataType({ commit, state }, typeName) {
      //if (state.columsOptions.length > 0) return;
      if(typeName !== "LayoutIntent"){ // 特殊情况 选择户型 判断渔安新城无单间
        if(state[typeName]) return; //对已缓存的数据无需发送请求
      }      
      return api
        .request("get", api.getURL("dictionary/selectByPCode"), {
          code: typeName
        })
        .then(res => {
          if (!res) return;
          let data = res.data;
          //
          let opt = data.data.map(item => {
            return {
              value: item.value,
              text: item.name || item.remark
            };
          });
          commit("GET_SELECT_HOUSE_TYPE", {
            list: opt,
            keyName: typeName
          });
        });
    },
    commitBindHouse( { commit, state }, params) {
      return api.request("post", api.getURL("cAPPHouse/commitBindHouse"), {
        houseId: state.houseId,
        relationship: state.filterOption["relation"].selected
      }).then(res => {
        return res;
      });
    },
    sendDiffList( { commit, state }, payload) {
      let urlMap = {
        region: {
          url: api.getURL("cAPPHouse/regions"),
          params: {
            pn: 1,
            pageSize: 2000 * payload.times
          }
        },
        room: {
          url: api.getURL("cAPPHouse/buildingNums"),
          params: {
            pn: 1,
            pageSize: 2000 * payload.times,
            regionId: state.regionId
          }
        },
        unit: {
          url: api.getURL("cAPPHouse/units"),
          params: {
            pn: 1,
            pageSize: 2000 * payload.times,
            regionId: state.regionId,
            buildingNum: state.buildingNum
          }
        },
        floor: {
          url: api.getURL("cAPPHouse/floors"),
          params: {
            pn: 1,
            pageSize: 2000 * payload.times,
            regionId: state.regionId,
            buildingNum: state.buildingNum,
            unit: state.unit
          }
        },
        roomNumber: {
          url: api.getURL("cAPPHouse/houses"),
          params: {
            pn: 1,
            pageSize: 2000 * payload.times,
            regionId: state.regionId,
            buildingNum: state.buildingNum,
            unit: state.unit,
            floor: state.floor
          }
        },
        // relation: {

        // }
      };
      return api.request("get", urlMap[payload.type].url, urlMap[payload.type].params).then(res => {
        
        if(payload.type === "room") {
          res.data.data.list.map(item => {
            item.name = item.buildingNum;
            item.id = item.buildingNum;
          });
        }
        if(payload.type === "unit") {
          res.data.data.list.map(item => {
            item.name = item.unit;
            item.id = item.unit;
          });
        }
        if(payload.type === "floor") {
          res.data.data.list.map(item => {
            item.name = item.floor;
            item.id = item.floor;
          });
        }
        if(payload.type === "roomNumber") {
          res.data.data.list.map(item => {
            item.name = item.houseCode;
            item.id = item.houseId;
          });
        }
        commit("UPDATE_FILTER_OPTION_LIST", {
          type: payload.type,
          list: res.data.data.list
        });
        return res.data.data;
      });
    }
  },
  mutations: {
    SET_GLOBAL_PHONE(state, phone) {
      
      Vue.set(state, "telephone", phone);
    },
    GET_SELECT_HOUSE_TYPE(state, data) {
      //
      Vue.set(state, data.keyName, data.list);
    },
    UPDATE_LOADING_STATUS(state, status) {
      state.loading = status;
      //更新全局loading状态
    },
    UPDATE_STATUS_VISIBLE(state, type) {
      state.modalData[type].visible = !state.modalData[type].visible;
    },
    UPDATE_FILTER_HOUSE_OPTION(state, type) {
      state.filterOption[type].visible = !state.filterOption[type].visible;
    },
    UPDATE_FILTER_OPTION_LIST(state, payload) {
      state.filterOption[payload.type].options = payload.list;
      // payload.list.map(item => {
      //   state.filterOption[payload.type].options.push(item)
      // })
    },
    SET_CURRENT_REGION_ID(state, payload) {
      state.regionName = payload.name;
      state.regionId = payload.id;
    },
    SET_CURRENT_BUILDINGNUM(state, payload) {
      state.buildNumName = payload.name;
      state.buildingNum = payload.id;
    },
    SET_CURRENT_UNIT(state, payload) {
      state.unitName = payload.name;
      state.unit = payload.id;
    },
    SET_CURRENT_FLOOR(state, payload) {
      state.floorName = payload.name;
      state.floor = payload.id;
    },
    SET_CURRENT_HOUSEID(state, payload) {
      state.houseCode = payload.name;
      state.houseId = payload.id;
    }
  },
  getters: {

  },
  modules: {
    loginModule,
    repairModule,
    subscribeModule,
    feedbackModule,
    onlineHouseModule,
    applyModule,
    activityModule,
    bindHouseModule,
    findMoreRegionsModule,
    finddetailOnCAppModule,
    upadtePassModule,
    updatePasswordModule,
    updatePhoneModule,
    updateUserModule,
    commonproblemModule,
    layoutListModule,
    sampleRoomModule,
    addCollectionModule,
    cleanSearchModule,
    allActivityModule,
    serviceModule,
    messageModule,
    couponModule,
    deviceModule,
    myBillModule
  }
});