{table}
'
, base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))) }
, format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) }
let startDate = ''
let endDate = ''
if (this.refInpDate.value) {
startDate = this.refInpDate.value.split(' - ')[0].split('/')[1] + '.' + this.refInpDate.value.split(' - ')[0].split('/')[0] + '.' + this.refInpDate.value.split(' - ')[0].split('/')[2];
endDate = this.refInpDate.value.split(' - ')[1].split('/')[1] + '.' + this.refInpDate.value.split(' - ')[1].split('/')[0] + '.' + this.refInpDate.value.split(' - ')[1].split('/')[2]
}
function exportExcel(table, name) {
if (!table.nodeType) table = document.getElementById(table)
var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML }
var a = document.createElement('a');
a.href = uri + base64(format(template, ctx))
a.download = 'Lich su gia ' + startDate + "-" + endDate + '.xls';
a.click();
}
return exportExcel(table, name)
}
},
handleClearMCK() {
this.refInputAC.value = '';
ownerCDL.iClose.style.display = 'none'
ownerCDL.iSearch.style.display = 'block'
},
handleFindDisclosure() {
this.refInputAC.value
this.pageIndex = 1;
this.reRender()
},
handleClickIitemStockCode(value) {
const lsMenuItem = document.getElementsByClassName('info-data-history-menu-item');
//Array.from(lsMenuItem).forEach((e, i) => {
// e.setAttribute('href', `/Lich-su-giao-dich-${value.toLowerCase()}-${i + 1}.chn#data`)
//})
this.refInputAC.value = value;
//window.history.pushState('', '', `/Lich-su-giao-dich-${value.toLowerCase()}-1.chn#data`);
window.location.href = `/du-lieu/Lich-su-giao-dich-${value.toLowerCase()}-1.chn#data`;
this.lsAC.style.display = 'none';
},
APICallRateLimit(func, delay) {
clearTimeout(this.timerId);
this.timerId = setTimeout(func, delay);
},
autocomplete() {
let currentFocus;
const ref = this.refInputAC
let search = '';
let resultSearch = [];
var timerId;
ref.addEventListener('blur', (e) => {
if (!ref.value) {
ref.value = ownerCDL.prevValue;
}
if (ownerCDL.refInputAC.value || ownerCDL.prevValue) {
ownerCDL.iClose.style.display = 'block'
ownerCDL.iSearch.style.display = 'none'
ownerCDL.iClose.before.color = 'none'
} else {
ownerCDL.iClose.style.display = 'none'
ownerCDL.iSearch.style.display = 'block'
}
setTimeout(() => {
const list = ownerCDL.lsAC
list.style.display = "none";
}, 500)
})
ref.addEventListener("focus", (e) => {
ownerCDL.prevValue = ref.value;
ref.value = "";
});
function addActive(x) {
if (!x) return false;
removeActive(x);
if (currentFocus >= x.length) currentFocus = 0;
if (currentFocus < 0) currentFocus = x.length - 1;
x[currentFocus].classList.add("autocomplete-active");
}
function removeActive(x) {
for (var i = 0; i < x.length; i++) {
x[i].classList.remove("autocomplete-active");
}
}
ref.addEventListener("keydown", function (e) {
var x = document.getElementById("search-stock-list");
if (x) x = x.getElementsByTagName("div");
if (e.keyCode == 40) {
currentFocus++;
addActive(x);
} else if (e.keyCode == 38) {
currentFocus--;
addActive(x);
} else if (e.keyCode == 13) {
e.preventDefault();
if (currentFocus > -1) {
if (x) x[currentFocus].click();
}
}
});
function getData() {
fetch('https://search.cafef.vn/api/searching/v1/Companies/SearchByKeyWord?' + new URLSearchParams({
keyword: search,
}), {
method: "GET",
headers: {
"Content-Type": "application/json",
},
})
.then((res) => res.json())
.then((result) => {
resultSearch = [...result?.value?.documents];
renderLisSearch();
});
}
function renderLisSearch() {
const lists = document.getElementById('search-stock-list')
let items = "";
if (resultSearch.length > 0) {
lists.style.display = "block";
} else {
lists.style.display = 'none';
}
currentFocus = 0;
if (resultSearch.length > 0) {
resultSearch.forEach((e, i) => {
const a =
`
${e.highlights.title || e.document.symbol}
${e.highlights.description || e.document.description}
`
items += a;
})
}
lists.innerHTML = items
}
ref.addEventListener("input", function () {
search = this.value;
if (search.length >= 2) {
ownerCDL.APICallRateLimit(getData, 500)
} else {
resultSearch = []
renderLisSearch()
}
});
},
renderDateSpicker() {
$('input[name="daterange"]').daterangepicker({
"autoUpdateInput": false,
"maxDate": moment().format('DD/MM/YYYY'),
"locale": {
"format": "DD/MM/YYYY",
"separator": " - ",
"applyLabel": "Apply",
"cancelLabel": "Cancel",
"fromLabel": "From",
"toLabel": "To",
"customRangeLabel": "Custom",
"daysOfWeek": [
"CN",
"Th2",
"Th3",
"Th4",
"Th5",
"Th6",
"Th7"
],
"applyLabel": "Chọn",
"cancelLabel": "Bỏ",
"monthNames": [
"Tháng 1",
"Tháng 2",
"Tháng 3",
"Tháng 4",
"Tháng 5",
"Tháng 6",
"Tháng 7",
"Tháng 8",
"Tháng 9",
"Tháng 10",
"Tháng 11",
"Tháng 12"
],
"firstDay": 1
},
opens: 'center'
});
$('input[name="daterange"]').on('apply.daterangepicker', function (ev, picker) {
$(this).val(picker.startDate.format('DD/MM/YYYY') + ' - ' + picker.endDate.format('DD/MM/YYYY'));
document.getElementById('tabletoExcel').style.background = "#33A42E";
});
$('input[name="daterange"]').on('cancel.daterangepicker', function (ev, picker) {
$(this).val('');
document.getElementById('tabletoExcel').style.background = "rgb(193, 227, 192)";
});
},
renderTable() {
const checkColor = (check) => {
return check.includes("-") === false ? "up" : "down";
};
let html = '';
var today = new Date();
var date = today.getDate() + '/' + ((today.getMonth() + 1) < 10 ? "0" + (today.getMonth() + 1) : (today.getMonth() + 1)) + '/' + today.getFullYear();
if (this.data?.Data.length > 0) {
this.data.Data.forEach((item, index) => {
html += `
${item.Ngay}
${(item.Ngay == date && today.getHours() < 15) ? "--" : item.GiaDongCua}
${(this.formatNumber(item.GiaDieuChinh) == item.GiaDongCua) || (item.Ngay == date && today.getHours() < 15) ? "--" : this.formatNumber(item.GiaDieuChinh) }
${(item.Ngay == date && today.getHours() < 15) ? "--" : item.ThayDoi}
${this.formatNumber(item.KhoiLuongKhopLenh)}
${this.formatNumber(item.GiaTriKhopLenh/1000000000)}
${this.formatNumber(item.KLThoaThuan)}
${this.formatNumber(item.GtThoaThuan/1000000000)}
${this.formatNumber(item.GiaMoCua)}
${this.formatNumber(item.GiaCaoNhat)}
${this.formatNumber(item.GiaThapNhat)}
`
})
} else {
html = `
KHÔNG CÓ KẾT QUẢ PHÙ HỢP
`
}
this.refTable.innerHTML = html
this.loading.style.display = 'none';
this.isLoading = false;
},
renderPaging() {
let str = ''
this.getMaxMin()
if (this.totalPage > 0) {
str += this.minPage !== 1 ? `
`: ''
for (let i = this.minPage; i <= this.maxPage; i++) {
str += `
`
}
str += this.maxPage !== this.totalPage ? `
`: ''
} else {
str += `
`
}
this.refContainerPaging.innerHTML = str;
this.activePaging()
},
getMaxMin() {
if (this.countViewPage < this.totalPage) {
if (this.pageIndex + 4 <= this.totalPage && this.pageIndex - 4 >= 1) {
this.minPage = this.pageIndex - 4
this.maxPage = this.pageIndex + 4
} else {
if (this.pageIndex + 4 > this.totalPage && this.pageIndex - 4 >= 1) {
this.maxPage = this.totalPage
this.minPage = this.totalPage - 8
} else {
this.minPage = 1;
this.maxPage = 9;
}
}
} else {
this.minPage = 1;
this.maxPage = this.totalPage
}
},
activePaging() {
if (this.pageIndex === this.totalPage && this.totalPage >= 1) {
this.checkVronLef.classList.remove('enable')
this.checkVronRight.classList.add('enable')
}
else if (this.pageIndex === 1 && this.totalPage > 1) {
this.checkVronLef.classList.add('enable')
this.checkVronRight.classList.remove('enable')
} else if (this.pageIndex === 1 && this.totalPage <= 1) {
this.checkVronLef.classList.add('enable')
this.checkVronRight.classList.add('enable')
}
else {
this.checkVronLef.classList.remove('enable')
this.checkVronRight.classList.remove('enable')
}
},
async reRender() {
if (this.isLoading === false) {
ownerCDL.getData()
}
},
handleChangePage(page) {
if (page !== this.pageIndex && page >= 1 && page <= this.totalPage) {
if (page <= 1) {
this.pageIndex = 1
} else if (page >= this.totalPage) {
this.pageIndex = this.totalPage
} else {
this.pageIndex = page
}
this.reRender()
}
this.APICallRateLimit(this.reRender, 700)
},
async init() {
this.renderDateSpicker()
await this.getData()
this.renderTable()
this.autocomplete()
this.renderPaging()
}
}
$(document).ready(function () {
ownerCDL.init()
});