/*
 * @Author: 杨水淼 yangshuimiao@jsjd.cc
 * @Date: 2025-03-21 11:58:51
 * @LastEditors: 杨水淼 yangshuimiao@jsjd.cc
 * @LastEditTime: 2025-07-16 16:15:16
 * @FilePath: \echarts\js\pubfunc.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/**
 * hide switch the iframe
 * @param cbid:checkbox id
 * @param iframeid: iframe id
 */
function hideSwitch(cbid,iframeid){
    if($("#"+cbid).prop("checked")) {
        $("#"+iframeid).css('display','block');
    }
    else {
        $("#"+iframeid).css('display','none');
    }
}
/**
 * 
 * @param {*} jsonData : from ajax, e.g: https://echart.doorcome.cn/inc/ajax.inc.php?t=esfTBD&t_start=2023-06-20&t_end=2023-08-23&district=合计'
 * @param {*} array :e.g: dataSel=['td','area'];
 * @returns 
 */
function getRows(jsonData,array){
    var listSplit = [];
    var tmp;
    for (let i = 0; i < jsonData.length; i++) {
        var arr = [];
        for (let j = 0; j < array.length; j++) {
            tmp=(jsonData[i][array[j]])?jsonData[i][array[j]]:'';
            arr[j]=tmp;
            
        }
        listSplit[i]={"value":arr};
    }

    return listSplit;

}

function dmChange(parid,dayblock,monblock){
    var v= $('#'+parid).val();
    if(v=='Monthly'){
       $('#'+dayblock).css('display','none');
       $('#'+monblock).css('display','inline');
    }
    if(v=='Daily'){
        $('#'+dayblock).css('display','inline');
        $('#'+monblock).css('display','none');
    }
}
/*
    <option value='000001.SH'>上证指数</option>
    <option value='399001.SZ'>深圳成指</option>
    <option value='399005.SZ'>中小板指</option>
    <option value='399006.SZ'>创业板指</option>
    <option value='000016.SZ'>上证50</option>
    <option value='399300.SZ'>沪深300</option>
    <option value='399905.SZ'>中证500</option>
*/
function convertIndexCode(tscode){
    switch(tscode){
        case '000001.SH':
            return '上证指数';
        case '399001.SZ':
            return '深圳成指';
        case '399005.SZ':
            return '中小板指';
        case '399006.SZ':
            return '创业板指';
        case '000016.SH':
            return '上证50';
        case '399300.SZ':
            return '沪深300';
        case '399905.SZ':
            return '中证500';
        default:
            return '';
    }
}

function formatDateToYYYYMMDD(dateStr) {
    return dateStr.replace(/-/g, '');
}

function pickData(data, dateKey, valueKey,fixed=2) {
    data.sort((a, b) => a[dateKey] - b[dateKey]);
    return data.map(item => ({
        value: [
            item[dateKey].slice(0, 4) + '-' + item[dateKey].slice(4, 6) + '-' + item[dateKey].slice(6, 8), // 格式化日期 yyyymmdd 转为 yyyy-mm-dd
            item[valueKey] === null || item[valueKey] === undefined || item[valueKey] === '' ? '' : parseFloat(item[valueKey]).toFixed(fixed)
        ]
    }));
}

/* 将上述pickData函数返回的结果，value计算返回最大值，最小值，均值，以及最后一个值 */
function calculateStats(data,fixed=2) {
    if (data.length === 0) {
        return {
            max: null,
            min: null,
            avg: null,
            last: null
        };
    }

    let max = -Infinity;
    let min = Infinity;
    let sum = 0;
    let last = null;
    let count = 0;

    for (const item of data) {
        const valueStr = item.value[1];
        if (valueStr === '' || valueStr === null || valueStr === undefined) continue;
        const value = parseFloat(valueStr);
        if (isNaN(value)) continue;

        if (value > max) {
            max = value;
        }
        if (value < min) {
            min = value;
        }
        sum += value;
        last = value;
        count++;
    }

    const avg = count > 0 ? sum / count : null;
    return {
        max: max !== -Infinity ? max.toFixed(fixed) : null,
        min: min !== Infinity ? min.toFixed(fixed) : null,
        avg: avg !== null ? avg.toFixed(fixed) : null,
        last: last !== null ? last.toFixed(fixed) : null
    };
}