<template>
  <div>
    <CustomSubHeader>
      <CBreadcrumb class="border-0 mb-0">{{ title }}</CBreadcrumb>
      <CButton size="sm" color="primary" class="mr-auto" :to="{name: 'admin-blog-posts'}">برگشت به صفحه قبل</CButton>
      <CButton size="sm" color="success" class="mr-1" @click="update">بروزرسانی</CButton>
    </CustomSubHeader>
    <CRow>
      <CCol lg="12">
        <CCard>
          <CCardBody>
            <CForm>
              <CRow>
                <CCol sm="12">
                  <p>دسته بندی</p>
                  <el-select v-model="blogPost.category" filterable :class="validation.category? 'err' : null">
                    <el-option v-for="item in blogCategories" :label="item.locale.fa.title" :value="item._id" :key="item._id"/>
                  </el-select>
                  <p class="text-danger" v-if="validation.category">{{ validation.category.msg }}</p>
                  <el-divider></el-divider>
                </CCol>
                <div>
                  <el-checkbox v-model="blogPost.published" label="پست منتشر شود"/>
                </div>
              </CRow>
            </CForm>
          </CCardBody>
        </CCard>
      </CCol>
      <CCol lg="6">
        <CCard>
          <CCardBody>
            <h4>فارسی</h4>
            <el-divider></el-divider>
            <CInput
                :class="validation.fa_title ? 'err' : null"
                label="عنوان فارسی"
                :description="validation.fa_title ? validation.fa_title.msg : null"
                v-model="blogPost.locale.fa.title"
            />
            <CTextarea
                :class="validation.fa_short_description ? 'err' : null"
                label="توضیح کوتاه فارسی"
                :description="validation.fa_short_description ? validation.fa_short_description.msg : null"
                v-model="blogPost.locale.fa.short_description"
            />
            <CInput
                :class="validation.fa_last_section_image_title ? 'err' : null"
                label="عنوان روی تصویر بخش آخر"
                :description="validation.fa_last_section_image_title ? validation.fa_last_section_image_title.msg : null"
                v-model="blogPost.locale.fa.last_section_image_title"
            />
            <CTextarea
                :class="validation.fa_last_section_image_description ? 'err' : null"
                label="توضیحات روی تصویر بخش آخر"
                :description="validation.fa_last_section_image_description ? validation.fa_last_section_image_description.msg : null"
                v-model="blogPost.locale.fa.last_section_image_description"
            />
            <CInput
                :class="validation.fa_last_section_title ? 'err' : null"
                label="عنوان زیر تصویر بخش آخر"
                :description="validation.fa_last_section_title ? validation.fa_last_section_title.msg : null"
                v-model="blogPost.locale.fa.last_section_title"
            />
            <CTextarea
                :class="validation.fa_last_section_description ? 'err' : null"
                label="توضیحات زیر تصویر بخش آخر"
                :description="validation.fa_last_section_description ? validation.fa_last_section_description.msg : null"
                v-model="blogPost.locale.fa.last_section_description"
            />
            <client-only>
              <p>توضیحات فارسی</p>
              <ckeditor v-model="blogPost.locale.fa.description" :config="editorConfigFa"></ckeditor>
              <p class="text-danger" v-if="validation.fa_description">{{ validation.fa_description.msg }}</p>
            </client-only>
          </CCardBody>
        </CCard>
      </CCol>
      <CCol lg="6">
        <CCard>
          <CCardBody>
            <h4>انگلیسی</h4>
            <el-divider></el-divider>
            <CInput
                :class="validation.en_title ? 'err' : null"
                label="عنوان انگلیسی"
                :description="validation.en_title ? validation.en_title.msg : null"
                v-model="blogPost.locale.en.title"
            />
            <CTextarea
                :class="validation.en_short_description ? 'err' : null"
                label="توضیح کوتاه انگلیسی"
                :description="validation.en_short_description ? validation.en_short_description.msg : null"
                v-model="blogPost.locale.en.short_description"
            />
            <CInput
                :class="validation.en_last_section_image_title ? 'err' : null"
                label="عنوان روی تصویر بخش آخر"
                :description="validation.en_last_section_image_title ? validation.en_last_section_image_title.msg : null"
                v-model="blogPost.locale.en.last_section_image_title"
            />
            <CTextarea
                :class="validation.en_last_section_image_description ? 'err' : null"
                label="توضیحات روی تصویر بخش آخر"
                :description="validation.en_last_section_image_description ? validation.en_last_section_image_description.msg : null"
                v-model="blogPost.locale.en.last_section_image_description"
            />
            <CInput
                :class="validation.en_last_section_title ? 'err' : null"
                label="عنوان زیر تصویر بخش آخر"
                :description="validation.en_last_section_title ? validation.en_last_section_title.msg : null"
                v-model="blogPost.locale.en.last_section_title"
            />
            <CTextarea
                :class="validation.en_last_section_description ? 'err' : null"
                label="توضیحات زیر تصویر بخش آخر"
                :description="validation.en_last_section_description ? validation.en_last_section_description.msg : null"
                v-model="blogPost.locale.en.last_section_description"
            />
            <client-only>
              <p>توضیحات انگلیسی</p>
              <ckeditor v-model="blogPost.locale.en.description" :config="editorConfigEn"></ckeditor>
              <p class="text-danger" v-if="validation.en_description">{{ validation.en_description.msg }}</p>
            </client-only>
          </CCardBody>
        </CCard>
      </CCol>
      <CCol lg="6">
        <CCard>
          <CCardBody>
            <p>کاور</p>
            <img :src="blogPost.cover" alt="" style="width: 100%;max-width: 600px;">
            <input type="file" ref="cover" @change="preview">
            <p class="text-danger" v-if="validation.cover">{{ validation.cover.msg }}</p>
          </CCardBody>
        </CCard>
      </CCol>
      <CCol lg="6">
        <CCard>
          <CCardBody>
            <p>تصویر بخش آخر</p>
            <img :src="blogPost.last_section_image" alt="" style="width: 100%;max-width: 600px;">
            <input type="file" ref="last_section_image" @change="preview2">
            <p class="text-danger" v-if="validation.last_section_image">{{ validation.last_section_image.msg }}</p>
          </CCardBody>
        </CCard>
      </CCol>
      <CCol lg="12">
        <CCard>
          <CCardBody>
            <p>آپلود ویدیو</p>
						<input type="file" ref="video" placeholder="انتخاب فایل ویدیو"/>
            <p class="text-danger" v-if="validation.last_section_image">{{ validation.last_section_image.msg }}</p>
          </CCardBody>
        </CCard>
      </CCol>
      <CCol lg="12" v-if="blogPost.video">
        <CCard>
          <CCardBody>
						<video class="mt-3" width="100%" height="400" muted controls
									 :src="blogPost.video"></video>
          </CCardBody>
        </CCard>
      </CCol>
    </CRow>
  </div>
