import"./index-4b46c3db.js";import{_ as C,c as n,w as m,r as i,o as d,j as p,d as c,k as F,e as r}from"./index-be2d4151.js";import"./InfoButton.vue_vue_type_style_index_0_scoped_635db2d1_lang-ea4dfeb4.js";import{C as x}from"./CloseButton-91fa6b44.js";import{S}from"./SubmitButton-1d5ea630.js";import{U as E}from"./UpdateButton-331adda6.js";import{F as V}from"./index-d65795c5.js";import{F as k}from"./index-5f541e4d.js";import{F as O}from"./default-f38ac8a4.js";import{T as q}from"./index-9c004d68.js";import{T as R}from"./index-5600e357.js";import{L as U}from"./Loading-c46365ec.js";import{B as I}from"./BrandService-b147be33.js";import{M as g}from"./ModelColorService-5dd23d7b.js";import{M as T}from"./ModelService-da301f16.js";import{M as L}from"./Multiselect-89305a7f.js";import{c as G,b as _,a as N,V as j}from"./array-9fca5ec5.js";import"./transition-4fc77ecd.js";import"./vue-cleave.min-19d21cac.js";const A={components:{FormModal:k,FormGroup:O,TextInput:q,TextArea:R,SubmitButton:S,UpdateButton:E,CloseButton:x,Loading:U,Multiselect:L,Fileinput:V},data(){return{brandOptions:[],modelOptions:[],form:{brand_id:"",model_id:"",color:"",imgFile:""},errors:{},state:{isEdit:!1,isSaving:!1,isLoading:!1}}},methods:{async fetchBrands(t={}){await I.fetchRecords(t).then(e=>{e.status==200&&(this.brandOptions=e.data.data.map(a=>({value:a.id,label:a.name})))}).catch(e=>{console.log(e)})},async fetchModelsByBranch(){},loadRecord(t){this.resetForm(),this.state.isEdit=!0;let e=Object.assign({},t);this.form={id:e.id,brand_id:e.model.brand_id,model_id:e.model_id,color:e.color,imgFile:""},this.fetchModelsUnderBrand()},closeModal(){this.state.showModal=!1},resetForm(){this.state.isEdit=!1,this.form={brand_id:"",model_id:"",color:"",imgFile:""}},reload(){this.$emit("reload")},async saveRecord(){if(this.state.isSaving=!0,!this.state.isEdit)this.state.isSaving=!1,await g.create(this.form).then(t=>{t.status==201&&(this.$swal.fire({title:"Great!",text:"Record successfully saved.",icon:"success",timer:2e3}),this.state.showModal=!1,this.resetForm(),this.reload())}).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})});else{const t=this.form.id;await g.update(t,this.form).then(e=>{console.log("response.status",e.status),e.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(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})}).finally(()=>{this.state.isSaving=!1,this.reload()})}},async submitForm(){try{await G().shape({brand_id:_().required("Brand field is required").positive().integer(),model_id:_().required("Model field is required").positive().integer(),color:N().required("Color name field is required")}).validate(this.form,{abortEarly:!1}),this.$swal.fire({title:"Are you sure you want to "+(this.state.isEdit?"update":"save")+" this record?",text:"",icon:"question",showCancelButton:!0,confirmButtonColor:"#34c38f",cancelButtonColor:"#f46a6a",confirmButtonText:"Yes",cancelButtonText:"No"}).then(e=>{e.value&&this.saveRecord()})}catch(t){if(t instanceof j){const e={};t.inner.forEach(a=>{e[a.path]=a.message}),this.errors=e}this.state.isSaving=!1}},async fetchModelsUnderBrand(){this.modelOptions=[],await T.fetchRecords({brand:this.form.brand_id}).then(t=>{t.status==200&&(this.modelOptions=t.data.data.map(e=>({value:e.id,label:e.name})))}).catch(t=>{console.log(t)})},handleImg(t){this.form.imgFile=t.target.files[0]}},Validating(t){console.log(t)},mounted(){this.fetchBrands()}},P={class:"lg:p-2"},z={class:"relative"},D={class:"relative"},Y={class:"flex gap-1 justify-end"};function H(t,e,a,J,o,l){const f=i("Loading"),h=i("Multiselect"),u=i("FormGroup"),b=i("TextInput"),w=i("Fileinput"),v=i("SubmitButton"),y=i("UpdateButton"),B=i("CloseButton"),M=i("FormModal");return d(),n(M,{title:o.state.isEdit?"Edit Model Color":"Add Model Color",active:o.state.showModal,onSubmit:t.confirmSubmit,onClose:l.closeModal,themeClass:o.state.isEdit?"bg-primary-600":"bg-primary-800",sizeClass:"lg:w-1/2"},{default:m(()=>[o.state.isEdit&&o.state.isLoading?(d(),n(f,{key:0,text:"Fetching record"})):p("",!0),c("div",P,[c("form",{onSubmit:e[4]||(e[4]=F((...s)=>t.confirmSubmit&&t.confirmSubmit(...s),["prevent"])),class:"space-y-4"},[o.state.isEdit&&o.state.isLoading?(d(),n(f,{key:0,text:"Fetching record"})):p("",!0),o.state.isSaving?(d(),n(f,{key:1,text:"Saving record"})):p("",!0),r(u,{label:"Brand",isRequired:!0},{default:m(()=>[c("div",z,[r(h,{modelValue:o.form.brand_id,"onUpdate:modelValue":e[0]||(e[0]=s=>o.form.brand_id=s),"close-on-select":!0,searchable:!0,id:"company_id",options:o.brandOptions,onSelect:l.fetchModelsUnderBrand},null,8,["modelValue","options","onSelect"])])]),_:1}),r(u,{label:"Model",isRequired:!0},{default:m(()=>[c("div",D,[r(h,{modelValue:o.form.model_id,"onUpdate:modelValue":e[1]||(e[1]=s=>o.form.model_id=s),"close-on-select":!0,searchable:!0,id:"company_id",options:o.modelOptions},null,8,["modelValue","options"])])]),_:1}),r(u,{label:"Color",isRequired:!0},{default:m(()=>[r(b,{type:"text",placeholder:"Input color ",modelValue:o.form.color,"onUpdate:modelValue":e[2]||(e[2]=s=>o.form.color=s),error:o.errors.color,classInput:"h-[48px]"},null,8,["modelValue","error"])]),_:1}),r(u,{label:"Image",isRequired:!0},{default:m(()=>[r(w,{name:"image 1",onChange:e[3]||(e[3]=s=>l.handleImg(s))})]),_:1}),c("div",Y,[o.state.isEdit?(d(),n(y,{key:1,onClick:l.submitForm},null,8,["onClick"])):(d(),n(v,{key:0,onClick:l.submitForm},null,8,["onClick"])),r(B,{onClick:l.closeModal},null,8,["onClick"])])],32)])]),_:1},8,["title","active","onSubmit","onClose","themeClass"])}const fe=C(A,[["render",H]]);export{fe as default};