/**
 * Created by CSL on 2017-09-22.
 * 项目属性之人工系数。
 */
let labourCoeView = {
    datas: [],
    spread: null,
    sheet: null,
    needUpdateDatas: [],

    buildSelect(){
        let me = this;

        function getStdLabourCoeFilesHtml(stdLabourCoeLibs) {
            let result = '<option value="">请选择人工系数标准库</option>';
            if (stdLabourCoeLibs.length <= 0) {
                return result;
            };

            for (let lib of stdLabourCoeLibs){
                result += '<option value='+ lib.id +'>'+ lib.name +'</option>';
            };
            return result;
        };

        let stdLCHtml = getStdLabourCoeFilesHtml(projectObj.project.projectInfo.engineeringInfo.artificial_lib);
        $("#std_labour_coe_files").html(stdLCHtml);
    },
    buildSheet: function (){
        let me = this;
        if (me.spread) {
            me.spread.destroy();
            me.spread = null;
        };
        me.spread = new GC.Spread.Sheets.Workbook($("#labourCoeSpread")[0], { sheetCount: 1 });
        me.spread.options.tabStripVisible = false;
        me.spread.options.showVerticalScrollbar = true;
        me.spread.options.showHorizontalScrollbar = true;
        me.spread.options.allowContextMenu = false;
        let sheetArea = GC.Spread.Sheets.SheetArea;
        let sheet = me.spread.getSheet(0);
        me.sheet = sheet;
        sheet.suspendPaint();
        sheet.suspendEvent();
        sheet.setColumnCount(1, sheetArea.viewport);
        sheet.setColumnWidth(0, 180, sheetArea.viewport);
        sheet.setRowCount(1, sheetArea.colHeader);
        sheet.setRowCount(20, sheetArea.viewport);
        sheet.setRowHeight(0, 40, sheetArea.colHeader);
        sheet.bind(GC.Spread.Sheets.Events.CellChanged, me.onCellChanged);
        sheet.resumeEvent();
        sheet.resumePaint();
        if(projectReadOnly){
            sheetCommonObj.disableSpread(me.spread);
        }
    },
    loadCrossData(datas){          // 交叉表：树结构转换二维表显示，行列转换
        let me = this;
        me.spread.suspendEvent();
        me.datas = datas;
        let libArr = [];
        for (let v of me.datas) {if (!v.ParentID) libArr.push(v);};

        // 行名称
        let row = 0;
        for (let v of me.datas) {
            if (v.ParentID == libArr[0].ID) {
                me.sheet.setText(row, 0, v.name, GC.Spread.Sheets.SheetArea.viewport);
                row++;
            };
        };

        me.sheet.setColumnCount(libArr.length + 1, GC.Spread.Sheets.SheetArea.viewport);    // 还多一列行名称
        me.sheet.setRowCount(row, GC.Spread.Sheets.SheetArea.viewport);
        me.sheet.setText(0, 0, "定额工种", GC.Spread.Sheets.SheetArea.colHeader);
        //项目只读的话，单元格锁定在disableSpread控制
        if(!projectReadOnly){
            me.sheet.options.isProtected = true;
            me.sheet.getRange(-1, 1, -1, libArr.length + 1, GC.Spread.Sheets.SheetArea.viewport).locked(false);
        }

        // 列名称
        for (let c = 0; c <= libArr.length - 1; c++) {
            me.sheet.setText(0, c + 1, libArr[c].name, GC.Spread.Sheets.SheetArea.colHeader);
            me.sheet.setColumnWidth(c + 1, 100, GC.Spread.Sheets.SheetArea.colHeader);
            // 值明细
            let libID = libArr[c].ID;
            for (let r = 0; r < row; r++) {
                let rowName = me.sheet.getText(r, 0);
                for (let v of me.datas) {
                    if ((v.ParentID == libID) && (v.name == rowName)) {
                        let cell = me.sheet.getCell(r, c+ 1);
                        cell.value(v.coe);
                        cell.tag(v.ID);
                        cell.hAlign(GC.Spread.Sheets.HorizontalAlign.center);
                        break;
                    };
                };
            };
        };
        me.spread.resumeEvent();
    },
    showData(){
        let me = this;
        let datas = projectObj.project.labourCoe.datas !== null ? projectObj.project.labourCoe.datas.coes : [];
        me.buildSelect();
        me.buildSheet();
        me.loadCrossData(datas);
    },
    addNeedUpdateData: function (data) {
        let me = this;
        let isExist = false;
        for (let el of me.needUpdateDatas){
             if (el.ID == data.ID){
                 isExist = true;
                 el.coe = data.coe;
                 return;
             };
        };
        if (!isExist){
            me.needUpdateDatas.push(data);
        }
    },
    needSave: function (){
        let me = this;
        return me.needUpdateDatas.length > 0;
    },
    clearSave: function(){
        let me = this;
        me.needUpdateDatas.splice(0, me.needUpdateDatas.length);
    },
    refresh(){
        let me = this;
        me.needUpdateDatas.splice(0, me.needUpdateDatas.length);
        $("#std_labour_coe_files").val('');
        calcProgramManage.buildSheet();
    },
    onCellChanged: function(sender, args){
        var me = labourCoeView;
        if (args.propertyName !== "value"){return;};
        let cell = me.sheet.getCell(args.row, args.col);
        let value = args.newValue;
        let oValue = args.oldValue;
        if (number_util.isNumber(value)) {
            value = value.toDecimal(2);
            me.sheet.suspendEvent();
            cell.value(value);
            me.sheet.resumeEvent();
            me.addNeedUpdateData({ID: cell.tag(), coe: value});
        } else {
            me.sheet.suspendEvent();
            cell.value(oValue);  // 屏蔽事件，否则这句会导致死循环
            me.sheet.resumeEvent();
            hintBox.infoBox('系统提示', '当前输入的数据类型不正确，请重新输入。', 1);
        }
    }
};

$(document).ready(function(){
    $('#poj-set').on('shown.bs.modal', function (e) {
        labourCoeView.showData();
    });

    $('#poj-set').on('hidden.bs.modal', function (e) {
        if(labourCoeView.spread){
            labourCoeView.spread.destroy();
            labourCoeView.spread = null;
        }
        labourCoeView.datas = [];
        labourCoeView.clearSave();
    });

    $("#tab_poj-settings-6").on('shown.bs.tab', function (e) {
        labourCoeView.spread.refresh();
    });

    $("#std_labour_coe_files").change(function() {
        // 取标准库数据过来显示。
        let libID = $(this).val();
        if (libID == ''){
            labourCoeView.loadCrossData([]);
            return false;
        };

        CommonAjax.post('/labourCoe/getStdLabourCoe', {"ID": libID}, function (data) {
            labourCoeView.buildSheet();
            labourCoeView.loadCrossData(data.coes);
            labourCoeView.clearSave();
            labourCoeView.needUpdateDatas.push(...data.coes);
        });
    });
});