$(function () {
    let more_btn_text=   {  '待审核':'审核','未发布':'取消审核' ,'收集中':'取消审核','已结束':'已结束'};
    let publish_btn_text=   {  '待审核':'发布','未发布':'发布' ,'收集中':'暂停收集','已结束':'已结束'};
    // 更多
    let $iosActionsheet = $('#iosActionsheet');
    let $iosMask = $('#iosMask');
    function hideActionSheet() {
        $iosActionsheet.removeClass('weui-actionsheet_toggle');
        $iosMask.fadeOut(200);
    }
    $iosMask.on('click', hideActionSheet);
    $('#iosActionsheetCancel').on('click', hideActionSheet);
    $("body").on("click", ".more-btn", function () {
        let published =$(this).parents('.w-list-item').find('.status-wrapper span').text()
        $iosActionsheet.addClass('weui-actionsheet_toggle');
        $iosMask.fadeIn(200);
        $iosActionsheet.find('.examine-btn').text(more_btn_text[published])
        $iosActionsheet.find('.publish-btn').text(publish_btn_text[published])
        $iosActionsheet.attr('ques-id',$(this).parents('.w-list-item').attr('ques-id'))
        $iosActionsheet.attr('ques-published',published)
        $iosActionsheet.attr('ques-title',$(this).parents('.w-list-item').find('.list-item-title').text())
    });
    //审核
    $('.examine-btn').on('click',function(){
        let ques_id = $iosActionsheet.attr('ques-id')
        let published = $iosActionsheet.attr('ques-published');
        let action = {  '待审核':'audit','未发布':'unaudit' ,'收集中':'unaudit'};
        let html = {'待审核':'<span class="text-warm">未发布</span>','未发布':'<span class="text-default">待审核</span>','收集中':'<span class="text-default">待审核</span>','已结束':'<span class="text-danger">已结束</span>'};
        let formdata ={ 'action':action[published], 'id':ques_id }
     
        $.post('', formdata, function (data) {
            $('.examine-btn').text(more_btn_text[published])
            $('.publish-btn').text(publish_btn_text[published])
            $('.w-list-item[ques-id='+ques_id+']').find('.status-wrapper span').remove();
            $('.w-list-item[ques-id='+ques_id+']').find('.status-wrapper').append(html[published])
            hideActionSheet();
        })
    })
    //发布
    $('.publish-btn').on('click',function(){
        let ques_id = $iosActionsheet.attr('ques-id')
        let published = $iosActionsheet.attr('ques-published');
        let action = {  '待审核':'published','未发布':'published' ,'收集中':'unpublished'};
        let html = {'待审核':'<span class="text-success">收集中</span>','未发布':'<span class="text-success">收集中</span>','收集中':'<span class="text-warm">未发布</span>'};

        let formdata = {"id": ques_id, 'action': action[published]}
        $.post('', formdata, function (data) {
            if(data.errcode==0){
                $('.examine-btn').text(more_btn_text[published])
                $('.publish-btn').text(publish_btn_text[published])
                $('.w-list-item[ques-id='+ques_id+']').find('.status-wrapper span').remove();
                $('.w-list-item[ques-id='+ques_id+']').find('.status-wrapper').append(html[published])
            }else{
                layer.open({
                    content: data.errmsg,
                    time:1
                })
            }
        }, 'json')
        hideActionSheet();
    })

    //编辑（暂停发布）
    $("body").on("click", ".edit_btn", function () {
        let id = $(this).parents('.w-list-item').attr('ques-id')
        let text = $(this).parents('.w-list-item').find('.status-wrapper span').text()    
        if(text == '收集中'){
            layer.open({
                content: '编辑问卷时，问卷会暂停发布，你需在修改完后再次发布问卷才可以继续进行收集，是否继续？',
                btn: ['是', '否'],
                yes: function (index) {
                    let formdata = {"id": id, 'action': 'unpublished'}
                    $.post('user_research_list_mobile', formdata, function (data){
                        if(data.errcode==0){
                            window.location.href=' user_research_update_mobile?id='+id
                        }
                    } , 'json')
                  
                }
            })
        }else{
            window.location.href=' user_research_update_mobile?id='+id
        }

    })
    $('.add-btn').on('click', function () {
        layer.open({
			content: '是否添加新的问卷？',
			btn: ['确定', '取消'],
			yes: function (index) {
                window.location.href='user_research_update_mobile'
            }
        })
    })
    
     $('.show_search').on('click',function(){
         $('.project-search').show()
     })
     $('.hide_search_btn').on('click',function(){
        $('.project-search').hide()
    })
    
     // 删除项目
     $('.delete-btn').on('click', function () {
        let ques_id = $iosActionsheet.attr('ques-id')
        let title = $iosActionsheet.attr('ques-title')
        layer.open({
			content: '是否删除《'+title+'》的问卷？',
			btn: ['确定', '取消'],
			yes: function (index) {
                layer.close(index);
                let formdata = {"action": 'del', 'id': ques_id};
                $.ajax({
                    url: "",
                    type: "post",
                    async: false,
                    data: formdata,
                    dataType: "json",
                    success: function (data) {
                        if (data.errcode == 0) {
                            layer.open({
                                content: '删除成功',
                                time:1
                            })
                            window.location.href='user_research_list_mobile'
                            hideActionSheet();
                        }
                    }
                })
			}
		});

    })
    //清空数据
    $('body').on('click','.claen_btn',function(){
        let ques_id = $iosActionsheet.attr('ques-id')
        layer.open({
            content: '确定要清除数据吗？',
            btn: ['确定', '取消'],
            yes: function (index) {
                let formdata = {"action": 'clean', 'id': ques_id};
                $.ajax({
                    url: "",
                    type: "post",
                    async: false,
                    data: formdata,
                    dataType: "json",
                    success: function (data) {
                        layer.open({
                            content: '清除数据成功',
                            time: 1
                          });
                        hideActionSheet();
                    },
                    error: function () {
                        layer.open({
                            content: '请求错误',
                            time: 1
                          });
                    }
                });
                layer.close(index);
            }
        })
    })
    //搜索
    let search_count=0;
    let no_result=false;
    $("#search").bind('input propertychange',function(){
        if($('#search').val()){
            $('.common-search .left .user-input .clear').show()
            showresult()
            var formdata = {
                'action': 'get_data',
                'up_time': 0,
                'status': 0,
                'word': $('#search').val(),
                'page': 1
                }
            $.ajax({
                url:"",
                type:"POST",
                dataType:"json",
                data:formdata,
                success:function (data) {
                    search_count=data['data'].length
                    $('.w-common-search .result-header span').text(search_count);
                    console.log(data['data'])
                    let html ='';
                    // 状态
                    let current = Date.parse(new Date()) / 1000
                    let status=0;
                    let CLASSES = ['text-default','text-warm','text-success','text-danger"']
                    let CLASSESNAME = ['待审核','未发布','收集中','已结束"']
                    $('.w-common-search .result-scroll .list .w-list-item').remove()
                    data['data'].forEach(val => {
                    if(val['limited_time']&&val['end']<current){
                            status =3 ;
                    }else{
                        if(val['audit']==0){
                            status = 2
                        }else if(val['audit'] == 1 && val['published'] == 0){
                            status = 1
                        }else{
                            status = 0
                        }
                    }
                    html+='<div class="w-list-item" ques-id='+ val['id']+'>'+
						'<div class="list-item-div">'+
							'<div class="list-item">'+
								'<div class="list-item-inner">'+
									'<div class="list-item-title">'+ val['title']+'</div>'+
									'<div class="list-item-details">'+
										'<span class="status-wrapper">状态：'+
											'<span class='+CLASSES[status]+'>'+CLASSESNAME[status]+'</span>'+
										'</span>'+
										'<span class="list-item-collect">收集数量：4份</span>'+
									'</div>'+
								'</div>'+
							'</div>'+
							'<div class="list-item-action-div">'+
								'<span class="w-icon-wrapper edit_btn">'+
									'<i class="fa fa-edit"></i>&nbsp;编辑'+
								'</span>'+
								'<span class="w-icon-wrapper share-btn">'+
									'<i class="fa fa-share"></i>&nbsp;分享'+
								'</span>'+
								'<span class="w-icon-wrapper">'+
									'<i class="fa fa-bar-chart-o"></i>&nbsp;数据'+
								'</span>'+
								'<span class="w-icon-wrapper more-btn">'+
									'<i class="fa fa-ellipsis-h"></i>&nbsp;更多'+
								'</span>'+
							'</div>'+
						'</div>'+
                    '</div>';
                    });
                    $('.w-common-search .result-scroll .list').append(html)
                }
            })
        }else{
            $('.common-search .left .user-input .clear').hide()
            $('.result').hide()
        }
    })
    $("#search").on('click',function(){     
        no_result=false
        showresult()      
    })

    $('.w-common-search .result').on('click',function(){
        if($("#search").val()&&search_count==0){
            no_result=true
        }
        showresult()
    
    })
    function showresult(){
        if($("#search").val()){
            $('.result').show()
            $('.result-empty').hide()
            if(no_result){
                $('.result').hide()
                $('.result-empty').show()
            }
        }else{
            $('.result-empty').hide()
            $('.result').hide()
        }
    }
    $('.common-search .left .user-input .clear').on('click',function(){
        $("#search").val('')
        $('.common-search .left .user-input .clear').hide()
        $('.w-common-search .result-scroll .list .w-list-item').remove()
    })
    // $('#search').on('change',function(){
    //     console.log($('#search').val())
    //     var formdata = {
    //         'action': 'get_data',
    //         'up_time': 0,
    //         'status': 0,
    //         'word': $('#search').val(),
    //         'page': 1
    //     }
    //     $.ajax({
    //         url:"",
    //         type:"POST",
    //         dataType:"json",
    //         data:formdata,
    //         success:function (data) {
    //             console.log(data)
    //         }
    //     })
    // })
})

