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

export default {
  namespaced: true,
  state: {
    house:"",
    regionId: null,
    layoutId: null,
    buildingNum: null,
    unit:null,
    houseDetail: {

    },
    regionColums: [],
	    layoutColums: [],
	    buildNums: [],
	    units: [],
	    houseList: []
  },
  actions: {
    fetchData({ commit, state }, ids) {
      return api.request("get", api.getURL("houseLayout/delete"), {
        ids: ids.join(",")
      });
    },
    selectListRegion({ commit, state }, payload) { // 项目下拉
      if(state.regionColums.length > 0) {
        return {
          data: {
            data: state.regionColums
          }
        };
      }
      return api.request("get", api.getURL("houseSailWebsite/listRegion"), payload).then(res => {				
				
        if(res) {					
          //
          
          commit("SET_REGION_LIST", res.data.data);
        }
        return res;
      });
    },
    selectListHouseType({ commit, state }) { // 户型下拉
      return api.request("get", api.getURL("houseLayout/listSelect"), {
        regionId: state.regionId
      }).then(res => {				
        //
        if(res) {					
          //
          commit("SET_LAYOUT_LIST", res.data.data);
        }
        return res;
      });
    },
    getBuildingNum( { commit, state }) {
      return api.request("get", api.getURL("cAPPHouse/buildingNumsNotRent"), {
				 pn: 1,
				 pageSize: 30000,
				 regionId: state.regionId,
				 layoutId: state.layoutId
      }).then(res => {
        if(res && res.data.data.list.length > 0) {
          commit("SET_CURRENT_BUILDINGNUM", res.data.data.list[0].buildingNum);
        }
        commit("SET_BUILD_NUMS", res.data.data.list);
        return res;
      });
    },
    getUnit( { commit, state }) {
      return api.request("get", api.getURL("cAPPHouse/unitsNotRent"), {
				 pn: 1,
				 pageSize: 30000,
				 regionId: state.regionId,
				 layoutId: state.layoutId,
				 buildingNum: state.buildingNum
      }).then(res => {
        if(res && res.data.data.list.length > 0) {
          commit("SET_CURRENT_UNIT", res.data.data.list[0].unit);
        }
        commit("SET_UNIT_NUMS", res.data.data.list);
        return res;
      });
    },
    getHouseList( { commit, state }, params) {
      return api.request("get", api.getURL("cAPPHouse/housesNotRent"), {
        pn: params.currentPage,
        pageSize: 10,
        regionId: state.regionId,
        layoutId: state.layoutId,
        buildingNum: state.buildingNum,
        unit: state.unit
      }).then(res => {
        if(res && res.data.data.list.length > 0 && !params.isScroll) {
          commit("SET_HOUSE_DETAIL", res.data.data.list[0]);
        }
        if(params.isScroll) {
          commit("SET_SCROLL_HOUSE_LIST", res.data.data.list);
        }else {
          commit("SET_HOUSE_LIST", res.data.data.list);
        }
        return res;
      });
    },
    chooseHouseCommit( { commit, state, rootState }, params) {
      return api.request("post", api.getURL("cAPPHouse/newchooseHouseCommit"), params).then(res => {
        return res;
      });
    }
  },
  mutations: {
    INIT_DATA_STATUS(state, data) {
			
    },
    SET_REGION_LIST(state, list) {
      list.map(item => {
        item.text = item.name;
      });
      Vue.set(state, "regionColums", list);
    },
    SET_LAYOUT_LIST(state, list) {
      //
      list.map(item => {
        item.text = item.layoutName;
      });
      Vue.set(state, "layoutColums", list);
    },
    SET_CURRENT_REGION_ID(state, id) {
      Vue.set(state, "regionId", id);
    },
    SET_CURRENT_LAYOUT_ID(state, id) {
      Vue.set(state, "layoutId", id);
    },
    SET_BUILD_NUMS(state, list) {
      list.map(item => {
        item.buildingNumText = item.buildingNum + "栋";
      });
      Vue.set(state, "buildNums", list);
    },
    SET_UNIT_NUMS(state, list) {
      list.map(item => {
        item.unitText = item.unit + "单元";
      });
      Vue.set(state, "units", list);
    },
    SET_CURRENT_BUILDINGNUM(state, num) {
      Vue.set(state, "buildingNum", num);
    },
    SET_CURRENT_UNIT(state, unit) {
      Vue.set(state, "unit", unit);
    },
    SET_SCROLL_HOUSE_LIST(state, list) {
      list.map(item => {
        state.houseList.push(item);
      });
    },
    SET_HOUSE_LIST(state, list) {
      Vue.set(state, "houseList", list);
    },
    CLEAR_HOUSE_LIST(state,data) {
      state.houseList = [];
    },
    CLEAR_BUILDNUMS(state,data) {
      state.buildNums = [];
    },
    CLEAR_UNINTS(state,data) {
      state.units = [];
    },
		

    SET_HOUSE_DETAIL(state, house) {
      Vue.set(state, "houseDetail", house);
    }
  },
  getters: {}
};