/**
 * Standard Bills Lib
 * Created by Mai on 2017/5/16.
 */

/**
 * showObj = {bills, jobAndFeatureTab, job, feature, remarkTab, remark}
 */
/*var stdBillsLibObj = {
    createNew: function (sessionID, showObj) {
        var tools = {
            sessionID: sessionID,
            billsSpread: null,
            billsJobSpread: null,
            billsFeatureSpread: null,
            checkSpread: function (spread, spreadObj) {
                if (!spread) {
                    spread = SheetDataHelper.createNewSpread(spreadObj);
                }
            }
        };
        var libObj = function () {
            this.billsTree = idTree.createNew({id: 'ID', pid: 'ParentID', nid: 'NextSiblingID', rootId: -1, autoUpdate: true});
            this.spread = tools.billsSpread;
        };

        libObj.prototype.LoadStdBills = function () {
            tools.checkSpread(tools.billsSpread, showObj.bills);

        };

        return new libObj(sessionID);
    },
    settings: {
        stdBillsTreeSetting: {
            "treeCol": 0,
            "emptyRows":0,
            "headRows":1,
            "headRowHeight":[
                35
            ],
            "cols":[{
                "width":100,
                "readOnly":null,
                "head":{
                    "titleNames":["项目编码"],
                    "spanCols":[1],
                    "spanRows":[1],
                    "vAlign":[1],
                    "hAlign":[1],
                    "font":["12px Arial"]
                },
                "data":{
                    "field":"code",
                    "vAlign":0,
                    "hAlign":3,
                    "font":"12px Arial"
                }
            }, {
                "width":120,
                "readOnly":null,
                "head":{
                    "titleNames":["项目名称"],
                    "spanCols":[1],
                    "spanRows":[1],
                    "vAlign":[1],
                    "hAlign":[1],
                    "font":["12px Arial"]
                },
                "data":{
                    "field":"name",
                    "vAlign":0,
                    "hAlign":3,
                    "font":"12px Arial"
                }
            }, {
                "width":40,
                "readOnly":null,
                "head":{
                    "titleNames":["计量单位"],
                    "spanCols":[1],
                    "spanRows":[1],
                    "vAlign":[1],
                    "hAlign":[1],
                    "font":["12px Arial"],
                    "wordWrap": true
                },
                "data":{
                    "field":"unti",
                    "vAlign":0,
                    "hAlign":3,
                    "font":"12px Arial"
                }
            }, {
                "width":100,
                "readOnly": true,
                "head":{
                    "titleNames":["工程量计算规则"],
                    "spanCols":[1],
                    "spanRows":[1],
                    "vAlign":[1],
                    "hAlign":[1],
                    "font":["12px Arial"]
                },
                "data":{
                    "field":"ruleText",
                    "vAlign":0,
                    "hAlign":3,
                    "font":"12px Arial"
                }
            }]
        },
        stdBillsJobSetting: {
            "emptyRows":0,
            "headRows":1,
            "headRowHeight":[
                20
            ],
            "cols":[{
                "width":200,
                "readOnly":true,
                "head":{
                    "titleNames":["工程内容"],
                    "spanCols":[1],
                    "spanRows":[1],
                    "vAlign":[1],
                    "hAlign":[1],
                    "font":["12px Arial"]
                },
                "data":{
                    "field":"content",
                    "vAlign":0,
                    "hAlign":3,
                    "font":"12px Arial"
                }
            }]
        },
        stdBillsFeatureSetting: {
            "emptyRows":0,
            "headRows":1,
            "headRowHeight":[
                20
            ],
            "cols":[{
                "width":200,
                "readOnly":null,
                "head":{
                    "titleNames":["项目特征"],
                    "spanCols":[1],
                    "spanRows":[1],
                    "vAlign":[1],
                    "hAlign":[1],
                    "font":["12px Arial"]
                },
                "data":{
                    "field":"content",
                    "vAlign":0,
                    "hAlign":3,
                    "font":"12px Arial"
                }
            }]
        }
    }
};*/

