/*
 * @LastEditors: gcz
 */
import { searchApi, smallClassMap } from "@/service/index.js";
const addr = {
  state: {
    page: "forest",
    parentId: "520000",
    parentName: "",
    mapLevel: "province",
    vuexSelectCity: "",
    vuexSelectDistrict: "",
    vuexSelectStreet: "",
    // 市
    vuexCityList: [],
    // 区
    vuexDistrictList: [],
    // 乡镇
    vuexStreetList: [],
    selectCity: {},
    selectDistrict: {},
    selectStreet: {},
    selectAddr: "贵州省",
    // 获取小班边界地图
    smallClassMapArr: {},
    // 根据小班号查询搜索小班
    smallClassSearch: []
  },
  mutations: {
    // 更改页面标识
    changePage(state, newPage) {
      state.page = newPage;
    },
    // 获取市
    changeCityList(state, cityList) {
      state.vuexCityList = cityList;
    },
    // 获取区
    changeDistrictList(state, districtList) {
      state.vuexDistrictList = districtList;
    },
    // 获取乡镇
    changeStreetList(state, streetList) {
      state.vuexStreetList = streetList;
    },
    // 城市选择data
    changeSelectCity(state, cityData) {
      state.selectCity = cityData;
    },
    // 区县选择data
    changeSelectDistrict(state, districtData) {
      state.selectDistrict = districtData;
    },
    // 乡镇选择data
    changeSelectStreet(state, streetData) {
      state.selectStreet = streetData;
    },
    changeSelectAddr(state, mapLevel) {
      // console.log("mapLevelmapLevel", mapLevel);
      // console.log('state', state);
      let cityName = null;
      if (state.selectCity.label == '黔东南苗族侗族自治州') {
        cityName = '黔东南州'
      } else if (state.selectCity.label == '黔南布依族苗族自治州') {
        cityName = '黔南州'
      } else if (state.selectCity.label == '黔西南布依族苗族自治州') {
        cityName = '黔西南州'
      }
      switch (mapLevel) {
        case "province":
          state.selectAddr = "贵州省";
          break;
        case "city":
          state.selectAddr = `${cityName || state.selectCity.label}`;
          break;

        case "district":
          state.selectAddr = `${state.selectDistrict.label}`;
          break;

        case "street":
          state.selectAddr = `${state.selectStreet.label}`;
          break;

        default:
          state.selectAddr = "贵州省";
          break;
      }
      // state.selectAddr = streetData;
    },
    changeSearchSmallClass(state, searchData) {
      state.smallClassSearch = searchData;
    },
    // 改变省市县级联选择获取数据
    changeParentId(state, n) {
      state.parentId = n.parentId;
      state.parentName = n.name;
      state.mapLevel = n.mapLevel;
    },
    // 获取小班边界
    getSmallClassMapArr(state, calssmap) {
      state.smallClassMapArr = calssmap;
    }
  },
  actions: {
    // 获取省市县
    searchArea({ commit }, params = {}) {
      console.log("searchArea", params);
      searchApi({ parentId: params.parentId }).then(res => {
        if (params.mapLevel == "city") {
          commit("changeDistrictList", res.data);
          commit("changeSelectDistrict", {});
          commit("changeSelectStreet", {});
        } else if (params.mapLevel == "district") {
          commit("changeStreetList", res.data);
          commit("changeSelectStreet", {});
        } else if (params.mapLevel == "province") {
          commit("changeCityList", res.data);
          commit("changeSelectCity", {});
          commit("changeSelectDistrict", {});
          commit("changeSelectStreet", {});
        }
        commit("changeParentId", params);
        commit("changeSelectAddr", params.mapLevel);
      });
    },
    // 获取小班边界
    getSmallClassMap({ commit }, params = {}) {
      smallClassMap(params).then(res => {
        console.log(res);
        commit("getSmallClassMapArr", res);
      });
    }
  }
};

export default addr;
