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

//test data start
// var data_getList = Mock.mock('/basicconfig/gates/getCEquipmentList.htm', {code: 0, list: [{id: '33', eqname: '显示器', equipmentno: 'e123213', mclientNo: 'mc12312321', type: '道闸', ip: '1.1.2.1', serviceIP: '2.2.3.3', port: '235', servicePort: '5555', aname: 'zhangsan', password: '', state: false, aspect: 1}]})
// var data_doDel = Mock.mock('/basicconfig/gates/delCEquipmentItem.htm', {code: 0, })
// var data_doUsed = Mock.mock('/basicconfig/gates/cequipmentChangeState.htm', {code: 0, })
//test data end

window.I = {
    relationno: $('#relationno').val()
}

var ViewModel = function() {
	var self = this;
    self.list = ko.observableArray();
    self.used = function (item) {
        // doUsed(item);
    }
    
    self.del = function(item) {
        delConfirm(item);
    }
}

$(function () {
    window.vm = new ViewModel();
    ko.applyBindings(vm);
    if (I.relationno) {
        getList(I.relationno);
    }
    pageInit();
    $('#newceq').click(function(event) {
        var relationno=$('#relationno').val();
        jQuery.post('/basicconfig/gates/getMClient.htm', {relationno:relationno}, function(data, textStatus, xhr) {
            if(data.item!=null){
                window.location.href="gates_cequipmentEdit.jsf?mclientNo="+data.item.mclientNo;
            }else{
               // parent.showErrmsg("没有门禁客户端,请先配置门禁客户端!");
                swal({title:'',text:'没有门禁客户端,请先配置门禁客户端',type:'error',confirmButtonText:'确定'}); 
            }
        }); 
    });
});

function pageInit() {
}

function getList(mclientNo) {
    $.post("/basicconfig/gates/getCEquipmentList.htm", {mclientNo: mclientNo},
        function (data, textStatus, jqXHR) {
            vm.list.removeAll();
            if (data.code >= 1) {
                //parent.showErrmsg(data.errmsg);
                swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'}); 
                return;
            }
            if (data.code == 0) {
                if (isList(data.list)) {
                    $.each(data.list, function (index, value) { 
                        value.state = ko.observable(value.state);
                        vm.list.push(value);
                    });
                }
                return;
            }
        },
        "json"
    );
}

function delConfirm(item) {
    $("#modalContent").html('确定要删除【' + item.eqname + '】吗?');
    $("#dialog-confirm").removeClass('hide').dialog({
        resizable: false,
        width: '320',
        modal: true,
        title: "<div class='widget-header'><h4 class='smaller'><i class='ace-icon fa fa-exclamation-triangle red'></i>确认信息</h4></div>",
        title_html: true,
        position: { my: "center", at: "center", of: window },
        buttons: [
            {
                html: "<i class='ace-icon fa fa-trash-o bigger-110'></i>&nbsp; 确定",
                "class" : "btn btn-danger btn-minier",
                click: function() {
                    $( this ).dialog( "close" );
                    doDel(item);
                }
            }
            ,
            {
                html: "<i class='ace-icon fa fa-times bigger-110'></i>&nbsp; 取消",
                "class" : "btn btn-minier",
                click: function() {
                    $( this ).dialog( "close" );
                }
            }
        ]
    });
}


function doDel(item) {
    if (!!I.AjaxDelItem) return;
    I.AjaxDelItem = true;
    $.post('/basicconfig/gates/delCEquipmentItem.htm', {id: item.id}, function(data, textStatus, xhr) {
        I.AjaxDelItem = false;
        if (data.code >= 1) {
           // parent.showErrmsg(data.errmsg);
            swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'}); 
            return;
        }
        if (data.code == 0) {
            vm.list.remove(item);
            location.replace(location.href);
            return;
        }
    }, 'json');
}

function doUsed(item) {
    if (!I.AjaxChangeUsed) {
        var used = !item.state();
        I.AjaxChangeUsed = true;
        $.post('/basicconfig/gates/cequipmentChangeState.htm', {id: item.id, state: used}, function(data, textStatus, xhr) {
            I.AjaxChangeUsed = false;
            if (data.code >= 1) {
                //parent.showErrmsg(data.errmsg);
               swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'}); 
                return;
            }
            if (data.code == 0) {
                item.state(used);
                return;
            }
        }, 'json');
    }
}