Joywork/js/vue_scripts/unisender_vue.js
2026-05-22 21:21:54 +03:00

345 lines
11 KiB
JavaScript

if ($('#campaigns_modal').length > 0) {
var campaigns = new Vue({
el: '#campaigns_modal',
components: {
vuejsDatepicker: window.vuejsDatepicker
},
data() {
return {
ru: vdp_translation_ru.js,
agency_id: $('#session_agency_id').val() || null,
user_id: $('#session_user_id').val(),
is_show: false,
is_load: 0,
all: true,
OpenIndicator: {
render: createElement => createElement('span', { class: { 'vueToggleSpan': true } }),
},
clients: [],
clients_ids: [],
list_api_id: 0,
isPreloader: 1,
errors: {
error_title_list: 0,
error_subject: 0,
error_template_id: 0,
error_sender_name: 0,
error_sender_email: 0,
},
title_list: '',
isCreateList: false,
isSendList: false,
isCreateApiList: false,
subject: '',
templates: [{ 'code': 0, 'name': '' }],
templats_api: [],
template_id: null,
sender_email: '',
sender_name: '',
type: 'client',
}
},
methods: {
//Открытие
open() {
this.isPreloader = 1;
this.list_api_id = 0;
this.errors.error_title_list = 0;
this.errors.error_subject = 0;
this.errors.error_sender_email = 0;
this.errors.error_sender_name = 0;
this.title_list = '';
this.isCreateList = false;
this.isSendList = false;
this.isCreateApiList = false;
this.subject = '';
this.template_id = null;
this.is_load = false;
this.sender_email = '';
this.sender_name = '';
this.clients = [];
axios.post('/ajax/ajax_vue_unisender.php', {
request: 'open_campaigns',
agency_id: this.agency_id,
all: this.all,
clients_ids: this.clients_ids,
type: this.type,
}, )
.then((response) => {
$("body").addClass("lock");
this.is_show = true;
this.isPreloader = 0;
console.log(response.data);
if (response.data.client_list && response.data.client_list.length > 0) {
this.clients = response.data.client_list;
this.title_list = response.data.title_list;
if (this.clients.length > 0) {
this.sendList();
}
} else {
alert("Нет выбранных клиентов");
}
})
.catch(function(error) {
console.log(error);
});
},
save() {
var error = false
this.errors.error_template_id = 0;
this.errors.error_subject = 0;
this.errors.error_sender_email = 0;
this.errors.error_sender_name = 0;
if (this.template_id == 0 || this.template_id == null) {
this.errors.error_template_id = 1;
error = true;
}
if (this.isEmpty(this.subject)) {
this.errors.error_subject = 1;
error = true;
}
if (this.isEmpty(this.sender_name)) {
this.errors.error_sender_name = 1;
error = true;
}
if (this.isEmpty(this.sender_email)) {
this.errors.error_sender_email = 1;
error = true;
}
if (!error) {
this.is_load = true;
axios.post('/ajax/ajax_vue_unisender.php', {
request: 'save_campaign',
agency_id: this.agency_id,
template_id: this.template_id,
subject: this.subject,
list_id: this.list_api_id,
sender_name: this.sender_name,
sender_email: this.sender_email,
clients: this.clients,
user_id: this.user_id,
}, )
.then((response) => {
console.log(response.data);
if (response.data.error) {
this.errors.error_sender_email = 2;
} else {
this.closes();
}
this.is_load = false;
//this.is_show = false;
//this.isPreloader = 0;
//is_load
//console.log(response.data);
//this.clients = response.data.client_list;
})
.catch(function(error) {
console.log(error);
});
}
},
closes() {
this.is_show = false;
this.clients_ids = [];
$("body").removeClass("lock");
},
sendList() {
this.list_api_id = 0;
this.isSendList = true;
this.errors.error_title_list = 0;
if (this.isEmpty(this.title_list)) {
this.errors.error_title_list = 1;
this.isSendList = false;
} else {
axios.post('/ajax/ajax_vue_unisender.php', {
request: 'create_api_list',
agency_id: this.agency_id,
title_list: this.title_list,
clients: this.clients,
}, )
.then((response) => {
this.isCreateApiList = true;
console.log(response.data);
var res = response.data;
if (res.result) {
this.list_api_id = res.result.id;
//console.log(this.list_api_id);
this.getTemplates();
}
})
.catch(function(error) {
console.log(error);
});
}
},
openCreateListform() {
this.isCreateList = true;
},
getTemplates() {
axios.post('/ajax/ajax_vue_unisender.php', {
request: 'get_api_templates',
agency_id: this.agency_id,
}, )
.then((response) => {
console.log(response.data);
this.templates = response.data.templats;
this.templats_api = response.data.templats_api
})
.catch(function(error) {
console.log(error);
});
},
//Выбор шаблона
select_template() {
if (isEmpty(this.subject)) {
this.subject = this.templats_api[this.template_id].subject;
}
// console.log(this.template_id);
},
//Проверка на пустоту
isEmpty(data) {
if (typeof(data) === 'object') {
if (JSON.stringify(data) === '{}' || JSON.stringify(data) === '[]') {
return true;
} else if (!data) {
return true;
}
return false;
} else if (typeof(data) === 'string') {
if (!data.trim()) {
return true;
}
return false;
} else if (typeof(data) === 'undefined') {
return true;
} else if (data == 0) {
return true;
} else {
return false;
}
},
},
mounted() {
},
created: function() {
$("#campaigns_modal").removeClass("hidden");
}
})
}
//Открыть модал подписок
/*$('body').on('click', '.campaign__create_all', function() {
campaigns.all = true;
campaigns.open();
});*/
$('body').on('click', '.campaign__create', function() {
var all = 0;
if ($('#changeClientAllFilter').prop('checked')) {
all = 1;
}
if (all == 0) {
campaigns.all = false;
var clientsId = [];
$('.changeClientInput').each(function() {
if ($(this).prop('checked')) {
var id = $(this).attr('id').replace("changeClient", "");
if (id != 'All') {
clientsId.push(id);
}
}
});
//console.log(clientsId);
if (clientsId.length == 0) {
alert("Нет выбранных клиентов");
} else {
campaigns.clients_ids = clientsId;
campaigns.open();
}
} else {
campaigns.all = true;
campaigns.open();
}
});
$('body').on('click', '.campaign__create_req', function() {
campaigns.all = false;
campaigns.type = 'req';
var reqsId = [];
$('.changeReqInput').each(function() {
if ($(this).prop('checked')) {
var id = $(this).attr('id').replace("changeReq", "");
if (id != 'All') {
reqsId.push(id);
}
}
});
if (reqsId.length == 0) {
alert("Нет выбранных заявок");
} else {
campaigns.clients_ids = reqsId;
campaigns.open();
}
});
$('body').on('click', '.campaign__create_req_all', function() {
campaigns.all = true;
campaigns.type = 'req';
campaigns.open();
});
function campaign__create() {
campaigns.all = false;
campaigns.type = 'object';
campaigns.open();
}
function campaign__create_all() {
campaigns.all = true;
campaigns.type = 'object';
campaigns.open();
}