$(function () { var currentChatUser = { { request.user.pk } } ; var socket = new SocketHandler(); var form = document.getElementById('message_form'); var csrftoken = getCookie('csrftoken'); $("#reserve-button").on("click", function (e) { e.preventDefault(); var orderId = $(this).attr('data-order-id'); $.ajax({ url: '/api/stages/', type: 'GET', data: {csrfmiddlewaretoken: csrftoken, 'order': orderId}, dataType: 'json', success: function (json) { console.log(json.results); } }); }); function getStages(orderId, senderId, recipentId) { $.ajax({ url: '/api/stages/', type: 'GET', data: {csrfmiddlewaretoken: csrftoken, 'order': orderId}, dataType: 'json', success: function (json) { var stageCount = json.results.length; if (stageCount == 0) { stageCountVal = 1; } else { stageCountVal = stageCount; } var htmlInbox = ""; var htmlInboxStage = '

Какое кол-во этапов подразумевает работа? ' + '

'; if (stageCount == 0) { htmlInboxStage += '
' + '

Этап 1

' + '' + '' + '' + '' + '' + '' + '
'; } var statusNotAgreed = true; $.each(json.results, function (i, v) { if (v.status == "not_agreed") { htmlInbox += '
' + '

Этап

' + '' + '' + '' + '' + '' + '
'; } else { statusNotAgreed = false; htmlInboxStage = ""; htmlInbox += '
' + '

Этап ' + v.pos + '' + v.name + '

' + '

Результаты этапа:' + v.result + '

' + '

до Дата

' + v.cost + '
'; } }); if (statusNotAgreed) { htmlInbox += '
Перейти в режим безопасной сделки' + '
' + 'отправить на согласование
'; } htmlInbox = htmlInboxStage + htmlInbox; $("#order-stages").html(htmlInbox); } }); } setTimeout(function () { $(".user-block").first().trigger('click'); }, 10); setTimeout(function () { $(".order-block").first().trigger('click'); }, 100); $("#order-stages").on('click', "#addStagesForm", function (e) { e.preventDefault(); $(".new-stages-form").each(function (i, v) { $.ajax({ url: '/api/stages/', type: 'POST', beforeSend: function (xhr) { xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken')) }, data: $(this).serialize(), dataType: 'json', success: function (json) { console.log(json); }, error: function (e) { console.log(e); } }); }); $(".update-stages-form").each(function (i, v) { var currentStageId = parseInt($(this).attr('data-stage-id')); $.ajax({ url: '/api/stages/' + currentStageId + '/', type: 'PUT', beforeSend: function (xhr) { xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken')) }, data: $(this).serialize(), dataType: 'json', success: function (json) { console.log(json); }, error: function (e) { console.log('error'); console.log(e); } }); }); var currentOrderId = $(this).attr('data-order-id'); var currentRecipentId = $(this).attr('data-recipent-id'); getStages(currentOrderId, userId, currentRecipentId); socket.send_stages_approve({ "format_type": "approve_stages", "data": { "sender_id": userId, "recipent_id": currentRecipentId, "order_id": currentOrderId, } }); }); $('#order-stages-tab').on('change', '#countStage', function () { var countStage = parseInt($(this).val()); var currentCountStage = $(".numberStepp").length; if (countStage > currentCountStage) { for (var jj = currentCountStage; jj < countStage; jj++) { var pos = jj + 1; var lastFormStage = $(".numberStepp").last(); var orderId = lastFormStage.find('.orderStagesInput').val(); var addFormTemplate = '
' + '

Этап ' + pos + '

' + '' + '' + '' + '' + '' + '' '
'; lastFormStage.after(addFormTemplate); } } else if (countStage < currentCountStage) { var ii = currentCountStage; $($(".numberStepp").get().reverse()).each(function () { var currenFormName = ($(this).find('form').attr('class')); if (ii > countStage) { $(this).remove(); } ii--; }); } }); $('.order-block').on('click', function () { $("#chat-order-add").css("display", "block"); $("#formsetStage").css("display", "block"); $('.order-block').each(function () { $(this).removeClass('orAct'); }); $(this).addClass('orAct'); var orderId = $(this).attr('data-id'); var recipentId = $(this).attr('data-recipent-id'); $("#chat-order-add #orderId").val(orderId); $("#projectReviewId").val(orderId); $("#reserve-button").attr('data-order-id', orderId); $("#targetContractorId").val(recipentId); $("#chat-order-add #recipentId").val(recipentId); $(".orderStagesInput").val(orderId); var inbox = document.getElementById('message-chat-order-space'); inbox.innerHTML = ''; $.ajax({ url: '/api/message', type: 'GET', data: {csrfmiddlewaretoken: csrftoken, 'order': orderId}, dataType: 'json', success: function (json) { $.each(json.results, function (i, v) { var senderName = 'Вы'; var className = 'youChat'; if (v.sender.id !== currentChatUser) { senderName = v.sender.username; className = ''; } inbox.innerHTML += '
' + '

' + senderName + '

' + v.created + '
' + '

' + v.text + '

'; }); } }); getStages(orderId, userId, recipentId); }); // Вытащить сообщения для конактов $('.user-block').on('click', function () { var userId = $(this).attr('data-id'); $("#contact-chat-form #recipentId").val(userId); $("#add-form-contractor-note #recipentNoteContractor").val(userId); $('.user-block').each(function () { $(this).removeClass('mesAct'); }); $(this).addClass('mesAct'); var inbox = document.getElementById('message-chat-space'); inbox.innerHTML = ''; $.ajax({ url: '/api/message', type: 'GET', data: { csrfmiddlewaretoken: csrftoken, 'operand': 'in', 'sender_id': currentChatUser, 'recipent_id': userId }, dataType: 'json', success: function (json) { $.each(json.results, function (i, v) { var senderName = 'Вы'; var className = 'youChat'; if (v.sender.id == userId) { senderName = v.sender.username; className = ''; } inbox.innerHTML += '
' + '

' + senderName + '

' + v.created + '
' + '

' + v.text + '

'; }); } }); }); $('#contact-chat-add-message').on('click', function (e) { e.preventDefault(); var chatMessage = $("#chat").val(); var recipentId = $("#recipentId").val(); var senderId = $("#senderId").val(); socket.add_contact_message({ "format_type": "add_message_contact", "data": { "sender_id": senderId, "recipent_id": recipentId, "chat_message": chatMessage, } }); $("#chat").val(""); }); $('#order-review-add').on('click', function () { alert('add review'); }); $('#order-chat-add-message').on('click', function (e) { e.preventDefault(); var chatMessage = $("#chat-order-add #chat").val(); var recipentId = $("#chat-order-add #recipentId").val(); var senderId = $("#chat-order-add #senderId").val(); var orderId = $("#chat-order-add #orderId").val(); socket.add_contact_message({ "format_type": "add_message_order", "data": { "sender_id": senderId, "recipent_id": recipentId, "chat_message": chatMessage, "order_id": orderId, } }); $("#chat-order-add #chat").val(""); }); }); var userId = '{{ request.user.pk }}';