Files
2024-10-21 10:22:26 +03:30

469 lines
14 KiB
Vue

<template>
<div>
<CustomSubHeader>
<CBreadcrumb class="border-0 mb-0">{{ title }}</CBreadcrumb>
<CButton size="sm" color="primary" class="mr-auto" :to="{ name: 'admin-polls',query: {page: 1} }">برگشت به صفحه قبل</CButton>
<CButton v-if="$route.params.details === '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 style="display : flex ; flex-direction: column">
<CRow>
<CCol lg="6">
<CCard>
<CCardBody>
<CRow>
<CCol sm="12">
<CInput
label="عنوان"
:class="validation.title ? 'err' : null"
:description="validation.title ? validation.title.msg : null"
v-model="poll.title"
/>
</CCol>
<CCol sm="12">
<CTextarea
:class="validation.description ? 'err' : null"
label="توضیحات"
:description="validation.description ? validation.description.msg : null"
v-model="poll.description"
/>
</CCol>
<el-divider></el-divider>
<CCol sm="12">
<h6>تاریخ شروع</h6>
<date-picker format="YYYY-MM-DD" display-format="jMMMM jD" v-model="poll.startDate"></date-picker>
<p class="text-danger" v-if="validation.startDate">{{ validation.startDate.msg }}</p>
</CCol>
<CCol sm="12" class="mt-3">
<h6>تاریخ پایان</h6>
<date-picker format="YYYY-MM-DD" display-format="jMMMM jD" v-model="poll.endDate"></date-picker>
<p class="text-danger" v-if="validation.endDate">{{ validation.endDate.msg }}</p>
</CCol>
</CRow>
</CCardBody>
</CCard>
</CCol>
<CCol lg="6">
<CCard>
<CCardBody>
<CCol sm="12">
<h6>{{ this.$route.params.details !== "new" ? "دسته بندی" : "انتخاب دسته بندی" }}</h6>
<el-select
filterable
clearable
style="width : 100%"
v-model="catId"
placeholder="انتخاب دسته بندی">
<el-option
v-for="item in categories"
v-if="availableCategory(item._id)"
:key="item._id"
:label="item.title"
:value="item._id"
>
<!-- <span>{{setTitle(item)}}</span> -->
</el-option>
</el-select>
<p style="margin-top : 5px !important ; color : red" class="form-err" v-if="validation.catId">{{ validation.catId.msg }}</p>
</CCol>
<el-divider></el-divider>
<CCol sm="12">
<h6>نوع گزینه ها</h6>
<el-select
style="width : 100%"
v-model="poll.surveyType"
placeholder="انتخاب نوع">
<el-option label="انتخاب چند گزینه همزمان" value="check"/>
<el-option label="فقط انتخاب یک گزینه" value="radio"/>
</el-select>
<p style="margin-top : 5px !important ; color : red" class="form-err" v-if="validation.surveyType">{{ validation.surveyType.msg }}</p>
</CCol>
<el-divider></el-divider>
<el-checkbox label="منتشر شود" v-model="poll.publish"/>
</CCardBody>
</CCard>
</CCol>
<CCol sm="12">
<CCard>
<CCardBody>
<CRow>
<CCol lg="6">
<h6>لیست گزینه ها</h6>
<el-divider></el-divider>
<el-table
style="width: 100%;"
:data="poll.options"
>
<el-table-column
type="index"
label="#"
>
</el-table-column>
<el-table-column
label="گزینه ها"
>
<template slot-scope="scope">
{{ scope.row }}
</template>
</el-table-column>
<el-table-column
label="ویرایش"
width="65"
>
<template slot-scope="scope">
<CButton color="danger" variant="outline" :key="scope.row" @click="removeOption(scope.row)">
<i class="far fa-trash-alt"></i>
</CButton>
</template>
</el-table-column>
</el-table>
<p class="text-danger" v-if="validation.options">{{ validation.options.msg }}</p>
</CCol>
<CCol lg="6">
<h6>افزودن گزینه</h6>
<el-divider></el-divider>
<el-input v-model="newOption"/>
<br>
<br>
<el-button type="success" @click="addOption">افروزدن</el-button>
<p style="margin-top : 5px !important ; color : red" class="form-err" v-if="validation.newOption">{{ validation.newOption.msg }}</p>
</CCol>
</CRow>
</CCardBody>
</CCard>
</CCol>
<CCol sm="12" v-if="$route.params.details !== 'new'">
<CCard>
<CCardBody>
<h6>جواب ها</h6>
<el-divider></el-divider>
<div class="survey-item2" v-for="(item, index) in poll.options" :key="index">
<h4>{{ item }}</h4>
<el-progress :percentage="progress(item)" style="direction: rtl" color="#CC0000"></el-progress>
</div>
</CCardBody>
</CCard>
</CCol>
</CRow>
</CRow>
</div>
</template>
<script>
export default {
data() {
return {
poll: null,
categories: null,
validation: {},
newOption: ''
}
},
computed: {
title() {
return this.$route.params.details === "new" ? "افزودن نظرسنجی جدید" : "مشاهده نظرسنجی"
},
catId: {
get() {
if (this.$route.params.details === 'new') {
return this.poll.catId
} else {
return this.poll.catId._id
}
},
set(value) {
if (this.$route.params.details === 'new') {
this.poll.catId = value
} else {
this.poll.catId._id = value
}
}
}
},
methods: {
progress(answerItem) {
let filteredAnswers = this.poll.answers.filter(item0 => this.poll.options.includes(item0))
let num = filteredAnswers.filter(item1 => item1 === answerItem).length
return Number(((num * 100 / filteredAnswers.length) || 0).toFixed(0))
},
addOption() {
this.validation = {}
if (!this.newOption) return this.validation = {
newOption: {
msg: 'ابتدا گزینه را بنویسید'
}
}
if (this.poll.options.includes(this.newOption)) return this.validation = {
newOption: {
msg: 'این گزینه قبلا اضافه شده'
}
}
this.poll.options.push(this.newOption)
this.newOption = ''
if (this.$route.params.details === 'new') {
this.$message({
type: 'success',
message: 'گزینه افزوده شد'
})
} else {
this.$message({
type: 'warning',
message: 'تا زمانی که دکمه بروزرسانی بالای صفحه رو نزنید گزینه ها افزوده نمیشوند'
})
}
},
removeOption(target) {
this.$confirm("گزینه حذف شود؟", "اخطار", {
confirmButtonText: "بله",
cancelButtonText: "لغو",
type: "warning",
})
.then(() => {
this.poll.options = this.poll.options.filter(item => item !== target)
if (this.$route.params.details === 'new') {
this.$message({
type: 'success',
message: 'گزینه حذف شد'
})
} else {
this.$message({
type: 'warning',
message: 'تا زمانی که دکمه بروزرسانی بالای صفحه رو نزنید تغییرات اعمال نمیشوند'
})
}
})
.catch(() => {
this.$message({
type: "warning",
message: "عملیات لغو شد"
})
})
},
availableCategory(id) {
return this.$auth.user.permissions.includes('superAdmin') || this.$auth.user.specialPermissions.includes(id)
},
post() {
this.validation = {}
this.$axios
.post(`/api/admin/poll`, this.poll)
.then((response) => {
this.$message({
type: "success",
message: "نظرسنجی با موفقیت ایجاد شد."
})
this.$router.push({name: "admin-polls"})
})
.catch((err) => {
if (err.response.status === 401) {
return this.$message({
type: "error",
message: "لطفا دوباره وارد سیستم شوید."
})
}
if (err.response.status === 403) {
return this.$message({
type: "error",
message: err.response.data.message
})
}
if (err.response.status === 500) {
return this.$message({
type: "error",
message: "مشکلی در ارسال درخواست پیش آمده"
})
}
if (err.response.status === 422) {
this.validation = err.response.data.validation
this.$message({
type: 'warning',
message: 'پارامترها رو بررسی کنید'
})
} else console.log(err.response.data)
})
},
update() {
this.validation = {}
this.$axios
.put(`/api/admin/poll/${this.poll._id}`, {...this.poll, catId: this.poll.catId._id})
.then((response) => {
this.$message({
type: "success",
message: "اطلاعات بروزرسانی شد."
})
})
.catch((err) => {
if (err.response.status === 401) {
return this.$message({
type: "error",
message: "لطفا دوباره وارد سیستم شوید."
})
}
if (err.response.status === 403) {
return this.$message({
type: "error",
message: err.response.data.message
})
}
if (err.response.status === 500) {
return this.$message({
type: "error",
message: "مشکلی در ارسال درخواست پیش آمده"
})
}
if (err.response.status === 422) {
this.validation = err.response.data.validation
this.$message({
type: 'warning',
message: 'پارامترها رو بررسی کنید'
})
} else console.log(err.response.data)
});
},
},
head() {
return {
title: this.title,
}
},
layout: "admin",
async asyncData({$axios, params, error}) {
try {
const categories = await $axios.get('/api/admin/category/getParent')
if (params.details !== 'new') {
const poll = await $axios.get(`/api/admin/poll/${params.details}`)
return {
categories: categories.data,
poll: poll.data
}
} else {
return {
categories: categories.data,
poll: {
title: '',
description: '',
surveyType: '',
catId: '',
options: [],
startDate: '',
endDate: '',
publish: true
}
}
}
} catch (e) {
if (e?.response?.status === 401) error({status: 401, message: "لطفا دوباره وارد سیستم شوید."})
if (e?.response?.status === 403) error({status: 403, message: e?.response?.data?.message})
if (e?.response?.status === 500) error({status: 500, message: "مشکلی در گرفتن اطلاعات پیش آمده"})
else error({status: 404, message: "پرونده خبری مورد نظر پیدا نشد"})
}
},
}
</script>
<style lang="scss">
.survey-item2 {
padding: 20px 0;
.el-radio-group {
direction: ltr;
.el-radio__input.is-checked .el-radio__inner {
background: red;
border-color: red;
}
.el-radio__input.is-checked + .el-radio__label {
color: red;
}
.el-radio__label {
&:hover {
color: red;
}
}
.el-radio__inner {
border: 1px solid #666666;
}
.el-radio__inner:hover {
border-color: red;
}
}
.comment-btn {
display: flex;
justify-content: flex-end;
button {
&:disabled {
border-color: #666666;
background-color: #666666;
color: white;
border-radius: 5px;
}
}
}
.el-checkbox-group {
direction: ltr;
text-align: right;
.el-checkbox__inner {
border: 1px solid #666666;
&:hover {
border-color: red;
}
}
}
.el-checkbox__input.is-checked .el-checkbox__inner, .el-checkbox__input.is-indeterminate .el-checkbox__inner {
background-color: red;
border-color: red;
}
.el-checkbox__label {
&:hover {
color: red;
}
}
.el-checkbox__input.is-checked + .el-checkbox__label {
color: red;
}
.el-progress {
font-family: 'sansNum', sans-serif;
.el-progress-bar__inner {
right: 0;
}
.el-progress__text {
margin-right: 10px;
}
}
}
</style>