<%@ page contentType="text/html;charset=UTF-8" %>
<script>
$(document).ready(function() {
	$('#basicTable').bootstrapTable({
		  //请求方法
               method: 'post',
               //类型json
               dataType: "json",
               contentType: "application/x-www-form-urlencoded",
               //显示检索按钮
	       showSearch: true,
               //显示刷新按钮
               showRefresh: true,
               //显示切换手机试图按钮
               showToggle: true,
               //显示 内容列下拉框
    	       showColumns: true,
    	       //显示到处按钮
    	       showExport: true,
    	       //显示切换分页按钮
    	       showPaginationSwitch: true,
    	       //显示详情按钮
    	       detailView: true,
    	       	//显示详细内容函数
	           // detailFormatter: "detailFormatter",
    	       //最低显示2行
    	       minimumCountColumns: 2,
               //是否显示行间隔色
               striped: true,
               //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
               cache: false,
               //是否显示分页（*）
               pagination: true,
                //排序方式
               sortOrder: "asc",
               //初始化加载第一页，默认第一页
               pageNumber:1,
               //每页的记录行数（*）
               pageSize: 10,
               //可供选择的每页的行数（*）
               pageList: [10, 25, 50, 100],
               //这个接口需要处理bootstrap table传递的固定参数,并返回特定格式的json数据
               url: "${ctx}/filing/basicInformation/data",
               //默认值为 'limit',传给服务端的参数为：limit, offset, search, sort, order Else
               //queryParamsType:'',
               ////查询参数,每次调用是会带上这个参数，可自定义
               queryParams : function(params) {
               	var searchParam = $("#searchForm").serializeJSON();
               	searchParam.pageNo = params.limit === undefined? "1" :params.offset/params.limit+1;
               	searchParam.pageSize = params.limit === undefined? -1 : params.limit;
               	searchParam.orderBy = params.sort === undefined? "" : params.sort+ " "+  params.order;
                   return searchParam;
               },
               //分页方式：client客户端分页，server服务端分页（*）
               sidePagination: "server",
               contextMenuTrigger:"right",//pc端 按右键弹出菜单
               contextMenuTriggerMobile:"press",//手机端 弹出菜单，click：单击， press：长按。
               contextMenu: '#context-menu',
               onContextMenuItem: function(row, $el){
                   if($el.data("item") == "edit"){
                   		edit(row.id);
                   }else if($el.data("item") == "view"){
                       view(row.id);
                   } else if($el.data("item") == "delete"){
                        jp.confirm('确认要删除该基础信息记录吗？', function(){
                       	jp.loading();
                       	jp.get("${ctx}/filing/basicInformation/delete?id="+row.id, function(data){
                   	  		if(data.success){
                   	  			$('#basicTable').bootstrapTable('refresh');
                   	  			jp.success(data.msg);
                   	  		}else{
                   	  			jp.error(data.msg);
                   	  		}
                   	  	})

                   	});

                   }
               },
		/*		onPostBody:function(){
					window.rowProps = undefined;
					_table_rowspan("#basicTable" , 1);
					_table_rowspan("#basicTable" , 2);
					_table_rowspan("#basicTable" , 3);
					_table_rowspan("#basicTable" , 4);
					_table_rowspan("#basicTable" , 5);
					return true;
				},*/
               onClickRow: function(row, $el){
               },
               	onShowSearch: function () {
			$("#search-collapse").slideToggle();
		},
               columns: [{
		        checkbox: true

		    }
			,{
		        field: 'projectCode',
		        title: '项目编码',
			    width: 100,
		        sortable: true,
		        sortName: 'projectCode'
		        ,formatter:function(value, row , index){
			   if(value == null || value ==""){
				   value = "-";
			   }
			   <c:choose>
				   <c:when test="${fns:hasPermission('test:filing:basicInformation:edit')}">
				      return "<a href='javascript:edit(\""+row.id+"\")'>"+value+"</a>";
			      </c:when>
				  <c:when test="${fns:hasPermission('test:filing:basicInformation:view')}">
				      return "<a href='javascript:view(\""+row.id+"\")'>"+value+"</a>";
			      </c:when>
				  <c:otherwise>
				      return value;
			      </c:otherwise>
			   </c:choose>

		        }
		    }
			,{
		        field: 'monomerCode',
		        title: '单体编码',
		        sortable: true,
			   width: 100,
		        sortName: 'monomerCode'

		    }
			,{
		        field: 'entryName',
		        title: '项目名称',
					   width: 100,
		        sortable: true,
		        sortName: 'entryName'

		    }
			,{
		        field: 'constructionYear',
		        title: '建设年度1',
					   width: 100,
		        sortable: true,
		        sortName: 'constructionYear'
		    }
			,{
		        field: 'annualInvestment',
		        title: '建设年度1</br>投资',
					   width: 100,
		        sortable: true,
		        sortName: 'annualInvestment'

		    }
				   ,{
					   field: 'constructionYearTwo',
					   title: '建设年度2',
					   width: 100,
					   sortable: true,
					   sortName: 'constructionYearTwo'
				   }
				   ,{
					   field: 'annualInvestmentTwo',
					   title: '建设年度2</br>投资',
					   width: 100,
					   sortable: true,
					   sortName: 'annualInvestmentTwo'

				   }
				   ,{
					   field: 'constructionYearThree',
					   title: '建设年度3',
					   width: 100,
					   sortable: true,
					   sortName: 'constructionYearThree'
				   }
				   ,{
					   field: 'annualInvestmentThree',
					   title: '建设年度3</br>投资',
					   width: 100,
					   sortable: true,
					   sortName: 'annualInvestmentThree'

				   }
				   ,{
					   field: 'constructionYearFour',
					   title: '建设年度4',
					   width: 100,
					   sortable: true,
					   sortName: 'constructionYearFour'
				   }
				   ,{
					   field: 'annualInvestmentFour',
					   title: '建设年度4</br>投资',
					   width: 100,
					   sortable: true,
					   sortName: 'annualInvestmentFour'

				   }
			,{
		        field: 'emphasis',
		        title: '工程属性/重点程度',
			   width: 100,
		        sortable: true,
		        sortName: 'emphasis'

		    }
		     ],
		onExpandRow: function (index, row, $detail) {
			initSubTable(index, row, $detail);
		}
		});

	//初始化子表格(无线循环)
	initSubTable = function (index, row, $detail) {
		var parentid = row.projectCode;
		var cur_table = $detail.html('<table></table>').find('table');
		$(cur_table).bootstrapTable({
			url: '${ctx}/filing/basicInformation/detail',
			method: 'post',
			//类型json
			dataType: "json",
			contentType: "application/x-www-form-urlencoded",
			queryParams : function(params) {
				var searchParam = $("#searchForm").serializeJSON();
				searchParam.pageNo = params.limit === undefined? "1" :params.offset/params.limit+1;
				searchParam.pageSize = params.limit === undefined? -1 : params.limit;
				searchParam.orderBy = params.sort === undefined? "" : params.sort+ " "+  params.order;
				searchParam.projectCode = parentid;
				return searchParam;
			},
			striped: true, //是否显示行间隔色
			pagination: false,//显示分页
			sidePagination: "server",
			pageNumber:1,
			pageSize: 10,
			pageList: [10, 25],
			columns: [{
				width: 63
			},{
				field: 'projectCode',
				title: '',
				width: 100
				,formatter:function(value, row , index){
					if(value == null || value ==""){
						value = "-";
					}
				<c:choose>
					<c:when test="${fns:hasPermission('test:filing:basicInformation:edit')}">
					return "<a href='javascript:editChild(\""+row.id+"\")'>"+value+"</a>";
				</c:when>
				<c:otherwise>
					return value;
				</c:otherwise>
					</c:choose>

				}
			}, {
				field: 'monomerCode',
				title: '',
				width: 100
			}, {
				field: 'entryName',
				title: '',
				width: 100,
			},{
					field: 'constructionYear',
					title: '',
				width: 100,
				}
				,{
					field: 'annualInvestment',
					title: '',
					width: 100,
				}
				,{
					field: 'constructionYearTwo',
					title: '',
					width: 100,
				}
				,{
					field: 'annualInvestmentTwo',
					title: '',
					width: 100,
				}
				,{
					field: 'constructionYearThree',
					title: '',
					width: 100,
				}
				,{
					field: 'annualInvestmentThree',
					title: '',
					width: 100,
				}
				,{
					field: 'constructionYearFour',
					title: '',
					width: 100,
				}
				,{
					field: 'annualInvestmentFour',
					title: '',
					width: 100,
				}
				,{
					field: 'emphasis',
					title: '',
					width: 100,
				}]
		});
	};

		  
	  if(navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)){//如果是移动端

		 
		  $('#basicTable').bootstrapTable("toggleView");
		}
	  
	  $('#basicTable').on('check.bs.table uncheck.bs.table load-success.bs.table ' +
                'check-all.bs.table uncheck-all.bs.table', function () {
            $('#remove').prop('disabled', ! $('#basicTable').bootstrapTable('getSelections').length);
            $('#view,#edit').prop('disabled', $('#basicTable').bootstrapTable('getSelections').length!=1);
        });
		  
		$("#btnImport").click(function(){
			jp.open({
			    type: 2,
                area: [500, 200],
                auto: true,
			    title:"导入数据",
			    content: "${ctx}/tag/importExcel" ,
			    btn: ['下载模板','确定', '关闭'],
				btn1: function(index, layero){
					  jp.downloadFile('${ctx}/filing/basicInformation/import/template');
				  },
			    btn2: function(index, layero){
						var iframeWin = layero.find('iframe')[0]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
						iframeWin.contentWindow.importExcel('${ctx}/filing/basicInformation/import', function (data) {
							if(data.success){
								jp.success(data.msg);
								refresh();
							}else{
								jp.error(data.msg);
							}
                            jp.close(index);
                        });//调用保存事件
                    return false;
				  },
				 
				  btn3: function(index){ 
					  jp.close(index);
	    	       }
			}); 
		});
	  $("#export").click(function(){//导出Excel文件
			jp.downloadFile('${ctx}/filing/basicInformation/export');
	  });
	  $("#search").click("click", function() {// 绑定查询按扭
		  $('#basicTable').bootstrapTable('refresh');
		});

	  $("#open").click(function () {
		  $("#basicTable").bootstrapTable('expandAllRows');
	  })

		$("#close").click(function (row) {
			$('#basicTable').bootstrapTable('refresh');
		})

	 $("#reset").click("click", function() {// 绑定查询按扭
		  $("#searchForm  input").val("");
		  $("#searchForm  select").val("");
		   $("#searchForm  .select-item").html("");
		  $('#basicTable').bootstrapTable('refresh');
		});
		
				$('#beginInDate').datetimepicker({
					 format: "YYYY-MM-DD HH:mm:ss"
				});
				$('#endInDate').datetimepicker({
					 format: "YYYY-MM-DD HH:mm:ss"
				});
		
	});
		
  function getIdSelections() {
        return $.map($("#basicTable").bootstrapTable('getSelections'), function (row) {
            return row.id
        });
    }
  
  function deleteAll(){

		jp.confirm('确认要删除该基础信息记录吗？', function(){
			jp.loading();  	
			jp.get("${ctx}/filing/basicInformation/deleteAll?ids=" + getIdSelections(), function(data){
         	  		if(data.success){
         	  			$('#basicTable').bootstrapTable('refresh');
         	  			jp.success(data.msg);
         	  		}else{
         	  			jp.error(data.msg);
         	  		}
         	  	})
          	   
		})
  }
  
    //刷新列表
  function refresh() {
      $('#basicTable').bootstrapTable('refresh');
  }
  function add(){
	  jp.openSaveDialog('新增基础信息', "${ctx}/filing/basicInformation/form",'1000px', '800px');
  }
  
   function edit(id){//没有权限时，不显示确定按钮
       if(id == undefined){
	      id = getIdSelections();
	}
	jp.openSaveDialog('编辑基础信息', "${ctx}/filing/basicInformation/updateForm?id=" + id, '800px', '500px');
  }

