/**
 * Created by CSL on 2017-05-18.
 */

var pageObj = {
    initPage: function (){
        $("#drirect-dinge").click(function(){
            $(this).attr('href', "/rationRepository/ration" + "?repository=" + getQueryString("repository"))
        });

        $("#gongliao").click(function(){
            $(this).attr('href', "/rationRepository/lmm" + "?repository=" + getQueryString("repository"))
        });

        var libID = getQueryString("repository");
        var libName = storageUtil.getSessionCache("RationGrp","repositoryID_" + libID);
        if (libName) {
            var html = $("#rationname")[0].outerHTML;
            html = html.replace("XXX定额库", libName);
            $("#rationname")[0].outerHTML = html;
        };

        coeList.show();
    }
};

var coeList = {
    mainSpread: null,
    detailSpread: null,
    datas: [],

    colDefMain: [
        {name: "ID", displayName: "编号", size: 100},
        {name: "name", displayName: "名称", size: 280},
        {name: "content", displayName: "内容", size: 250}
    ],
    colDefContent: [
        {name: "coeType", displayName: "类型", size: 100, hAlign: "center"},
        {name: "gljID", displayName: "工料机ID", size: 100, hAlign: "center",formatter: "00000000"},
        {name: "operator", displayName: "操作符", size: 60, hAlign: "center"},
        {name: "amount", displayName: "数量", size: 80, hAlign: "right"}
    ],

    show: function (){
        var me = this;
        me.getDatas();
        me.mainSpread = sheetObj.create($('#mainSpread')[0], me.colDefMain, me.datas);
        me.detailSpread = sheetObj.create($('#contentSpread')[0], me.colDefContent, me.datas[0].coes);

        var coeType = new GC.Spread.Sheets.CellTypes.ComboBox();
        coeType.items(["单个","全部","人工类","材料类","机械类"]);
        me.detailSpread.getSheet(0).getRange(-1, 0, -1, 1).cellType(coeType);

        var operType = new GC.Spread.Sheets.CellTypes.ComboBox();
        operType.items(["+","-","*","/","="]);
        me.detailSpread.getSheet(0).getRange(-1, 2, -1, 1).cellType(operType);

        me.detailSpread.options.showVerticalScrollbar = false;

        me.mainSpread.getSheet(0).bind(GC.Spread.Sheets.Events.EnterCell, me.onMainEnterCell);
        me.detailSpread.getSheet(0).bind(GC.Spread.Sheets.Events.CellChanged, me.onDetailCellChanged);
    },

    getDatas: function () {
        var me = this;
        var libID = getQueryString("repository");

        $.ajax({
            type:"POST",
            url:"/rationRepository/api/getCoeList",
            data:{"libID": libID},
            dataType:"json",
            cache:false,
            async: false,
            timeout:20000,
            success: function (result) {
                if (result.data) {
                    me.datas = result.data;
                };
            },
            error: function (result) {
                alert('内部程序错误！');
            }
        })
    },

    getLibID: function(libName){
        var me = this;
        $.ajax({
            type:"POST",
            url:"/rationRepository/api/getLibIDByName",
            data:{"libName": libName},
            dataType:"json",
            cache:false,
            async: false,
            timeout:20000,
            success: function (result) {
                if (result.data) {
                    me.libID = result.data;
                }
            },
            error: function (result) {
                alert('内部程序错误！');
            }
        })
    },

    onMainEnterCell: function(sender, args) {
        var me = coeList;
        var row = args.sheet.getActiveRowIndex();
        me.detailSpread.suspendPaint();
        me.detailSpread.getSheet(0).setDataSource(me.datas[row].coes);
        me.detailSpread.resumePaint();
    },

    onDetailCellChanged:  function(sender, args) {
        //alert('onDetailCellChanged');
        var me = coeList;
        var row = args.sheet.getActiveRowIndex();
        var mainRow = me.mainSpread.getSheet(0).getActiveRowIndex();
        var curType = args.sheet.getValue(row,0);
        if (!(curType == '单个')){
            me.detailSpread.suspendEvent();
            args.sheet.setValue(row, 1, null);
            me.detailSpread.resumeEvent();
            //args.sheet.getCell(row, 1).backColor("red");
        }
        else{
            //args.sheet.getCell(row, 1).backColor("Blue");
        };
        var obj = me.datas[mainRow];
        me.save([],[],[obj]);
    },

    save: function(addArr, deleteArr, updateArr) {
        $.ajax({
            type:"POST",
            url:"api/saveCoeList",
            data: {"data": JSON.stringify({"addArr": addArr, "deleteArr": deleteArr, "updateArr": updateArr})},
            dataType:"json",
            cache:false,
            timeout:5000,
            success:function(result){
                if (result.error) {
                    alert(result.message);
                } else {
                    // 成功。
                }
            },
            error:function(err){
                alert("内部程序错误！");
            }
        })
    }
};

pageObj.initPage();




