// 'id' => "tableLastFixtureCompare",
// 'label' => 'เปรียบเทียบผลบอล',
// 'homeTeam' => $homeTeam,
// 'awayTeam' => $awayTeam,
// 'homeFixtures' => $fixture->fixture_analysis_homes,
// 'awayFixtures' => $fixture->fixture_analysis_aways,
// 'headerClass' => 'bg-primary'
const hcMatchCount = {};
const hcSelectedCount = {};
const hcHalf = {};
const hcLeague = {};
const hcHome = {};
const hcAway = {};
const hcHomeFixture = {};
const hcAwayFixture = {};
function renderHistoryCompare(divId, label, homeTeam, awayTeam, headerClass, data) {
if (!data?.home?.length || !data?.away?.length) {
return;
}
const maxLength = Math.min(20, data.home.length, data.away.length);
const selectedCount = Math.min(10, maxLength);
hcMatchCount[divId] = Math.min(10, maxLength);
hcSelectedCount[divId] = selectedCount;
hcHome[divId] = homeTeam;
hcAway[divId] = awayTeam;
hcHomeFixture[divId] = data.home;
hcAwayFixture[divId] = data.away;
let html = `
`;
html += `
`;
html += `
${label}
`;
html += `
`;
html += ``;
html += `
`;
html += `
`;
html += `
`;
html += ``;
html += ``;
html += `ทีม | `;
html += `ได้ | `;
html += `เสีย | `;
html += `ผลต่างประตู | `;
html += `เฉลี่ยได้ประตู | `;
html += `ชนะ% | `;
html += `เสมอ% | `;
html += `แพ้% | `;
html += `เหย้า/เยือน | `;
html += `ได้ | `;
html += `เสีย | `;
html += `ผลต่างประตู | `;
html += `เฉลี่ยได้ประตู | `;
html += `ชนะ% | `;
html += `เสมอ% | `;
html += `แพ้% | `;
html += `
`;
html += ``;
html += ``;
html += ``;
html += `
`;
html += `
`;
$('#historyCompareSection').html(html);
renderHistoryCompareContent(divId);
}
function renderHistoryCompareContent(divId) {
let len = Math.min(hcMatchCount[divId], hcHomeFixture[divId].length, hcAwayFixture[divId].length);
const hs = {
'a': { 'ga': 0, 'gl': 0, 'w': 0, 'd': 0, 'l': 0, 'c': 0 },
't': { 'ga': 0, 'gl': 0, 'w': 0, 'd': 0, 'l': 0, 'c': 0 },
};
for (let i = 0; i < len; i++) {
const f = hcHomeFixture[divId][i];
const g = f['score'].split('-');
const gh = parseInt(g[0]);
const ga = parseInt(g[1]);
if (hcHome[divId] == f['team_home']) {
hs['t']['ga'] += gh;
hs['t']['gl'] += ga;
hs['a']['ga'] += gh;
hs['a']['gl'] += ga;
if (gh == ga) {
hs['t']['d']++;
hs['a']['d']++;
} else if (gh > ga) {
hs['t']['w']++;
hs['a']['w']++;
} else {
hs['t']['l']++;
hs['a']['l']++;
}
hs['t']['c']++;
hs['a']['c']++;
} else {
hs['a']['ga'] += ga;
hs['a']['gl'] += gh;
if (gh == ga) {
hs['a']['d']++;
} else if (gh > ga) {
hs['a']['l']++;
} else {
hs['a']['w']++;
}
hs['a']['c']++;
}
}
hs['t']['c'] = Math.max(1, hs['t']['c']);
hs['a']['c'] = Math.max(1, hs['a']['c']);
const as = {
'a': { 'ga': 0, 'gl': 0, 'w': 0, 'd': 0, 'l': 0, 'c': 0 },
't': { 'ga': 0, 'gl': 0, 'w': 0, 'd': 0, 'l': 0, 'c': 0 },
};
for (let i = 0; i < len; i++) {
const f = hcAwayFixture[divId][i];
const g = f['score'].split('-');
const gh = parseInt(g[0]);
const ga = parseInt(g[1]);
if (hcAway[divId] == f['team_away']) {
as['t']['ga'] += ga;
as['t']['gl'] += gh;
as['a']['ga'] += ga;
as['a']['gl'] += gh;
if (gh == ga) {
as['t']['d']++;
as['a']['d']++;
} else if (gh < ga) {
as['t']['w']++;
as['a']['w']++;
} else {
as['t']['l']++;
as['a']['l']++;
}
as['t']['c']++;
as['a']['c']++;
} else {
as['a']['ga'] += gh;
as['a']['gl'] += ga;
if (gh == ga) {
as['a']['d']++;
} else if (gh < ga) {
as['a']['l']++;
} else {
as['a']['w']++;
}
as['a']['c']++;
}
}
as['t']['c'] = Math.max(1, as['t']['c']);
as['a']['c'] = Math.max(1, as['a']['c']);
const thn = hcHome[divId];
const tan = hcAway[divId];
let html = ``;
html += ``
html += `${thn} | `
html += `${hs['a']['ga']} | `;
html += `${hs['a']['gl']} | `;
html += `${hs['a']['ga'] - hs['a']['gl']} | `;
html += `${(hs['a']['ga'] / hs['a']['c']).toFixed(1)} | `;
html += `${(hs['a']['w'] / hs['a']['c'] * 100).toFixed(0)}% | `;
html += `${(hs['a']['d'] / hs['a']['c'] * 100).toFixed(0)}% | `;
html += `${(hs['a']['l'] / hs['a']['c'] * 100).toFixed(0)}% | `;
html += `ทีมเหย้า | `;
html += `${hs['t']['ga']} | `;
html += `${hs['t']['gl']} | `;
html += `${hs['t']['ga'] - hs['t']['gl']} | `;
html += `${(hs['t']['ga'] / hs['t']['c']).toFixed(1)} | `;
html += `${(hs['t']['w'] / hs['t']['c'] * 100).toFixed(0)}% | `;
html += `${(hs['t']['d'] / hs['t']['c'] * 100).toFixed(0)}% | `;
html += `${(hs['t']['l'] / hs['t']['c'] * 100).toFixed(0)}% | `;
html += `
`;
html += ``
html += `${tan} | `
html += `${as['a']['ga']} | `;
html += `${as['a']['gl']} | `;
html += `${as['a']['ga'] - as['a']['gl']} | `;
html += `${(as['a']['ga'] / as['a']['c']).toFixed(1)} | `;
html += `${(as['a']['w'] / as['a']['c'] * 100).toFixed(0)}% | `;
html += `${(as['a']['d'] / as['a']['c'] * 100).toFixed(0)}% | `;
html += `${(as['a']['l'] / as['a']['c'] * 100).toFixed(0)}% | `;
html += `ทีมเหย้า | `;
html += `${as['t']['ga']} | `;
html += `${as['t']['gl']} | `;
html += `${as['t']['ga'] - as['t']['gl']} | `;
html += `${(as['t']['ga'] / as['t']['c']).toFixed(1)} | `;
html += `${(as['t']['w'] / as['t']['c'] * 100).toFixed(0)}% | `;
html += `${(as['t']['d'] / as['t']['c'] * 100).toFixed(0)}% | `;
html += `${(as['t']['l'] / as['t']['c'] * 100).toFixed(0)}% | `;
html += `
`;
$('#'+divId+'Tbody').html(html);
}
function hcMatchCountChange(event, divId) {
hcMatchCount[divId] = event.target.value;
renderHistoryCompareContent(divId);
}