<template>
	<div class="container-fluid">
		<div class="row">
			<admin-title-bar :title="title">
				<el-button @click="$router.back()" type="primary">برگشت به صفحه قبل</el-button>
				<el-button type="success" @click="upload">ایجاد</el-button>
			</admin-title-bar>
			
			<div class="col-12">
				<admin-panel>
					<el-form>
						
						<p style="margin-bottom: 50px;color: green;font-weight: bold;font-size: 20px;">ابتدا پروژه را ثبت کرده، سپس در مرحله بعد عکس ها را وارد کنید.</p>
						
						<el-divider></el-divider>
						
						<el-form-item prop="title" :class="validation.fa_name ? 'is-error' : ''" label="نام فارسی">
							<el-input v-model="formData.project_details.fa.name"></el-input>
							<p class="err" v-if="validation.fa_name">{{validation.fa_name.msg}}</p>
						</el-form-item>
						<el-form-item prop="title" :class="validation.en_name ? 'is-error' : ''" label="نام انگلیسی">
							<el-input v-model="formData.project_details.en.name"></el-input>
							<p class="err" v-if="validation.en_name">{{validation.en_name.msg}}</p>
						</el-form-item>
						
						<el-divider></el-divider>
						
						<el-form-item prop="title" :class="validation.fa_description ? 'is-error' : ''" label="توضیح فارسی">
							<el-input type="textarea" v-model="formData.project_details.fa.description"></el-input>
							<p class="err" v-if="validation.fa_description">{{validation.fa_description.msg}}</p>
						</el-form-item>
						
						<el-form-item prop="title" :class="validation.en_description ? 'is-error' : ''" label="توضیح انگلیسی">
							<el-input type="textarea" v-model="formData.project_details.en.description"></el-input>
							<p class="err" v-if="validation.en_description">{{validation.en_description.msg}}</p>
						</el-form-item>
						
						<el-divider></el-divider>
						
						<el-form-item prop="date" :class="validation.date ? 'is-error' : ''" label="تاریخ اجرا">
							<date-picker v-model="formData.date" format="YYYY-MM-DD" display-format="jYYYY/jMM/jDD"></date-picker>
							<p class="err" v-if="validation.date">{{validation.date.msg}}</p>
						</el-form-item>
					
					</el-form>
				</admin-panel>
			</div>
		
		</div>
	</div>
</template>


<script>
   export default {
      data() {
         return {
            formData: {
               date: '',
               project_details: {
                  fa: {
                     name: '',
                     description: ''
                  },
                  en: {
                     name: '',
                     description: ''
                  }
               }
            },
            validation: {},
            uploading: false,
            uploadProgress: null
         }
      },
      computed: {
         title() {
            if (!this.uploading) {
               return 'افزودن محصول'
            } else {
               return this.uploadProgress
            }
         }
      },
      methods: {
         upload() {
            this.validation = {}
            const data = new FormData()
            data.append('fa_name', this.formData.project_details.fa.name)
            data.append('fa_description', this.formData.project_details.fa.description)
            data.append('en_name', this.formData.project_details.en.name)
            data.append('en_description', this.formData.project_details.en.description)
            data.append('date', this.formData.date)

            const axiosConfig = {
               onUploadProgress: progressEvent => {
                  this.uploading = true
                  this.uploadProgress = 'درحال آپلود ' + Math.floor(((progressEvent.loaded / progressEvent.total) * 100)) + '%'

                  if ((progressEvent.loaded / progressEvent.total) * 100 === 100) {
                     this.uploading = false
                  }
               }
            }
            this.$axios.post(`/api/private/projects`, data, axiosConfig)
               .then(response => {
                  if (response.data) {
                     this.$message({
                        message: 'پروژه با موفقیت ثبت شد.',
                        type: 'success'
                     })
                     this.$router.push(`/admin/projects/${response.data._id}`)
                  }
               }).catch(error => {
               if (error.response.status === 422) {
                  this.validation = error.response.data.validation
                  this.$message({
                     type: 'error',
                     message: 'اطلاعات رو بررسی و دوباره تلاش کنید'
                  })
               } else {
                  this.$alert(error.response.data.message, 'خطا', {
                     confirmButtonText: 'باشه',

                  })
               }
            })
         }
      },
      head() {
         return {
            title: this.title
         }
      },
      layout: 'admin'
   }
</script>
