'use strict';

/**
 *
 *
 * @author Mai
 * @date
 * @version
 */

$(document).ready(function () {
    let per = _.toNumber(getLocalCache('StageGatherOverPercent'));
    if (per && !_.isNaN(per)) {
        $('#over-percent').val(per >= 50 ? (per <= 100 ? per : 100) : 50);
    }
    autoFlashHeight();
    // 初始化工程量清单
    const gclSpread = SpreadJsObj.createNewSpread($('#gcl-spread')[0]);
    SpreadJsObj.initSheet(gclSpread.getActiveSheet(), gclSpreadSetting);
    // 初始化所属项目节
    const leafXmjSpread = SpreadJsObj.createNewSpread($('#leaf-xmj-spread')[0]);
    SpreadJsObj.initSheet(leafXmjSpread.getActiveSheet(), leafXmjSpreadSetting);
    // 上下窗口resizer
    $.divResizer({
        select: '#main-resize',
        callback: function () {
            gclSpread.refresh();
            let bcontent = $(".bcontent-wrap") ? $(".bcontent-wrap").height() : 0;
            $(".sp-wrap").height(bcontent-40);
            leafXmjSpread.refresh();
        }
    });
    // 解析清单汇总数据
    gclGatherModel.loadLedgerData(ledger, curLedgerData, []);
    gclGatherModel.loadPosData(pos, curPosData);
    gclGatherModel.loadDealBillsData(dealBills);
    const gclGatherData = gclGatherModel.gatherGclData();
    // 获取项目节数据
    function loadLeafXmjData(iGclRow) {
        const gcl = gclGatherData[iGclRow];
        if (gcl) {
            SpreadJsObj.loadSheetData(leafXmjSpread.getActiveSheet(), SpreadJsObj.DataType.Data, gcl.leafXmjs);
        } else {
            SpreadJsObj.loadSheetData(leafXmjSpread.getActiveSheet(), SpreadJsObj.DataType.Data, []);
        }
    }
    // 超计显示
    function checkOverRange() {
        const sheet = gclSpread.getActiveSheet();
        const bQty = $('#customRadio1')[0].checked, bDealQty = $('#customRadio2')[0].checked;
        const nPercent = Math.min(Math.max(ZhCalc.div(parseFloat($('#over-percent').val()), 100), 0.5), 1);
        SpreadJsObj.beginMassOperation(sheet);
        for (let iRow = 0, iLength = sheet.getRowCount(); iRow < iLength; iRow++) {
            const node = sheet.zh_data[iRow];
            if (node) {
                const bOverRangeQty = node.quantity ? node.end_gather_qty > ZhCalc.mul(node.quantity, nPercent) : node.end_gather_qty;
                const bOverRangeDealQty = node.deal_bills_qty ? node.end_gather_qty > ZhCalc.mul(node.deal_bills_qty, nPercent) : node.end_gather_qty;
                const bOverRange = bQty ? bOverRangeQty : (bDealQty ? bOverRangeDealQty : bOverRangeQty || bOverRangeDealQty);
                const color = bOverRange ? '#f8d7da' : '';
                sheet.getRange(iRow, -1, 1, -1).backColor(color);
            }
        }
        SpreadJsObj.endMassOperation(sheet);
    }
    // 加载清单数据
    SpreadJsObj.loadSheetData(gclSpread.getActiveSheet(), SpreadJsObj.DataType.Data, gclGatherData);
    loadLeafXmjData(0);
    checkOverRange();
    // 切换清单行，读取所属项目节数据
    gclSpread.getActiveSheet().bind(spreadNS.Events.SelectionChanged, function (e, info) {
        const iOldRow = info.oldSelections[0].row, iNewRow = info.newSelections[0].row;
        if (iNewRow !== iOldRow) {
            loadLeafXmjData(iNewRow);
        }
    });
    $('.custom-radio').click(() => {
        checkOverRange();
        // 收起菜单
        $('.dropdown-menu').click();
    });
    $('#over-percent').change(function () {
        setLocalCache('StageGatherOverPercent', this.value);
        checkOverRange();
    });
});