(function ($) {

    $.fn.TCClient = function (config) {
        var defaults = { url: '', parms: [] };
        return this.each(function () {
            var $this = $(this);

            $(this).bind('mouseover', function () {
                $('.p0001').remove();
                ini();
                getByajax();
                $('html').append($Parent);
                $('.p0001').css({ left: $this.offset().left + $this.width(), top: $this.offset().top });
            });

            if (config) $.extend(defaults, config);
            if (defaults.url.length <= 0) {
                return;
            }
            var $Parent, $base, $contract, leftpos, toppos;
            //create ui
            function ini() {
                $Parent = $('<div class="p0001 col-sm-3 "  id="p0001" style="position:absolute;z-index:999;"></div>');
                $Parent.append($('<div class="widget-box" style="border:1px solid #ccc;"></div>'));
                $Parent.find('.widget-box').append($('<div class="widget-header widget-header-flat widget-header-small" style="border:none;color:#999;"><h5 class="widget-title">客户基本信息</h5><img src="../../Scripts/BootStrap/img/1_28.png" class="close " style="float:right;margin-right: 0px;"></img></div><div class="widget-body"><div class="widget-main"><div class="TCContainer"></div></div></div>'));
                $Parent.find('.widget-box').append($('<div class="widget-header widget-header-flat widget-header-small" style="border:none;color:#999;"><h5 class="widget-title">其他联系人</h5></div><div class="widget-body"><div class="widget-main"><div class="TCContract"></div></div></div>'));
                $Parent.find('.close').bind('click', function (e) {
                    $('.p0001').remove();
                });
            }

            //create base
            function cerateBase(data) {
                if (data.clientbase.length > 0) {
                    $Parent.find('.TCContainer').append($('<div class="c-group"><lable  class="tcname lable-field1 label-field-content"></lable></div>'));


                    $Parent.find('.TCContainer').append($('<div class="c-group"><lable class="control-label1">地址:</lable><lable  class="tcaddress lable-field1 label-field-content"></lable></div>'));

                    $Parent.find('.TCContainer').append($('<div class="c-group"><lable class="control-label1">联系人:</lable><lable  class="tcmaincontr lable-field1 label-field-content"></lable></div>'));
                    $Parent.find('.TCContainer').append($('<div class="c-group"><lable class="control-label1">手机:</lable><lable  class="tcphone lable-field1 label-field-content"></lable></div>'));
                    $Parent.find('.TCContainer').append($('<div class="c-group"><lable class="control-label1">固话:</lable><lable  class="tcgphone lable-field1 label-field-content"></lable></div>'));


                    $Parent.find('.TCContract').append($('<table style="width:100%;"></table>'));

                    $Parent.find('.tcname').text(data.clientbase[0].KeHuMingCheng);
                    $Parent.find('.tcaddress').text(data.clientbase[0].KeHuDiZhi);
                    $Parent.find('.tcmaincontr').text(data.clientbase[0].Contact);
                    $Parent.find('.tcphone').text(data.clientbase[0].DianHuaHaoMa);
                    $Parent.find('.tcgphone').text(data.clientbase[0].msn);
                    for (var i = 0; i < data.otherContract.length; i++) {
                        var $tr = $('<tr><td ></td></tr>');
                        $tr.find('td').eq(0).html('<div class="c-group"><lable class="control-label1" >联系人:</lable><lable  class=" lable-field1 label-field-content" style="width:100px;">' + data.otherContract[i].LianXiRenMingCheng + '</lable><lable class="control-label1" style="width:40px;">电话:</lable><lable  class=" lable-field1 label-field-content">' + data.otherContract[i].DianHuaHaoMa + '</lable></div>');
                        //$tr.find('td').eq(1).text('电话:'+data.otherContract[i].DianHuaHaoMa);
                        //$tr.find('td').eq(2).text(data.otherContract[i].XingBie);
                        $Parent.find('table').append($tr);
                    }
                }
                else {
                    $Parent.find('.TCContainer').text('没有数据！');
                }

            }
            //ajax
            function getByajax() {
                $Parent.find('.TCContainer').empty();
                $Parent.find('.TCContainer').append('<span class="animate">正在加载，请稍后...</span>');
                var purl = defaults.url;
                var parms = "";
                for (var i = 0; i < defaults.parms.length; i++) {
                    parms += "&" + defaults.parms[i].prmname + "=" + defaults.parms[i].pvalue;
                }
                purl = purl + parms;
                $.ajax({ url: purl, type: 'post', cache: false, dataType: "json", success: function (data) {
                    $Parent.find('.TCContainer').empty();
                    cerateBase(data);
                }, error: function () {
                    $Parent.find('.animate').text('加载数据错误，请与系统管理员联系！'); ;
                }
                });
            }


        });

    }



})(window.jQuery);