import { listDept } from "@/api/system/dept.js";
import { listBuilding } from "@/api/taicangpop/building.js";

const xiaoqu = {
  state: {
    //页面接口参数
    queryParmas: {},
    mapParmas: {},
    netWorkintroduce: {},
    panel: {},
    houseItem: {},
    refreshBuild: 1,
    personNum: {},
    reset: false,
    clickResult: {}, //通过列表单机查看building + Houser
    erweiBuild: {},
  },
  mutations: {
    SET_CLICK_RESULT: (state, data) => {
      state.clickResult = data;
    },
    SET_PERSON_NUM: (state, data) => {
      state.personNum = data;
    },
    SET_RESET: (state, boole) => {
      state.reset = boole;
    },
    SET_REFRESH: (state, refresh) => {
      state.refreshBuild = refresh;
    },
    SET_ERWEI_BUILD: (state, data) => {
      for (let key in data) {
        state.erweiBuild[key] = data[key];
      }
    },
    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 (state.queryParmas.buildingId) {
        state.panel.rightPanel = true;
        state.panel.bottomPanel = true;
      } else {
        state.panel.rightPanel = false;
      }
      if (
        state.queryParmas.color ||
        state.queryParmas.type ||
        state.queryParmas.isd ||
        state.queryParmas.isk ||
        state.queryParmas.isx ||
        state.queryParmas.isj ||
        state.queryParmas.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.mapParmas = {
        sd: false,
        networkName: undefined,
      };
      state.houseItem = {};
      state.refreshBuild = 1;
      state.personNum = {
        zz: 0,
        cz: 0,
      };
      state.clickResult = {};
      state.erweiBuild = {
        open: false,
        title: "",
      };
    },
  },
  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;
