/**
 * Created by CSL on 2017-08-03.
 * 计算程序管理。
 */
let calcProgramManage = {
    datas: [],
    mainSpread: null,
    detailSpread: null,
    mainSheet: null,
    detailSheet: null,
    mainSetting: {
        header:[
            // {headerName:"ID",headerWidth:80,dataCode:"ID", hAlign: "center"},
            {headerName:"名称",headerWidth:200,dataCode:"name", dataType: "String"}
        ],
        view:{
            comboBox:[],
            lockColumns:[0,1],
            colHeaderHeight: CP_Col_Width.colHeader,
            rowHeaderWidth: CP_Col_Width.rowHeader
        }
    },
    detailSetting: {
        header:[
            // {headerName:"ID",headerWidth:80,dataCode:"ID", hAlign: "center"},
            {headerName:"费用代号",headerWidth:CP_Col_Width.code, dataCode:"code", dataType: "String"},
            {headerName:"费用名称",headerWidth:CP_Col_Width.name, dataCode:"name", dataType: "String"},
            {headerName:"计算基数",headerWidth:CP_Col_Width.dispExprUser, dataCode:"dispExprUser", dataType: "String",cellType:'cusButton',callback:'calcBase',disable:'calcBase'},
            {headerName:"费率",headerWidth:CP_Col_Width.feeRate, dataCode:"feeRate", dataType: "Number",hAlign: "right",decimalField:"feeRate"},
            {headerName:"费用类别",headerWidth:CP_Col_Width.displayFieldName, dataCode:"displayFieldName", dataType: "String", hAlign: "center"},
            {headerName:"基数说明",headerWidth:CP_Col_Width.statement, dataCode:"statement", dataType: "String"},
            {headerName:"备注",headerWidth:CP_Col_Width.memo, dataCode:"memo", dataType: "String"}
        ],
        view:{
            comboBox:[],
            lockColumns:[2,5],
            colHeaderHeight: CP_Col_Width.colHeader,
            rowHeaderWidth: CP_Col_Width.rowHeader
        },
        //callback 和disable都是和 cusButton配套使用的
        callback:{
            calcBase:function (hitinfo) {
                calcBaseView.onCalcBaseButtonClick(hitinfo,'ration');
            }
        },
        disable:{
            calcBase:function (row,col) {
                return projectReadOnly || !calcBaseView.ifEdit('ration', row)
            }
        }
    },

    buildSheet: function (){
        let me = this;
        me.datas = projectObj.project.calcProgram.datas.templates;
        if (me.mainSpread) {
            me.mainSpread.destroy();
            me.mainSpread = null;
        };
        if (me.detailSpread) {
            me.detailSpread.destroy();
            me.detailSpread = null;
        };
        me.mainSpread = sheetCommonObj.buildSheet($('#mainSpread')[0], me.mainSetting, me.datas.length);
        me.mainSheet = me.mainSpread.getSheet(0);
        sheetCommonObj.spreadDefaultStyle(me.mainSpread);
        me.detailSpread = sheetCommonObj.buildSheet($('#detailSpread')[0], me.detailSetting, me.datas[0].calcItems.length);
        me.detailSheet = me.detailSpread.getSheet(0);
        sheetCommonObj.spreadDefaultStyle(me.detailSpread);
        let arr = projectObj.project.calcProgram.compiledFeeTypeNames.slice();
        // arr.delete('暂估费');
        let fieldName = new GC.Spread.Sheets.CellTypes.ComboBox();
        fieldName.items(arr);
        me.detailSheet.getRange(-1, 4, -1, 1).cellType(fieldName);

        me.mainSheet.bind(GC.Spread.Sheets.Events.EnterCell, me.onMainEnterCell);
        me.detailSheet.bind(GC.Spread.Sheets.Events.ValueChanged, me.onDetailValueChanged);
        me.detailSheet.bind(GC.Spread.Sheets.Events.RangeChanged, me.onRangeChanged);
        me.detailSheet.bind(GC.Spread.Sheets.Events.EnterCell, me.onDetailEnterCell);
        me.detailSheet.bind(GC.Spread.Sheets.Events.ClipboardPasting, me.onClipboardPasting);
        sheetCommonObj.showData(me.mainSheet, me.mainSetting, me.datas);
        me.mainSheet.setRowCount(me.datas.length);

        me.detailSheet.name('calc_detail');
        feeRateObject.setFeeRateCellCol(me.detailSheet, _.findIndex(me.detailSetting.header,{'dataCode':'feeRate'}));
        sheetCommonObj.showData(me.detailSheet, me.detailSetting, me.datas[0].calcItems);
        me.getfeeRateColor(me.datas[0].calcItems);
        customRowHeader(me.detailSheet, me.datas[0].calcItems.length);
        if(!projectReadOnly){
            me.loadMainContextMenu();
            me.loadDetailContextMenu();
        }
        else {
            if(me.mainSetting.view.lockColumns){
                me.mainSetting.view.lockColumns = null;
            }
            if(me.detailSetting.view.lockColumns){
                me.detailSetting.view.lockColumns = null;
            }
            sheetCommonObj.disableSpread(me.mainSpread);
            sheetCommonObj.disableSpread(me.detailSpread);
        }
    },
    getStdCalcProgramFiles: function(){
        function getStdCPFilesHtml(stdCPLibs) {
            let result = '<option value="">请选择计算程序标准库</option>';
            if (stdCPLibs.length <= 0) {
                return result;
            };

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

        let stdCPHtml = getStdCPFilesHtml(projectObj.project.projectInfo.engineeringInfo.program_lib);
        $("#calcProgramFileSelect").html(stdCPHtml);
    },
    onMainEnterCell: function(sender, args) {
        var me = calcProgramManage;
        var row = args.sheet.getActiveRowIndex();

        me.detailSpread.suspendPaint();
        var dData = me.datas[row].calcItems;
        me.detailSheet.setRowCount(dData.length, GC.Spread.Sheets.SheetArea.viewport);
        sheetCommonObj.showData(me.detailSheet, me.detailSetting, dData);
        me.getfeeRateColor(dData);
        customRowHeader(me.detailSheet, dData.length);
        me.detailSpread.resumePaint();
    },
    onRangeChanged:function (sender,args) {
        let me = calcProgramManage;
        let editInfo= me.getSelectionInfo();
        let dataCode = me.detailSetting.header[args.col].dataCode;
        // if (args.action == GC.Spread.Sheets.RangeChangedAction.clear&&args.changedCells.length == 1) {//清除操作
        if (args.action == GC.Spread.Sheets.RangeChangedAction.clear) {//清除操作
            if (dataCode == 'feeRate') {
                $.bootstrapLoading.start();
                projectObj.project.FeeRate.updateFeeRateFromCalc(null, editInfo);
            }
            else if (dataCode == 'displayFieldName'){
                $.bootstrapLoading.start();
                for (let cell of args.changedCells){
                    let curCalcItem = editInfo.template.calcItems[cell.row];
                    curCalcItem.fieldName = '';
                    curCalcItem.displayFieldName = '';
                };

                let data = {
                    'projectID': projectObj.project.ID(),
                    'ID': editInfo.template.ID,
                    'calcItems': editInfo.template.calcItems
                };
                calcProgramManage.updateTemplate(data, function (rst) {
                    if (rst){
                        calcProgramManage.refreshDetailSheet();
                        $.bootstrapLoading.end();
                    }
                });
            }
        }
    },
    onDetailValueChanged: function(sender, args) {
        $.bootstrapLoading.start();
        let me = calcProgramManage;
        let editInfo= me.getSelectionInfo();
        let curCalcItem = editInfo.calcItem;
        let dataCode = me.detailSetting.header[args.col].dataCode;

        if (dataCode == 'feeRate') {
            projectObj.project.FeeRate.updateFeeRateFromCalc(args.newValue, editInfo);
            $.bootstrapLoading.end();
            return;
        }

        let template = me.getSelectionInfo().template;
        if (dataCode == 'displayFieldName') {
            if (curCalcItem.displayFieldName == args.newValue) {
                $.bootstrapLoading.end();
                return;
            }

            if (analyzer.fieldNameIsUsed(template, args.newValue)){
                let sheet = me.detailSpread.getActiveSheet();
                sheet.suspendEvent();
                sheet.setValue(args.row, args.col, args.oldValue);
                sheet.resumeEvent();
                $.bootstrapLoading.end();
                let s = hintBox.font(template.fieldNameTempUsed + 1);
                hintBox.infoBox('系统提示', `“${args.newValue}” 已被第 ${s} 行使用，不允许重复选择！`, 1);
                delete template.fieldNameTempUsed;
                return;
            }

            curCalcItem.fieldName = projectObj.project.calcProgram.compiledFeeTypeMaps[args.newValue];
        }
        else if (dataCode == 'dispExprUser'){  // 除非直接改单元格，弹窗不会走这里
            hintBox.infoBox('系统提示', '用户直接在sheet中修改了dispExprUser值，触发onDetailEditEnded事件。', 1);
        };

        curCalcItem[dataCode] = args.newValue;

        if (dataCode == 'name'){
            analyzer.refreshUsedCalcItemsStatement(template, curCalcItem);
            let data = {
                'projectID': projectObj.project.ID(),
                'ID': template.ID,
                'calcItems': template.calcItems
            };
            calcProgramManage.updateTemplate(data, function (rst) {
                if (rst){
                    calcProgramManage.refreshDetailSheet();
                    $.bootstrapLoading.end();
                }
            });
            if (activeSubSheetIsCalcProgram())
                calcProgramObj.refreshCalcProgram(projectObj.project.mainTree.selected, 1);
        }
        else{
            let data = {
                'projectID': projectObj.project.ID(),
                'templatesID': template.ID,
                'calcItem': curCalcItem
            };
            me.saveCalcItem(data, function (rst) {
                if (rst){
                    let relationNodes = calcTools.getNodesByProgramID(template.ID);
                    projectObj.project.calcProgram.calcNodesAndSave(relationNodes);
                    $.bootstrapLoading.end();
                }
            });
        }
    },
    onDetailEnterCell: function (sender, args) {
        // if (args.col == 2)    // 加这句，切换单元格后，原单元格的三点图片不会消失。
            calcProgramManage.detailSheet.repaint();   // 这句是为了触发sheetCommonObj → getCusButtonCellType → CusButtonCellType → paint方法。
        // for test.
        // let t = calcProgramManage.getSelectionInfo().template;
        // let c = calcProgramManage.getSelectionInfo().calcItem;
        // let lc = analyzer.calcItemLabourCoe(c);
        // c.dispExpr = analyzer.getDispExpr(c.expression, t);
        // c.dispExprUser = analyzer.getDispExprUser(c.dispExpr, lc);
        // c.compiledExpr = analyzer.getCompiledExpr(c.expression, lc);
        // let e = `ID:${c.ID}　${c.expression}　${c.dispExpr}　${c.dispExprUser}　${c.compiledExpr}　${c.custom}`;
        // projectObj.testDisplay('', e);
    },
    onClipboardPasting: function (sender, args) {
         args.cancel = true;
    },
    loadMainContextMenu: function () {
        $.contextMenu({
            selector: '#mainSpread',
            build: function ($trigger, e) {
                SheetDataHelper.safeRightClickSelection($trigger, e, calcProgramManage.mainSpread);
            },
            items: {
                "copyTemplate": {
                    name: "另存为",
                    icon: 'copy',
                    callback: function (key, opt) {
                        $.bootstrapLoading.start();
                        let template = calcProgramManage.getSelectionInfo().template;
                        let idx = calcProgramManage.mainSpread.getActiveSheet().getActiveRowIndex();

                        let newTemplate = {};
                        newTemplate.ID = analyzer.templateMaxID() + 1;
                        newTemplate.name = analyzer.templateNewName(template.name);
                        newTemplate.custom = true;
                        newTemplate.calcItems = [];
                        $.extend(true, newTemplate.calcItems, template.calcItems);
                        // 清理掉费率ID关联
                        for (let ci of newTemplate.calcItems){
                            if (ci.feeRateID || ci.feeRateID == null)
                                delete ci.feeRateID;
                        };

                        let data = {
                            'projectID': projectObj.project.ID(),
                            'ID': newTemplate.ID,
                            'name': newTemplate.name,
                            'custom': newTemplate.custom,
                            'calcItems': newTemplate.calcItems
                        };

                        calcProgramManage.addTemplate(data, function (rst) {
                            if (rst){
                                let ts = projectObj.project.calcProgram.templates;
                                ts.push(newTemplate);
                                projectObj.project.calcProgram.compileTemplateMaps();
                                projectObj.project.calcProgram.compileTemplate(newTemplate);
                                calcProgramManage.buildSheet();
                                calcProgramManage.mainSheet.setSelection(ts.length - 1, 0, 1, 1);
                                calcProgramManage.mainSheet.showRow(ts.length - 1, GC.Spread.Sheets.VerticalPosition.center);
                                calcProgramManage.refreshDetailSheet();
                                $.bootstrapLoading.end();
                            }
                        });
                    }
                },
                "reNameTemplate": {
                    name: "重命名...",
                    icon: 'edit',
                    disabled: function () {
                        let custom = calcProgramManage.getSelectionInfo().template.custom;
                        let canReName = custom ? custom : false;
                        return !canReName;
                    },
                    callback: function (key, opt) {
                        $.bootstrapLoading.start();
                        let template = calcProgramManage.getSelectionInfo().template;
                        let idx = calcProgramManage.mainSpread.getActiveSheet().getActiveRowIndex();

                        let newName = '';
                        hintBox.valueBox('重命名', template.name, function () {
                            newName = hintBox.value;

                            if (!newName){
                                hintBox.error(`名称不能为空！`);
                                return false;
                            };

                            if (newName == template.name) {
                                $.bootstrapLoading.end();
                                return;
                            }

                            if (analyzer.templateNameIsExist(newName)){
                                hintBox.error(`“${newName}” 已存在，请重新输入！`);
                                return false;
                            };

                            template.name = newName;
                            let data = {
                                'projectID': projectObj.project.ID(),
                                'ID': template.ID,
                                'name': template.name
                            };
                            calcProgramManage.updateTemplate(data, function (rst) {
                                if (rst){
                                    projectObj.project.calcProgram.compileTemplateMaps();
                                    projectObj.mainController.refreshTreeNode(calcTools.getNodesByProgramID(template.ID));
                                    sheetCommonObj.showData(calcProgramManage.mainSpread.getSheet(0), calcProgramManage.mainSetting, calcProgramManage.datas);
                                    $.bootstrapLoading.end();
                                }
                            });
                        });
                    }
                },
                "spr1": '--------',
                "deleteTemplate": {
                    name: '删除',
                    icon: 'fa-remove',
                    disabled: function () {
                        let custom = calcProgramManage.getSelectionInfo().template.custom;
                        let canDelete = custom ? custom : false;
                        return !canDelete;
                    },
                    callback: function () {
                        $.bootstrapLoading.start();
                        let template = calcProgramManage.getSelectionInfo().template;
                        if (analyzer.templateIsUsed(template.ID)) {
                            $.bootstrapLoading.end();
                            hintBox.infoBox('系统提示', `计算模板“${template.name}”已被使用，不允许删除！`, 1);
                            return;
                        };
                        hintBox.infoBox('系统提示', `确定要删除计算模板“${template.name}”吗？`, 2, cbYes);
                        function cbYes() {
                            let data = {
                                'projectID': projectObj.project.ID(),
                                'ID': template.ID
                            };
                            calcProgramManage.deleteTemplate(data, function (rst) {
                                if (rst){
                                    let idx = calcProgramManage.mainSheet.getActiveRowIndex();
                                    projectObj.project.calcProgram.templates.splice(idx, 1);
                                    projectObj.project.calcProgram.compileTemplateMaps();
                                    calcProgramManage.buildSheet();
                                    calcProgramManage.mainSheet.setSelection(idx - 1, 0, 1, 1);
                                    calcProgramManage.mainSheet.showRow(idx - 1, GC.Spread.Sheets.VerticalPosition.center);
                                    calcProgramManage.refreshDetailSheet();
                                    $.bootstrapLoading.end();
                                }
                            });
                        };
                    }
                }
            }
        });
    },
    loadDetailContextMenu: function () {
        $.contextMenu({
            selector: '#detailSpread',
            build: function ($triggerElement, event) {
                SheetDataHelper.safeRightClickSelection($triggerElement, event, calcProgramManage.detailSpread);
            },
            items: {
                "newCalcItem": {
                    name: "插入行",
                    icon: 'fa-sign-in',
                    callback: function () {
                        $.bootstrapLoading.start();
                        let template = calcProgramManage.getSelectionInfo().template;
                        let idx = calcProgramManage.detailSpread.getActiveSheet().getActiveRowIndex();

                        let newItem = {};
                        newItem.ID = analyzer.calcItemMaxID(template) + 1;
                        // newItem.name = '新建';
                        newItem.memo = '自定义';
                        newItem.custom = true;
                        newItem.expression = '0';                           // 这里必须得有值，否则程序内部公式解析会出现诸多问题
                        template.calcItems.splice(idx + 1, 0, newItem);
                        let data = {
                            'projectID': projectObj.project.ID(),
                            'ID': template.ID,
                            'calcItems': template.calcItems
                        };
                        calcProgramManage.updateTemplate(data, function (rst) {
                            if (rst){
                                projectObj.project.calcProgram.compileTemplate(template);
                                calcProgramManage.refreshDetailSheet();
                                calcProgramManage.detailSpread.getActiveSheet().setSelection(idx + 1, 0, 1, 1);
                                let relationNodes = calcTools.getNodesByProgramID(template.ID);
                                projectObj.project.calcProgram.calcNodesAndSave(relationNodes);
                            }
                        });
                        $.bootstrapLoading.end();
                    }
                },
                "spr1": '--------',
                "deleteCalcItem": {
                    name: '删除行',
                    icon: 'fa-remove',
                    callback: function () {
                        $.bootstrapLoading.start();
                        let template = calcProgramManage.getSelectionInfo().template;
                        let idx = calcProgramManage.detailSpread.getActiveSheet().getActiveRowIndex();
                        let item = template.calcItems[idx];

                        if (item.fieldName == 'common'){
                            $.bootstrapLoading.end();
                            hintBox.infoBox('系统提示', `费用类别为“工程造价”的行不允许删除！`, 1);
                            return;
                        };

                        if (analyzer.calcItemIsUsed(template, item)){
                            $.bootstrapLoading.end();
                            let s = hintBox.font(item.tempUsed + 1);
                            hintBox.infoBox('系统提示', `第 ${idx + 1} 行“${item.name}”已被第 ${s} 行引用，不允许删除！`, 1);
                            delete item.tempUsed;
                            return;
                        };

                        hintBox.infoBox('系统提示', `确定要删除计算规则“${item.name}”吗？`, 2, cbYes);
                        function cbYes() {
                            template.calcItems.splice(idx, 1);
                            let data = {
                                'projectID': projectObj.project.ID(),
                                'ID': template.ID,
                                'calcItems': template.calcItems
                            };
                            calcProgramManage.updateTemplate(data, function (rst) {
                                if (rst){
                                    projectObj.project.calcProgram.compileTemplate(template);
                                    calcProgramManage.refreshDetailSheet();
                                    let relationNodes = calcTools.getNodesByProgramID(template.ID);
                                    projectObj.project.calcProgram.calcNodesAndSave(relationNodes);
                                    $.bootstrapLoading.end();
                                }
                            });
                        };
                    }
                }
            }
        });

    },
    saveCalcItem: function (data, callback) {//data
        CommonAjax.post('/calcProgram/saveCalcItem', data,
            function (result) {
                if(callback){
                    callback(result);
                }
            }
        );
    },
    updateTemplate: function (data, callback) {
        CommonAjax.post('/calcProgram/updateTemplate', data,
            function (result) {
                if(callback){
                    callback(result);
                }
            }
        );
    },
    addTemplate: function (data, callback) {
        CommonAjax.post('/calcProgram/addTemplate', data,
            function (result) {
                if(callback){
                    callback(result);
                }
            }
        );
    },
    deleteTemplate: function (data, callback) {
        $.bootstrapLoading.start();
        CommonAjax.post('/calcProgram/deleteTemplate', data,
            function (result) {
                if(callback){
                    callback(result);
                }
                $.bootstrapLoading.end();
            }
        );
    },
    getSelectionInfo:function () {
        var templateIndex = this.mainSpread.getActiveSheet().getActiveRowIndex();
        var dIndex = this.detailSpread.getActiveSheet().getActiveRowIndex();
        var info = {
            template:this.datas[templateIndex],
            calcItem:this.datas[templateIndex].calcItems[dIndex]
        }
        return info;
    },
    refreshDetailSheet:function () {
        var me = this;
        if(me.mainSpread && me.detailSpread){
            let mainRowIdx = me.mainSpread.getActiveSheet().getActiveRowIndex();
            let calcItems = me.datas[mainRowIdx].calcItems;
            let detailSheet = me.detailSpread.getActiveSheet();
            detailSheet.setRowCount(calcItems.length);
            sheetCommonObj.showData(detailSheet, me.detailSetting, calcItems);
            me.getfeeRateColor(calcItems);
            customRowHeader(detailSheet, calcItems.length);
        }
    },
    getfeeRateColor: function (calcItems) {    // 有费率ID关联的变个色
        var me = this;
        for (let i = 0; i < calcItems.length; i++) {
            if (calcItems[i].feeRateID != undefined && calcItems[i].feeRateID != null)
                me.detailSheet.getCell(i, 3).foreColor("#0aa8ea")
            else
                me.detailSheet.getCell(i, 3).foreColor("black");
        }
    }
};

$(document).ready(function(){
    $('#tab_calc_program_manage').on('shown.bs.tab', function (e) {
        sessionStorage.setItem('mainTab', '#tab_calc_program_manage');
        $(e.relatedTarget.hash).removeClass('active');
        if (!calcProgramManage.mainSpread)
            calcProgramManage.buildSheet()
        else
            calcProgramManage.mainSpread.refresh();

        let count = calcProgramManage.datas[calcProgramManage.mainSheet.getActiveRowIndex()].calcItems.length;
        calcProgramManage.detailSheet.setRowCount(count, GC.Spread.Sheets.SheetArea.viewport);
    });

    $("#calcProgramFileSelect").change(function() {
        // 取标准库数据过来显示。
        let libID = $(this).val();
        if (libID == ''){
            calcProgramManage.mainSpread.getSheet(0).setRowCount(0);
            calcProgramManage.detailSpread.getSheet(0).setRowCount(0);
            return false;
        };

        $.bootstrapLoading.start();
        libID = parseFloat(libID);
        let projectID = projectObj.project.ID();
        CommonAjax.post('/calcProgram/updateTemplateFile', {"projectID": projectID, "libID": libID}, function (data) {
            projectObj.project.calcProgram.datas.templates = data;
            projectObj.project.calcProgram.compileAllTemps();
            projectObj.project.calcProgram.calcAllNodesAndSave();
            calcProgramManage.buildSheet();
            $.bootstrapLoading.end();
        });
    });
});


