/**
 * 
 * Created by Mai on 2017/10/20.
 */

let ConfirmModal = {  
    reBind: function (obj, eventName, fun) {
         obj.unbind(eventName);
         obj.bind(eventName, fun);
    },
    stdBillsUnit: {
        modalObj: $('#std_bills_unit'),
        okObj: $('#std_bills_unit_ok'),
        cancelObj: $('#std_bills_unit_cancel'),
        closeObj: $('#std_bills_unit_close'),
        spread: null,
        setting: {
            "emptyRows":0,
            "headRows":1,
            "headRowHeight":[25],
            "defaultRowHeight": 21,
            "cols":[{
                "width":120,
                "readOnly":true,
                "head":{
                    "titleNames":["编码"],
                    "spanCols":[1],
                    "spanRows":[1],
                    "vAlign":[1],
                    "hAlign":[1],
                    "font":["Arial"]
                },
                "data":{
                    "field":"code",
                    "vAlign":1,
                    "hAlign":0,
                    "font":"Arial"
                }
            }, {
                "width":200,
                "readOnly":true,
                "head":{
                    "titleNames":["名称"],
                    "spanCols":[1],
                    "spanRows":[1],
                    "vAlign":[1],
                    "hAlign":[1],
                    "font":["Arial"]
                },
                "data":{
                    "field":"name",
                    "vAlign":1,
                    "hAlign":0,
                    "font":"Arial"
                }
            }, {
                "width":50,
                "readOnly":true,
                "head":{
                    "titleNames":["单位"],
                    "spanCols":[1],
                    "spanRows":[1],
                    "vAlign":[1],
                    "hAlign":[1],
                    "font":["Arial"]
                },
                "data":{
                    "field":"unit",
                    "vAlign":1,
                    "hAlign":1,
                    "font":"Arial"
                }
            }]
        },
        check: function (std, okCallBack, cancelCallBack) {
            if (!this.spread) {
                this.spread = SheetDataHelper.createNewSpread($('#std_bills_unit_spread')[0], {sheetCount: 1});
                this.spread.options.showScrollTip = GC.Spread.Sheets.ShowScrollTip.vertical;
                this.spread.bind(GC.Spread.Sheets.Events.CellDoubleClick, function (sender, args) {
                    ConfirmModal.stdBillsUnit.okObj.click();
                });
                SheetDataHelper.loadSheetHeader(this.setting, this.spread.getActiveSheet());
            }
            let sheet = ConfirmModal.stdBillsUnit.spread.getActiveSheet();
            let modalObj = this.modalObj;
            let okFun = function () {
                modalObj.modal('toggle');
                if (okCallBack) {
                    let newStd = JSON.parse(JSON.stringify(std));
                    newStd.unit = sheet.getText(sheet.getSelections()[0].row, 2);
                    okCallBack(newStd);
                }
            };
            let cancelFun = function () {
                modalObj.modal('toggle');
                if (cancelCallBack) {
                    cancelCallBack();
                }
            };
            ConfirmModal.reBind(this.okObj, 'click', okFun);
            ConfirmModal.reBind(this.cancelObj, 'click', cancelFun);
            ConfirmModal.reBind(this.closeObj, 'click', cancelFun);
            let datas = [];
            let units = std.unit.split('/');
            for (let unit of units) {
                datas.push({'code': std.code, 'name': std.name, 'unit': unit});
            }
            SheetDataHelper.loadSheetData(this.setting, sheet, datas);
            this.spread.getActiveSheet().setActiveCell(0, 0);
            this.modalObj.modal('show');  
            ConfirmModal.stdBillsUnit.spread.refresh();  
        }
    }
};