//-Created by zhuyan6 on 16/6/3.
block ui-widget-select
    -if(!document||(document&&!(document.querySelector("style[UID='ui-widget-select']"))))
    style(UID='ui-widget-select').
        .ui-widget-select {
            position: relative;
            display: inline-block;
            font: 12px/20px "Microsoft YaHei";
            color: #99999d;
            border: 1px solid #e7eaec;
            border-radius: 2px;
            vertical-align: middle;
        }

        .ui-widget-select .ui_select_title {
            display: block;
            text-align: left;
            text-indent: 4px;
        }

        .ui-widget-select span {
            pointer-events: none;
            overflow: hidden;
            text-overflow: clip;
            display: inline-block;
            white-space: nowrap;
            /*vertical-align: middle;*/
        }

        .ui-widget-select select {
            position: relative;
            top: 0;
            left: 0;
            width: 150px;
            height: 25px;
            filter: alpha(opacity:0);
            font: 12px/20px "Microsoft YaHei";
            color: #f80;
            display: none;
            opacity: 0;
        }

        .ui-widget-select select option {
            text-indent: 10px;
            opacity: 0;
            display: none;
        }

        .ui-widget-select .ui_select_datalist {
            width: 100%;
            position: absolute;
            left: -1px;
            right: 0;
            /*top: 39px;*/
            max-height: 100px;
            padding: 0;
            border: 1px solid #e7eaec;
            background-color: #fff;
            overflow: auto;
            display: none;
            z-index: 1;
        }

        .ui-widget-select .ui_select_datalist_li {
            display: block;
            line-height: 20px;
            padding: 6px 8px;
            text-overflow: ellipsis;
            white-space: nowrap;
            overflow: hidden;
            cursor: pointer;
            text-align: left;
            font-size: 12px;
        }

        .ui-widget-select .ui_select_datalist_li:hover {
            background-color: #0cbc9a !important;
            color: #fff;
        }

        .ui-widget-select .triangle {
            width: 0;
            height: 0;
            float: right;
            margin: 10px;
            overflow: hidden;
            border-width: 5px;
            border-style: solid;
            _border-style: dashed;
            border-color: transparent;
            pointer-events: none;
        }
        .ui-widget-select .top {
            border-top-color: #c3c3c3;
            _border-top-style: solid;
        }
        .ui-widget-select .left {
            border-right-color: #c3c3c3;
            _border-right-style: solid;
        }

    mixin init-ui-widget-select(id,content,arr,width,height,bgColor)
        - function addselectList(dom,options) {
        - var list = Array.prototype.slice.apply(options);
        - var ins = "";
        - list.forEach(function(item,index){
        //- console.log('ui_select_datalist_li:',arr[index])
        - if(arr[index]){
        - ins += '<li class="ui_select_datalist_li" style="background-color:'+bgColor+'" value="'+arr[index].value+ '" data-index='+index+'>'+arr[index].name+'</li>';
        - }
        - })
        - dom.innerHTML = ins;
        -}
        +nextTick(function() {
//            if(document.widget.selectHandler){//已经创建过的,不重复创建
//                return;
//            }
            var sel = document.getElementById(id);
            if(!sel)return;
            var oSel = sel.getElementsByTagName("select")[0];
            var dlist = sel.getElementsByTagName("ul")[0];

            addselectList(dlist, oSel.options);

            //$(oSel).off('change');
            $(oSel).on('change',function(){//如果后面有覆盖监听,只会执行一次
                var val = $(this).find('option:selected').val();
                var txt = $(this).find('option:selected').text();
                $(this).siblings('.ui_select_title' ).find('span').html( txt );
                $(this).siblings('.ui_select_title' ).find('span').attr( 'value',val );
            });

            function selectHandler(e) {
                var touchTarget = e.target;
                var dlist;
                //console.log('---->',e.target,$(touchTarget).closest('.ui-widget-select')[0]);
                var widgetSelect = $(touchTarget).closest('.ui-widget-select')[0];
                //widgetSelect&&console.log('widgetSelect:',widgetSelect,selectHandler.id,widgetSelect.id)
                if(widgetSelect&&selectHandler.id!=widgetSelect.id){
                    selectHandler.id = widgetSelect.id;
                    //return;
                }
                if($(touchTarget).hasClass('ui_select_datalist')){
                    return false;
                }
                $('.ui_select_datalist').hide();
                //var selTarget = $(document).find('#' + id + ' .ui_select_title')[0];
                //if (touchTarget == selTarget)
                if ($(touchTarget).hasClass('ui_select_title')) {
                    dlist = $(touchTarget).siblings('.ui_select_datalist');
                    dlist.show();
                } else {
                    if ($(touchTarget).hasClass('ui_select_datalist_li')) {
                        var $select_title = $(touchTarget).parent().siblings('.ui_select_title');
                        if ($select_title[0]) {

                            $select_title.find('span').text(touchTarget.innerText);
                            var targetValue = $(touchTarget).attr('value');

                            var $select = $select_title.siblings('select')//改变原生select显示内容,并触发change事件
                            $select.val(targetValue);
                            $select.find('option[value="'+targetValue+'"]').prop('selected',true);
                            $select.trigger('change');
                        }
                    }
                }
                return false;
            }
            document.widget.selectHandler&&$(document).unbind('mouseup', document.widget.selectHandler);
            $(document).bind('mouseup',selectHandler)
            document.widget.selectHandler=selectHandler;
        })
    //-通用按钮元素,默认类型,以组的方式存在,组内样式通用
    //-示例:ui-widget-select-default()(content='文本内容',width='px',height='px',bgColor='#',dataArr='[]')
    mixin ui-widget-select-default(custom)
        +createHashId()
        - var content = attributes.content||'',
        - width = attributes.width||'127px',
        - height = attributes.height||'27px',
        - bgColor = attributes.bgColor||'#ffffff',
        - dataArr = attributes.dataArr||['请选择']

        - var classHashId=__classHashId__;
        - var id = 'select_'+classHashId;
        - var spanWidth = parseInt(width)-40+'px';

        .ui-widget-select(custom=custom id=id style="width:" + width + ";height:"+ height +";line-height:" + height+";background-color:"+bgColor)
            .ui_select_title
                span(style='width:' + spanWidth value=content) #{content}
                .triangle.top
            ul.ui_select_datalist(style="top:"+height) 请选择
            select(id="#{id}" required)
                - for (var i = 0; i < dataArr.length; i++) {
                    option(value=dataArr[i].value) #{dataArr[i].name}
                - }

        +init-ui-widget-select(id,content,dataArr,width,height,bgColor)



