Files
2024-10-10 21:57:37 +03:30

207 lines
6.8 KiB
Vue

<template>
<div>
<CustomSubHeader>
<CBreadcrumb class="border-0 mb-0">{{ title }}</CBreadcrumb>
<CButton size="sm" color="primary" class="mr-auto" :to="{name: 'admin-comments'}">برگشت به صفحه قبل</CButton>
<CButton v-if="$route.params.comment === 'new'" size="sm" color="success" class="mr-1" @click="post">افزودن</CButton>
<CButton v-else size="sm" color="success" class="mr-1" @click="update">بروزرسانی</CButton>
</CustomSubHeader>
<CRow>
<CCol lg="6">
<CCard>
<CCardBody>
<CForm>
<h4 class="mt-5">فارسی</h4>
<el-divider></el-divider>
<CInput
:class="validation.fa_name ? 'err' : null"
label="نام فارسی"
:description="validation.fa_name ? validation.fa_name.msg : null"
v-model="comment.locale.fa.name"
/>
<CInput
:class="validation.fa_position ? 'err' : null"
label="سمت فارسی"
:description="validation.fa_position ? validation.fa_position.msg : null"
v-model="comment.locale.fa.position"
/>
<CTextarea
:class="validation.fa_comment ? 'err' : null"
label="کامنت فارسی"
:description="validation.fa_comment ? validation.fa_comment.msg : null"
v-model="comment.locale.fa.comment"
/>
<h4 class="mt-5">انگلیسی</h4>
<el-divider></el-divider>
<CInput
:class="validation.en_name ? 'err' : null"
label="نام انگلیسی"
:description="validation.en_name ? validation.en_name.msg : null"
v-model="comment.locale.en.name"
/>
<CInput
:class="validation.en_position ? 'err' : null"
label="سمت انگلیسی"
:description="validation.en_position ? validation.en_position.msg : null"
v-model="comment.locale.en.position"
/>
<CTextarea
:class="validation.en_comment ? 'err' : null"
label="کامنت انگلیسی"
:description="validation.en_comment ? validation.en_comment.msg : null"
v-model="comment.locale.en.comment"
/>
</CForm>
</CCardBody>
</CCard>
</CCol>
<CCol lg="6">
<CCard>
<CCardBody>
<h2>تصویر</h2>
<el-divider></el-divider>
<img :src="comment.image" alt="" style="width: 100%;max-width: 600px;">
<input type="file" ref="image" @change="preview">
<p class="text-danger" v-if="validation.image">{{ validation.image.msg }}</p>
</CCardBody>
</CCard>
</CCol>
</CRow>
</div>
</template>
<script>
export default {
data() {
return {
comment: null,
validation: {}
}
},
computed: {
title() {
return this.$route.params.comment === 'new' ? 'افزودن کامنت جدید' : 'مشاهده اطلاعات کامنت'
}
},
methods: {
preview() {
this.comment.image = URL.createObjectURL(event.target.files[0])
},
post() {
this.validation = {}
const data = new FormData()
data.append('fa_name', this.comment.locale.fa.name)
data.append('en_name', this.comment.locale.en.name)
data.append('fa_position', this.comment.locale.fa.position)
data.append('en_position', this.comment.locale.en.position)
data.append('fa_comment', this.comment.locale.fa.comment)
data.append('en_comment', this.comment.locale.en.comment)
if (this.$refs.image.files[0]) data.append('image', this.$refs.image.files[0])
this.$axios.post(`/api/admin/comment`, data, this.axiosConfig)
.then(response => {
this.$message({
type: 'success',
message: 'کامنت با موفقیت ایجاد شد.'
})
this.$router.push({name: 'admin-comments'})
})
.catch(err => {
if (err.response.status === 401) {
return this.$message({
type: 'error',
message: 'لطفا دوباره وارد سیستم شوید.'
})
}
if (err.response.status === 422) {
this.validation = err.response.data.validation
return this.$message({
type: 'warning',
message: 'پارامتر ها رو بررسی و دوباره تلاش کنید'
})
} else console.log(err.response.data)
})
},
update() {
this.validation = {}
const data = new FormData()
data.append('fa_name', this.comment.locale.fa.name)
data.append('en_name', this.comment.locale.en.name)
data.append('fa_position', this.comment.locale.fa.position)
data.append('en_position', this.comment.locale.en.position)
data.append('fa_comment', this.comment.locale.fa.comment)
data.append('en_comment', this.comment.locale.en.comment)
if (this.$refs.image.files[0]) data.append('image', this.$refs.image.files[0])
this.$axios.put(`/api/admin/comment/${this.comment._id}`, data, this.axiosConfig)
.then(response => {
this.$message({
type: 'success',
message: 'اطلاعات بروزرسانی شد.'
})
this.$refs.image.value = null
this.$nuxt.refresh()
})
.catch(err => {
if (err.response.status === 401) {
return this.$message({
type: 'error',
message: 'لطفا دوباره وارد سیستم شوید.'
})
}
if (err.response.status === 422) this.validation = err.response.data.validation
else console.log(err.response.data)
})
}
},
head() {
return {
title: this.title
}
},
layout: 'admin',
async asyncData({$axios, params, error}) {
try {
if (params.comment !== 'new') {
const team = await $axios.get(`/api/admin/comment/${params.comment}`)
return {
comment: team.data
}
} else {
return {
comment: {
locale: {
fa: {
name: '',
position: '',
comment: ''
},
en: {
name: '',
position: '',
comment: ''
}
},
image: ''
}
}
}
} catch (e) {
error({status: 404, message: 'page not found'})
}
}
}
</script>