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

//test data start
// var data_getList = Mock.mock('/company/getList.htm', {code: 0, 'list|5-20': [{id: 'id', companyNo: 'companyNo', note: 'note', name: 'name'}], pages: 12})
// var data_doDel = Mock.mock('/company/delItem.htm', {code: 0, errmsg: 'errmsg'})
// var data_doSave = Mock.mock('/company/saveItem.htm', {code: 0, errmsg: 'errmsg'})
//test data end
var YWDY = [
{'name': '河南明泰科技发展有限公司', 'code': '103'},
{'name': '郑州明泰实业有限公司', 'code': '102'},
{'name': '河南明晟新材料科技有限公司', 'code': '111'},
{'name': '河南义瑞新材料科技有限公司', 'code': '114'}
];

window.I = {
    modal: $('#modal'),
    save: $('#save')
}

var Obj = {
    code: '',
    name: '',
}

var ViewModel = function() {

    var self = this;
    self.o = ko.observable(ko.mapping.fromJS(Obj));

    //业务单元
    self.ywdyList=ko.observableArray(YWDY);
    //仓库list
    self.ckList = ko.observableArray();
    //选择的仓库
    self.selectdCkList = ko.observableArray();
    self.edit = function(item) {
        doEdit(item);
    }
}

$(function () {
    window.vm = new ViewModel();
    ko.applyBindings(vm);
    $(".chosen-select").chosen();
    $("#dp_chosen").width(270);
    autoHeight();
    getList();
    pageInit();
});

function pageInit() {

    $('.table-responsive').scroll(function() {
        $('.th').css('top', $(this).scrollTop());
    })
    //保存
    I.save.click(function(event) {
        console.log('........save.....')
        doSave();
    });

    I.modal.on('hidden.bs.modal', function (e) {
        clearError();
    });

}


function getList() {
    getckList();
}

function getckList() {
    $.post('/repertorynexus/getallCkList.htm', {}, function(data, textStatus, xhr) {
        vm.ckList.removeAll();
        if (data.code >= 1) {
            swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'});
            return;
        }
        if (data.code == 0) {
            if(isList(data.list)){
                $.each(data.list, function(index, val) {
                    vm.ckList.push(val);
                });
            }
        }
    }, 'json');
}


function doEdit(item) {
    vm.o(ko.mapping.fromJS(item));
    $.post('/repertorynexus/getChooseList.htm', {sdepartNo:item.code}, function(data, textStatus, xhr) {
        vm.selectdCkList.removeAll();
        if (data.code >= 1) {
            swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'});
            return;
        }
        if (data.code == 0) {
            if(isList(data.list)){
                $.each(data.list, function(index, val) {
                    val.code=val.ywdyNo+'_'+val.ckNo
                    vm.selectdCkList.push(val.code);
                });
            }
            $('#dp').val(vm.selectdCkList()).trigger("chosen:updated");
            I.modal.modal('show');
        }
    }, 'json');
}

function doSave() {
    if (!I.AjaxSaveItem) {
        I.AjaxSaveItem = true;
        I.save.prop('disabled', true);
        var o = ko.mapping.toJS(vm.o());
        o.ckNos=vm.selectdCkList().join(',');
        $.post('/repertorynexus/saveItem.htm', o, function(data, textStatus, xhr) {
            I.AjaxSaveItem = false;
            I.save.prop('disabled', false);
            if (data.code >= 1) {
                swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'});
                return;
            }
            if (data.code == 0) {
                parent.showErrmsg('保存成功!');
                I.modal.modal('hide');
                return;
            }
        }, 'json');
    }
}

function clearError() {
    $('#form input').removeClass('error');
    $('#form .text-error').html('');
}

function autoHeight() {
    $('.page-content').css('min-height', $(parent.window).height() - 117);
    $('.table-responsive').css('height', $(parent.window).height() - 217);
}