$(function(){
	let scoring_style_model_,index_, new_scollTop, type,options,topic,id,scollTop,index__;
	let ques_id=$('#ques_id').val()//问卷id
	let isedit=1//是否为编辑
	// 自动扩展文本域 textarea
	$('textarea').each(function () {
		let target = $(this).get(0);
		// 保存初始高度，之后需要重新设置一下初始高度，避免只能增高不能减低。
		let dh = $(target).attr('defaultHeight') || 0;
		if (!dh) {
			dh = target.clientHeight;
			$(target).attr('defaultHeight', dh);
		}
		target.style.height = dh + 'px';
		let clientHeight = target.clientHeight;
		let scrollHeight = target.scrollHeight;
		if (clientHeight !== scrollHeight) {
			target.style.height = scrollHeight + "px";
		}
	})
	$(document).on("input propertychange", "textarea", function (e) {
		let target = e.target;
		// 保存初始高度，之后需要重新设置一下初始高度，避免只能增高不能减低。
		let dh = $(target).attr('defaultHeight') || 0;
		if (!dh) {
			dh = target.clientHeight;
			$(target).attr('defaultHeight', dh);
		}
		target.style.height = dh + 'px';
		let clientHeight = target.clientHeight;
		let scrollHeight = target.scrollHeight;
		if (clientHeight !== scrollHeight) {
			target.style.height = scrollHeight + "px";
		}
	});

	// 获取滚动高度
	$(window).scroll(function () {
		if (document.documentElement && document.documentElement.scrollTop) {
			scollTop = document.documentElement.scrollTop;
		} else if (document.body) {
			scollTop = document.body.scrollTop;
		}
	});

	// 显示关闭题目类型	
	$("body").on("click", ".show_questionbox_btn", function () {
		isedit = 0
		$('.create-question-box').addClass('questionbox_active')
	})
	$("body").on("click", ".hide_question_btn", function () {
		$('.create-question-box').removeClass('questionbox_active')
	})
	// 添加题目	
	$("body").on("click", ".type-item", function () {	
		type = $(this).attr("data-question-type");
		let formdata={}
		formdata['action'] = "create"
		formdata['id'] = ques_id
		formdata['type'] = type
		console.log(formdata);
		layer.open({
			content:'是否添加此题型',
			btn:['是','否'],
			yes:function(index){
				$.ajax({
					url: '',
					type: "POST",
					dataType: "json",
					data: formdata,
					success: function (data) {		
						let id = data['id']
						let title =data['data']['title']
						let getdata = {}				
						getdata['id'] = id
						getdata['action'] = 'get_set'
						$.ajax({
							url: '',
							type: "POST",
							dataType: "json",
							data: getdata,
							success: function (data) {	
								let options =data['set']['options']
								let topic =data['set']['topic']
								layer.open({
									shadeClose: true,
									type: 1,
									content: questionType(type,id,title,options,topic,1),
									anim: 'up',
									style: 'position:fixed; left:0;overflow: auto; top:0; width:100%; height:100%; border: none;background-color: #f8f8f8; -webkit-animation-duration: .5s; animation-duration: .5s;',
									success: function (elem) {
										new_scollTop = scollTop;
										$(".box").addClass("on").css({ "top": -scollTop, });
										index_ = $(elem).attr("index");
										sort_title(1,id)
									}
								});
							}
						})
						
					},error:function(){
						layer.open({
							content: '业务繁忙',	
							time:1		
						});
					}
				})
				layer.close(index);
			}
		})
	})
	// 取消编辑题目
	$("body").on("click", ".questionCancel-btn", function () {
		layer.open({
			content: '确定放弃对此题的编辑？',
			btn: ['确定', '取消'],
			yes: function (index) {
				layer.close(index);
				layer.close(index_);
				$(".box").removeClass("on");
				$(window).scrollTop(new_scollTop);
				if(isedit==1){
					$('.create-question-box').removeClass('questionbox_active')
				}else{
					location.replace(location.href);
				}
				
				
			}
		});

	})

	//获取图片url地址
	// function getObjectURL(file){//this.files[0]
	// 	let url = null ;
    //         if (window.createObjectURL!=undefined) { // basic
    //             url = window.createObjectURL(file) ;
    //         } else if (window.URL!=undefined) { // mozilla(firefox)
    //             url = window.URL.createObjectURL(file) ;
    //         } else if (window.webkitURL!=undefined) { // webkit or chrome
    //             url = window.webkitURL.createObjectURL(file) ;
    //         }
    //         return url ;
	// }

	// 题目内选项图片上传
	$("body").on("click",".btn-item-upload",function(){
		$(this).parents('.item-btns').find('.upload-image').click()
	})
	$("body").on("change",".upload-image",function(){
		let file = this.files[0];
		if(file){
			$(this).parents('.question-item').find('.show-item-img').removeClass('hide')
		}
		upload_image(file,$(this).parents('.question-item').find('.show-item-img img'))
		
	})
	$("body").on("click",".remove-item-img",function(){
		$(this).parents('.show-item-img').find('img').attr('src','')
		$(this).parents('.show-item-img').addClass('hide')
	})

	// 删除题目内选项
	$("body").on("click",".btn-item-del",function(){
		choice_limit_length = $(this).parents('.question-list').find('.question-item').length-1
		update_limit_choice_select(type,choice_limit_length)
		$(this).parents(".question-item").remove();
	})
	$("body").on("click",".btn-select-item-del",function(){
		$(this).parents(".question-select-item").remove();
	})
	//点击添加选项
	$('body').on('click', '.btn-add-single', function () {
		let index=1
		let length = $(this).parents('.question-list').find('.question-item').length+1
		let selected=$('.question-item-select').val()
		if(type=='scoring'){
			index =$('.scoring-limit-grade').val()
			length=10
		}	
	
		let item = additem($(this), type,"",index);
		$(this).parents(".question_option_operate").before(item);	
		update_limit_choice_select(type,length,selected)
	})
	$('body').on('click', '.btn-select-single', function () {
		let index = $(this).parents('.question-list').find('.question-select-item').length
		if(index<4){
			let item = addselectitem($(this), index,"");
			$(this).parents(".question_option_operate").before(item);
		}

	})
	// 批量添加
	$('body').on('click', '.btn-add-more', function () {
		let this_=$(this);
		let length = $(this).parents('.question-list').find('.question-item').length
		let selected = $('.scoring-limit-grade input').val()
		layer.open({
			title: ['批量添加选项','background-color: #f1f1f1;'],
			className:"more-show",
			content: addBatch(),
			btn: ['保存', '取消'],
			yes: function (index) {
				let item = '', //内容
					con = $('.more-show textarea').val().split("\n"); //字符串转数组
				if (con != '') {
					for (let i = 0; i < con.length; i++) {
						item += additem(this_, type, $.trim(con[i]));
					}
					if (type !="fill"){
						this_.parents(".question_option_operate").before(item);
					}

					length += con.length
				}
				$('.more-show textarea').val(''); //清空编辑框
				layer.close(index);
				update_limit_choice_select(type,length,selected)				
				
			},
		})
	})

	// 添加其它
	$("body").on("click", ".btn-add-other", function (event) {
		if (type == "single_choice" || type == "multiple_choice"){
			let item = initOtherChoice();
			$(this).parents(".question_option_operate").before(item);

			let length = $(this).parents('.question-list').find('.question-item').length
			update_limit_choice_select(type,length)
		}
		event.stopPropagation(); // 阻止事件冒泡
	})

	// 开关
	$("body").on("change", ".questio-type-set input", function () {
		let text =	$(".question-title").text();
		if ($(this).attr('checked')==undefined) {
			$(".set-least-range,.set-most-range").show();
			$(this).attr('checked', "checked");
			$(this).val(1)
			// if(type == 'single_choice'||type == 'multiple_choice'){
			// 	text = '多选题'
			// 	type = 'multiple_choice'
			// }
			//$(".question-title").text("");	
		} else {
			$(".set-least-range,.set-most-range").hide();
			$(this).removeAttr('checked');
			$(this).val(0)
			// if(type == 'single_choice'||type == 'multiple_choice'){
			// 	text = '单选题'
			// 	type = 'single_choice'
			// }		
		}

		//$(".question-title").text(text);
	})


	// 点击题目
	$("body").on("click",".question-panel",function(){
		isedit = 1
		type = $(this).attr("data-question-type"); // 获取题目类型
		id = $(this).attr("data-id");// 获取题目ID
		title = $(this).attr("data-title");// 获取题目标题
		options = $(this).attr("data-options");// 获取题目选项
		topic = $(this).attr("data-topic");// 获取题目设置
		// if (type == "single_choice") {
		// 	checked = "";
		// } else if (type == "multiple_choice") {
		// 	checked = "checked";
		// }
		let close = $(this).attr("data-close");
		if(close!=1){
			layer.open({
				shadeClose: false,
				type: 1,
				content: questionType(type,id,title,options,topic),
				anim: 'up',
				style: 'position:fixed; left:0;overflow: auto; top:0; width:100%; height:100%; border: none;background-color: #f8f8f8; -webkit-animation-duration: .5s; animation-duration: .5s;',
				success: function (elem) {
					new_scollTop = scollTop;
					$(".box").addClass("on").css({ "top": -scollTop, });
					index_ = $(elem).attr("index");	
				}
			});		
		}
			
	
	})

	// 更多里的按钮
	$("body").on("click", ".operate-more-item", function (event) {
		$.sweetDropdown.hideAll(); // 关闭所有的下拉列表
		event.stopPropagation(); // 阻止事件冒泡
	})

	// 复制题目
	$("body").on("click", ".btn-question-clone", function () {
		//复制节点
		let copy = $(this).parents('.question-panel').clone();
		//粘贴
		$(this).parents('.question-panel').after(copy);
		let id = $(this).parents('.question-panel').attr('data-id')
		let data ={}
		data['id'] = id
		post_ajax_formdata('question_copy',data)
		sort_title(2,id)
	})

	// 上移
	$(".question-panel:first").find(".btn-move-up").addClass("disable");
	$("body").on("click", ".btn-move-up:not(.disable)", function () {
		let copy = $(this).parents('.question-panel').clone();
		$(this).parents('.question-panel').prev().before(copy);
		$(this).parents('.question-panel').remove();
		$(".question-panel:first").find(".btn-move-up").addClass("disable");
		$(".question-panel:first").find(".btn-move-up").parents(".question-panel").siblings().find(".btn-move-up").removeClass("disable");
		$(".question-panel:last").find(".btn-move-down").addClass("disable");
		$(".question-panel:last").find(".btn-move-down").parents(".question-panel").siblings().find(".btn-move-down").removeClass("disable");
		sort_title(0)
	})

	// 下移
	$(".question-panel:last").find(".btn-move-down").addClass("disable");
	$("body").on("click", ".btn-move-down:not(.disable)", function () {
		let copy = $(this).parents('.question-panel').clone();
		$(this).parents('.question-panel').next().after(copy);
		$(this).parents('.question-panel').remove();
		$(".question-panel:first").find(".btn-move-up").addClass("disable");
		$(".question-panel:first").find(".btn-move-up").parents(".question-panel").siblings().find(".btn-move-up").removeClass("disable");
		$(".question-panel:last").find(".btn-move-down").addClass("disable");
		$(".question-panel:last").find(".btn-move-down").parents(".question-panel").siblings().find(".btn-move-down").removeClass("disable");
		sort_title(0)
	})

	// 删除题目
	$("body").on("click", ".btn-question-del", function () {
		let formdata={}
		formdata['id']=$(this).parents(".question-panel").attr('data-id')
		formdata['action']='question_del'
		layer.open({
			content: '是否删除t题目为“'+$(this).parents(".question-panel").find('.editable').text()+'”这道题',
			btn: ['是', '否'],
			yes: function (index) {
				post_ajax_formdata('question_del',formdata)
				if ($(".question-panel").length<=0){
					$(".question_null_tip").show();
					sort_title(0)			
				}
				layer.close(index)
				window.location.reload()				
			}
		})
	})

	// 设置
	// $("body").on("click", ".show_settings_btn", function () {
	// 	layer.open({
	// 		shadeClose: false,
	// 		type: 1,
	// 		content: setting(),
	// 		anim: 'up',
	// 		style: 'position:fixed; left:0;overflow: auto; top:0; width:100%; height:100%; border: none;background-color: #f8f8f8; -webkit-animation-duration: .5s; animation-duration: .5s;',
	// 		success: function (elem) {
	// 			new_scollTop = scollTop;
	// 			$(".box").addClass("on").css({ "top": -scollTop, });
	// 			index__ = $(elem).attr("index");
	// 		}
	// 	});
	// })

	// 取消设置
	$("body").on("click", ".setCancel-btn", function () {
		layer.close(index__);
		$(".box").removeClass("on");
		$(window).scrollTop(new_scollTop);
	})

	//发布
	$('.publish_btn').on("click",function(){
		let title = $('#title').val()
		let published = $(this).attr('data-published')
		let audit = $(this).attr('data-audit')
		layer.open({
			content: '是否发布《'+title+'》问卷？',
			btn: ['是', '否'],
			yes: function (index) {
				let formdata = {"id": ques_id, 'action': 'audit'}
				if(audit==0){
					$.post('user_research_list_mobile', formdata, function (data){} , 'json')
				}
				formdata = {"id": ques_id, 'action': 'published'}
				$.post('user_research_list_mobile', formdata, function (data) {
					console.log(data)
					if(data.errcode==0){
						layer.open({
							content: '发布《'+title+'》问卷成功',
							time:1
						})
						window.location.href=' user_research_list_mobile'
					}
					
					
				}, 'json')
                layer.close(index)
            }
        })
	})

	//结束语选择
	$('#end-select').on('click',function(){
		$('#end-word').val($(this).find('option:selected').attr('conclusion'))
	})
	$('.question-type-box .list_title_type .title-item').on('click',function(){	
		let index=$('.question-type-box .list_title_type .title-item').index(this)
		$.each($('.question-type-box .list_title_type .title-item'),function(){
			if($('.question-type-box .list_title_type .title-item').index(this)==index){
				$(this).addClass('title-item-active')
			}else{
				$(this).removeClass('title-item-active')
			}
		})
		$.each($('.question-type-box .t_type'),function(){
			if($('.question-type-box .t_type').index(this)==index){
				$(this).removeClass('hide')
			}else{
				$(this).addClass('hide')
			}
		})
	})

	//题目设置限制值
	$("body").on("change", ".cascade-limit-start", function () {//修改初始值（select）--暂时不用
		let index = $(this).val()
		$('.cascade-limit-level option').remove();
		let init='';
		for(let i=index;i<cascade_limit_level_list.length;i++){
			init+=`<option value=`+i+`>`+cascade_limit_level_list[i]+`</option> `
		}
		$('.cascade-limit-level').append(init)
	})
	$("body").on("change", ".cascade-limit-type", function () {//修改极值--暂时不用
		let index = $(this).val()
		if(index==99){
			$('.cascade-limit-type-input').removeClass('hide')
		}else{
			$('.cascade-limit-type-input').addBatch('hide')
		}
	})
	$("body").on("click", ".balank-size .bs_btn", function () {//修改填空题输入框高度--无此功能
		$('.balank-size .weui-btn_primary').addClass('weui-btn_default')
		$('.balank-size .weui-btn_primary').removeClass('weui-btn_primary')
		$(this).removeClass('weui-btn_default')
		$(this).addClass('weui-btn_primary')
		let height = ['35px','70px','105px']
		$('.question-list .fill-input').css('height',height[$(this).attr('data-id')])
	})
	$("body").on("change", ".scoring-limit-grade", function () {//修改打分图片数量
		let length = $(this).val()
		let index = $('.scoring-style').find('input').val()
		console.log(index)
		update_scoring_item(length,index)
	})
	$("body").on("change", "#must", function () {//修改必选题值
		let index =$(this).val()
		if(index==1){
			$(this).val(0)
		}else{
			$(this).val(1)
		}
	})
	$("body").on("change",'.digest', function () {//图片选择题---备注说明
		$(this).parents('.question-item').find('.weui-textarea').eq(0).attr('options-digest',$(this).val())
	})

	$("body").on("click",'.scoring-style', function () {//显示打分图标类型模态框
		layer.open({
			shadeClose: false,
			type: 1,
			content: show_limit_scoring_style_model($(this).find('input').val()),
			anim: 'up',
			style: 'position:fixed; left:0;overflow: auto; bottom:0; width:100%; height:auto; border: none;background-color: #f8f8f8; -webkit-animation-duration: .5s; animation-duration: .5s;',
			success: function (elem) {
				scoring_style_model_ = $(elem).attr("index");
			}
		});		
	})
	$("body").on("click",'.scoring_style_close_btn', function (){//关闭打分图标类型模态框
		layer.close(scoring_style_model_);
	 })
	 $("body").on("click",'.score_style_item_btn', function (){//修改打分图标类型
		 let index = $(this).find('input').val()
		 let length = $('.scoring-limit-grade').val()
		$('#question_setting .scoring-style span').remove()
		$('#question_setting .scoring-style input').remove()
		let init =`<span class="`+score_style_color[index]+` evaluation-icon"><i class="fa `+score_style[index]+`"></i></span>
				<span class="star evaluation-icon"><i class="fa `+score_style[index]+`"></i></span>
				<span class="star evaluation-icon"><i class="fa `+score_style[index]+`"></i></span>
				<input type="hidden" name="style" value=`+index+`>`
		$('#question_setting .scoring-style').append(init)
		update_scoring_item(length,index)
	 })

	//修改最多最少选项
	$("body").on("change", "#limit_max,#limit_min", function () {
		$(this).val($(this).find('option:selected').val())
	})

	//动态修改 
	$("#title").on("change", function () {//修改问卷名称
		let data ={}
		data['id'] = ques_id
		data['title'] = $(this).val()
		post_ajax_formdata('title',data)
	})
	$("#description").on("change", function () {//修改问卷名称
		let data ={}
		data['id'] = ques_id
		data['description'] = $(this).val()
		post_ajax_formdata('description',data)
	})
	$("#end-word").on("change", function () {//修改问卷结束语
		let end_select =$('#end-select').val()
		let data ={},end_select_type=['normal_end','early_closure']
		data['id'] = ques_id
		data[end_select_type[end_select]] = $(this).val()
		post_ajax_formdata(end_select_type[end_select],data)
	})
	//保存题目
	$("body").on("click", ".preserve-btn", function () {
		let formdata=get_json_formdata(type)
		formdata['options'] = get_formdata_options(formdata['options_title[]'])
		formdata['topic'] = get_formdata_topic(formdata)
		formdata['act'] = 'edit'
		console.log(formdata);
		save_question(formdata)
	})



	//问卷设置
	$('.show_settings_btn').on("click", function () {
		$('.setting-from-edit').removeClass('hide')
	})
	$('.setting-from-edit .cancel-btn').on("click", function () {//关闭问卷设置	
		$('.setting-from-edit').addClass('hide')
	})
	$('.setting-from-edit .weui-navbar .weui-navbar__item').on("click", function () {//显示问卷设置
		$('.setting-from-edit .weui-navbar .active').removeClass('active')
		$('.setting-from-edit .weui-form  .active').removeClass('active')
		$('.setting-from-edit .weui-form  .items').eq($(this).index()).addClass('active')	
		$(this).addClass('active')
	})

	//修改问卷设置
	$('.setting-from-edit .items .weui-switch').on("change", function () {//开关类	
		let action = $(this).attr('id')
		let name = $(this).attr('name')
		let data ={}
		data['id'] = ques_id

		if(action == 'authority'){		
			let list =new Array()
			$.each($('.'+action+'_group input'),function(){
				if($(this).is(':checked')){				
					list.push(parseInt($(this).val()))	
				}
			})
			if($(this).val()==0){
				$(this).val(1)
				$('.'+action+'_group').removeClass('hide')
				if(list.length!=0){
					data[name] = list
				}else{
					data =undefined
				}				
			}else{
				$(this).val(0)
				$('.'+action+'_group').addClass('hide')
				if(list.length!=0){
					data[name] = 'all'
				}else{
					data =undefined
				}	
			}
		
		}else if(action =='wx_freq'|| action =='dev_freq'|| action =='ip_freq'){
			
			if($(this).val()==0){
				$(this).val(1)
				$('.'+action+'_group').removeClass('hide')
				
				// data['freq'] = parseInt($('.'+action+'_group .weui-input').val())
				// if(data['freq']!=0){
				// 	data['time_type'] = $('.'+action+'_group .weui-select').val()
				// }
				data=undefined
			}else{
				$(this).val(0)
				$('.'+action+'_group').addClass('hide')
				if($('.'+action+'_group .weui-input').val()==0){
					data = undefined
					
				}else{
					data['freq'] = '0'
					$('.'+action+'_group .weui-input').val(0)
				}
				
				
			}
		}else if(action == 'password'){
			if($(this).val()==0){
				$(this).val(1)
				$('.'+action+'_group').removeClass('hide')
			
				if($('.'+action+'_group .weui-input').val()!=''){
					data['password'] = $('.'+action+'_group .weui-input').val()
				}else{
					data= undefined
					$('.'+action+'_group .weui-input').val('')
				}
			}else{
				$(this).val(0)
				$('.'+action+'_group').addClass('hide')
				//data['password'] = ''
				data= undefined
			}		
		}else if(action =='deadline'){
			if($(this).val()==0){
				$(this).val(1)
				$('.'+action+'_group').removeClass('hide')
			}else{
				$(this).val(0)
				$('.'+action+'_group').addClass('hide')
			}
			data['limited_time'] = $(this).val()			
		}else if(action == 'wx_remind'){
			data = undefined
		}
		else{
			if($(this).val()==0){
				$(this).val(1)
				$('.'+action+'_group').removeClass('hide')
			}else{
				$(this).val(0)
				$('.'+action+'_group').addClass('hide')
			}
			data[name] = $(this).val()
		}
		if(!action){
			data = undefined
		}
		
		if(data!=undefined){
			post_ajax_formdata(action,data)
		}	
	})
	$('.setting-from-edit .items .weui-select').on('change',function(){//下拉选择类
		let action = $(this).attr('data-action')
		let name = $(this).attr('name')
		if(action){
			let data ={}
			data['id'] = ques_id
			if(name == 'time_type'){
				data[name] = $(this).val()
				data['freq'] =  $(this).parents('.weui-cell').find('.weui-input').val()
			}else{
				data[action] =$(this).val()
			}
			post_ajax_formdata(action,data)
		}
	})
	$('.setting-from-edit .items .weui-input').on('change',function(){//文本输入类
		let action = $(this).attr('data-action')
		let name = $(this).attr('name')
		if(action){
			let data ={}
			data['id'] = ques_id
			if(name == 'freq'){
				data[name] = $(this).val()
				data['time_type'] =  $(this).parents('.weui-cell').find('.weui-select').val()
			}else if(name == 'start' ||name =='end'){
				data['limited_time'] =1
				data['start'] = $('#start').parents('.weui-cell').find('.weui-input').val()		
				data['end'] = $('#end').parents('.weui-cell').find('.weui-input').val()	
			}else{
				data[name] = $(this).val()
			}
			post_ajax_formdata(action,data)
		}
	})
	$('.setting-from-edit .items .weui-check').on('change',function(){//单选按钮类
		let action = $(this).attr('name')
		let id =  $(this).attr('id')
		let data ={}
		data['id'] = ques_id
		if(action == 'authority'){
			let list =new Array()
			$.each($('.'+action+'_group input'),function(){
				if($(this).is(':checked')){				
					list.push(parseInt($(this).val()))
				}
			})
			if(list.length!=0){
				data['authority[]'] = list
			}			
		}else if(action == 'deadline'){	
			if($(this).val()==0){
				$(this).val(1)
				$('#'+id).parents('.weui-cell').find('.weui-input').val(get_datetime())				
			}else{
				$(this).val(0)		
				$('#'+id).parents('.weui-cell').find('.weui-input').val('')		
			}
			data['limited_time'] =1
			data['start'] = $('#start').parents('.weui-cell').find('.weui-input').val()		
			data['end'] = $('#end').parents('.weui-cell').find('.weui-input').val()		
		}else if(action=='record_info'){
			data[action] = 1
			data['enforce'] = $(this).val()
		}
		else{
			data[action] = $(this).val()
		}
		post_ajax_formdata(action,data)
		
	})

	//预览
	$('.research_btn').on('click',function(){
		
	})
})