</template>

<script>
import axiosUploadProcess from "@/mixins/axiosUploadProcess"

export default {
  data() {
    return {
      title: 'مشاهده پست',
      blogPost: null,
      blogCategories: null,
			editorConfigFa: {
				language: 'en',
				extraPlugins: ['bidi', 'justify']
			},
      editorConfigEn: {
        language: 'en',
        extraPlugins: ['bidi', 'justify']
      },
      validation: {}
    }
  },
  mixins: [axiosUploadProcess],
  methods: {
    preview() {
      this.blogPost.cover = URL.createObjectURL(event.target.files[0])
    },
    preview2() {
      this.blogPost.last_section_image = URL.createObjectURL(event.target.files[0])
    },
    update() {
      this.validation = {}
      const data = new FormData()
      data.append('fa_title', this.blogPost?.locale?.fa?.title || '')
      data.append('en_title', this.blogPost?.locale?.en?.title || '')

      data.append('fa_description', this.blogPost?.locale?.fa?.description || '')
      data.append('en_description', this.blogPost?.locale?.en?.description || '')

      data.append('fa_short_description', this.blogPost?.locale?.fa?.short_description || '')
      data.append('en_short_description', this.blogPost?.locale?.en?.short_description || '')

      data.append('fa_last_section_image_title', this.blogPost?.locale?.fa?.last_section_image_title || '')
      data.append('en_last_section_image_title', this.blogPost?.locale?.en?.last_section_image_title || '')

      data.append('fa_last_section_image_description', this.blogPost?.locale?.fa?.last_section_image_description || '')
      data.append('en_last_section_image_description', this.blogPost?.locale?.en?.last_section_image_description || '')

      data.append('fa_last_section_title', this.blogPost?.locale?.fa?.last_section_title || '')
      data.append('en_last_section_title', this.blogPost?.locale?.en?.last_section_title || '')

      data.append('fa_last_section_description', this.blogPost?.locale?.fa?.last_section_description || '')
      data.append('en_last_section_description', this.blogPost?.locale?.en?.last_section_description || '')

      data.append('category', this.blogPost.category)
      data.append('published', this.blogPost.published)
			console.log(this.$refs)

      if (this.$refs.cover.files[0]) data.append('cover', this.$refs.cover.files[0])
      if (this.$refs.last_section_image.files[0]) data.append('last_section_image', this.$refs.last_section_image.files[0])
      if (this.$refs?.video?.files[0]) data.append('video', this.$refs?.video?.files[0])

      this.$axios.put(`/api/admin/blogPost/${this.blogPost._id}`, data, this.axiosConfig)
          .then(response => {
            this.$message({
              type: 'success',
              message: 'اطلاعات بروزرسانی شد.'
            })
            this.$refs.cover.value = null
            this.$refs.last_section_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
              this.$message({
                type: 'error',
                message: 'پارامتر هارو بررسی کنید'
              })
            } else console.log(err.response.data)
          })
    }
  },
  head() {
    return {
      title: this.title
    }
  },
  layout: 'admin',
  async asyncData({$axios, params, error}) {
    try {
      const blogCategories = await $axios.get(`/api/public/blogCategories`)
      const blogPost = await $axios.get(`/api/public/blogPost/${params.post}?noCategory=true`)
      return {
        blogCategories: blogCategories.data,
        blogPost: blogPost.data
      }
    } catch (e) {
      error({status: 404, message: 'page not found'})
    }
  }
}
</script>

