'use strict';

/**
 * 指标库-项目节参数
 *
 * @author Mai
 * @date 2018/4/25
 * @version
 */

$(document).ready(function() {
    const billsSpread = SpreadJsObj.createNewSpread($('#bills-spread')[0]);
    const billsSheet = billsSpread.getActiveSheet();
    const billsTree = createNewPathTree({
        id: 'n_id',
        pid: 'n_pid',
        order: 'n_order',
        level: 'n_level',
        full_path: 'n_full_path',
        is_leaf: 'n_is_leaf',
        rootId: -1,
        keys: ['id', 'lib_id', 'n_id'],
        preUrl: '/lib/detail',
    });
    billsTree.loadDatas(bills);
    SpreadJsObj.initSheet(billsSheet, {
        cols: [
            {title: '项目节编号', field: 'code', width: 120, cellType: 'tree', vAlign: 1, readOnly: true},
            {title: '清单编号', field: 'b_code', width: 80, vAlign: 1, readOnly: true},
            {title: '名称', field: 'name', width: 200, vAlign: 1, readOnly: true},
            {title: '单位', field: 'units', width: 62, vAlign: 1, hAlign: 1, readOnly: true},
            {title: '数量1', field: 'dgn_quantity1', width: 60, type: 'Number', vAlign: 1, readOnly: true},
            {title: '数量2', field: 'dgn_quantity2', width: 60, type: 'Number', vAlign: 1, readOnly: true},
            {title: '金额', field: 'total_price', width: 60, type: 'Number', vAlign: 1, readOnly: true},
        ],
        //treeCol: 0,
        emptyRows: 3,
        headRows: 2,
        headRowHeight: [28],
        defaultRowHeight: 30,
    });
    SpreadJsObj.loadSheetData(billsSheet, 'tree', billsTree);
    billsSheet.selectionPolicy(0);
    billsSheet.selectionUnit(1);
    billsSheet.clearSelection();
    billsSheet.setActiveCell(0);
    setquotaParamIndex();
    const spreadNS = GC.Spread.Sheets;

    //项目节选中
    billsSheet.bind(spreadNS.Events.SelectionChanged, function (e, info) {
        setquotaParamIndex();
    });

    function setquotaParamIndex() {
        const billsInfo = billsTree.nodes[billsSheet.getActiveRowIndex()];
        $('#codeName').text(billsInfo.name);
        $('#indexList').html('');
        $('#paramList').html('');
        if(billsInfo.match_node !== null){
            const postUrl = '/lib/getParamAndIndex';
            const postInfo = {
                lib_id: billsInfo.lib_id,
                node_id: billsInfo.match_node,
                bills_id: billsInfo.id
            };
            postData(postUrl, postInfo, function (result) {
                updateIndexHTML(result.indexList);
                updateParamHTML(result.paramList);
            });
        }
    }
    
    function updateIndexHTML(indexlist) {
        let index_html = '';
        const index_list = indexlist;
        for(let i in index_list) {
            index_html += '<tr> <td>'+ index_list[i].code +'</td> <td>'+ index_list[i].name +'</td>' +
                '<td>'+ (index_list[i].unit1 !== null ? index_list[i].unit1 : '') +'</td>' +
                '<td>'+ (index_list[i].unit2 !== null ? index_list[i].unit2 : '') +'</td>' +
                '<td>'+ index_list[i].rule +'</td> <td class="text-right">'+ (index_list[i].eval_rule !== null ? index_list[i].eval_rule : '') +'</td>' +
                '<td class="text-right">'+ (index_list[i].value !== null ? index_list[i].value : '') +'</td> </tr>';
        }
        $('#indexList').html(index_html);
    }
    
    function updateParamHTML(paramlist) {
        let param_html = '';
        const param_list = paramlist;
        for(let i in param_list) {
            param_list[i].calc_value = param_list[i].calc_value !== null ? param_list[i].calc_value : '';
            let subNode = param_list[i].match_type === 3 ? '(自动绑定)' : (param_list[i].match_type === 4 ? param_list[i].match_key : '');
            param_html += '<tr> <td>'+ param_list[i].name +'</td><td>'+ subNode +'</td>' +
                '<td><input type="text" class="form-control form-control-sm text-right" value="'+ param_list[i].calc_value +'" ' +
                'data-old-value="'+ param_list[i].calc_value +'" data-lib-id="'+ param_list[i].lib_id +'" ' +
                'data-node-id="'+ param_list[i].node_id +'" data-code="'+ param_list[i].code +'"></td> </tr>';
        }
        $('#paramList').html(param_html);
    }

    $('body').on('blur', '#paramList input', function () {
        const self = $(this);
        if(self.val() != self.attr('data-old-value')){
            const postInfo = {
                lib_id: parseInt(self.attr('data-lib-id')),
                node_id: parseInt(self.attr('data-node-id')),
                code: self.attr('data-code'),
                updateType: 'modify',
                value: parseFloat(self.val())
            };
            const postUrl = '/lib/updateParamValue';
            postData(postUrl, postInfo, function (result) {
                self.attr('data-old-value', result.param.calc_value);
                updateIndexHTML(result.indexes);
            },function (result) {
                self.val(self.attr('data-old-value'));
            });
        }
    });

    $('#param-visible').click(function () {
        if (this.innerHTML === '展开') {
            this.innerHTML = '收起';
            $('.sjs-bottom-4').show();
            autoFlashHeight();
        } else {
            this.innerHTML = '展开';
            $('.sjs-bottom-4').hide();
            autoFlashHeight();
        }
    });
});