///初始化设置
let choice_limit_length = 2//题目选项数量（即最多选几项）
let cascade_limit_type_list = ['满意度','认同度','愿意度','重要度','符合度','感知度','可能性']//评价题类型
let cascade_limit_level_list = ['0级','1级','2级','3级','4级','5级','6级','7级','8级','9级','10级']//量标题级别
let cascade_limit_start = 1 //量标题初始值
let fill_limit_content_list = ['不限','数字','字母','中文','Email','手机号码']//填空题限制输入类型
let horz_blank_limit_type_list = ['下划线','空格框']//横向填空题输入底部类型
let type_text = {//题目类型题目名称
	'single_choice':'单选题',
	'multiple_choice':'多选题',
	'fill':'填空题',
	'picture_selection':'图片选择',
	'picture_voting':'图片投票',
	'written_voting':'文字投票',
	'drop_down':'下拉题',
	'scoring':'打分题',
	'nps_score':'NPS题',
	'sort':'排序题',
	'upload_file':'文件上传',
	'undefined':''
}
let score_style = ['fa-star','fa-heart','fa-thumbs-up','fa-smile-o']//打分题图标样式
let score_style_color=['star','text-danger','text-primay','star']//打分题图标颜色



let get_set_ajax_formdata ={//获取ajax请求cformdata参数
	'title':{
		'id':['id','title'],
		'text':'修改问卷名称'
	},
	'description':{
		'id':['id','description'],
		'text':'修改问卷描述'
	},
	'normal_end':{
		'id':['id','normal_end'],
		'text':'修改问卷结束语(正常完成)'
	},
	'early_closure':{
		'id':['id','early_closure'],
		'text':'修改问卷结束语(提前结束(不计入结果))'
	},
	'question_copy':{
		'id':['id'],
		'text':'复制题型'
	},
	'question_sort':{
		'id':['sort'],
		'text':'修改排序'
	},
	'question_del':{
		'id':['id'],
		'text':'删除题型'
	},
	'ques_title':{
		'id':['id','title'],
		'text':'修改题目'
	},
	'ques_options':{
		'id':['id','act','options'],
		'text':'修改题目选项'
	},
	'setsetion':{
		'id':['id','topic'],
		'text':'修改题目选项'
	},
	'category':{
		'id':['id','category'],
		'text':'修改所属栏目'
	},
	'authority':{
		'id':['id','authority[]'],
		'text':'修改访问限制'
	},
	'wx_freq':{
		'id':['id','freq','time_type'],
		'text':'每个微信答题次数限制'
	},
	'dev_freq':{
		'id':['id','freq','time_type'],
		'text':'每个微信答题次数限制'
	},
	'ip_freq':{
		'id':['id','freq','time_type'],
		'text':'每个IP答题次数限制'
	},
	'password':{
		'id':['id','password'],
		'text':'启动访问密码'
	},
	'deadline':{
		'id':['id','limited_time','start','end'],
		'text':'设置开始/结束时间'
	},
	'only_wx':{
		'id':['id','only_wx'],
		'text':'只有邮件邀请的受访者可回答'
	},
	'record_info':{
		'id':['id','record_info','enforce'],
		'text':'记录微信用户个人信息'
	},
	'only_fans':{
		'id':['id','only_fans'],
		'text':'必须关注我的官方微信才能回答'
	},
	'wx_share':{
		'id':['id','wx_share'],
		'text':'禁止微信右上角分享'
	},
	'wx_remind':{
		'id':['id','wx_remind'],
		'text':'有新数据时，通过微信提醒我'
	}
}

