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

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: "",
      //tambahan jumlah notification
      notification_count: 0,
      change_password_dialog: false,
      change_password_error: "",
      branch: {},
      total: 0,
      notification_list: [],
      dialogNotification: false
   },

   mutations: {
      update_branch(state,val) {
         state.branch = val
      },
      update_change_password_dialog(state,val) {
         state.change_password_dialog = val
      },
      update_change_password_error(state,val) {
         state.change_password_error= val
      },
      update_bread_crumb(state,val) {
         state.bread_crumb = val
      },
      update_dashboard(state,val) {
         state.dashboard= val
      },
      update_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
      },

      update_total(state, val) {
         state.total = val
      },

      update_notification_list(state, val) {
         state.notification_list = val
      },

      update_dialogNotification(state, val) {
         state.dialogNotification = val
      }
   },
   actions: {
      async get_menu(context) {
         context.commit("update_search_status",1)
         try {

            let prm = {token:one_token(),path:window.location.pathname}
            let resp= await menu_api.get_menu(prm)
            if (resp.status != "OK") {
               context.commit("update_search_status",3)
               context.commit("update_search_error_message",resp.message)
               //alert('adsda')
               window.location = "/one-ui/test/vuex/one-space/";
            } 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])
            }
            //sipe :
            // tambahan get bread_crumb
            resp = await menu_api.get_bread_crumb(one_token()) 
            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_branch",resp.data.branch)
               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 )
         }
      },
      async change_password(context,prm) {
         try {
            let resp = await menu_api.change_password(prm)
            if (resp.status != "OK") {
               context.commit("update_change_password_error",resp.message)
            } else {
               context.commit("update_change_password_error","")
            }
         } catch(e) {
            console.log(e)
         } 
      },
      async getlistnotif(context, prm) {
         context.commit("update_search_status",1)
         try {
            let prm = {
               token: one_token()
            }
            let resp = await menu_api.getlistnotif(prm)
            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", "")

               let data = {
                  records: resp.data.records,
                  total: resp.data.total
               }

               context.commit("update_notification_list", data.records)
               context.commit("update_total", data.total)
            }
         } catch (e) {
            context.commit("update_search_status",3)
            context.commit("update_search_error_message", e.message )
         }
      },
      async readnotif(context, prm) {
         context.commit("update_search_status",1)
         try {
            prm.token = one_token()
            let resp = await menu_api.readnotif(prm)
            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_dialogNotification", false)
               window.location = BASE_URL + "/one-ui/" + prm.url;
               context.dispatch("getlistnotif")
            }
         } catch (e) {
            context.commit("update_search_status",3)
            context.commit("update_search_error_message", e.message )
         }
      }
   }
}
