(function() {
    $.fn.xiaoqu = function(options) {
        var defaults = {
            'color': 'red',
            'url':''
        };
        var settings = $.extend({},defaults, options);
        this.css({
            'color': settings.color
        });
        var name = this.attr('name');
        this.attr('name','');
        var value = this.attr('data-value');
        var html = '<input type="hidden" name="'+name+'" value="'+value+'" class="hidden-id">'+
        '<div class="jquery-choice choice" >'+
                '<div class="search-input">'+
                '    <input type="text" name="keyword" lay-verify="" placeholder="请输入关键字" autocomplete="off" class="layui-input">'+
                '    <i>清空</i>'+
                '</div>'+
                '<div class="layui-row">'+
                '       <div class="layui-col-xs4 l l1">'+
                '          <dl>'+
                '          </dl>'+
                '        </div>'+
                '        <div class="layui-col-xs4 l l2">'+
                '          <dl>'+
                '          </dl>'+
                '        </div>'+
                '        <div class="layui-col-xs4 l l3">'+
                '          <dl>'+
                '          </dl>'+
                '        </div>'+
                '</div>'+
                '<div class="search-keyword">'+
                '       <div class="">'+
                '          <dl>'+
                '          </dl>'+
                '        </div>'+
                '</div>'+
        '</div>';
        var _this = $(this);
        _this.attr('readonly',true).parent().append(html);
        var _parent = _this.parent();

        $.ajax({
            url:settings.url,
            type:"get",
            data:{},
            dataType:"json",
            success:function(r){
                var xzq = r.data.xzq;
                var pq = r.data.pq;
                var xiaoqu = r.data.xiaoqu;
                var xzq_s = '';
                for(var i in xzq){
                    var dd = '<dd data-id="'+xzq[i]['id']+'" >'+xzq[i]['xzqming']+'</dd>';
                    xzq_s = xzq_s + dd;
                }
                _parent.find('.jquery-choice div.layui-row>.l1>dl').html(xzq_s);
                var pq_s = '';
                for(var i in pq){
                    var dd = '<dd data-id="'+pq[i]['id']+'" data-p1="'+pq[i]['ssxzq']+'">'+pq[i]['pianqum']+'</dd>';
                    pq_s = pq_s + dd;
                }
                _parent.find('.jquery-choice div.layui-row>.l2>dl').html(pq_s);
                var xiaoqu_s = '';
                for(var i in xiaoqu){
                    var dd = '<dd data-id="'+xiaoqu[i]['id']+'" data-p1="'+xiaoqu[i]['ssxzq']+'" data-p2="'+xiaoqu[i]['sspianqu']+'">'+xiaoqu[i]['xiaoqum']+'</dd>';
                    xiaoqu_s = xiaoqu_s + dd;
                }
                _parent.find('.jquery-choice div.layui-row>.l3>dl').html(xiaoqu_s);
            },
            error:function(){
                layer.msg('网络错误！',{anim: 6});//错误提示
            }
        });

        _parent.on('click','.jquery-choice',function(e){
            e.stopPropagation();
        })

        _parent.on('click','.search-input i',function(e){
            _parent.find('.search-input input').val('');
            _parent.find(".search-keyword").hide();
            _parent.find(".layui-row").show();
            e.stopPropagation();
        })

        _this.click(function(e){
            var obj = $('.choice');
            obj.hide();
            $(this).parent().find('.jquery-choice').show();
            e.stopPropagation();
        });

        _parent.on('keyup','.jquery-choice  input',function(e){
            var txt=$(this).val();
            if (txt!="") {
                _parent.find(".search-keyword").show();
                _parent.find(".layui-row").hide();
              $.ajax({
                  url:settings.url,
                  type:"get",
                  data:"txt="+txt,
                  dataType:"json",
                  success:function(data){
                    var sousuo = data.data;
                    _parent.find(".search-keyword div dl dd").remove();
                    for(var i in sousuo){
                      var dd=$("<dd></dd>");
                      $(dd).attr("data-name",sousuo[i]['xiaoqum']);
                      $(dd).attr("data-value",sousuo[i]['id']);
                      $(dd).html(sousuo[i]['xiaoqum']);
                      _parent.find(".search-keyword div dl").append(dd);
                    }
                    
                  }
                });
            }else{
                _parent.find(".search-keyword").hide();
                _parent.find(".layui-row").show();
            }
            e.stopPropagation();
        })

        _parent.on('click','.jquery-choice .search-keyword div dl dd',function(e){
            var id = $(this).attr('data-id');
            var val = $(this).html();
            _parent.find('input[type=hidden]').val(id);
            _this.val(val);
            _parent.find('.jquery-choice').hide();
            e.stopPropagation();
        })

        _parent.on('click','.jquery-choice .l1 dl dd',function(e){
            var id = $(this).attr('data-id');
            $(this).addClass("on").siblings().removeClass("on");
            _parent.find('.jquery-choice .l2 dl dd').each(function(){
                $(this).removeClass("on");
                var p1 = $(this).attr('data-p1');
                if (p1 != id) {
                    $(this).hide();
                }else{
                    $(this).show();
                }
            });
            _parent.find('.jquery-choice .l3 dl dd').each(function(){
                var p1 = $(this).attr('data-p1');
                if (p1 != id) {
                    $(this).hide();
                }else{
                    $(this).show();
                }
            });
            e.stopPropagation();
        })
        _parent.on('click','.jquery-choice .l2 dl dd',function(e){
            var id = $(this).attr('data-id');
            $(this).addClass("on").siblings().removeClass("on");
            _parent.find('.jquery-choice .l3 dl dd').each(function(){
                var p2 = $(this).attr('data-p2');
                if (p2 != id) {
                    $(this).hide();
                }else{
                    $(this).show();
                }
            });
            var pid = $(this).attr('data-p1');
            _parent.find('.jquery-choice .l1 dl dd[data-id='+pid+']').addClass("on").siblings().removeClass("on");
            e.stopPropagation();
        })


        _parent.on('click','.jquery-choice .l3 dl dd',function(e){
            var id = $(this).attr('data-id');
            var val = $(this).html();
            _parent.find('input[type=hidden]').val(id);
            _this.val(val);
            _parent.find('.jquery-choice').hide();
            e.stopPropagation();
        });

        $(document).click(function(){ 
          var obj = $('.choice');
          obj.hide();
        }) 
    }
})();