let get_datetime = () =>{//获取当前时间
	let myDate = new Date();
	let m = myDate.getMonth() + 1
	if (m < 10) {
		m = '0' + m
	}
	let d = myDate.getDate()
	if (d < 10) {
		d = '0' + d
	}
	let h = myDate.getHours()
	if (h < 10) {
		h = '0' + h
	}
	let M = myDate.getMinutes()
	if (M < 10) {
		M = '0' + M
	}
	let S = myDate.getSeconds()
	if (S < 10) {
		S = '0' + S
	}

	return myDate.getFullYear() + '-' + m + '-' + d + ' ' + h + ':' + M + ':' + S
}
//重新排序题目
let sort_title = (type=0,id=0) =>{
	let sort_list=[],data={},c_id
	$.each($('.question-panel'),function(){
		$(this).find('.seq').text(($('.question-panel').index(this)+1)+'.')
		sort_list.push(parseInt($(this).attr('data-id')))	
	})
	if(type==1){
		sort_list.push(id)
	}else if(type==2){
		for(let i=0;i<sort_list.length;i++){
			if(sort_list[i]==id){
				sort_list.splice(i+1, 0,id)
				break
			}
		}
	}else{}
	data['sort'] = sort_list
	post_ajax_formdata('question_sort',data)
}

