var customerData = [
];
var customerDataLeft = [
{
"key": "999",
"val": "上海2222(22)",
"optional_res": 0
},
{
"key": "101160",
"val": "上海ss展有限公司(3)",
"optional_res": 0
},
{
"key": "101154",
"val": "江苏限公司(99)",
"optional_res": 1
},
{
"key": "2",
"val": "北京(666)2",
"optional_res": 0
},
{
"key": "3",
"val": "南京(444)2",
"optional_res": 0
},
{
"key": "4",
"val": "重庆(9880890)",
"optional_res": 0
},
];
var customerRight = [];
var filteredData =[]
var salesUser= []
$(document).ready(function() {
$('input[name*="operating_account"]').on('click',function(){
var $this=$(this);
var mediaId=$this.parents('.medias-item').find('select[name*="media_id"]').val();
var projectId=$('select[name="project_id"]').val();
var customerId=$('select[name="customer_id"]').val();
if($('select[name="customer_id"]').val()=='' || $('select[name="project_id"]').val()=='' || $this.parents('.medias-item').find('select[name*="media_id"]').val()==''){
$this.attr('data-target','');
return swal({
title:"提示框",
text: '请先选择客户、项目、投放媒体!',
type:"warning",
showCancelButton:"true",
confirmButtonText:"确定",
cancelButtonText:"取消"
});
}else{
$.ajax({
url:'/admin/project/account/operation_account',
data:{media_id:mediaId,project_id:projectId,customer_id:customerId},
type:'POST',
complete:function(data){
var result = JSON.parse(data.responseText);
if(result.error_code === 0){
var list=result.list;
$this.parents('.wrapper').find('input.chosend_account_number').removeClass('chosend_account_number');
$this.addClass('chosend_account_number');
$this.parents('.wrapper').find('input.chosend_account_id').removeClass('chosend_account_id');
$this.parents('.accountModal').find('.operating_account_id').addClass('chosend_account_id');
let ceshidata= list
customerData=ceshidata
console.log('ceshidata',ceshidata)
if(customerDataLeft&&customerDataLeft.length>0){
console.log('ceshidata',ceshidata)
var handInputHtml =''
$.each(customerDataLeft, function(index, customer) {
handInputHtml+=`<div class=${customer.key}><label class="everylabel"><input style="margin:0px" class="everyInput" type="checkbox" value=${customer.key} ${customer.optional_res === 1 ? '' : 'disabled'} ></input><div class="everySpan"> <div class="everySpanIn">${customer.val}</div> <div class="everySpanIn">${customer.val}</div> <div class="everySpanIn">${customer.val}</div></div></label></div>`
});
$('#selectOptions').html(handInputHtml);
}
selectEvery()
selectAllFn()
console.log('customerDataLeft',customerDataLeft)
}else{
$('.handoverCustomers').hide()
return swal({
title:"提示框",
text:result.error_msg,
type:"warning",
showCancelButton:"true",
confirmButtonText:"确定",
cancelButtonText:"取消"
});
}
}
});
}
$this.attr('data-target','#chosenAccountModal');
})
function selectAllFn(){
$('#selectAllCheckbox').change(function() {
if ($(this).prop('checked')) {
$('#selectOptions input[type="checkbox"]').each(function() {
if (!$(this).prop('disabled')) {
$(this).prop('checked', true);
}else {
$(this).prop('checked', false);
}
});
console.log('customerDataLeft',customerDataLeft)
let customerDisData = customerDataLeft.filter(item => item.optional_res === 1);
console.log('customerDisData',customerDisData)
let dataToMerge=[]
if(filteredData && filteredData.length > 0){
dataToMerge=filteredData
}else {
dataToMerge= customerDisData
}
let mergedData = [...customerRight, ...dataToMerge];
mergedData = mergedData.filter((item, index, self) =>
index === self.findIndex((t) => (
t.key === item.key
))
);
customerRight=mergedData
console.log('customerRight',customerRight)
displayRight(mergedData);
$.each(customerDataLeft, function(index, leftItem) {
const leftKey = leftItem.key;
let isChecked = false;
$.each(mergedData, function(index, mergedItem) {
const mergedKey = mergedItem.key;
if (leftKey === mergedKey) {
isChecked = true;
return false;
}
});
$('#selectOptions .' + leftKey).find('input[type="checkbox"]').prop('checked', isChecked);
});
} else {
let remainingData = filteredData && filteredData.length > 0 ?
customerRight.filter(item => !filteredData.some(filteredItem => filteredItem.key === item.key)) : [];
console.log('filteredData',filteredData)
customerRight= JSON.parse(JSON.stringify(remainingData))
displayRight(remainingData);
if (filteredData && filteredData.length > 0) {
$.each(filteredData, function(index, filteredItem) {
$('#selectOptions .'+filteredItem.key).find('input[type="checkbox"]').prop('checked', false);
});
} else {
$('#selectOptions input[type="checkbox"]').prop('checked', false);
filteredData=[]
}
}
});
}
function selectEvery(){
$('#selectOptions').find('input[type="checkbox"]').on('change', function() {
var key = $(this).val();
var checkboxdata =$(this)
if(checkboxdata.prop('checked')){
const addIndex = customerDataLeft.find(item => item.key === key);
if(addIndex){
customerRight.push(JSON.parse(JSON.stringify(addIndex)));
}
}else {
const index = customerRight.findIndex(item => item.key === key);
if (index !== -1) {
customerRight.splice(index, 1);
}
}
displayRight(customerRight);
allCheckedFn()
});
}
function allCheckedFn(){
var allChecked = $('#selectOptions input[type="checkbox"]:checked').length;
var totalCheckboxes = $('#selectOptions input[type="checkbox"]').length;
if (allChecked === totalCheckboxes) {
$('#selectAllCheckbox').prop('indeterminate', false);
$('#selectAllCheckbox').prop('checked', true);
} else if (allChecked > 0) {
$('#selectAllCheckbox').prop('checked', false);
$('#selectAllCheckbox').prop('indeterminate', true);
} else {
$('#selectAllCheckbox').prop('indeterminate', false);
$('#selectAllCheckbox').prop('checked', false);
}
}
$('#changeOptions').on('click', '.delete-span', function() {
var deletedData = $(this).prev().text();
var prevClass = $(this).prev().attr('class');
console.log(deletedData,prevClass)
const indexRight = customerRight.findIndex(item => item.key === prevClass);
console.log('indexRight',indexRight)
if (indexRight !== -1) {
customerRight.splice(indexRight, 1);
displayRight(customerRight)
$('#selectOptions .' + prevClass).find('input[type="checkbox"]').prop('checked', false);;
}
allCheckedFn()
});
$('#handFilterInput').on('input', function() {
var keyword = $(this).val().trim().toLowerCase();
searchButton(keyword)
});
function searchButton(keyword) {
if (keyword === '') {
for (var n=0;n<customerDataLeft.length;n++){
$('#selectOptions .' + customerDataLeft[n].key).show()
}
filteredData=[]
} else {
filteredData = customerDataLeft.filter(function(item) {
if(item['val'].toLowerCase().includes(keyword)){
return item ;
}
});
console.log('filteredData',filteredData)
if (filteredData.length > 0) {
var keysToShow = {};
for (var m = 0; m < filteredData.length; m++) {
keysToShow[filteredData[m].key] = true;
}
for (var n = 0; n < customerDataLeft.length; n++) {
var key = customerDataLeft[n].key;
if (keysToShow[key]) {
$('#selectOptions .' + key).show();
} else {
$('#selectOptions .' + key).hide();
}
}
console.log('filteredData', filteredData, 'customerDataLeft', customerDataLeft);
} else {
for (var n=0;n<customerDataLeft.length;n++){
$('#selectOptions .' + customerDataLeft[n].key).hide()
}
}
}
}
function displayRight(data) {
var changeOptionsDiv = $('#changeOptions');
changeOptionsDiv.empty();
for (var i = 0; i < data.length; i++) {
var val = data[i].val;
var dataDiv = $('<div>').addClass('rightDiv').append(
$('<span style="display:inline-block">').text(val).addClass(data[i].key),
$('<span>').text('x').addClass('delete-span')
);
changeOptionsDiv.append(dataDiv);
}
}
function clearHandOverForm (){
$('.hand-over-form input[type="checkbox"]').prop('checked', false);
$('.hand-over-form input[type="text"]').val('');
$('.hand-over-form textarea').val('');
$('.hand-over-form select').val('');
customerDataLeft=[]
filteredData=[]
customerRight=[]
displayRight([])
$('.save-hand-over-btn').prop('disabled', false).css({
'background-color':'#18a689',
'border-color': '#18a689'
});
}
$('.cancel-hand-over-btn').click(function() {
clearHandOverForm()
});
$('#handoverCustomers').on('hidden.bs.modal', function () {
clearHandOverForm()
});
$('.closeOverCustomer').on('click', function() {
clearHandOverForm()
});
$('.save-hand-over-btn').click(function() {
$('.save-hand-over-btn').prop('disabled', true).css({
'background-color': '#cccccc',
'border-color': '#cccccc'
})
if($(".hand-over-form").valid()){
console.log(' // 如果校验通过')
var formData = {};
$('.hand-over-form').serializeArray().forEach(function(item) {
formData[item.name] = item.value;
});
let customerIdsFilter = customerRight.map(item => item.key);
let objSubmit = {
customerIds:[],
receiver:formData.receiver,
year:formData.year,
remarks:''
}
if(customerIdsFilter.length==0){
return swal({
title:"提示框",
text:'客户为必填项,请完善信息!',
type:"warning",
showCancelButton:"true",
confirmButtonText:"确定",
cancelButtonText:"取消",
},function(isConfirm){
if(isConfirm){
}else {
console.log('取消了吗')
}
$('.save-hand-over-btn').prop('disabled', false).css({
'background-color':'#18a689',
'border-color': '#18a689'
});
});
}else {
objSubmit.customerIds=customerIdsFilter
}
if(formData.remarks==3){
objSubmit.remarks=formData.remark
}else {
remarkdata.map((item) => {
if (item.key == formData.remarks) {
objSubmit.remarks = item.val;
}
return objSubmit;
})
}
saveHandOverCheck(objSubmit)
}else {
$('.save-hand-over-btn').prop('disabled', false).css({
'background-color':'#18a689',
'border-color': '#18a689'
});
}
});
function saveHandOverCheck (data){
console.log('校验datadata',data)
let subdata=data
$.ajax({
url: '/admin/crm/customer/handover/check',
type: 'POST',
data:data,
dataType: 'json',
complete: function(response) {
let data= response.responseJSON
if(data.error_code==0){
if( data.result.remind_msg!=''){
swal({
title: "提示框",
text: data.result.remind_msg,
type: "warning",
showCancelButton: true,
allowOutsideClick :false,
confirmButtonColor: "#1ab394",
confirmButtonText: "是",
cancelButtonText:"否",
closeOnConfirm: true,
},function(isConfirm){
if(isConfirm){
$('.save-hand-over-btn').prop('disabled', false).css({
'background-color':'#18a689',
'border-color': '#18a689'
});
}else {
HandOverSubmit(subdata)
}
})
}else {
HandOverSubmit(subdata)
}
}else {
$('.save-hand-over-btn').prop('disabled', false).css({
'background-color':'#18a689',
'border-color': '#18a689'
});
return swal({
title:"提示框",
text: data.error_msg,
type:"warning",
showCancelButton:"true",
confirmButtonText:"确定",
cancelButtonText:"取消",
});
}
},
error: function(xhr, status, error) {
console.error('请求失败', error);
}
});
}
});
function HandOverSubmit(subdata){
$.ajax({
url: '/admin/crm/customer/handover/submit',
type: 'POST',
data: subdata,
dataType: 'json',
complete: function(response){
var data = JSON.parse(response.responseText);
if (data.error_code === 0) {
window.location.href = '/admin/work/check/list?tab=2';
clearHandOverForm()
} else if (data.error_code === 200) {
location.reload();
} else {
setTimeout(function () {
swal({
title: "提示框",
text: data.error_msg,
type: "warning",
showCancelButton: "true",
confirmButtonText: "确定",
cancelButtonText: "取消",
});
}, 100);
}
}
})
}