import { listDept } from "@/api/system/dept.js";
import { listBuilding } from "@/api/taicangpop/building.js";
const xiaoqu = {
  state: {
    //页面接口参数
    queryParmas: {},
    mapParmas: {
      sd: false,
      networkName: undefined,
    },
    netWorkintroduce: {},
    panel: {},
    houseItem: {},
  },
  mutations: {
    SET_PANEL: (state, boolean) => {
      for (let key in boolean) {
        state.panel[key] = boolean[key];
      }
    },
    SET_BUILDING_WIDTH: (state, data) => {
      state.houseItem = data;
    },
    //参数变化
    CHANGE_QUERY: (state, query) => {
      for (let key in query) {
        state.queryParmas[key] = query[key];
      }
      if (query.buildingId) {
        state.panel.rightPanel = true;
        state.panel.bottomPanel = true;
        return;
      }
      {
        state.panel.rightPanel = false;
      }

      if (
        query.color ||
        query.type ||
        query.isd ||
        query.isk ||
        query.isx ||
        query.isj ||
        query.parentid
      ) {
        state.panel.bottomPanel = false;
      }
    },
    CHANGE_QUERY_MAP: (state, query) => {
      for (let key in query) {
        state.mapParmas[key] = query[key];
      }
    },
    SET_NETWORK_INTRODUCE: (state, data) => {
      state.netWorkintroduce = data;
    },
    //重置参数
    RESET_QUERY: (state, query) => {
      state.panel = {
        leftPanel: false,
        rightPanel: false,
        bottomPanel: true,
      };
      state.netWorkintroduce = {
        intro: "",
        list: [],
      };
      state.queryParmas = {
        xiaoquId: undefined, //小区
        parentid: undefined, //网格
        buildingId: undefined, //楼栋
        deptId: undefined, //苑id
        color: undefined,
        type: undefined,
        isd: undefined,
        isk: undefined,
        isx: undefined,
        isj: undefined,
      };
      state.houseItem = {};
    },
  },
  actions: {
    async GetBuildingId({ commit, state }, queryParams) {
      let deptData = await getDeptId({
        xiaoquId: state.queryParmas.xiaoquId,
        deptName: queryParams.deptName,
      });
      let buildingData = await getBuildingId({
        name: queryParams.name,
        deptId: deptData.deptId,
      });
      commit("CHANGE_QUERY", {
        buildingId: buildingData.id,
        // deptId: deptData.deptId,
      });
    },
  },
};

// 获取苑id
async function getDeptId(query) {
  let res = await listDept(query);
  return res.data[0];
}
//获取楼栋id
async function getBuildingId(query) {
  let res = await listBuilding(query);
  return res.rows[0];
}

export default xiaoqu;