//修改多选题select选择参数
let update_limit_choice_select = (type,length,selected=0)=>{
	let init
	if(type == 'single_choice'|| type == "multiple_choice" || type =="picture_selection" || type =='written_voting' || type =='picture_voting'){
		$('.question-item-select option').remove()
		init=`<option value=0>不限</option>`;
		for(let i=1;i<=length;i++){
			init+=`<option value=`+i+`>`+i+`</option> `
		}
		$('.question-item-select').append(init)
	}
	if(type=='scoring'){
		
	}
}
//修改打分题选项图标类型
let update_scoring_item = (length,index)=>{
	$('.question-list .score-title-wrapper .score-item').remove();
	let init='',size=''
	if(length>8){
		size=`style="font-size:16px"`
	}
	for(let i=0;i<length;i++){
		if(i==0){
			init +=`<div class="score-item `+score_style_color[index]+`" `+size+`>`
		}else{
			init +=`<div class="score-item star" `+size+`>`
		}
		init+=`
		<span class="evaluation-icon"><i class="fa `+score_style[index]+`"></i></span>
		</div> `
	}
	$('.question-list .score-title-wrapper').append(init)
}

//获取json类型的formdata
let get_json_formdata = (type='')=>{
	let data='{',list='[',list_name;
    $.each($('form input,form select,form textarea'),function(i,n){  
      	if(n['name']!=''){	
			if(n['name'].indexOf("[]") != -1){
				list_name = n['name']
				if(list!='['){
					list += ','
				}
				if(type =='sort'||type =='scoring'){
					list +=  '{"title":"'+n['value']+'"'
				}else{
					list +=  '{"name":"'+n['value']+'"'
				}
				
				
				if($(this).attr('options-type')){
					console.log($(this).attr('options-type'));
					list +=',"type":'+$(this).attr('options-type')
				}
				if($(this).attr('options-file')!=undefined){
					list +=',"url":"'+$(this).attr('options-file')+'"'
				}
				if($(this).attr('options-digest')!=undefined){
					list += ',"digest":"'+$(this).attr('options-digest')+'"'
				}
				list+='}'
			}else{
				if(i>1&&data!='{'){
					data += ','
				}	
				data+= '"'+n['name']+'":"'+ n['value']+'"';
			}
	  	}
	})
	list +=']'
	data +=',"'+list_name + '":'+ list
	data+='}';
	
    return JSON.parse(data);
}

let post_ajax_formdata =(action,data)=>{
	let formdata={}
	formdata['action'] =action
	get_set_ajax_formdata[action].id.forEach(el => {
		if(data[el]){
			formdata[el] = data[el]
		}
	});
	console.log(formdata,get_set_ajax_formdata[action].text)
	formdta_ajax(formdata,get_set_ajax_formdata[action].text)
}


let formdta_ajax = (formdata,errtext) =>{//ajax传值
	$.ajax({
		url: '',
		type: "POST",
		dataType: "json",
		data: formdata,
		success: function (data) {
			if (data.errcode != 0) {
				layer.open({
					content: errtext+'失败',	
					time:2			
				});
				formdata_err='修改'+errtext+'失败'
			}
			
		},
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			layer.open({
				content: '网络繁忙，'+errtext+'错误',	
				time:1	
			});
			formdata_err=errtext+'错误'
		},
	})
}
//字符串化题目选项 options
let get_formdata_options = (options) =>{
	
	let init ='{"title":'
	if(options){
		init +='['
		options.forEach((e,i) => {
			if(i>0){
				init +=','
			}
			init += JSON.stringify(e)
		});
		init +=']'
	}else{
		init +='""'
	}

	init+='}'
	console.log(init);
	
	return init
}

//字符串化题目选项 topic
let get_formdata_topic = (data) =>{
	let init ='{'
	if(data['must']!=undefined){
		init +='"must":'+data['must']
	}
	if(data['types']!=undefined){
		init +=',"types":'+data['types']
	}else{
		init +=',"types":0'
	}
	if(data['limit_long_min']!=undefined&&data['limit_long_max']!=undefined){
		init +=',"limit_long":{"max":'+data['limit_long_max']+',"min":'+data['limit_long_min']+'}'
	}else{
		init +=',"limit_long":{"min":0,"max":1}'
	}
	if(data['limit_min']!=undefined&&data['limit_max']!=undefined){
		init +=',"limit_select":{"max":'+data['limit_max']+',"min":'+data['limit_min']+'}'
	}else{
		init +=',"limit_select":{"min":0,"max":1}'
	}
	if(data['limit_content']!=undefined){
		init +=',"limit_content":'+data['limit_content']
	}else{
		init +=',"limit_content":0'
	}
	if(data['style']!=undefined){
		init +=',"style":'+data['style']
	}
	if(data['length']!=undefined){
		init +=',"length":'+data['length']
	}
	if(data['left']!=undefined){
		init +=',"left":"'+data['left']+'"'
	}
	if(data['mid']!=undefined){
		init +=',"mid":"'+data['mid']+'"'
	}
	if(data['right']!=undefined){
		init +=',"right":"'+data['right']+'"'
	}
	init +='}'
	return init
}

//ajax向后台传值
let formdata_err=''
let save_question = (data) =>{
	post_ajax_formdata('ques_title',data)
	post_ajax_formdata('ques_options',data)
	post_ajax_formdata('setsetion',data)
	if(formdata_err==''){
		layer.open({
			content: '修改成功',
			time:1	
		});
		location.replace(location.href);
	}else{
		layer.open({
			content: formdata_err,	
			time:1
		});
	}

	
}

