import {
  queryAreas,
  lookAreasMap,
  queryAreasSpe,
} from "@/api/grid/draw-area";
import { businessBelongList } from "@/views/grid/create/utils";

const featuredAreas = {
  state: {
    layerData: [], // 图层列表
    shapeList: [], // 图形列表
    level: 1,
    isDrawing: false, // 是否在绘图
    layerInfo:{}, // 图层信息
    pageNum:1,
    layerDataSpe:[], // 特色网格-网格类型
  },

  mutations: {
    SET_LARERDATA: (state, data) => {
      state.layerData = data
    },
    SET_SHAPEDATA: (state, data) => {
      state.shapeList = data
    },
    SET_LEVEL: (state, level) => {
      state.level = level
    },
    SET_ISDRAW:(state, isDrawing) => {
      state.isDrawing = isDrawing
    },
    SET_LAYER_INFO:(state, layerInfo) => {
      state.layerInfo = layerInfo
    },
    SET_NUM:(state, pageNum) => {
      state.pageNum = pageNum;
    },
    SET_LARERDATA_SPE: (state, data) => {
      state.layerDataSpe = data
    },
  },

  actions: {
    // 获取特色网格-网格类型
    queryMenuSpe({
      commit
    }, params) {
      console.log(params)
      return new Promise((resolve, reject) => {
        queryAreasSpe({opsDept:params.opsDept}).then(res => {
          const {
            code,
            data
          } = res;
          if (code === 200) {
            const newData = data.map((item) => {
              return {
                ...item,
                id: item.layerId,
                label: item.layerName,
                color: item.layerColor,
              };
            });
            commit('SET_LARERDATA_SPE', newData)
          }
          resolve(data)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 获取特色区域
    queryMenu({
      commit
    }, params) {
      return new Promise((resolve, reject) => {
        queryAreas().then(res => {
          const {
            code,
            data
          } = res;
          if (code === 200) {
            const newData = data.map((item) => {
              return {
                ...item,
                id: item.layerId,
                label: item.layerName,
                color: item.layerColor,
              };
            });
            commit('SET_LARERDATA', newData)
          }
          resolve(data)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 查看全部区域
    lookAreas({
      commit
    }, params) {
      return new Promise((resolve, reject) => {
        commit('SET_NUM',params.pageNum);
        lookAreasMap({
          layerId: params.layerId,
          shapeId:params.shapeId,
          pageNum:params.pageNum || this.pageNum,
          pageSize:10,
        }).then(res => {
          let result={};
          if (res.code === 200) {
            result = res;
            const newData = res.rows.map((item) => {
              const index = businessBelongList.findIndex(i => i.key === item.opsDept)
              return {
                ...item,
                id: item.shapeId,
                label: item.shapeName,
                color: params.color,
                layerId: item.layerId,
                layerName: item.layerName,
                areades: `${ item?.businessCount || 0}企业, ${ item?.merchantCount || 0}商户, ${item?.retailCount || 0}个人`,
                opsDeptName: index > -1 ? businessBelongList[index].label : '',
                auditStatusName:item.auditStatus == '0'?'审批中':
                                item.auditStatus == '1'?'通过':
                                item.auditStatus == '2'?'拒绝':''
              };
            });
            commit('SET_SHAPEDATA', [{
              ...params,
              layerName: params.layerName,
              label: `${params.layerName}(${newData.length})`,
              level: 1,
              children: newData,
            }])
            commit('SET_LEVEL', 2);
          }
          resolve(result)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 设置导航菜单层级
    goBack({
      commit
    }, num) {
      commit('SET_LEVEL', num)
    },
    setIsDrawing({
      commit
    }, isDrawing) {
      commit('SET_ISDRAW', isDrawing)
    },
  }
}

export default featuredAreas
