'use strict';

/**
 *
 *
 * @author Mai
 * @date 2018/5/30
 * @version
 */

function getTenderId() {
    return window.location.pathname.split('/')[2];
}

function checkTzMeasureType () {
    return tender.measure_type === measureType.tz.value;
}

$(document).ready(() => {
    autoFlashHeight();
    const ledgerSpread = SpreadJsObj.createNewSpread($('#ledger-spread')[0]);
    const ledgerTree = createNewPathTree('base', {
        id: 'ledger_id',
        pid: 'ledger_pid',
        order: 'order',
        level: 'level',
        rootId: -1,
        keys: ['id', 'tender_id', 'ledger_id'],
        preUrl: '/ledger'
    });
    ledgerTree.loadDatas(ledger);
    SpreadJsObj.initSheet(ledgerSpread.getActiveSheet(), ledgerSpreadSetting);
    SpreadJsObj.loadSheetData(ledgerSpread.getActiveSheet(), 'tree', ledgerTree);

    // 初始化 部位明细
    const pos = new PosData({
        id: 'id', ledgerId: 'lid',
    });
    const posSpread = SpreadJsObj.createNewSpread($('#pos-spread')[0]);

    // 台账模式加载部位明细数据
    if (checkTzMeasureType()) {
        $.divResizer({
            select: '#main-resize',
            callback: function () {
                ledgerSpread.refresh();
                let bcontent = $(".bcontent-wrap") ? $(".bcontent-wrap").height() : 0;
                $(".sp-wrap").height(bcontent-40);
                posSpread.refresh();
            }
        });
        SpreadJsObj.initSheet(posSpread.getActiveSheet(), posSpreadSetting);
        postData('/tender/' + getTenderId() + '/pos', null, function (data) {
            pos.loadDatas(data);
        });
    }

    ledgerSpread.bind(GC.Spread.Sheets.Events.SelectionChanged, function (e, info) {
        const sheet = ledgerSpread.getActiveSheet();
        let node;
        if (!sheet || !sheet.zh_tree) {
            node = null;
        } else {
            const sel = sheet.getSelections()[0];
            node = sheet.zh_tree.nodes[sel.row];
        }

        if (node) {
            const posData = pos.ledgerPos[itemsPre + node.id] || [];
            SpreadJsObj.loadSheetData(posSpread.getActiveSheet(), 'data', posData);
        } else {
            SpreadJsObj.loadSheetData(posSpread.getActiveSheet(), 'data', []);
        }
    });

    let dealBills;
    // 展开收起 签约清单|清单意见
    $('a', '.side-menu').bind('click', function () {
        const tab = $(this), tabPanel = $(tab.attr('content'));
        const showSideTools = function (show) {
            if (show) {
                $('.c-body.col-12').removeClass('col-12').addClass('col-8');
                $('.c-body.col-0').removeClass('col-0').addClass('col-4').show();
            } else {
                $('.c-body.col-8').removeClass('col-8').addClass('col-12');
                $('.c-body.col-4').removeClass('col-4').addClass('col-0').hide();
            }
        };
        if (!tab.hasClass('active')) {
            $('a', '#side-menu').removeClass('active');
            tab.addClass('active');
            showSideTools(tab.hasClass('active'));
            $('.tab-content .tab-pane').hide();
            tabPanel.show();
            if (tab.attr('content') === '#deal-bills' && !dealBills) {
                dealBills = new DealBills($('#deal-bills-spread')[0], {
                    cols: [
                        {title: '清单编号', field: 'code', width: 120, formatter: '@', readOnly: true},
                        {title: '名称', field: 'name', width: 230, formatter: '@', readOnly: true},
                        {title: '单位', field: 'unit', width: 50, formatter: '@', readOnly: true},
                        {title: '单价', field: 'unit_price', width: 50, readOnly: true},
                        {title: '数量', field: 'quantity', width: 50, readOnly: true},
                    ],
                    emptyRows: 0,
                    headRows: 1,
                    headRowHeight: [40],
                    defaultRowHeight: 21,
                });
                dealBills.loadData();
            }
        } else {
            tab.removeClass('active');
            showSideTools(tab.hasClass('active'));
            tabPanel.hide();
        }
        ledgerSpread.refresh();
    });
    class DealBills {
        constructor (obj, spreadSetting) {
            this.obj = obj;
            this.url = '/tender/' + getTenderId() + '/deal';
            this.spreadSetting = spreadSetting;
            this.spread = SpreadJsObj.createNewSpread(this.obj);
            SpreadJsObj.initSheet(this.spread.getActiveSheet(), this.spreadSetting);
        }
        loadData () {
            const self = this;
            postData(this.url+'/get-data', {}, function (data) {
                SpreadJsObj.loadSheetData(self.spread.getActiveSheet(), 'data', data);
            });
        }
    }
});