// 添加题目
let questionType = function (type,id,title='',options='[{"name": "选项一", "type": 0,"url":"","disgest":""},{"name": "选项二", "type": 0,"url":"","disgest":""}]',topic='{"must":1,"types":1,"limit_long":{"max":0,"min":100},"limit_select":{"max":0,"min":0},"limit_content":0}',edit=0){
	if(edit==0){
		options =JSON.parse(options.replace(/'/g, '"'))  
		topic =JSON.parse(topic.replace(/'/g, '"'))  		
	}
	choice_limit_length = options['title'].length
	console.log(options)
	console.log(topic)
	let checked='';
	if(topic.must==1){
		checked ='checked'
	}

	let init, footer;
	let item_operation =`
		<div class="question_option_operate">
			<span class="btn-add-single"><i class="fa fa-plus"></i>&nbsp;选项</span>			
		`;
	if (type == "single_choice" || type == "multiple_choice"|| type == 'written_voting' ||type =='drop_down' ||type == 'scoring') {
		item_operation += `<span class="btn-add-more"><i class="fa fa-th"></i>&nbsp;批量添加</span>`;
	}
	if (type == "single_choice" || type == "multiple_choice") {
		item_operation += `<span class="btn-add-other"><i class="fa fa-plus"></i>...&nbsp;其他___</span>`;
	}
	item_operation+=`</div>`;
	let item_select_operation =`
	<div class="question_option_operate">
		<span class="btn-select-single"><i class="fa fa-plus"></i>&nbsp;选项</span>
	</div>
	`;
	let item_limit_voting_operation =`
		<div class="weui-cell  weui-cell_switch ">
			<div class="weui-cell__bd">答题后显示投票结果</div>
			<div class="weui-cell__ft">
				<input class="weui-switch" type="checkbox" >
			</div>
		</div>`
	init=`
		<div class="edit-question-popup">
			<div class="question-type-tab" style="background-color: #fff;border-bottom: 1px solid #ccc;height:45.6px;line-height: 1.5;">
				<span class="cancel-btn questionCancel-btn">取消</span>
				<div class="question-text question-title" style="text-align: center;font-weight: 600;">`+ type_text[type] +`</div>
				<span class="preserve-btn">保存</span>
			</div>
			<form action="" method="post">
				<input type="hidden" name="id" value=`+id+`>
				<input type="hidden" id="type" name="type" value=0>
				<div class="weui-form">
					<div class="weui-form__control-area">
						<div class="weui-cells__group weui-cells__group_form">
							<div class="weui-cells_form bg_white question-list">
								<div class="weui-cell">
									<div class="weui-cell__bd">
										<textarea class="weui-textarea" name="title" id="" rows="2" placeholder="请输入题目名称">`+title+`</textarea>
									</div>
								</div>
								`;
								if (type == "single_choice" || type == "multiple_choice" || type =="fill_more" || type =='written_voting' || type =='drop_down'|| type== 'auto_table'|| type =='sort'){
									options['title'].forEach( (e,i) => {
										if(type =='sort'){
											e.name=e.title
										}
										init += `
											<div class="weui-cell question-item">`
										if(e.type!=1){
											init += `<div class="weui-cell__bd" style="padding-right: 5px;">
														<textarea class="weui-textarea" name="options_title[]" options-type=`+e.type+` rows="1" placeholder="请输入选项">`+e.name+`</textarea>
													</div>`		
										}else{
											init += `<div class="weui-cell__bd" style="padding-right: 5px;">
														<textarea class="weui-textarea" name="options_title[]" options-type=`+e.type+` rows="1" placeholder="请输入选项">`+e.name+`</textarea>
													</div>
													<span class="question-other-input" style="margin-right:145px;"></span>`
										}											
										init +=	`<div class="item-btns" ><span class="btn-item-del"><i class="fa fa-minus-square-o"></i></span></div>
											</div>
										`;
									});								
									init += item_operation;
								}else if(type == "fill"){
									init += `<div class="weui-cell question-item"><div class="input-wrap fill-input"></div></div>`;
								}else if(type =='picture_selection'||type =='picture_voting'){	
									if(options['title']){
										options['title'].forEach( (e,i) => {
											init += `
												<div class="weui-cell question-item">
													<div class="show-item-img `
													if(e.url==''){
														init +='hide'
													}
													init +=`">
														<span class="remove-item-img"><span class="fa fa-remove text-danger"></span> </span>
														<img src=`+e.url+`>
													</div>
													<div class="weui-cell__bd" style="padding-right: 5px;">
														<textarea class="weui-textarea" name="options_title[]" options-type=0 `
														if(e.digest!=''){
															init +=` options-digest=`+e.digest
														}
														if(e.url!=''){
															init +=` options-file=`+e.url
														}
														init +=` rows="1" placeholder="请输入选项">`+e.name+`</textarea>`
											if(type =='picture_voting'){
												init +=`<textarea class="weui-textarea digest"  rows="1" placeholder="选项说明（选填）">`+e.digest+`</textarea>`
											}
											init +=`</div>											
													<div class="item-btns" >
														<input type="file"  class="upload-image hide" >
														<span class="btn-item-upload"><i class="fa fa-image"></i></span>
														<span class="btn-item-del"><i class="fa fa-minus-square-o"></i></span>
													</div>
												</div>
											`;									
										})
									}
									init+=item_operation
								}else if(type =='scale'){
									let jblevel=['一级选项','二级选项','三级选项','四级选项'];
									for (let i = 0; i < 2; i++) {
										init += `<div class="question-select-item">
											<div class="weui-cell question-type-tab">
												<div class="weui-cell__bd" style="padding-right: 5px;">
													<div class="question-text">`+jblevel[i]+`</div>
												</div>
												<span class="btn-select-item-del"><i class="fa fa-minus-square-o"></i></span>
											</div>
											<div class="">							
												<div class="weui-cell__bd" style="padding-right: 5px;">
													<div class="weui-cell"><textarea class="weui-textarea" name="" id="" rows="1" placeholder="请输入`+jblevel[i]+`"></textarea></div>							
											`
										for (let i = 1; i < 3; i++) {
											init += `
												<div class="weui-cell question-item">
													<div class="weui-cell__bd" style="padding-right: 5px;">
														<textarea class="weui-textarea" name="" id="" rows="1" placeholder="请输入子选项"></textarea>
													</div>
													<span class="btn-item-del"><i class="fa fa-minus-square-o"></i></span>
												</div>
											`;
										}	
										init +=	`
										<div class="question_option_operate">
											<span class="btn-add-single"><i class="fa fa-plus"></i>&nbsp;选项</span>
										</div></div></div></div>`																		
									}
									init += item_select_operation
								}else if(type=='classify'){
										init += `
											<div class="question-type-tab" style="height:45.6px;line-height: 1.5;">
												<div class="question-text" style="font-weight: 300;">选项</div>
											</div>`
										for (let i = 1; i < 3; i++) {
											init += `
												<div class="weui-cell question-item">
													<div class="weui-cell__bd" style="padding-right: 5px;">
														<textarea class="weui-textarea" name="" id="" rows="1" placeholder="请输入选项"></textarea>
													</div>
													<span class="btn-item-del"><i class="fa fa-minus-square-o"></i></span>
												</div>
											`;
										}
										init += item_operation;
										init += `<div class="question-type-tab" style="height:45.6px;line-height: 1.5;">
													<div class="question-text" style="font-weight: 300;">类别</div>
												</div>`
										for (let i = 1; i < 3; i++) {
											init += `
												<div class="weui-cell question-item">
													<div class="weui-cell__bd" style="padding-right: 5px;">
														<textarea class="weui-textarea" name="" id="" rows="1" placeholder="请输入选项"></textarea>
													</div>
													<span class="btn-item-del"><i class="fa fa-minus-square-o"></i></span>
												</div>
											`;
										}
										init += item_operation;
								}else if(type == 'horz_blank'){
									init +=`
									<div class="weui-cell question-item">
									<div class="weui-cell__bd" style="padding-right: 5px;">
										<textarea class="weui-textarea" name="" id="" rows="1" placeholder="请输入内容"></textarea>
									</div>
								</div>`
								}else if(type == 'scoring'){
									if(options){
										let size =''
										if(topic.length>8){
											size=`style="font-size:16px"`
										}
										options['title'].forEach( (e,i) => {
											init += `
											<div class="weui-cell question-item">
												<div class="weui-cell__bd text-center" style="padding-right: 5px;">
													<textarea class="weui-textarea" name="options_title[]" rows="1" placeholder="请输入选项" >`+e.title+`</textarea>
													<div class="score-title-wrapper start-items">`
													for(let i=0;i<topic.length;i++){
														if(i==0){
															init+=	`<div class="score-item `+score_style_color[topic.style]+`" `+size+`>`
														}else{
															init+=	`<div class="score-item star" `+size+`>`
														}
														init+=	`<span class="evaluation-icon"><i class="fa `+score_style[topic.style]+`"></i></span>
															</div>`
													}																						
													init+=	`</div>
														</div>
														<div class="item-btns" ><span class="btn-item-del"><i class="fa fa-minus-square-o"></i></span></div>
													</div>
													`;
											})
									}
									init += item_operation;
								}else if(type == 'upload_file'){
									init += `<div class="weui-cell question-item"><div class="input-wrap file-input">
										<div  class=""> <i class="fa fa-cloud-upload"></i> 选择图片30M以内</div>
									</div></div>`;
								}else if(type =='nps_score'){
									init +=`
										<div class="item-box">
											<div class="text-center">
												<span class="score-radius">0</span>
												<span class="score-radius">1</span>
												<span class="score-radius">2</span>
												<span class="score-radius">3</span>
												<span class="score-radius">4</span>
												<span class="score-radius">5</span>
												<span class="score-radius">6</span>
												<span class="score-radius">7</span>
												<span class="score-radius">8</span>
												<span class="score-radius">9</span>
												<span class="score-radius">10</span>
											</div>
											<div class="score-title-wrapper text-default">
												<div class="score-title scale-title-left">0分：不可能</div>
												<div class="score-title scale-title-right">10分：极有可能</div>
											</div>
										</div>`
								}
					init += `</div>`;
					if(type=="matrix" || type=="matrix_choice"){
						init += `
							<div class="weui-cells_form bg_white question-list">
								<div class="question-type-tab" style="height:45.6px;line-height: 1.5;"><div class="question-text" style="font-weight: 300;">矩形选项(横轴)</div></div>`;
						for (let i = 1; i < 3; i++) {
							init += `
								<div class="weui-cell question-item">
									<div class="weui-cell__bd" style="padding-right: 5px;">
										<textarea class="weui-textarea" name="" id="" rows="1" placeholder="请输入选项"></textarea>
									</div>
									<span class="btn-item-del"><i class="fa fa-minus-square-o"></i></span>
								</div>
							`;
						}
						init += item_operation;
						init+=`</div>`;
						init += `
							<div class="weui-cells_form bg_white question-list">
								<div class="question-type-tab" style="height:45.6px;line-height: 1.5;"><div class="question-text" style="font-weight: 300;">矩形选项(竖轴)</div></div>`;
						for (let i = 1; i < 3; i++) {
							init += `
								<div class="weui-cell question-item">
									<div class="weui-cell__bd" style="padding-right: 5px;">
										<textarea class="weui-textarea" name="" id="" rows="1" placeholder="请输入选项"></textarea>
									</div>
									<span class="btn-item-del"><i class="fa fa-minus-square-o"></i></span>
								</div>
							`;
						}
						init += item_operation;
						init += `</div>`;
					}
					init +=`
							<div class="setting-main">
								<div class="question-type-tab" style="height:45.6px;line-height: 1.5;"><div class="question-text" style="font-weight: 300;">题目设置</div></div>
								<div class="weui-cells_form bg_white" id="question_setting">
									<div class="weui-cell weui-cell_switch">
										<div class="weui-cell__bd">此题必答</div>
										<div class="weui-cell__ft">
											<input class="weui-switch" id="must" name="must" value=`+topic.must+` type="checkbox" `+checked+`>
										</div>
									</div>
								`;
								if(type == "single_choice"){
									init+=`<input class="weui-switch hide" name="types" value="1" type="checkbox">`
								}
								else if( type=="multiple_choice"|| type== 'written_voting'){
									init += `
										<div class="weui-cell  weui-cell_switch questio-type-set">
											<div class="weui-cell__bd">此题可多选</div>
											<div class="weui-cell__ft">
												<input class="weui-switch" type="checkbox" name="types" value=`+topic.types
												if(topic.types==1){
													init += ` checked`
												}
												init+=`>
											</div>
										</div>
										<div class="weui-cell  set-least-range" `
										if(topic.types==0){
											init +=`style="display:none"`
										}
										init+=`>
											<div class="weui-cell__bd">最少选择项数</div>
											<div class="weui-cell__bd ">
												<select class="question-item-select" name="limit_min" id="limit_min"  value=`+topic.limit_select.min+`>
												<option value="0" ` 
												if(topic.limit_select.min==0){
													init +=`selected`
												}
												init +=`>不限</option>`
									for (let i = 1; i <= choice_limit_length; i++) {
										init += `<option value=`+i 	
										if(topic.limit_select.min==i){
											init +=` selected`
										}
										init +=`>`+i+`</option> `
									}
											
									init +=  `</select>	</div>
											<div class="weui-cell__ft"></div>
										</div>
										<div class="weui-cell  set-most-range" `
										if(topic.types==0){
											init +=`style="display:none"`
										}
										init+=`>
											<div class="weui-cell__bd">最多选择项数</div>
											<div class="weui-cell__bd ">
												<select class="question-item-select" name="limit_max" id="limit_max" value=`+topic.limit_select.max+`>
												<option value="0" `
												if(topic.limit_select.max==0){
													init +=`selected`
												}
												init +=`>不限</option>`
									for (let i = 1; i <= choice_limit_length; i++) {
										init += `<option value=`+i
										if(topic.limit_select.max==i){
											init +=` selected`
										}
										init +=`>`+i+`</option> `
									}
									init +=  `</select>	</div>
											<div class="weui-cell__ft"></div>
										</div>`
									if(type== 'written_voting'){
										init += item_limit_voting_operation
									}
									init += `<div class="weui-cell  hide">
											<div class="weui-cell__bd">跳题设置</div>
											<span class="question-item-select">未设置&nbsp;</span>
											<div class="weui-cell__ft"></div>
										</div>
									`;
								}else if(type == 'cascade'){
									init += `
										<div class="weui-cell  ">
											<div class="weui-cell__bd">极值标签</div>
											<div class="weui-cell__bd ">
											<select class="question-item-select cascade-limit-type">`
									for (let i = 0; i < cascade_limit_type_list.length; i++) {
										init += `<option value=`+i+`>`+cascade_limit_type_list[i]+`</option> `
									}
									init += `<option value="99">自定义</option></select></div>
												<div class="weui-cell__bd cascade-limit-type-input hide">
														<input type="text" class="weui-textarea" placeholder="请输入内容" maxlength=5>
														<input type="text" class="weui-textarea" placeholder="请输入内容" maxlength=5>
												</div>
											<div class="weui-cell__ft"></div>
										</div>
										<div class="weui-cell  ">
											<div class="weui-cell__bd">量级设置</div>
											<div class="weui-cell__bd ">
											<select class="question-item-select cascade-limit-level">`
									for (let i = cascade_limit_start; i < cascade_limit_level_list.length; i++) {
										init += `<option value=`+i+`>`+cascade_limit_level_list[i]+`</option> `
									}
									init +=	`</select></div>
											<div class="weui-cell__ft"></div>
										</div>
										<div class="weui-cell  ">
											<div class="weui-cell__bd">初始值</div>
											<div class="weui-cell__bd ">
											<select class="question-item-select cascade-limit-start">`
									for (let i = 0; i < cascade_limit_level_list.length; i++) {
										init += `<option value=`+i+`>`+i+`</option> `
									}	
									init +=	`</select></div>
											<div class="weui-cell__ft"></div>
										</div>
									`;
								}else if(type == "fill"){
									init += `
									<div class="weui-cell  hide">
										<div class="weui-cell__bd">输入框大小</div>
										<div class="weui-cell__bd ">
											<div class="balank-size pull_right">
												<div class="weui-btn weui-btn_mini weui-btn_primary bs_btn" data-id="0">小</div> 
												<div class="weui-btn weui-btn_mini weui-btn_default bs_btn" data-id="1">中 </div> 
												<div class="weui-btn weui-btn_mini weui-btn_default bs_btn" data-id="2">大 </div>
											</div>
										</div>									
									</div>
									<div class="weui-cell ">
										<div class="weui-cell__bd">最少填写</div>
										<div class="weui-cell__bd ">
											<input class="weui-textarea" name="limit_long_min" type="number" value=`+topic.limit_long.min+` oninput="if(value>9999)value=9999;if(value<0)value=0"> 
										</div>	
										<div>字</div>							
									</div>
									<div class="weui-cell">
										<div class="weui-cell__bd">最多填写</div>
										<div class="weui-cell__bd ">
											<input class="weui-textarea" name="limit_long_max" type="number" value=`+topic.limit_long.max+` oninput="if(value>9999)value=9999;if(value<0)value=0">
										</div>
										<div>字</div>											
									</div>
									<div class="weui-cell">
										<div class="weui-cell__bd">内容限制</div>
										<div class="weui-cell__bd">
										<select class="question-item-select fill-limit-type" name="limit_content" >`
									for (let i = 0; i < fill_limit_content_list.length; i++) {
										init += `<option value=`+i
										if(topic.limit_content==i){
											init += ` selected`
										}
										init +=`>`+fill_limit_content_list[i]+`</option> `
									}
									init += `</select></div>
											<div class="weui-cell__ft"></div>
										</div>
									</div>`
								}else if(type == 'horz_blank'){
									init += `
									<div class="weui-cell  ">
										<div class="weui-cell__bd">内容限制</div>
										<div class="weui-cell__bd weui-cell_access">
										<select class="question-item-select  cascade-limit-type">`
									for (let i = 0; i < horz_blank_limit_type_list.length; i++) {
										init += `<option value=`+i+`>`+horz_blank_limit_type_list[i]+`</option> `
									}
									init += `</select></div>`
								}else if(type == 'picture_selection'||type =='picture_voting'){
									init += `									
										<div class="weui-cell  weui-cell_switch questio-type-set">
											<div class="weui-cell__bd">此题可多选</div>
											<div class="weui-cell__ft">
												<input class="weui-switch" type="checkbox" name="types" value=`+topic.types
												if(topic.types==1){
													init += ` checked`
												}
												init+=`>
											</div>
										</div>
										<div class="weui-cell  set-least-range" `
										if(topic.types==0){
											init +=`style="display:none"`
										}
										init+=`>
											<div class="weui-cell__bd">最少选择项数</div>
											<div class="weui-cell__bd weui-cell_access">
												<select class="question-item-select" name="limit_min" id="limit_min"  value=`+topic.limit_select.min+`>
												<option value="0" ` 
												if(topic.limit_select.min==0){
													init +=`selected`
												}
												init +=`>不限</option>`
												for (let i = 1; i <= choice_limit_length; i++) {
													init += `<option value=`+i
													if(topic.limit_select.min==i){
														init +=` selected`
													}
													init +=`>`+i+`</option> `
												}
											
											init +=  `</select>	</div>
													<div class="weui-cell__ft"></div>
												</div>
												<div class="weui-cell set-least-range"`
												if(topic.types==0){
													init +=`style="display:none"`
												}
												init+=`>
													<div class="weui-cell__bd">最多选择项数</div>
													<div class="weui-cell__bd weui-cell_access">
													<select class="question-item-select" name="limit_max" id="limit_max" value=`+topic.limit_select.max+`>
													<option value="0" `
													if(topic.limit_select.max==0){
														init +=`selected`
													}
													init +=`>不限</option>`
											for (let i = 1; i <= choice_limit_length; i++) {
												init += `<option value=`+i
												if(topic.limit_select.max==i){
													init +=` selected`
												}
												init +=`>`+i+`</option> `
											}
											init +=  `</select>	</div>
													<div class="weui-cell__ft"></div>
												</div>
												`
											if(type =='picture_voting'){
												init += item_limit_voting_operation
											}
											
											init +=	`<div class="weui-cell  hide">
													<div class="weui-cell__bd">跳题设置</div>
													<span class="question-item-select">未设置&nbsp;</span>
													<div class="weui-cell__ft"></div>
												</div>
									`;
								}else if(type == 'scoring'){
									init += `
									<input type="hidden" name="types" value=1>
									<div class="weui-cell">
										<div class="weui-cell__bd">显示样式</div>
										<div class="weui-cell__bd ">											
												<div class="score-title-wrapper scoring-style">
													<span class="`+score_style_color[topic.style]+` evaluation-icon"><i class="fa `+score_style[topic.style]+`"></i></span>
													<span class="star evaluation-icon"><i class="fa `+score_style[topic.style]+`"></i></span>
													<span class="star evaluation-icon"><i class="fa `+score_style[topic.style]+`"></i></span>
													<input type="hidden"  name="style" id="style" value=`+topic.style+`>
												</div>
										</div>
									</div>
									<div class="weui-cell">
										<div class="weui-cell__bd">最高分</div>
										<div class="weui-cell__bd">
										<select class="question-item-select scoring-limit-grade" name="length" value=`+topic.length+`>`
										for(let i = 2; i <= 10; i++) {
											init += `<option value=`+i
											if(i==topic.length){
												init+=` selected`
											}
											init+=`>`+i+`</option> `
										}
									init +=  `</select>	</div></div>
										<div class="weui-cell">
											<div class="weui-cell__hd hide">提示文字</div>
											<div class="weui-cell__bd ">
												<div class="tips_left">左间文字</div><div class="tips_right"><input class="weui-textarea" type="text" name="left" value=`+topic.left+`  maxlength=10 placeholder="左边文字" style="padding: 5px;"></div>
												<div class="tips_left">中间文字</div><div class="tips_right"><input class="weui-textarea" type="text"name="mid"  value=`+topic.mid+`  maxlength=10 placeholder="中间文字" style="padding: 5px;"></div>
												<div class="tips_left">右间文字</div><div class="tips_right"><input class="weui-textarea" type="text"name="right" value=`+topic.right+`  maxlength=10 placeholder="右边文字" style="padding: 5px;"></div>
											</div>
										</div>
									`
								}else if(type == 'upload_file'){
									init += `<div class="weui-cell  ">
									<div class="weui-cell__bd">最大上传文件数量</div>
									<div class="weui-cell__bd">
									<select class="question-item-select upload_file-limit-num">`
									for (let i = 1; i <= 10; i++) {
										init += `<option value=`+i+`>`+i+`</option> `
									}
									init +=  `</select>	</div></div>`
								}else if(type == 'sort'){
									init +=`
									<div class="weui-cell weui-cell_switch">
										<div class="weui-cell__bd">选项随机排列</div>
										<div class="weui-cell__ft">
											<input class="weui-switch" type="checkbox">
										</div>
									</div>`
								}
							init += `
								</div>
							</div>
						</div>
					</div>
				</div>
			</form>
		</div>
	`;
	footer = `
		<div id="footer-copyright" class="weui-footer" style="margin-bottom:5px;">
			<p class="weui-footer__text">Processed in 0.09705 second(s), 17 queries</p>
			<p class="weui-footer__links"><a class="weui-footer__link" href="http://www.kgcms.com/static/copyright/kgcms.html" target="_blank">软件著作权登记号：2019SR1217715</a></p>
			<p class="weui-footer__links"><a class="weui-footer__link" href="http://www.kgcms.com" target="_blank">广东凯格科技有限公司</a></p>
		</div>
	`;
	return init + footer;
}

// 添加该题目内的子项
let additem =  (this_, type, title,) =>{
	let init = `<div class="weui-cell question-item">`
		if(type == 'picture_selection'||type =='picture_voting'){
			init +=	`
				<div class="show-item-img hide">
					<span class="remove-item-img"><span class="fa fa-remove text-danger"></span> </span>
					<img>
				</div>`
		}
		init +=	`<div class="weui-cell__bd text-center" style="padding-right: 5px;">
				<textarea class="weui-textarea" name="options_title[]" options-type="0" options-file options-digest rows="1" placeholder="请输入选项">`+ title +`</textarea>`
		if(type =='picture_voting'){
			init +=`<textarea class="weui-textarea digest"  rows="1" placeholder="选项说明（选填）"></textarea>`
		}
		if(type =='scoring'){
			let index =$('.scoring-style input').val(),size
			let length=$('.scoring-limit-grade').val()
			if(length>8){
				size=`style="font-size:16px"`
			}
			init +=`
				<div class="score-title-wrapper start-items">`
				for (let i = 0; i < length; i++) {
					if(i==0){
						init += `<div class="score-item `+score_style_color[index]+`" `+size+`>`
					}else{
						init += `<div class="score-item star" `+size+`>`
					}
					init += `
						<span class="evaluation-icon"><i class="fa `+score_style[index]+`"></i></span>
					</div>`
				}	
			init +=`</div>`
		}
		init +=	`</div><div class="item-btns">`
		if(type == 'picture_selection'||type =='picture_voting'){
			init +=	`
			<input type="file" class="upload-image hide">
			<span class="btn-item-upload"><i class="fa fa-image"></i></span>`
		}
		init +=	`<span class="btn-item-del"><i class="fa fa-minus-square-o"></i></span>
		</div></div>
	`;
	return init;
}
let addselectitem =  (this_, index=1, title)=> {
	let jblevel=['一级选项','二级选项','三级选项','四级选项'];
	let init='';
	init += `<div class="question-select-item">
		<div class="weui-cell question-type-tab">
			<div class="weui-cell__bd" style="padding-right: 5px;">
				<div class="weui-textarea">`+jblevel[index]+`</div>
			</div>
			<span class="btn-select-item-del"><i class="fa fa-minus-square-o"></i></span>
		</div>
		<div class="">							
			<div class="weui-cell__bd" style="padding-right: 5px;">
				<div class="weui-cell"><textarea class="weui-textarea" name="" id="" rows="1" placeholder="请输入选项"></textarea></div>							
		`
	for (let i = 1; i < 3; i++) {
		init += `
			<div class="weui-cell question-item">
				<div class="weui-cell__bd" style="padding-right: 5px;">
					<textarea class="weui-textarea" name="" id="" rows="1" placeholder="请输入选项"></textarea>
				</div>
				<span class="btn-item-del"><i class="fa fa-minus-square-o"></i></span>
			</div>
		`;
	}
	init += `
	<div class="question_option_operate">
		<span class="btn-add-single"><i class="fa fa-plus"></i>&nbsp;选项</span>
	</div>
	</div></div></div>`																		
	return init;
}

// 批量添加
let addBatch = function (type, id, matrix) {
	let init = `
		<div class="more-show" style="padding:15px;" data-more-question="" id="" data-matrix="">
			<p style="color:#484848;font-size: 14px;margin-bottom:15px;">每行代表一个选项，可以添加多个选项</p>
			<textarea rows="5" placeholder="选项1&#xA选项2" style="width: 100%;border-radius: 2px;padding: 5px 10px;font-size:16px;"></textarea>
		</div>
	`;
	return init;
}

//其他选项
let initOtherChoice = function () {
	let  init;
	init = `
		<div class="weui-cell question-item">
			<div class="weui-cell__bd" style="padding-right: 5px;">
				<textarea class="weui-textarea" name="options_title[]"  options-type=1 id="" rows="1" placeholder="请输入选项">其他</textarea>
			</div>
			<span class="question-other-input" style="margin-right:145px;"></span>
			<span class="btn-item-del"><i class="fa fa-minus-square-o"></i></span>
		</div>
	`;
	return init;
}

// 设置
let setting =function(){
	let init=`
		<div class="setting-from-edit" id="share_right_nav">
			<div class="question-type-tab" style="background-color: #fff;border-bottom: 1px solid #ccc;height:45.6px;line-height: 1.5;">
				<span class="cancel-btn setCancel-btn">取消</span>
				<div class="question-text question-title" style="text-align: center;font-weight: 600;padding-right: 32px;">设置</div>
			</div>
			<form action="" method="post">
				<div class="weui-form">
					<div class="weui-form__control-area">
						<div class="weui-cells__group weui-cells__group_form">
							<div class="items">
								<div class="weui-cells__title">问卷设置</div>
								<div class="weui-cells_form bg_white">
									<div class="weui-cell weui-cell_switch">
										<div class="weui-cell__bd">所属栏目</div>
										<div class="weui-cell__ft">
											<input class="weui-switch" type="checkbox" checked="checked">
										</div>
									</div>
									<div class="weui-cell weui-cell_switch">
										<div class="weui-cell__bd">访问限制</div>
										<div class="weui-cell__ft">
											<input class="weui-switch" type="checkbox" checked="checked">
										</div>
									</div>
									<div class="weui-cell weui-cell_switch">
										<div class="weui-cell__bd">记录微信用户昵称、性别、城市信息</div>
										<div class="weui-cell__ft">
											<input class="weui-switch" type="checkbox" checked="checked">
										</div>
									</div>
								</div>
							</div>
							<div class="items">
								<div class="weui-cells__title">微信设置</div>
								<div class="weui-cells_form bg_white">
									<div class="weui-cell weui-cell_switch">
										<div class="weui-cell__bd">每个微信号只能答一次</div>
										<div class="weui-cell__ft">
											<input class="weui-switch" type="checkbox" checked="checked">
										</div>
									</div>
									<div class="weui-cell weui-cell_switch">
										<div class="weui-cell__bd">只能通过微信答题</div>
										<div class="weui-cell__ft">
											<input class="weui-switch" type="checkbox" checked="checked">
										</div>
									</div>
									<div class="weui-cell weui-cell_switch">
										<div class="weui-cell__bd">记录微信用户昵称、性别、城市信息</div>
										<div class="weui-cell__ft">
											<input class="weui-switch" type="checkbox" checked="checked">
										</div>
									</div>
								</div>
							</div>
							<div class="items">
								<div class="question-type-tab" style="height:45.6px;line-height: 1.5;"><div class="question-text" style="font-weight: 300;">答题限制</div></div>
								<div class="weui-cells_form bg_white">
									<div class="weui-cell weui-cell_switch">
										<div class="weui-cell__bd">每台手机或电脑只能答一次</div>
										<div class="weui-cell__ft">
											<input class="weui-switch" type="checkbox" checked="checked">
										</div>
									</div>
									<div class="weui-cell weui-cell_switch">
										<div class="weui-cell__bd">访问密码正确才能答题</div>
										<div class="weui-cell__ft">
											<input class="weui-switch" type="checkbox" checked="checked">
										</div>
									</div>
									<div class="weui-cell question-item">
										<div class="weui-cell__hd"><label class="weui-label" style="font-weight: 500;padding-right: 15px;width:auto;color:#000;">密码</label></div>
										<div class="weui-cell__bd" style="padding-right: 5px;">
											<textarea class="weui-textarea" name="" id="" rows="1" placeholder="请填写密码"></textarea>
										</div>
									</div>
									<div class="weui-cell weui-cell_switch">
										<div class="weui-cell__bd">只有在规定时间段才能答题</div>
										<div class="weui-cell__ft">
											<input class="weui-switch" type="checkbox" checked="checked">
										</div>
									</div>
									<div class="weui-cell question-item">
										<div class="weui-cell__hd"><label class="weui-label" style="font-weight: 500;padding-right: 15px;width:auto;color:#000;">开始时间</label></div>
										<div class="weui-cell__bd" style="padding-right: 5px;">
											<input type="datetime-local" class="weui-input" id="" placeholder="请选择开始日期" value="" name="datePickerStart">
										</div>
									</div>
									<div class="weui-cell question-item">
										<div class="weui-cell__hd"><label class="weui-label" style="font-weight: 500;padding-right: 15px;width:auto;color:#000;">结束时间</label></div>
										<div class="weui-cell__bd" style="padding-right: 5px;">
											<input type="datetime-local" class="weui-input" id="" placeholder="请选择开始日期" value="" name="datePickerStart">
										</div>
									</div>
									<div class="weui-cell weui-cell_switch">
										<div class="weui-cell__bd">设定答题数据上限</div>
										<div class="weui-cell__ft">
											<input class="weui-switch" type="checkbox" checked="checked">
										</div>
									</div>
									<div class="weui-cell question-item">
										<div class="weui-cell__hd"><label class="weui-label" style="font-weight: 500;padding-right: 15px;width:auto;color:#000;">收集份数</label></div>
										<div class="weui-cell__bd" style="padding-right: 5px;">
											<textarea class="weui-textarea" name="" id="" rows="1" placeholder="请填写份数"></textarea>
										</div>
									</div>
								</div>
							</div>
							<div class="items">
								<div class="question-type-tab" style="height:45.6px;line-height: 1.5;"><div class="question-text" style="font-weight: 300;">其他设置</div></div>
								<div class="weui-cells_form bg_white">
									<div class="weui-cell weui-cell_switch">
										<div class="weui-cell__bd">显示题目序号</div>
										<div class="weui-cell__ft">
											<input class="weui-switch" type="checkbox" checked="checked">
										</div>
									</div>
									<div class="weui-cell weui-cell_switch">
										<div class="weui-cell__bd">允许答题者查看结果</div>
										<div class="weui-cell__ft">
											<input class="weui-switch" type="checkbox" checked="checked">
										</div>
									</div>
									<div class="weui-cell weui-cell_switch">
										<div class="weui-cell__bd">保存上次填写内容</div>
										<div class="weui-cell__ft">
											<input class="weui-switch" type="checkbox" checked="checked">
										</div>
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>
			</form>
		</div>
	`;
	return init;
}

//创建题目
// let initQuestion = function (type, ques_id) {}

//打分题模态框
let show_limit_scoring_style_model = (index=0)=>{
	let init='';

	init += `<div class=" text-center">
				<div class="text-primay scoring_style_close_btn" style="padding:10px;font-size:16px">关闭</div> 
			</div>
			<div class="weui-cells weui-cells_radio text-center" style="margin-top:0">`
		for(let i=0;i<score_style.length;i++){
		init += `
			<label class="weui-cell weui-cell_active weui-check__label score_style_item_btn" for="score_style`+i+`">
				<div class="weui-cell__bd">
					<div class="score-title-wrapper" style="width: 90px;float: left;margin: 0px 65px;">
						<span class="`+score_style_color[i]+` evaluation-icon"><i class="fa `+score_style[i]+`"></i></span>
						<span class="star evaluation-icon"><i class="fa `+score_style[i]+`"></i></span>
						<span class="star evaluation-icon"><i class="fa `+score_style[i]+`"></i></span>
					</div>
				</div>
				<div class="weui-cell__ft">
					<input type="radio" class="weui-check" name="style1" id="score_style`+i+`" value=`+i
					if(i==index){
						init +=` checked="checked"`
					}
					init +=`>
					<span class="weui-icon-checked"></span>
				</div>
			</label>`
		}				
	init += `</div>`		
	return init
}

//模拟触屏
let imitate_mousemve = (classname) =>{
	let touch=0,start_Y,end_Y
	$(classname).on('touchstart',function(e){
		start_Y =  e.touches[0].pageY;      
	})
	$(classname).on('touchend',function(e){
		end_Y =   e.changedTouches[0].pageY; 
		touch += (end_Y-start_Y)*30
		if(touch<0){
			$(this).css('margin-top',touch)
		}else{
			touch=0
			$(this).css('margin-top',touch)
		}
		
		})
}