function editChild(id){//没有权限时，不显示确定按钮
	if(id == undefined){
		id = getIdSelections();
	}
	jp.openSaveDialog('编辑基础信息', "${ctx}/filing/basicInformation/childForm?id=" + id, '800px', '500px');
}

  
 function view(id){//没有权限时，不显示确定按钮
      if(id == undefined){
             id = getIdSelections();
      }
        jp.openViewDialog('查看基础信息', "${ctx}/filing/basicInformation/form?id=" + id, '800px', '500px');
 }

function _table_rowspan(_t_id, _t_colnum) {
	//声明合并数组
	var _t_merge_arr = [];
	//声明起始行、合并行数
	var _t_start_index = 0;
	var _t_merge_number = 0;
	//声明临时比较变量
	var _t_compare_value = '';
	//获取数据行数
	var _t_rows = null;
	if($(_t_id).length == 1){
		_t_rows = $(_t_id + " tr td:nth-child(" + _t_colnum + ")");
	} else if($(_t_id).length > 1){
		_t_rows = $(_t_id).eq($(_t_id).length - 1).find("tr td:nth-child(" + _t_colnum + ")");
	} else {
		return;
	}
	_t_rows.each(function(i) {
		var _t_temp_value = $(this).text();
		if (i==0) {
			//首行不比较
			_t_compare_value = _t_temp_value;
			_t_merge_number ++;
		} else {
			//对比本行数据和上行数据是否一样
			if (_t_compare_value == _t_temp_value) {
				_t_merge_number ++;
				if(_t_merge_number > 1){
					//声明合并行大于1的对象
					var _t_merge_object = {index: _t_start_index,rowspan: _t_merge_number};
					//取出合并数组最后一个对象
					var _t_merge_arr_last_object = _t_merge_arr[_t_merge_arr.length - 1];
					if (_t_merge_arr.length > 0) {
						//根据起始下标看是否是同一合并
						if (_t_merge_arr_last_object.index == _t_merge_object.index) {
							_t_merge_arr[_t_merge_arr.length - 1] = _t_merge_object;
						} else {
							_t_merge_arr.push(_t_merge_object);
						}
					} else {
						_t_merge_arr.push(_t_merge_object);
					}
				}
			} else {
				_t_merge_number = 1;
				_t_start_index = i;
				_t_compare_value = _t_temp_value;
			}
		}
	});
	for (var i = 0; i < _t_merge_arr.length; i++) {
		var item = _t_merge_arr[i];
		var rowsnum = item.rowspan;
		for ( var j = 0; j < rowsnum-1; j++) {
			$(_t_rows[item.index+j+1]).css('display','none');
		}
		$(_t_rows[item.index]).attr("rowSpan", item.rowspan);
	}
}
  
  
  
		   
  function detailFormatter(index, row) {
	  var htmltpl =  $("#testDataMain1ChildrenTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
	  var html = Mustache.render(htmltpl, {
			idx:row.id
		});
	  $.get("${ctx}/filing/basicInformation/detail?id="+row.id, function(testDataMain1){
    	var testDataMain1Child1RowIdx = 0, testDataMain1Child1Tpl = $("#testDataMain1Child1Tpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
		var data1 =  testDataMain1.basicChildList;
		for (var i=0; i<data1.length; i++){
			/*data1[i].dict = {};
			data1[i].dict.isHave = jp.getDictLabel(${fns:toJson(fns:getDictList('yes_no'))}, data1[i].isHave, "-");*/
			addRow('#testDataMain1Child-'+row.id+'-1-List', testDataMain1Child1RowIdx, testDataMain1Child1Tpl, data1[i]);
			testDataMain1Child1RowIdx = testDataMain1Child1RowIdx + 1;
		}
      })
        return html;
    }
  
	function addRow(list, idx, tpl, row){
		$(list).append(Mustache.render(tpl, {
			idx: idx, delBtn: true, row: row
		}));
	}
			
</script>
<script type="text/template" id="testDataMain1ChildrenTpl">//<!--
	<div class="tabs-container">
		<div class="tab-content">
				 <div id="tab-{{idx}}-1" class="tab-pane fade in active">
					<table class="ani table">
						<thead>
							<tr>
								<th>项目编码</th>
								<th>单体编码</th>
								<th>项目名称</th>
							</tr>
						</thead>
							<tbody id="testDataMain1Child-{{idx}}-1-List">
							</tbody>
					</table>
		</div>
		</div>//-->
	</script>
	<script type="text/template" id="testDataMain1Child1Tpl">//<!--
				<tr>
					<td>
						{{row.projectCode}}
					</td>
					<td>
						{{row.monomerCode}}
					</td>
					<td>
						{{row.entryName}}
					</td>
				</tr>//-->
	</script>

