import{_ as P,r,o as l,c as d,w as m,j as p,d as o,k as F,e as i,t as k,b as E}from"./index-be2d4151.js";import{S as h}from"./SurveySectionService-39f07999.js";import{L,C as D}from"./index-124eb047.js";import{C as I,E as V,D as T}from"./EditButton-6b424ab5.js";import{I as q}from"./index-4b46c3db.js";import{I as G}from"./index-f10fdf98.js";import{L as B}from"./Loading-c46365ec.js";import{P as Y}from"./index-8cbf5e02.js";import{l as N}from"./lodash-cc03f756.js";import"./InfoButton.vue_vue_type_style_index_0_scoped_635db2d1_lang-ea4dfeb4.js";import{C as U}from"./CloseButton-91fa6b44.js";import{S as j}from"./SubmitButton-1d5ea630.js";import{U as Q}from"./UpdateButton-331adda6.js";import{F as A}from"./index-5f541e4d.js";import{F as z}from"./default-f38ac8a4.js";import{T as H}from"./index-9c004d68.js";import{T as J}from"./index-5600e357.js";import{S as K}from"./SurveyService-8283c5c9.js";import{M as W}from"./Multiselect-89305a7f.js";import{c as X,a as S,V as Z}from"./array-9fca5ec5.js";import"./logo-black-d3c46f69.js";import"./logo-white-e1b05cf3.js";import"./transition-4fc77ecd.js";import"./vue-cleave.min-19d21cac.js";const $={components:{FormModal:A,FormGroup:z,TextInput:H,TextArea:J,SubmitButton:j,UpdateButton:Q,CloseButton:U,Loading:B,Multiselect:W},emits:["reload","close"],data(){return{surveyOptions:[],form:{survey_id:"",name:""},errors:{},state:{isEdit:!1,isSaving:!1,isLoading:!1}}},methods:{async fetchSurveys(){await K.fetchRecords().then(e=>{e.status==200&&(this.surveyOptions=e.data.data.map(t=>({value:t.id,label:t.name})))}).catch(e=>{console.log(e)})},loadRecord(e){this.resetForm(),this.state.isEdit=!0,this.form=e},closeModal(){this.state.showModal=!1},resetForm(){this.state.isEdit=!1,this.form={survey_id:"",name:""}},reload(){this.$emit("reload")},async saveRecord(){if(this.state.isSaving=!0,!this.state.isEdit)this.state.isSaving=!1,await h.create(this.form).then(e=>{e.status==201&&(this.$swal.fire({title:"Great!",text:"Record successfully saved.",icon:"success",timer:2e3}),this.state.showModal=!1,this.resetForm(),this.reload())}).catch(e=>{console.log(e),e.response.status==422?this.$swal.fire({title:"Ooops! something is wrong.",text:e.response.data.message,icon:"warning"}):this.$swal.fire({title:"Ooops! something is wrong.",text:"Please contact system administrator",icon:"error",timer:2e3})});else{const e=this.form.id;await h.update(e,this.form).then(t=>{t.status==200?(this.$swal.fire({title:"Great!",text:"Record successfully updated.",icon:"success",timer:2e3}),this.state.showModal=!1):(this.$swal.fire({title:"Ooops! something is wrong.",text:"Please contact system administrator",icon:"error",timer:2e3}),this.state.showModal=!1)}).catch(t=>{console.log(t),t.response.status==422?this.$swal.fire({title:"Ooops! something is wrong.",text:t.response.data.message,icon:"warning"}):this.$swal.fire({title:"Ooops! something is wrong.",text:"Please contact system administrator",icon:"error",timer:2e3})}).finally(()=>{this.state.isSaving=!1,this.reload()})}},async submitForm(){try{await X().shape({survey_id:S().required("Survey field is required"),name:S().required("Name field is required").min(2)}).validate(this.form,{abortEarly:!1}),this.$swal.fire({title:"Are you sure you want to save this record?",text:"",icon:"section",showCancelButton:!0,confirmButtonColor:"#34c38f",cancelButtonColor:"#f46a6a",confirmButtonText:"Yes",cancelButtonText:"No"}).then(t=>{t.value&&this.saveRecord()})}catch(e){if(e instanceof Z){const t={};e.inner.forEach(n=>{t[n.path]=n.message}),this.errors=t}this.state.isSaving=!1}}},Validating(e){console.log(e)},mounted(){this.fetchSurveys()}},ee={class:"lg:p-2"},te={class:"relative"},se={class:"flex gap-1 justify-end"};function oe(e,t,n,y,s,a){const f=r("Loading"),_=r("Multiselect"),g=r("FormGroup"),v=r("TextInput"),w=r("SubmitButton"),b=r("UpdateButton"),C=r("CloseButton"),x=r("FormModal");return l(),d(x,{title:s.state.isEdit?"Edit Record":"Create Record",active:s.state.showModal,onSubmit:a.submitForm,onClose:a.closeModal,themeClass:s.state.isEdit?"bg-primary-600":"bg-primary-800",sizeClass:"lg:w-1/2"},{default:m(()=>[s.state.isEdit&&s.state.isLoading?(l(),d(f,{key:0,text:"Fetching record"})):p("",!0),o("div",ee,[o("form",{onSubmit:t[2]||(t[2]=F((...u)=>a.submitForm&&a.submitForm(...u),["prevent"])),class:"space-y-4"},[s.state.isEdit&&s.state.isLoading?(l(),d(f,{key:0,text:"Fetching record"})):p("",!0),s.state.isSaving?(l(),d(f,{key:1,text:"Saving record"})):p("",!0),i(g,{label:"Survey",isRequired:!0},{default:m(()=>[o("div",te,[i(_,{modelValue:s.form.survey_id,"onUpdate:modelValue":t[0]||(t[0]=u=>s.form.survey_id=u),"close-on-select":!0,searchable:!0,id:"survey_id",options:s.surveyOptions,required:!0},null,8,["modelValue","options"])])]),_:1}),i(g,{label:"Name",isRequired:!0},{default:m(()=>[i(v,{type:"text",placeholder:"Input name ",modelValue:s.form.name,"onUpdate:modelValue":t[1]||(t[1]=u=>s.form.name=u),error:s.errors.name,classInput:"h-[48px]",required:!0},null,8,["modelValue","error"])]),_:1}),o("div",se,[s.state.isEdit?(l(),d(b,{key:1,onClick:a.submitForm},null,8,["onClick"])):(l(),d(w,{key:0,onClick:a.submitForm},null,8,["onClick"])),i(C,{onClick:a.closeModal},null,8,["onClick"])])],32)])]),_:1},8,["title","active","onSubmit","onClose","themeClass"])}const re=P($,[["render",oe]]),ae={setup(){},components:{Layout:L,CreateButton:I,Loading:B,InputGroup:G,EditButton:V,DeleteButton:T,Form:re,Pagination:Y,Card:D,Icon:q},data(){return{title:"Sections",breadcrumbs:[{text:"Home",href:"/"},{text:"Survey",href:"/surveys"},{text:"Sections",href:"/survey-sections",active:!0}],columns:[{label:"#",field:"#",sortable:!1},{label:"Survey",field:"survey_name",sortable:!0},{label:"Name",field:"name",sortable:!0},{label:"Created",field:"created_at",sortable:!1},{label:"Action",field:"action",sortable:!1}],perPageOptions:[{value:"10",label:"10"},{value:"50",label:"50"},{value:"100",label:"100"}],state:{isLoading:!1,isMounted:!1,showModal:!1,isDeleting:!1},items:[],totalItems:0,error:[],searchQuery:"",serverOptions:{page:1,rowsPerPage:10,sortBy:"id",sortType:"asc"}}},watch:{serverOptions:{handler(){this.fetchRecords()},deep:!0},searchQuery:N.debounce(function(){this.fetchRecords()},800)},computed:{perPage:function(){return this.serverOptions.rowsPerPage}},methods:{async fetchRecords(){this.state.isLoading=!0,this.state.isMounted=!1,await h.fetchRecords({keyword:this.searchQuery,page:this.serverOptions.page,per_page:this.serverOptions.rowsPerPage,sortBy:this.serverOptions.sortBy,sortType:this.serverOptions.sortType}).then(e=>{let t=e.data.data;this.items=t,this.items=t.map((n,y)=>({...n,created_at:moment(n.created_at).format("MMM D, YYYY hh:mma"),"#":(this.serverOptions.page-1)*this.serverOptions.rowsPerPage+(y+1)})),this.totalItems=e.data.total}).finally(()=>{this.state.isLoading=!1,this.state.isMounted=!0})},handlePageChange(e){this.serverOptions.page!=e&&(this.serverOptions.page=e)},handlePerPageChange(e){this.serverOptions.page=1,this.serverOptions.rowsPerPage=e},onSortChange(e){e[0].type!="none"&&(this.serverOptions.sortBy=e[0].field,this.serverOptions.sortType=e[0].type)},async deleteRecord(e){this.$swal.fire({title:"Are you sure?",text:"You want to delete this record?",icon:"warning",showCancelButton:!0,confirmButtonColor:"#34c38f",cancelButtonColor:"#f46a6a",confirmButtonText:"Yes, delete it!",background:this.$store.themeSettingsStore.isDark?"#1e293b":"#fff"}).then(t=>{t.value&&(this.state.isDeleting=!0,h.remove(e).then(n=>{n.status==200&&(this.$swal.fire({title:"Yaaay!",text:"Record has been deleted successfully.",type:"success",icon:"success",timer:2e3}),this.fetchRecords())}))}).catch(t=>{t.response&&t.response.status==401&&this.$swal.fire({title:"Ooops! something is wrong.",text:"Error deleting record. Please contact system administrator",type:"error",icon:"error",timer:2e3})}).finally(()=>{this.state.isDeleting=!1})},createRecord(){this.$refs.form.resetForm(),this.$refs.form.state.showModal=!0},editRecord(e){this.$refs.form.resetForm(),this.$refs.form.loadRecord(e),this.$refs.form.state.showModal=!0},async submitForm(){if(this.state.isLoading=!0,this.state.isSubmitted=!0,!this.v$.$invalid)try{this.state.isEdit==!0?(await h.update(this.categoryData.category_code,this.categoryData),Swal.fire("Success!","Data successfully updated.","success")):(await h.create(this.categoryData),Swal.fire("Success!","Data succesfully created.","success")),this.state.showModal=!1,this.state.isLoading=!1,this.resetData(),this.fetchRecords()}catch(e){this.error=e||"",this.state.isResetError=!0,this.state.isLoading=!1}},reload(){this.fetchRecords()},closeModal(){this.state.showModal=!1,this.resetData(),this.fetchRecords()},resetData(){this.state={isLoading:!1,isMounted:!1,isResetError:!1,isSubmitted:!1,showModal:!1},this.categoryData={category_code:"",category_desc:""},this.error=[]}},mounted(){this.fetchRecords()}},ie={class:"rounded-t bg-[url('@/assets/images/background/survey-blur.jpg')] bg-cover bg-[center] mih-h-24 pr-8"},ne={class:"grid grid-cols-2"},le={class:"p-8"},ce={class:"bg-white bg-opacity-[0.5] rounded grid lg:grid-cols-2 p-2"},de={class:"flex gap-2 px-2"},ue={class:"h-12 w-12 flex flex-col items-center justify-center rounded-full bg-white text-2xl mb-2 drop-shadow"},me={class:"text-3xl font-sans text-primary-800 py-1 drop-shadow"},he={class:"bg-slate-200 slant-container w-full h-full float-right p-8"},fe={class:"flex justify-between"},pe={class:"flex-grow"},ge={class:"flex","gap-2":""},ye={key:0,class:"flex gap-1"},_e={class:"py-4 flex justify-end"};function ve(e,t,n,y,s,a){const f=r("Icon"),_=r("InputGroup"),g=r("CreateButton"),v=r("EditButton"),w=r("DeleteButton"),b=r("Pagination"),C=r("vue-good-table"),x=r("Loading"),u=r("Form"),M=r("Card"),R=r("Layout");return l(),d(R,{breadcrumbs:s.breadcrumbs},{default:m(()=>[o("div",null,[i(M,{bodyClass:"p-0"},{default:m(()=>[o("div",ie,[o("div",ne,[o("div",le,[o("div",ce,[o("div",de,[o("div",ue,[i(f,{icon:"ri:survey-line",class:"text-4xl text-primary-700"})]),o("div",me,k(s.title),1)])])]),o("div",he,[o("div",null,[o("div",fe,[o("div",pe,[i(_,{modelValue:s.searchQuery,"onUpdate:modelValue":t[0]||(t[0]=c=>s.searchQuery=c),class:"w-full focus:border-cyan-500 pt-1 pr-4",placeholder:"Search",type:"text",prependIcon:"heroicons-outline:search",merged:""},null,8,["modelValue"])]),o("div",ge,[i(g,{onClick:a.createRecord},null,8,["onClick"])])])])])])]),o("div",null,[i(C,{styleClass:"vgt-table lesspadding v-middle",columns:s.columns,rows:s.items,mode:"remote",search:!0,"sort-options":{enabled:!0},"pagination-options":{enabled:!0},onSortChange:a.onSortChange,"max-height":"600px"},{"table-row":m(c=>[c.column.field=="action"?(l(),E("div",ye,[i(v,{onClick:O=>a.editRecord(c.row)},null,8,["onClick"]),i(w,{onClick:O=>a.deleteRecord(c.row.id)},null,8,["onClick"])])):p("",!0)]),"pagination-bottom":m(c=>[o("div",_e,[i(b,{total:s.totalItems,current:s.serverOptions.page,"per-page":s.serverOptions.rowsPerPage,onPageChanged:a.handlePageChange,pageChanged:c.pageChanged,perPageChanged:a.handlePerPageChange,enableSelect:"",perPageOptions:s.perPageOptions,onChangePerPage:a.handlePerPageChange},null,8,["total","current","per-page","onPageChanged","pageChanged","perPageChanged","perPageOptions","onChangePerPage"])])]),_:1},8,["columns","rows","onSortChange"]),s.state.isLoading?(l(),d(x,{key:0,text:"Fetching records"})):p("",!0),i(u,{onCloseModal:t[1]||(t[1]=c=>s.state.showModal=!1),ref:"form",onReload:a.reload},null,8,["onReload"])])]),_:1})])]),_:1},8,["breadcrumbs"])}const Qe=P(ae,[["render",ve]]);export{Qe as default};