// import '../../css/bootstrap.less';
import 'bootstrap/dist/js/bootstrap.js';
import '../../css/public.css';
import '../../css/main_banner.css';
import '../../js/public.js';
import '../../js/main_banner.js';
import '../../css/technologyTrading/achievement.css';
import {
    provinceList
    } from '../../js/NewDicProvinceList';
import {
    industryObject,
    getIndustryCategory
} from '../DicIndustryList';


$(function(){
    var theKeyword, theMode, theFieldA, theFieldB,dataCategory,category,internationalFlag,dateSort,lowerPrice,upperPrice;
    theKeyword=localStorage.getItem("keyWord");
    theKeyword=localStorage.getItem("indexKeyWord");
    var typ=localStorage.getItem("pageType");
    if(theKeyword){
		$('.text_replace').show()
		$('.text_replace span').text(theKeyword);
	}
    localStorage.removeItem("keyWord");
    localStorage.removeItem("indexKeyWord");
    localStorage.removeItem("pageType");
    loadDate();
    
	//数据处理
    var thePageNo = 1,
    thePageLength = 1,
    pageSize =10;
    var categoryObj = { 
    "0": "专利",
    "1": "软著",
    "2": "项目",
    "3": "版权",
    "4": "工业设计",
    "5": "配方",
    "6": "非标"
};
    var transferModeObj = {
    "0": "完全转让",
    "1": "许可转让",
    "2": "技术入股",
    "3": "5年独占许可转让"
};

function loadDate(pageNo) {
    $.ajax({
        method: "get",
        dataType: "json",
        url: globalConfig.context + "/portal/search/achievementList",
        data: {
            pageNo: pageNo || 1,
            pageSize: 10,
            boutiqueFlag :-1,
            keyword: theKeyword,
            transferMode: theMode != 999 ? theMode : undefined,
            fieldA: theFieldA != 999 ? theFieldA : undefined,
            fieldB: theFieldB != 999 ? theFieldB : undefined,
            dataCategory:dataCategory != 999 ? dataCategory : undefined,
            category:category != 999 ? category : undefined,
            dateSort:dateSort,
            lowerPrice:lowerPrice,
	        upperPrice:upperPrice,
	        internationalFlag:internationalFlag != 999 ? internationalFlag : undefined,
        },
        success: function (data) {
            var theArr = [];
            if (data.data && data.data.list) {
                for (let i = 0; i < data.data.list.length; i++) {
                    let thisdata = data.data.list[i];
                    let isJingpin="";
                    if(thisdata.boutique==1){
	                    	isJingpin="jingpin";
	                    }else{
	                    	isJingpin="jingpin1";
	                    }
                    if(thisdata.technicalPictureUrl==null){
	                    	thisdata.technicalPictureUrl=''
	                    }	                    	                    
	                var imgUrl=thisdata.technicalPictureUrl!=''?globalConfig.avatarHost + '/upload' +thisdata.technicalPictureUrl:globalConfig.avatarHost+ "/upload/default/zhuanliimg_null.png";                
                    var star='';
                    for(var n=0;n<thisdata.maturity;n++){
                    	star+='<li></li>';
                    }
                    var percentage='';
                    if(thisdata.maturityS=='正在研发'){
                    	percentage=20
                    }
                    if(thisdata.maturityS=='已有样品'){
                    	percentage=40
                    }
                    if(thisdata.maturityS=='通过小试'){
                    	percentage=60
                    }
                    if(thisdata.maturityS=='通过中试'){
                    	percentage=80
                    }
                    if(thisdata.maturityS=='可以量产'){
                    	percentage=100
                    }                     
                    theArr.push([
 						'<li val="'+thisdata.id+'" typ="'+thisdata.ownerType+'" class="list"><span class="'+isJingpin+'"></span><div>'+(thisdata.name?thisdata.name:"未知名称")+'</div>',
						'<ol><li>类型:<span>' + (thisdata.category ? categoryObj[thisdata.category] : '') + '</span></li>',
						'<li>行业:<span>' + (thisdata.fieldA?getIndustryCategory(thisdata.fieldA, thisdata.fieldB):"未知行业") + '</span></li>',					
						'<li>交易方式:<span>' + (thisdata.transferMode ? transferModeObj[thisdata.transferMode] : "当面交易") + '</span></li>',
						'<li>成熟度:<span>'+(thisdata.maturityS?thisdata.maturityS:"通过小试")+'</span></li>',
						//'<li val="'+thisdata.id+'" typ="'+thisdata.ownerType+'">技术方:<span>'+(thisdata.ownerName?thisdata.ownerName:"佚名")+'</span>',
//						'<div class="progressBar">',
//						'<p class="bjtxt"></p>',
//						'<span class="percentagePic" style="width:'+percentage+'px"></span>',
//						'<span class="percentage">'+percentage+'%</span>',
//						'<p class="txt"></p>',
//						'</div>',						
						'</li>',
						'<li class="consultation"><a href="javascript:;">我要咨询</a></li>',
						'</ol>',
						'<input type="hidden" class="achievementId" value="' + thisdata.id + '">',
                        '<input type="hidden" class="achievementType" value="' + thisdata.dataCategory + '">',
					    '</li>'	,   
                    ].join(''));
                };
            };
            $('.achievementList ul').empty();
            $('.achievementList ul').append(theArr.join(''));
            
            //页面里面的详情链接   
			$(".achievementList ul li .consultation").siblings().on('click', function () {    	      
			        var theId = $(this).parents('.list').attr('val');
			        var thetype=$(this).parents('.list').attr('typ');			       
			        window.open(globalConfig.context + '/portal/technologyTrading/achievementDetail?id=' + theId+'&type='+thetype );
		    });
		    $(".achievementList ul li div").on("click",function(){
			    	$(this).siblings("ol").children("li:first").click();
			    });
			$(".achievementList ul li ol .consultation").click(function(){
			    	$('#lxkf').click()			    	
			})
            
            if(data.data.list.length===0){
	            	$('.achievementList ul').html("<div class='list_none'></div>")	;
	            };
            thePageLength = data.data.totalCount ? Math.ceil(data.data.totalCount / pageSize) : 1;
            var pageArr = [],
                firstNo = 1,
                endNo = 5;
            if (thePageNo > 3) {
                firstNo = thePageNo - 2;
                endNo = Math.min((Number(thePageNo) + 2), thePageLength);
            } else {
                endNo = Math.min(thePageLength, 5);
            };
            for (let i = firstNo; i <= endNo; i++) {
                if (i == thePageNo) {
                    pageArr.push(
                        '<li class="pageNumber active"><a href="#" value=' + i + ' >' + i + '</a></li>'
                    );
                } else {
                    pageArr.push(
                        '<li class="pageNumber"><a href="#" value=' + i + ' >' + i + '</a></li>'
                    );
                }
            };
            $('.pageNumber').remove();
            $('.pagePre').after(pageArr.join(''));
        }
    });
}
    if (window.location.search) {
        let theUrl = window.location.search
        theKeyword = theUrl.substring(1, theUrl.length);
        theKeyword = decodeURIComponent(theKeyword);
        $('#search_on input').val(theKeyword);
    };
 
    var industryListArr = [],
        industryChildrenArr = [];
    industryListArr.push(['<li value="999" class="active">',
        '<span>',
        '不限',
        '</span>',
        '</li>',
    ].join(''));
    industryObject.map(function (item) {
        industryListArr.push(['<li value="' + item.value + '">',
            '<span>',
            item.label,
            '</span>',
            '</li>',
        ].join(''));
    });
    if (industryListArr && industryListArr.length) {
        $('#industryList').append(industryListArr.join(''));
    };
    $('#industryList li').click(function () {
        var theValue = this.value;
        $(this).siblings("li").removeClass("active");
        $(this).addClass("active");
        industryChildrenArr = [];
        industryChildrenArr.push(['<li value="999" class="active">',
            '<span>',
            '不限',
            '</span>',
            '</li>',
        ].join(''));
        if (theValue !== 999) {
            $('#industryChildrenBox').css("display", "block");
            industryObject.map(function (item) {
                if (item.value == theValue) {
                    item.children.map(function (child) {
                        industryChildrenArr.push(['<li value="' + child.value + '">',
                            '<span>',
                            child.label,
                            '</span>',
                            '</li>',
                        ].join(''));
                    });
                };
            });
        } else {
            $('#industryChildrenBox').css("display", "none");
        };
        if (industryChildrenArr && industryChildrenArr.length) {
            $('#industryChildrenList').empty();
            $('#industryChildrenList').append(industryChildrenArr.join(''));
        };
        theFieldA = this.value;
        theFieldB = '999';
        theKeyword='';
        loadDate();
    });
    $('#industryChildrenList').on('click', 'li', function () {
        $(this).siblings("li").removeClass("active");
        $(this).addClass("active");
        theFieldB = this.value;
        loadDate();
    });
    $('#transferModeList').on('click', 'li', function () {
        $(this).siblings("li").removeClass("active");
        $(this).addClass("active");
        theMode = this.value;
        loadDate();
    });
    $('#btn_search').click(function () {   	
        theKeyword = $('#search_on input').val();
        loadDate();
    });
    $('.pagination').on('click', 'li', function (e) {
        e.preventDefault();
        if (this.className === 'pagePre') {
            if (thePageNo > 1) {
                thePageNo = 1;
                loadDate(thePageNo);
            }
        } else if (this.className === 'pageNext') {
            if (thePageNo < thePageLength) {
                thePageNo = thePageLength;
                loadDate(thePageNo);
            }
        } else {
            var nextPageNo = $(this).children()[0].text;
            if (thePageNo != nextPageNo) {
                $(this).siblings("li").removeClass("active");
                $(this).addClass("active");
                thePageNo = nextPageNo;
                loadDate(thePageNo);
            };
        };
    });
	//点击行业分类
    $('#industryList li').click(function(){
    	theKeyword="";
    	$('.text_replace').hide();
    	$('#search_on>input').val('');
		$('.achievement_header ul li').eq(0).show();
		var text_ind=$(this).find('span').text();		
		$('.achievement_header ul li').eq(0).find('p').text(text_ind);
	})
     //点击搜索下面的字段快捷搜索
    $('.head_right ol li').click(function () {        	        
        theKeyword=$(this).text();
        $('#technologyModeList li').eq(0).addClass('active').siblings().removeClass('active');
        $('#transferModeList li').eq(0).addClass('active').siblings().removeClass('active');
        $('.text_replace').show();
        $('.text_replace span').text(theKeyword);
        $('#search_on input').val(theKeyword);
         $('.achievement_header ul li').eq(0).hide();
        $('#industryList>li').removeClass("active");
		$('#industryList>li:nth-child(1)').addClass("active");
        loadDate();       
    });
    //技术类型单选
	$('#patent').prop('checked',false);
	$('#selectAct li:first-child').click(function(){
		$(this).addClass('active');
		$('#patent').prop('checked',false);
		$('#demand').prop('checked',false);
		$('#demand,#patent').parent().addClass('demand').removeClass('patent');					
		category=null;
		dataCategory=null;
		loadDate();  
	})
	$('#patent').click(function(){	
		$('#selectAct li').eq(0).removeClass('active')
		$(this).prop('checked',true);		
		$(this).parent().addClass('patent').removeClass('demand');
		$('#demand').attr('checked',false);
		$('#demand').parent().addClass('demand').removeClass('patent')
		category=null;
		dataCategory=1;
		loadDate(); 
	})
	$('#demand').click(function(){
		$('#selectAct li').eq(0).removeClass('active')
		$(this).prop('checked',true);
		$(this).parent().addClass('patent').removeClass('demand');
		$('#patent').attr('checked',false);
		$('#patent').parent().addClass('demand').removeClass('patent')
		category=0;
		dataCategory=null;
		loadDate(); 
	})
	//是否国际单选
	$('#isInt').prop('checked',false);
	$('#selectInt li:first-child').click(function(){
		$(this).addClass('active');
		$('#isInt').prop('checked',false);
		$('#noInt').prop('checked',false);
		$('#noInt,#isInt').parent().addClass('demand').removeClass('patent');					
		category=null;
		dataCategory=null;
		internationalFlag=null;
		loadDate();  
	})
	$('#isInt').click(function(){	
		$('#selectInt li').eq(0).removeClass('active')
		$(this).prop('checked',true);		
		$(this).parent().addClass('patent').removeClass('demand');
		$('#noInt').attr('checked',false);
		$('#noInt').parent().addClass('demand').removeClass('patent');
		internationalFlag=1;
		loadDate(); 
	})
	$('#noInt').click(function(){
		$('#selectInt li').eq(0).removeClass('active')
		$(this).prop('checked',true);
		$(this).parent().addClass('patent').removeClass('demand');
		$('#isInt').attr('checked',false);
		$('#isInt').parent().addClass('demand').removeClass('patent');
		internationalFlag=0;
		loadDate(); 
	})
	//排序下拉选择
	$('.head-left ul li p').click(function(){
		$(this).siblings().slideToggle(300);	
	})
	$('.head-left ul li').mouseleave(function(){
		$(this).find('.time_select').slideUp(300);
	})
	$('.time_select ul li').click(function(){
		var txt=$(this).text();
		$(this).parents('.time_select').siblings('p').find('span').text(txt);
		$('.time_select').hide();		
	});	
	$('.timeSelect li').click(function(){
		var timeTxt=$(this).attr('value');				
		dateSort=timeTxt;
		loadDate();
	})
	//价格	
	$('.inp_select input').keyup(function(){
		var val_min=$(this).val();
		if(val_min<0){
			$(this).val('0')
		}
	})
	$('.inp_select button').click(function(){				
		var minpri=$('.inp_select #minprice').val();
		var maxpri=$('.inp_select #maxprice').val();						
		lowerPrice =minpri;
	    upperPrice =maxpri;
		loadDate();
	})
	$('#industryList li:first-child').click(function(){		 
		theFieldA = $(this).attr('value');
        theFieldB = '999';
        loadDate();
	})	
	//搜索城市下拉
	var addlength=provinceList.length;
	var soption='';	
	for(var i=0;i<addlength;i++){		
		soption+="<option value='"+provinceList[i].id+"'>"+provinceList[i].name+"</option>";			
	};	
	$('#selt').html(soption);	
	//全部结果查询
	$('.achievement_header ul li img').click(function(){
		$(this).parent().hide();				
		var lilen=$('.achievement_header ul li').css('display');
		if(lilen=='none'){			
			$('#search_on input').val('');
			$('.searchs button').click();
		}
	});
	//$('.text_replace').hide()
	//	搜索功能关联关键字	
	$('.searchs button').click(function(e){		
		e.preventDefault();		
		var search_txt=$('#search_on input').val();		
		$('.text_replace span').text(search_txt);
		if(search_txt==''){
			$('.text_replace').hide()
		}else{
			$('.text_replace').show()
		}
		$('.achievement_header ul li').eq(0).hide();
		$('#industryList>li').removeClass("active");
		$('#industryList>li:nth-child(1)').addClass("active");
	});
	//点击交易方式分类		
	$('#transferModeList li').click(function(){	
		$('.achievement_header ul li').eq(1).show();
		$(this).addClass('active').siblings().removeClass('active');
		var txt_tech=$(this).find('span').text();		
		$('.achievement_header ul li').eq(1).find('p').text(txt_tech);
	});
	
});