import{a as l,d as p}from"./vendor.d12b5734.js";import{h as o,u as d}from"./main.465728e1.js";const w=(u=!1)=>{const m=u?window.pinia.defineStore:p,{global:n}=window.i18n;return m({id:"users",state:()=>({roles:[],users:[],totalUsers:0,currentUser:null,selectAllField:!1,selectedUsers:[],customerList:[],userList:[],userData:{name:"",email:"",password:null,phone:null,companies:[]}}),actions:{resetUserData(){this.userData={name:"",email:"",password:null,phone:null,role:null,companies:[]}},fetchUsers(s){return new Promise((i,t)=>{l.get("/api/v1/users",{params:s}).then(e=>{this.users=e.data.data,this.totalUsers=e.data.meta.total,i(e)}).catch(e=>{o(e),t(e)})})},fetchUser(s){return new Promise((i,t)=>{l.get(`/api/v1/users/${s}`).then(e=>{var a,r;this.userData=e.data.data,((r=(a=this.userData)==null?void 0:a.companies)==null?void 0:r.length)&&this.userData.companies.forEach((c,f)=>{this.userData.roles.forEach(h=>{h.scope===c.id&&(this.userData.companies[f].role=h.name)})}),i(e)}).catch(e=>{console.log(e),o(e),t(e)})})},fetchRoles(s){return new Promise((i,t)=>{l.get("/api/v1/roles").then(e=>{this.roles=e.data.data,i(e)}).catch(e=>{o(e),t(e)})})},addUser(s){return new Promise((i,t)=>{l.post("/api/v1/users",s).then(e=>{this.users.push(e.data),d().showNotification({type:"success",message:n.t("users.created_message")}),i(e)}).catch(e=>{o(e),t(e)})})},updateUser(s){return new Promise((i,t)=>{l.put(`/api/v1/users/${s.id}`,s).then(e=>{if(e){let r=this.users.findIndex(c=>c.id===e.data.data.id);this.users[r]=e.data.data}d().showNotification({type:"success",message:n.t("users.updated_message")}),i(e)}).catch(e=>{o(e),t(e)})})},deleteUser(s){const i=d();return new Promise((t,e)=>{l.post("/api/v1/users/delete",{users:s.ids}).then(a=>{let r=this.users.findIndex(c=>c.id===s);this.users.splice(r,1),i.showNotification({type:"success",message:n.tc("users.deleted_message",1)}),t(a)}).catch(a=>{o(a),e(a)})})},deleteMultipleUsers(){return new Promise((s,i)=>{l.post("/api/v1/users/delete",{users:this.selectedUsers}).then(t=>{this.selectedUsers.forEach(a=>{let r=this.users.findIndex(c=>c.id===a.id);this.users.splice(r,1)}),d().showNotification({type:"success",message:n.tc("users.deleted_message",2)}),s(t)}).catch(t=>{o(t),i(t)})})},searchUsers(s){return new Promise((i,t)=>{l.get("/api/v1/search",{params:s}).then(e=>{this.userList=e.data.users.data,this.customerList=e.data.customers.data,i(e)}).catch(e=>{o(e),t(e)})})},setSelectAllState(s){this.selectAllField=s},selectUser(s){this.selectedUsers=s,this.selectedUsers.length===this.users.length?this.selectAllField=!0:this.selectAllField=!1},selectAllUsers(){if(this.selectedUsers.length===this.users.length)this.selectedUsers=[],this.selectAllField=!1;else{let s=this.users.map(i=>i.id);this.selectedUsers=s,this.selectAllField=!0}}}})()};export{w as u};
