Tailored for You: Custom MOC Solutions and Creative Freedom
require([
'jquery',
'Magento_Ui/js/modal/alert',
'mage/translate',
'jquery/jquery.cookie'
], function ($, alert, $t) {
$(document).ready(function () {
$('#file').change(function () {
var number = parseInt($("#number").val());
if(number>4){
alert({
title: $t('Error'),
content: $t('You can upload a maximum of 5 files'),
actions: {
always: function () {}
}
});
return false;
}
var formData = new FormData();
var files = $('#file')[0].files;
formData.append('file', files[0]);
$.ajax({
url: '/activity/partsintroduction/upload',
type: 'POST',
data: formData,
dataType: 'json',
contentType: false,
processData: false,
async: false,
success: function (res) {
if(res.status==200){
$("#number").val(number + 1);
$(".block7").find('input[name="files[]"]').each(function() {
if($(this).val() == ''){
$(this).val(res.file_path);
var parent = $(this).parent();
parent.find('.pic').attr("src",'/media/activity/partsintroduction/uploaded-icon.png?v='+new Date().getTime());
parent.find('.close').show();
return false;
}
});
}
},
error: function (err) {
console.log(err)
}
});
$('#fileForm')[0].reset();
});
$(".close").click(function () {
var parent = $(this).parent();
parent.find('.pic').attr('src','/media/activity/partsintroduction/no-upload.png?v='+new Date().getTime());
parent.find('.close').hide();
parent.find('input').val('');
var number = parseInt($("#number").val()) - 1;
var newNumber =number<0 ? 0 : number;
$("#number").val(newNumber)
});
$("#btn").click(function () {
var checkOk = true;
var f = ['first_name','first_name','email','quantity','needs'];
f.forEach(function (v,i) {
if(undefined==$(".block7").find("#"+v).val() || ''==$(".block7").find("#"+v).val()){
checkOk = v;
return false;
}
})
if(checkOk!==true){
alert({
title: $t('Error'),
content: $t('The '+checkOk+' field cannot be empty'),
actions: {
always: function () {}
}
});
return false;
}
var formData = new FormData();
formData.append('first_name',$(".block7").find("#first_name").val());
formData.append('last_name',$(".block7").find("#first_name").val());
formData.append('email',$(".block7").find("#email").val());
formData.append('quantity',$(".block7").find("#quantity").val());
formData.append('desc',$(".block7").find("#needs").val());
var filesArr = [];
$(".block7").find('input[name="files[]"]').each(function() {
filesArr.push($(this).val());
});
formData.append('filesArr',JSON.stringify(filesArr));
$.ajax({
url: '/activity/partsintroduction/save',
type: 'POST',
data: formData,
dataType: 'json',
contentType: false,
processData: false,
async: true,
success: function (res) {
if(res.status==200){
alert({
title: $t('Success'),
content: $t(res.message),
actions: {
always: function () {}
}
});
fbq('track', 'Lead');
$(".block7").find('input').each(function() { $(this).val(""); });
$(".block7").find("#needs").val("");
$("#number").val('0');
$(".block7").find('input[name="files[]"]').each(function() {
var parent = $(this).parent();
parent.find('.pic').attr("src",'/media/activity/partsintroduction/no-upload.png?v='+new Date().getTime());
parent.find('.close').hide();
parent.find('input').val("");
});
}else{
alert({
title: $t('Error'),
content: $t('An error occurred.'),
actions: {
always: function () {}
}
});
}
},
error: function (err) {
alert({
title: $t('Error'),
content: $t('Network exception please try again later'),
actions: {
always: function () {}
}
});
}
});
});
$("#link-window").click(function () {
var modal = $('#modal-downloadable ');
modal.modal({
'type': 'popup',
'title': 'Choose the format you need',
'trigger': '[data-trigger=trigger]',
'responsive': true,
'class': 'download111',
'buttons': []
});
modal.modal('openModal');
return false;
});
});
});