CommonAjax.post('/stdBillsEditor/getStdBillsLib', {userId: userID}, function (datas) {
    var i, select = $('#stdBillsLibSelect');
    select.empty();
    datas.forEach(function (data) {
        var option = $('<option>').val(data.billsLibId).text(data.billsLibName);
        select.append(option);
    });
    if (select.children.length !== 0) {
        LoadStdBills(select.val());
    }
});

var LoadStdBills = (function () {
    var stdBillsSpread = SheetDataHelper.createNewSpread($('#stdBillsSpread')[0]);
    var stdBillsJobSpread = null, stdBillsFeatureSpread = null;
    var stdBillsTreeSetting = {
        "treeCol": 0,
        "emptyRows":0,
        "headRows":1,
        "headRowHeight":[
            40
        ],
        "defaultRowHeight": 21,
        "cols":[{
            "width":150,
            "readOnly":null,
            "head":{
                "titleNames":["项目编码"],
                "spanCols":[1],
                "spanRows":[1],
                "vAlign":[1],
                "hAlign":[1],
                "font":["Arial"]
            },
            "data":{
                "field":"code",
                "vAlign":1,
                "hAlign":0,
                "font":"Arial"
            }
        }, {
            "width":120,
            "readOnly":null,
            "head":{
                "titleNames":["项目名称"],
                "spanCols":[1],
                "spanRows":[1],
                "vAlign":[1],
                "hAlign":[1],
                "font":["Arial"]
            },
            "data":{
                "field":"name",
                "vAlign":1,
                "hAlign":0,
                "font":"Arial"
            }
        }, {
            "width":40,
            "readOnly":null,
            "head":{
                "titleNames":["计量单位"],
                "spanCols":[1],
                "spanRows":[1],
                "vAlign":[1],
                "hAlign":[1],
                "font":["Arial"],
                "wordWrap": true
            },
            "data":{
                "field":"unit",
                "vAlign":1,
                "hAlign":1,
                "font":"Arial"
            }
        }, {
            "width":100,
            "readOnly": true,
            "head":{
                "titleNames":["工程量计算规则"],
                "spanCols":[1],
                "spanRows":[1],
                "vAlign":[1],
                "hAlign":[1],
                "font":["Arial"]
            },
            "data":{
                "field":"ruleText",
                "vAlign":1,
                "hAlign":0,
                "font":"Arial"
            }
        }]
    };
    var stdBillsData, stdBillsJobData, stdBillsFeatureData;
    var stdBillsTree = idTree.createNew({id: 'ID', pid: 'ParentID', nid: 'NextSiblingID', rootId: -1, autoUpdate: true});
    var stdBillsTreeController = TREE_SHEET_CONTROLLER.createNew(stdBillsTree, stdBillsSpread.getActiveSheet(), stdBillsTreeSetting);

    var findData = function (value, field, Array) {
        var i = 0;
        for (i = 0; i < Array.length - 1; i++) {
            if (value === Array[i][field]) {
                return Array[i];
            }
        }
        return null;
    };
    var getBillsJobs = function (node) {
        var jobs = [], i, jobData = null;
        if (stdBillsJobData && node && node.data.jobs) {
            for (i = 0; i < node.data.jobs.length - 1; i++) {
                jobData = findData(node.data.jobs[i], 'id', stdBillsJobData);
                if (jobData) {
                    jobs.push(jobData);
                }
            }
        }
        return jobs;
    };
    var getBillsFeatures = function (node) {
        var features = [], i, featureData = null;
        if (stdBillsFeatureData && node && node.data.items) {
            for (i = 0; i < node.data.items.length - 1; i++) {
                featureData = findData(node.data.items[i], 'id', stdBillsFeatureData);
                if (featureData) {
                    features.push(featureData);
                }
            }
        }
        return features;
    };
    var showJobs = function (jobs) {
        var jobsSetting = {
            "emptyRows":0,
            "headRows":1,
            "headRowHeight":[25],
            "defaultRowHeight": 21,
            "cols":[{
                "width":200,
                "readOnly":true,
                "head":{
                    "titleNames":["工程内容"],
                    "spanCols":[1],
                    "spanRows":[1],
                    "vAlign":[1],
                    "hAlign":[1],
                    "font":["Arial"]
                },
                "data":{
                    "field":"content",
                    "vAlign":0,
                    "hAlign":3,
                    "font":"Arial"
                }
            }]
        };
        SheetDataHelper.loadSheetHeader(jobsSetting, stdBillsJobSpread.getActiveSheet());
        SheetDataHelper.loadSheetData(jobsSetting, stdBillsJobSpread.getActiveSheet(), jobs);
    };
    var showFeatures = function (features) {
        var featuresSetting = {
            "emptyRows":0,
            "headRows":1,
            "headRowHeight":[25],
            "defaultRowHeight": 21,
            "cols":[{
                "width":200,
                "readOnly":true,
                "head":{
                    "titleNames":["项目特征"],
                    "spanCols":[1],
                    "spanRows":[1],
                    "vAlign":[1],
                    "hAlign":[1],
                    "font":["Arial"]
                },
                "data":{
                    "field":"content",
                    "vAlign":0,
                    "hAlign":3,
                    "font":"Arial"
                }
            }]
        };
        SheetDataHelper.loadSheetHeader(featuresSetting, stdBillsFeatureSpread.getActiveSheet());
        SheetDataHelper.loadSheetData(featuresSetting, stdBillsFeatureSpread.getActiveSheet(), features);
    }
    var showJobsAndFeatures = function (node) {
        $('#stdBillsJobTab').show();
        $('#stdBillsRemarkTab').hide();
        if (!stdBillsJobSpread) {
            stdBillsJobSpread = SheetDataHelper.createNewSpread($('#stdBillsJobs')[0]);
        }
        showJobs(getBillsJobs(node));
        if (!stdBillsFeatureSpread) {
            stdBillsFeatureSpread = SheetDataHelper.createNewSpread($('#stdBillsFeatures')[0]);
        }
        showFeatures(getBillsFeatures(node));
    };
    var showBillsRemark = function (node) {
        $('#stdBillsJobTab').hide();
        $('#stdBillsRemarkTab').show();
        $('#stdBillsRemark').text(node && node.data.recharge ? node.data.recharge : '');
    };
    var showBillsRela = function (node) {
        if (node && node.children.length === 0) {
            showJobsAndFeatures(node);
        } else {
            showBillsRemark(node);
        }
    }

    var LoadData = function (stdBillsLibID) {
        CommonAjax.post('/stdBillsEditor/getJobContent', {userId: userID, billsLibId: stdBillsLibID}, function (datas) {
            stdBillsJobData = datas;
        }, function () {
            stdBillsJobData = [];
        });
        CommonAjax.post('/stdBillsEditor/getItemCharacter', {userId: userID, billsLibId: stdBillsLibID}, function (datas) {
            stdBillsFeatureData = datas;
        }, function () {
            stdBillsFeatureData = [];
        });
        CommonAjax.post('/stdBillsEditor/getBills', {userId: userID, billsLibId: stdBillsLibID}, function (datas) {
            stdBills = datas;
            stdBillsTree.loadDatas(stdBills);
            stdBillsTreeController.showTreeData();
            showBillsRela(stdBillsTree.firstNode());

            stdBillsTreeController.bind(TREE_SHEET_CONTROLLER.eventName.treeSelectedChanged, showBillsRela);
        });
    };

    return LoadData;
})();

$('#stdBillsLibSelect').change(function () {
    var select = $(this);
    if (this.children.length !== 0) {
        LoadStdBills(select.val());
    }
});
