import { getYj } from "@/api/taicangpop/block.js";
import {
  getxiaoqu,
  getTotaldata,
  echartsData,
  getPersonData,
} from "@/api/home";
import { getColorList } from "@/api/taicangpop/new.js";

const taicang = {
  state: {
    queryParams: {
      shequId: "",
      xiaoquId: "",
    },
    yjTotal: 0,
    leftColor: {
      HZ: "#008c5e",
      HL: "#008c5e",
      HO: "#008c5e",
      HF: "#008c5e",
      NZ: "#7f8c95",
      NL: "#7f8c95",
      NO: "#7f8c95",
      NF: "#7f8c95",
    },
    rightTopColor: {
      HZ: "#008c5e",
      HL: "red",
      HO: "#7f8c95",
      HF: "#008c5e",
      NZ: "#008c5e",
      NL: "red",
      NO: "#7f8c95",
      NF: "#008c5e",
    },
    xiaoquIntroduce: {
      intro: {
        shequ: {},
        kaiguo: {
          zhuzhai: {},
        },
      },
    },
    colorListL: [],
    threeCell: {
      houseTotal: 0,
      personTotal: 0,
      netTotal: 0,
      buildingTotal: 0,
    },
    pie: {
      total: 0,
      update: 0,
    },
  },
  mutations: {
    SET_QUERY_PARAMS: (state, query) => {
      state.queryParams.shequId = query.shequId;
      state.queryParams.xiaoquId = query.xiaoquId;
    },
    SET_YJ_TOTAL: (state, total) => {
      state.yjTotal = total;
    },
    SET_INTRODUCE: (state, introduce) => {
      state.xiaoquIntroduce = introduce;
    },
    SET_COLOR_LIST: (state, list) => {
      state.colorListL = handleColor(state.colorListL, list);
    },
    CLEAR_COLOR: (state) => {
      let colorListL = [
        { name: "HZ", house: 0, person: 0 },
        { name: "HL", house: 0, person: 0 },
        { name: "HO", house: 0, person: 0 },
        { name: "HF", house: 0, person: 0 },
        { name: "NZ", house: 0, person: 0 },
        { name: "NL", house: 0, person: 0 },
        { name: "NO", house: 0, person: 0 },
        { name: "NF", house: 0, person: 0 },
        { name: "户籍", house: 0, person: 0 },
        { name: "流动", house: 0, person: 0 },
        { name: "中共党员", house: 0, person: 0 },
        { name: "帮扶对象", house: 0, person: 0 },
        { name: "重点人群", house: 0, person: 0 },
        { name: "退伍军人", house: 0, person: 0 },
      ];

      state.colorListL = resetColor(
        colorListL,
        state.leftColor,
        state.rightTopColor
      );
    },
    SET_THREE_CELL: (state, obj) => {
      state.threeCell = {
        houseTotal: obj["网格户数据"],
        personTotal: obj["网格人数据"],
        netTotal: obj["网格总数数据"],
        buildingTotal: obj["网格楼栋数据"],
      };
    },
    SET_PIE: (state, obj) => {
      state.pie = {
        total: obj["所有户数据"],
        update: obj["修改户数据"],
      };
    },
  },
  actions: {
    /**预警总数 */
    GetYjtotal({ commit }, queryParams) {
      return new Promise((resolve, reject) => {
        getYj(queryParams).then((res) => {
          commit("SET_YJ_TOTAL", res.data);
          resolve();
        });
      });
    },

    /**
     * 获取小区详情
     */
    GetXiaoquInfo({ commit }, deptId) {
      return new Promise((resolve, reject) => {
        getxiaoqu(deptId).then((res) => {
          res.data.intro = JSON.parse(res.data.intro);
          commit("SET_INTRODUCE", res.data);
          resolve();
        });
      });
    },

    /**获取colorList */
    GetcolorList({ commit }, queryParams) {
      return new Promise((resolve, reject) => {
        commit("CLEAR_COLOR");
        getColorList(queryParams).then((res) => {
          commit("SET_COLOR_LIST", res.data);
          resolve();
        });
      });
    },
    /**
     * 获取户总数 /人口数 / 网格总数 / 楼栋总数
     */
    getThreeCell({ commit }, queryParams) {
      return new Promise((resolve, reject) => {
        getTotaldata(queryParams).then((res) => {
          commit("SET_THREE_CELL", res.data);
          resolve();
        });
      });
    },
    /**
     * 获取住户 / 居民 饼图数据
     */
    GetPie({ commit }, queryParams) {
      return new Promise((resolve, reject) => {
        if (queryParams.type == "house") {
          delete queryParams.type;
          echartsData(queryParams).then((res) => {
            commit("SET_PIE", res.data);
            resolve();
          });
        } else {
          delete queryParams.type;
          getPersonData(queryParams).then((res) => {
            commit("SET_PIE", res.data);
            resolve();
          });
        }
      });
    },
  },
};

function handleColor(colorList, list) {
  colorList.map((item) => {
    let index = list.findIndex((it) => it.name == item.name);
    if (index > -1) {
      item.house = list[index].house;
      item.person = list[index].person;
    }
  });

  return colorList;
}

function resetColor(colorList, leftColor, rightTopColor) {
  colorList.map((item) => {
    let isColor = /^[a-zA-Z]+$/.test(item.name);
    if (isColor) {
      item.color = item.name;
      for (let key in leftColor) {
        if (item.name == key) {
          item.leftColor = leftColor[key];
          item.rightTopColor = rightTopColor[key];
        }
      }
    }
  });
  return colorList;
}

export default taicang;
