// 1 => LOADING
// 2 => DONE
// 3 => ERROR
import * as menu_api from "../../api/system/menu.js?x=1"

export default  {
   namespaced: true,
   state: {
      search : '',
      search_status: 0,
      search_error_message: "",
      menu_level_0: [],
      menu_level_1: [],
      menu_level_2: [],
      total_menu: 0,
      //sipe add breadcrumb dan hak akses
      bread_crumb : "",
      is_page_allowed : true,
      dashboard:""
   },

   mutations: {
      update_bread_crumb(state,val) {
         state.bread_crumb= val
      },
      update_dashboard(state,val) {
         state.dashboard= val
      },
      update_is_page_allowed(state,val) {
         state.is_page_allowed = val
      },
      update_search(state,val) {
         state.search=val 
      },
      update_search_error_message(state,status) {
         state.search_error_message = status
      },
      update_search_status(state,status) {
         state.search_status = status
      },

    update_menu_level_0 (state, data) {
        state.menu_level_0 = data
    },

    update_menu_level_1 (state, data) {
    state.menu_level_1 = data
    },

    update_menu_level_2 (state, data) {
        state.menu_level_2 = data
    }
   },
   actions: {
      async get_menu(context) {
         context.commit("update_search_status",1)
         try {
            let resp= await menu_api.get_menu()
            if (resp.status != "OK") {
               context.commit("update_search_status",3)
               context.commit("update_search_error_message",resp.message)
               alert("nothave access")
            } else {
               context.commit("update_search_status",2)
               context.commit("update_search_error_message","")

               let x = resp.data
                if (x[0])
                    context.commit("update_menu_level_0", x[0]['p_0'])

                if (x[1])
                    context.commit("update_menu_level_1", x[1])

                if (x[2])
                    context.commit("update_menu_level_2", x[2])
            //    let data = {
            //       total : resp.data.total,
            //       records : resp.data.records
            //    }
               
            //    context.commit("update_status", data)
            }
            //sipe :
            // tambahan get bread_crumb
            resp = await menu_api.get_bread_crumb() 
            if (resp.status != "OK") {
               context.commit("update_search_status",3)
               context.commit("update_search_error_message",resp.message)
            } else {
               context.commit("update_search_status",2)
               context.commit("update_search_error_message","")
               context.commit("update_bread_crumb",resp.data.bread_crumb)
               context.commit("update_page_allowed",resp.data.is_page_allowed)
               context.commit("update_dashboard",resp.data.dashboard)
            }
         } catch(e) {
            context.commit("update_search_status",3)
            context.commit("update_search_error_message", e.message )
         }
      }
   }
}
