import FormRecipient from "./generic/FormRecipient.js"; export default { name: 'SendImage', components: { FormRecipient }, data() { return { phone: '', view_once: false, compress: false, caption: '', type: window.TYPEUSER, loading: false, selected_file: null, image_url: null, preview_url: null } }, computed: { phone_id() { return this.phone + this.type; }, }, methods: { openModal() { $('#modalSendImage').modal({ onApprove: function () { return false; } }).modal('show'); }, isShowAttributes() { return this.type !== window.TYPESTATUS; }, isValidForm() { if (this.type !== window.TYPESTATUS && !this.phone.trim()) { return false; } if (!this.selected_file && !this.image_url) { return false; } return true; }, async handleSubmit() { if (!this.isValidForm() || this.loading) { return; } try { let response = await this.submitApi() showSuccessInfo(response) $('#modalSendImage').modal('hide'); } catch (err) { showErrorInfo(err) } }, async submitApi() { this.loading = true; try { let payload = new FormData(); payload.append("phone", this.phone_id) payload.append("view_once", this.view_once) payload.append("compress", this.compress) payload.append("caption", this.caption) const fileInput = $("#file_image"); if (fileInput.length > 0 && fileInput[0].files.length > 0) { const file = fileInput[0].files[0]; payload.append('image', file); } if (this.image_url) { payload.append('image_url', this.image_url) } let response = await window.http.post(`/send/image`, payload) this.handleReset(); return response.data.message; } catch (error) { if (error.response) { throw new Error(error.response.data.message); } throw new Error(error.message); } finally { this.loading = false; } }, handleReset() { this.view_once = false; this.compress = false; this.phone = ''; this.caption = ''; this.preview_url = null; this.selected_file = null; this.image_url = null; $("#file_image").val(''); }, handleImageChange(event) { const file = event.target.files[0]; if (file) { this.preview_url = URL.createObjectURL(file); // Add small delay to allow DOM update before scrolling setTimeout(() => { const modalContent = document.querySelector('#modalSendImage .content'); if (modalContent) { modalContent.scrollTop = modalContent.scrollHeight; } this.selected_file = file.name; }, 100); } } }, template: `