/**
* date: 
* author: neeler
*/

// var data_ = Mock.mock('/biz/weightDoc/getItemDetails.htm', {"imgList":["/WeightPic/01002/2017/11/02/E001002201711020002GrossPic1.jpg","/WeightPic/01002/2017/11/02/E001002201711020002GrossPic2.jpg","/WeightPic/01002/2017/11/02/E001002201711020002GrossPic3.jpg","/WeightPic/01002/2017/11/02/E001002201711020002TarePic1.jpg","/WeightPic/01002/2017/11/02/E001002201711020002TarePic2.jpg","/WeightPic/01002/2017/11/02/E001002201711020002TarePic3.jpg"],"item":{"id":"a05f74eb-db2a-40d8-9523-cac64e7ad0bd","poundNo":"E001002201711020002","businessNo":"BO201711020002","tareWeighman":"自动称重","tare":16680.0,"taretime":"2017-11-02 07:59","grossWeighman":"自动称重","gross":85560.0,"grosstime":"2017-11-02 09:12","fubang":0.0,"fubangtime":null,"weight":68880.0,"subtra1":0.0,"subtra2":0.0,"worsebang":0.0,"cardID":"3220065529","vehicleID":"豫AE8500","whousename":"老厂2#地磅","whouseNo":"01002","tarePound":"01002","grossPound":"01002","tareFrom":"auto","lastSaveTime":"2017-11-02 09:12","note":null,"grossFrom":"auto","tares":"16680,","tareTimes":"2017-11-02 07:59:11,","grosses":"85560,","grossTimes":"2017-11-02 09:12:35,","weightStyle":"两次去皮","fromunitno":"10","tounitno":"32","fromunitname":"板带一分厂","tounitname":"河南明泰科技发展有限公司","materialguige":"1.47*1250*2500","materialname":"1系热轧板","materialno":"C1S001","balanceWeight":null,"primaryNetWeight":null,"wdstate":"正常","destroyTime":null,"destroyer":null,"batchNo":"01006_20171102075917","canfub":false,"vtexception":"0","vgexception":"0","vwexception":"0"},"code":0})

var HOSTNAME=window.location.hostname;

var Obj = {
    id: '',
    poundNo: '',
    businessNo: '',
    tareWeighman: '',
    tare: '',
    taretime: '',
    grossWeighman: '',
    gross: '',
    grosstime: '',
    fubang: '',
    fubangtime: '',
    weight: '',
    subtra1: '',
    subtra2: '',
    worsebang: '',
    cardID: '',
    vehicleID: '',
    whousename: '',
    whouseNo: '',
    tarePound: '',
    grossPound: '',
    tareFrom: '',
    lastSaveTime: '',
    note: '',
    grossFrom: '',
    tares: '',
    tareTimes: '',
    grosses: '',
    grossTimes: '',
    weightStyle: '',
    fromunitno: '',
    tounitno: '',
    fromunitname: '',
    tounitname: '',
    materialguige: '',
    materialname: '',
    materialno: '',
    balanceWeight: '',
    primaryNetWeight: '',
    wdstate: '',
    destroyTime: '',
    destroyer: '',
    batchNo: '',
}


window.I = {
    workernamev: $('#workername').val(),
    poundNov: $('#poundNo').val(),
    printP: $('#printP')
}

var ViewModel = function() {
    var self = this;
    self.o = ko.observable(ko.mapping.fromJS(Obj));
    self.list = ko.observableArray();

    self.bussinessDetails = function() {
        newTab();
    }
}

$(function () {
    window.vm = new ViewModel();
    ko.applyBindings(vm);
    if (!!I.poundNov) {
        getItem(I.poundNov);
    }
    pageInit();
});

function pageInit() {
    I.printP.click(function (e) { 
        e.preventDefault();
        // var cardno = vm.o().cardno;
        // var gatesNo = I.gatesNo;
        // if (cardno != '' && gatesNo != '') {
        //     canPrint(cardno, gatesNo);
        // }
        printTab();
    });
}

function printTab() {
    var poundNo = vm.o().poundNo;
	parent.closableTab.addTab({
		'id': poundNo,
		'name': '打印磅单',
		'url': encodeURI('http://' + HOSTNAME + ':9088/ReportServer?reportlet=print%2Fprintdj_gbd_all.cpt&poundNo=' + poundNo + '&pname=' + I.workernamev),
		'closable': true
	});
}

function newTab(item) {
	parent.closableTab.addTab({
		'id': new Date().getTime(),
		'name': '业务详情',
		'url': '/business/initDetalis.htm?bussinessNo=' + vm.o().businessNo,
		'closable': true
	});
}

function getItem(poundNo) {
    $.post("/biz/weightDoc/getItemDetails.htm", {poundNo: poundNo},
        function (data, textStatus, jqXHR) {
            if (data.code >= 1) {
                swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'}); 
                return;
            }
            if (data.code == 0) {
                data.item.balanceWeight = (data.item.weight - data.item.subtra1 - data.item.subtra2 + '').weightFormat();
                data.item.gross = (data.item.gross + '').weightFormat();
                data.item.tare = (data.item.tare + '').weightFormat();
                data.item.weight = (data.item.weight + '').weightFormat();
                data.item.subtra1 = (data.item.subtra1 + '').weightFormat();
                data.item.subtra2 = (data.item.subtra2 + '').weightFormat();
                data.item.primaryNetWeight = (data.item.primaryNetWeight + '').weightFormat();
                vm.o(data.item);
                if (isList(data.imgList)) {
                    $.each(data.imgList, function (index, value) { 
                         vm.list.push({url: value});
                    });
                    colorboxInit();
                }
            }
        },
        "json"
    );
}

function colorboxInit() {
	var $overflow = '';
	var colorbox_params = {
		rel: 'colorbox',
		reposition:true,
		scalePhotos:true,
		scrolling:false,
		previous:'<i class="ace-icon fa fa-arrow-left"></i>',
		next:'<i class="ace-icon fa fa-arrow-right"></i>',
		close:'&times;',
		current:'{current} of {total}',
		maxWidth:'100%',
		maxHeight:'100%',
		onOpen:function(){
			$overflow = document.body.style.overflow;
			document.body.style.overflow = 'hidden';
		},
		onClosed:function(){
			document.body.style.overflow = $overflow;
		},
		onComplete:function(){
			$.colorbox.resize();
		}
	};

	$('.ace-thumbnails [data-rel="colorbox"]').colorbox(colorbox_params);
	$("#cboxLoadingGraphic").html("<i class='ace-icon fa fa-spinner orange fa-spin'></i>");//let's add a custom loading icon
	$(document).one('ajaxloadstart.page', function(e) {
		$('#colorbox, #cboxOverlay').remove();
   });
}

String.prototype.weightFormat = function() {
    if (this + '' == 'null' || this + '' == '') return;
    var arr = (this + '').split('.');
    var i = parseInt(arr[0].length / 3);
    var j = arr[0].length % 3;
    var a = [];
    var s = '';
    if (!!j) {
        a.push(arr[0].substring(0, j));
    }
    for (var m = 0; m < i; m++) {
        a.push(arr[0].substring(m * 3 + j, (m+1) * 3 + j));
    }
    if (arr.length == 1) {
        return a.join(',') + '.00';
    } else {
        if (arr[1].length > 2) {
            s = arr[1].substring(0, 2);
        } else {
            s = (arr[1] + '00').substring(0, 2);
        }
        return a.join(',') + '.' + s;
    }
}