/**
 * API参数列表插件
 * modified by HsuChan 2015-04-17
 */
$(function() {
    	
	//---------------------------------------------------------新增参数begin-----------------------------------------------------------
	//新增参数
	var oTable = $('#tb_parameter').dataTable({
		"bLengthChange": false,//是否开启分页功能
		"bInfo": null,//去掉记录数信息Showing 1 to 5 of 6 entries
       	"bPaginate" : false,//是否分页
		"bFilter" : false,//关闭搜索
		"bSort" : false,//是否开启列排序 ,默认为true
		"bProcessing": false,//当datatable获取数据时候是否显示正在处理提示信息
		"oLanguage": {"sEmptyTable": "请添加参数..."},
		"bAutoWidth":true //列的宽度会根据table的宽度自适应 
		
    });
	
	//行索引
	var lineNum="0";
	//行索引初始化
	var rowIndex = $("#rowIndex").val();
	if(rowIndex != "-1"){//-1表示当前操作在新增界面， 非-1则表示编辑界面
		lineNum = rowIndex*1;
	}
    //新增一行
    var nEditing = null;
    $('#addNewParameter').click(function (e) {
    	
    	//alert($("#apiParameterRequiredOrNotMap").val());
    	var apiParameterType = $("#apiParameterTypeMap").val();
    	var apiParameterRequiredOrNot = $("#apiParameterRequiredOrNotMap").val();
        e.preventDefault();
        var aiNew = oTable.fnAddData(['', '', '', '','','','<a class="delete" data-mode="new" href="">delete</a>']);
        var nRow = oTable.fnGetNodes(aiNew[0]);
        //editRow(oTable, nRow);
        var aData = oTable.fnGetData(nRow);
        var jqTds = $('>td', nRow);
        jqTds[0].innerHTML = '<input name="parameters['+lineNum+'].parameterName" placeholder="请输入参数名称" type="text" class="inpt" />';
        jqTds[1].innerHTML = '<select class="my_option" name="parameters['+lineNum+'].parameterType">'+getOptions(apiParameterType);
        jqTds[2].innerHTML = '<select class="my_option" name="parameters['+lineNum+'].isNoMandatory">'+getOptions(apiParameterRequiredOrNot);
        jqTds[3].innerHTML = '<input name="parameters['+lineNum+'].defaultValue" type="text" class="inpt" />';
        jqTds[4].innerHTML = '<input name="parameters['+lineNum+'].sampleValue" type="text" class="inpt" />';
        jqTds[5].innerHTML = '<input name="parameters['+lineNum+'].description" type="text" class="inpt" />';
        jqTds[6].innerHTML = '<a class="delete" data-mode="new" href="">delete</a>';
        $("#tb_parameter").css({ width:"100%"});
        nEditing = nRow;
        //行计数加一
        lineNum++;
    });
   
  
    //删除一行
    //$('#tb_parameter a.delete').live('click', function (e) {
   $(document).on('click', '#tb_parameter a.delete', function (e) {
        e.preventDefault();
        if (confirm("确认删除?删除请别忘记提交！") == false) {
            return;
        }
        var nRow = $(this).parents('tr')[0];
        oTable.fnDeleteRow(nRow);
        oTable.fnDraw();//re-draw the table
        //alert("Deleted! Do not forget to do some ajax to sync with backend :)");
        $("#tb_parameter").css({ width:"100%"});
    });
    
   //---------------------------------------------------------新增参数end-----------------------------------------------------------
   
   //---------------------------------------------------------新增返回字段 begin-----------------------------------------------------------
   /**
    * 新增返回字段
    */
       
    var oTableFiled = $('#tb_resultField').dataTable({
	   
		"bLengthChange": false,//是否开启分页功能
		"bInfo": null,//去掉记录数信息Showing 1 to 5 of 6 entries
		"bPaginate" : false,//是否分页
		"bFilter" : false,//关闭搜索
		"bSort" : false,//是否开启列排序 ,默认为true
		"bProcessing": false,//当datatable获取数据时候是否显示正在处理提示信息
		"oLanguage": {"sEmptyTable": "请添加返回字段..."},
		"bAutoWidth":true //列的宽度会根据table的宽度自适应 
		
    });

   
    	//行索引
    	var fieldRowNum="0";
		//行索引初始化
		var filedRowIndex = $("#filedRowIndex").val();
		if(filedRowIndex != "-1"){//-1表示当前操作在新增页面，否则表示在修改页面
			fieldRowNum = filedRowIndex*1;
		}
		//新增一行
		var nEditingFiled = null;
		$('#addNewResultField').click(function (e) {
			//api参数类型和api查询返回结果字段类型公用--修改时请注意
			var returFieldTypeMap = $("#apiParameterTypeMap").val();
			e.preventDefault();
			var aiNew = oTableFiled.fnAddData(['', '', '', '<a class="delete" data-mode="new" href="">delete</a>']);
			var nRow = oTableFiled.fnGetNodes(aiNew[0]);
			//editRow(oTable, nRow);
			var aData = oTableFiled.fnGetData(nRow);
			var jqTds = $('>td', nRow);
			jqTds[0].innerHTML = '<input name="returnFields['+fieldRowNum+'].returnValueName" placeholder="请输入返回字段名称" type="text" class="inpt" />';
			jqTds[1].innerHTML = '<select class="my_option" name="returnFields['+fieldRowNum+'].fieldType">'+getOptions(returFieldTypeMap);
			jqTds[2].innerHTML = '<input name="returnFields['+fieldRowNum+'].fieldDesc" type="text" class="inpt" />';
			jqTds[3].innerHTML = '<a class="delete" data-mode="new" href="">delete</a>';
			$("#tb_resultField").css({ width:"100%"});
			nEditingFiled = nRow;
			//行计数加一
			fieldRowNum++;
	   });
	  
	 
	   //删除一行
	  $(document).on('click', '#tb_resultField a.delete', function (e) {
	       e.preventDefault();
	       if (confirm("确认删除?删除请别忘记提交！") == false) {
	           return;
	       }
	       var nRow = $(this).parents('tr')[0];
	       oTableFiled.fnDeleteRow(nRow);
	       oTableFiled.fnDraw();//re-draw the table
	       //alert("Deleted! Do not forget to do some ajax to sync with backend :)");
	       $("#tb_resultField").css({ width:"100%"});
	   });
	//---------------------------------------------------------新增返回字段 end-----------------------------------------------------------
	//---------------------------------------------------------公用-----------------------------------------------------------
    //动态select框生成
    function getOptions(options){
    	var opt= new Array();   
    	opt = options.substring(options.indexOf('{')+1,options.indexOf('}')).split(",");
    	var optionHtml = "";
    	for(var i=0;i<opt.length;i++){
    		var map = opt[i].split("=");
    		optionHtml+='<option value="'+map[0]+'">'+map[1]+'</option>';
    	}
    	return optionHtml+'</select>';
    }
});