/**
 * Created by CSL on 2017-03-23.
 */
var region = '重庆';
var feeRateFileID = 5;
var datas = [];
var spreadView;

$(document).ready(function () {
    $("#inlineFormCustomSelect").change(function () {
        var libID = $("#inlineFormCustomSelect").val();
        loadLibFeeRates(libID);
    });

    $("#projectFeeFile").click(function () {
        loadProjectFeeRates(feeRateFileID);
    });

    $("#logout").click(function () {
        location.href = '/logout';
    });
});

function loadProjectFeeRates(fileID) {
    $.ajax({
        type: "POST",
        url: '/feeRates/getProjectFeeRates',
        data: {"fileID": fileID},
        success: function (result) {
            if (result.data) {
                datas = result.data[0].rates;
                datas=rate_data;
                createSpreadView(true);
            }
        },
        error: function (jqXHR, textStatus, errorThrown) {
            ajaxErrorInfo(jqXHR, textStatus, errorThrown);
        }
    });
}

function loadStdFeeRateLibNames(region) {
    $('#inlineFormCustomSelect').empty();

    $.ajax({
        type: "POST",
        url: '/feeRates/getLibNames',
        data: {"region": region},
        success: function (result) {
            if (result.data) {
                for (var i = 0; i < result.data.length; i++) {
                    $("#inlineFormCustomSelect").append("<option value=" + result.data[i].libID + '>' +
                        result.data[i].libName + "</option>");
                }
                $("#inlineFormCustomSelect").get(0).selectedIndex = 0;
            }
        },
        error: function (jqXHR, textStatus, errorThrown) {
            ajaxErrorInfo(jqXHR, textStatus, errorThrown);
        }
    });
}

function loadLibFeeRates(libID) {
    $.ajax({
        type: "POST",
        url: '/feeRates/getLibFeeRates',
        data: {"libID": libID},
        success: function (result) {
            if (result.data) {
                datas = result.data[0].rates;
                createSpreadView(false);
            }
        },
        error: function (jqXHR, textStatus, errorThrown) {
            ajaxErrorInfo(jqXHR, textStatus, errorThrown);
        }
    });
}

function createSpreadView(canEdit) {
    // 创建前先销毁旧树表。
    if (spreadView) {
        spreadView.destroy();
        spreadView = null;
    }

    var columns = [
        {
            id: 'name',
            caption: '名称',
            dataField: 'name',
            width: 250,
            allowEditing: false
        },
        {
            id: 'rate',
            caption: '费率',
            dataField: 'rate',
            format: '0.000',
            width: 80,
            minWidth: 50,
            allowEditing: true
        },
        {
            id: 'memo',
            caption: '说明',
            dataField: 'memo',
            width: 200,
            minWidth: 120,
            allowEditing: false
        },
        {
            id: 'ID',
            caption: 'ID',
            dataField: 'ID',
            width: 80,
            visible: false,
            allowEditing: false
        },
        {
            id: 'ParentID',
            caption: '父结点ID',
            dataField: 'ParentID',
            width: 80,
            visible: false,
            allowEditing: false
        }
    ];

    var options = {
        allowSorting: false,
        showRowHeader: true,
        colMinWidth: 80,
        colHeaderHeight: 35,
        rowHeight: 30,
        allowEditing: canEdit,
        editMode: 'inline',
        editUnit: 'cell',
        selectionUnit: (canEdit == true) ? "cell" : "row",
        hierarchy: {
            keyField: 'ID',
            parentField: 'ParentID',
            collapsed: false,
            column: 'name'
        }
    };

    var dataSource = {
        loadRange: function(params) {
            params.success(rate_data);
        },

        update: function(params) {
            $.ajax({
                type: 'POST',
                url: '/feeRates/updateProjectFeeRate',
                data: {"fileID": feeRateFileID, "rateID": params.dataItem.ID, "rateValue": params.dataItem.rate},
                success: function(data) {
                    var iCode = data.data;
                    if (iCode == 1){
                        params.success();
                    }else{
                        alert('(' + iCode + ') 修改失败！');
                        spreadView.cancelEditing();
                        params.failed();
                    }
                },
                error: function(xhr, status) {
                    alert('内部程序错误！');
                    params.failed();
                }
            });
        }
    };

    function rowClickFun(sender, args) {
        alert(args.item.dataItem.rate);
    }

    spreadView = new GC.Spread.Views.DataView($('#divFee')[0],
        dataSource, columns, new GC.Spread.Views.Plugins.GridLayout(options));

    spreadView["rowDbClick"].addHandler(rowClickFun);
    spreadView.invalidate();
    document.querySelector('#divFee').focus();
}

function reFreshRateViews() {
    if(spreadView){
        spreadView.refresh();
    }else {
        loadProjectFeeRates(feeRateFileID);
    }
}

var feeRateObj = {
    getFeeRate: function (){


    }
}


$('#tab_fee_rate').bind('click', function () {
    reFreshRateViews();
    //creatennnSpreadView();
});