diff --git a/app/assets/javascripts/admins/salesman_customers/index.js b/app/assets/javascripts/admins/salesman_customers/index.js new file mode 100644 index 000000000..2227cb43f --- /dev/null +++ b/app/assets/javascripts/admins/salesman_customers/index.js @@ -0,0 +1,74 @@ +$(document).on('turbolinks:load', function() { + if ($('body.admins-salesmans-index-page').length > 0) { + + // ============= 添加销售人员 ============== + var $addMemberModal = $('.admin-add-salesman-customer-user-modal'); + var $addMemberForm = $addMemberModal.find('.admin-add-salesman-customer-user-form'); + var $memberSelect = $addMemberModal.find('.salesman-user-select'); + // var $salesmanIdInput = $addMemberForm.find('input[name="salesman_id"]') + + $addMemberModal.on('show.bs.modal', function(event){ + var $link = $(event.relatedTarget); + var salesmanId = $link.data('salesman-id'); + $salesmanIdInput.val(salesmanId); + + $memberSelect.select2('val', ' '); + }); + + $memberSelect.select2({ + theme: 'bootstrap4', + placeholder: '请输入要添加的销售姓名', + multiple: true, + minimumInputLength: 1, + ajax: { + delay: 500, + url: '/admins/users', + dataType: 'json', + data: function(params){ + return { name: params.term }; + }, + processResults: function(data){ + return { results: data.users } + } + }, + templateResult: function (item) { + if(!item.id || item.id === '') return item.text; + return $("" + item.real_name + " " + item.school_name + ' ' + item.hidden_phone + ""); + }, + templateSelection: function(item){ + if (item.id) { + } + return item.real_name || item.text; + } + }); + + $addMemberModal.on('click', '.submit-btn', function(){ + $addMemberForm.find('.error').html(''); + + // var salesmanId = $salesmanIdInput.val(); + var memberIds = $memberSelect.val(); + if (memberIds && memberIds.length > 0) { + $.ajax({ + method: 'POST', + dataType: 'json', + url: '/admins/salesman_customers/batch_add', + data: { user_ids: memberIds }, + success: function(){ + $.notify({ message: '创建成功' }); + $addMemberModal.modal('hide'); + + setTimeout(function(){ + window.location.reload(); + }, 500); + }, + error: function(res){ + var data = res.responseJSON; + $form.find('.error').html(data.message); + } + }); + } else { + $addMemberModal.modal('hide'); + } + }); + } +}); \ No newline at end of file diff --git a/app/controllers/admins/salesman_customers_controller.rb b/app/controllers/admins/salesman_customers_controller.rb new file mode 100644 index 000000000..1c57d4862 --- /dev/null +++ b/app/controllers/admins/salesman_customers_controller.rb @@ -0,0 +1,13 @@ +class Admins::SalesmanCustomersController < Admins::BaseController + before_action :set_salesman + + def index + @customers = @salesman.salesman_customers.includes(:user, :school) + end + + private + def set_salesman + @salesman = Salesman.find params[:salesman_id] + end + +end \ No newline at end of file diff --git a/app/models/salesman_customer.rb b/app/models/salesman_customer.rb index cfa732b1e..c5e84650b 100644 --- a/app/models/salesman_customer.rb +++ b/app/models/salesman_customer.rb @@ -1,4 +1,22 @@ class SalesmanCustomer < ApplicationRecord belongs_to :salesman, :touch => true, counter_cache: true belongs_to :school + belongs_to :user + + def name + user.real_name + end + + def school_name + school.name + end + + def courses_count + CourseMember.where(user_id: id).teachers_and_admin.count + end + + def shixuns_count + ShixunMember.where(user_id: id, role: [1, 2]).count + end + end diff --git a/app/views/admins/salesman_customers/index.html.erb b/app/views/admins/salesman_customers/index.html.erb new file mode 100644 index 000000000..f7948b0f0 --- /dev/null +++ b/app/views/admins/salesman_customers/index.html.erb @@ -0,0 +1,15 @@ +<% define_admin_breadcrumbs do %> + <% add_admin_breadcrumb("#{@salesman.name}的客户", admins_salesmans_path) %> +<% end %> + +
+
+ <%= javascript_void_link '新增客户', class: 'btn btn-primary', data: {salesman_id: @salesman.id, toggle: 'modal', target: '.admin-add-salesman-customer-user-modal' } %> +
+
+ +
+ <%= render(partial: 'admins/salesman_customers/shared/list') %> +
+ +<%= render 'admins/salesman_customers/shared/add_salesman_customers_user_modal' %> diff --git a/app/views/admins/salesmans/shared/_edit_salesman_modal.html.erb b/app/views/admins/salesman_customers/shared/_add_salesman_customers_user_modal.html.erb similarity index 51% rename from app/views/admins/salesmans/shared/_edit_salesman_modal.html.erb rename to app/views/admins/salesman_customers/shared/_add_salesman_customers_user_modal.html.erb index b27a036f7..f31499f52 100644 --- a/app/views/admins/salesmans/shared/_edit_salesman_modal.html.erb +++ b/app/views/admins/salesman_customers/shared/_add_salesman_customers_user_modal.html.erb @@ -1,18 +1,25 @@ -