//----利润表测算模型----列表页js----
function dateInit(){
//	var start=getDateTime();
//	$(".startdate_input").val(start);
	$(".startdate_input").datetimepicker({
		language:  "zh-CN",
	    weekStart: 1,
	    todayBtn:  1,
	    autoclose: 1,
	    todayHighlight: 1,
	    startView: 3,
	    minView: 3,
	    forceParse: 0,
	    format: "yyyy-mm"
	});
//	var today=getDateTime('today');
//	$(".enddate_input").val(today);
	$(".enddate_input").datetimepicker({
		language:  "zh-CN",
	    weekStart: 1,
	    todayBtn:  1,
	    autoclose: 1,
	    todayHighlight: 1,
	    startView: 3,
	    minView: 3,
	    forceParse: 0,
	    format: "yyyy-mm"
//	    startDate:start,
//	    endDate:"2116-12-31"
	});
}
$(function(){
	dateInit();
	$(".profitNetForecastHeader").delegate(".startdate_input","click",function(){//起始日期点击，限制开始日期范围
		var end=$(".enddate_input").val();
		if(!end){
			end='2116-12-31';
		}
		$('.startdate_input').datetimepicker('setEndDate', end);
	})
	$(".profitNetForecastHeader").delegate(".enddate_input","click",function(){//结束日期点击，限制结束日期范围
		var start=$(".startdate_input").val();
		if(!start){
			start='1970-01-01';
		}
		$('.enddate_input').datetimepicker('setStartDate', start);
	})
	function loadData(){
		$('#profitNetForecastList').setGridParam({
			mtype:'GET',
			url : '/profitNetForecast/doList',
			datatype : 'json',
			postData: {
				'startTime':$('.startdate_input').val(),
				'endTime':$('.enddate_input').val()
			},
			page : 1
		});
		$('#profitNetForecastList').trigger('reloadGrid');
	}
	
	function operationFmatter(cellvalue, options, rowObject){ 
		return $("#hasPermissionProfitNetForecastDiv").html();
	};
	//保存列表页参数	
	var localdata=jQuery.parseJSON(localStorage.getItem("localdata"));
	var startDate='';var endDate='';
	if(localdata){
		startDate=localdata.start;
		endDate=localdata.end;
	}
	sxUtil.jqGrid('#profitNetForecastList',{
		colNames:['id','方案名称','起始年月','创建时间','操作'],
		colModel:[
			{name: 'id', label: 'id', hidedlg:true, hidden:true,sortable:false,title:false},
			{name: 'name', label: '方案名称',sortable:false,align:'left',title:false},
            {name: 'startTime', label: '起始年月',sortable:false,align:'center',title:false},
            {name: 'gmtCreate', label: '创建时间',sortable:false,align:'center',title:false,width:300},
            {name: 'amountUnitName', label: '操作',formatter:operationFmatter,sortable:false,align:'left',width:200,title:false}
		], 
		height:'auto',
		mtype:'GET',
		url : '/profitNetForecast/doList',
		pager : '#profitNetForecastListPage',
		multiselect: false,
		postData : {
			'startTime':startDate,
			'endTime':endDate
		},
		page:mylistpage,
		loadError:function(){
			new $.zui.Messager('列表加载失败', {
			    type: 'warning',
			    time:1500
			}).show();
		},
		onPaging:function(pgButton){
			console.log(pgButton)
		}
	},true);
	//给序号列添加列头
	jQuery('#profitNetForecastList').jqGrid('setLabel','rn', '序号', {  
       	'color' : 'white'  
   	}); 
	//刷新
	$('#refresh').click(function(){
		$('.startdate_input').val(null);$('.enddate_input').val('');
		$('#profitNetForecastList').jqGrid('setGridParam',{ 
            mtype:'GET',
            url : '/profitNetForecast/doList',
            postData: {
            	'startTime':'',
				'endTime':''
			},
            page:1 
        }).trigger('reloadGrid'); //重新载入
	});
	
	//日期选择change事件
	$(".profitNetForecastHeader input").change(function(){
		loadData();
	})
	
	/* 新增按钮点击 */
	$('#profitNetForecastAddBtn').click(function(){
		var localdata={};
		localdata.start=$('.startdate_input').val();
		localdata.end=$('.enddate_input').val();
		mylistpage=$('#profitNetForecastList').getGridParam('page');
		$(".yk_rightcontent").load('/profitNetForecast/add',function(){
			localStorage.setItem('localdata',JSON.stringify(localdata));
		});
	})
	
	/* 查询 */
	$('.profitCalculationQuery').click(function(){
		loadData();
	})
})
/* 查看--不需要保存和刷新列表 */
function profitNetForecastView(t){
	var id = $(t).parents('tr').attr('id');
	var localdata={};
	localdata.start=$('.startdate_input').val();
	localdata.end=$('.enddate_input').val();
	mylistpage=$('#profitNetForecastList').getGridParam('page');
	$(".yk_rightcontent").load('/profitNetForecast/view?id='+id,function(){
		localStorage.setItem('localdata',JSON.stringify(localdata));
	});
}
/* 修改 */
function profitNetForecastEdit(t){
	var id = $(t).parents('tr').attr('id');
	var localdata={};
	localdata.start=$('.startdate_input').val();
	localdata.end=$('.enddate_input').val();
	mylistpage=$('#profitNetForecastList').getGridParam('page');
	$(".yk_rightcontent").load('/profitNetForecast/update?id='+id,function(){
		localStorage.setItem('localdata',JSON.stringify(localdata));
	}); 
}
//删除
function profitNetForecastDelete(t){
	var id = $(t).parents('tr').attr('id');
	if(null!=id){
		bootbox.confirm({
			title : "删除",
			message : '<div style="text-align:center;font-size:15px">您是否确认删除？</div>',
			size : 'small',
			buttons : {
				cancel : {
					label : '<i class="fa fa-times"></i> 关闭'
				},
				confirm : {
					label : '<i class="fa fa-check"></i> 确认'
				}
			},
			callback : function(result) {
				if (result) {
					$.ajax({ 
			              type:'POST',
						  url: '/profitNetForecast/doDelete', 
			              data: {'id':id}, 
			              success: function(msg){ 
			            	  if(msg.success==true){ 
			            		   $('#profitNetForecastList').trigger('reloadGrid'); //重新载入 
			            		   new $.zui.Messager('删除成功', {
					          		    type: 'success',
					          		    time:1500
					          		}).show();
			                   }else{ 
			                	   new $.zui.Messager(e.msg, {
					          		    type: 'danger',
					          		    time:1500
					          		}).show();
			                   } 
			              },
			              error:function(){
			            	  new $.zui.Messager('删除错误', {
			          		    type: 'danger',
			          		    time:1500
			          		}).show();
			              }
			        });
				}
			}
		});	 
	}else{
		new $.zui.Messager('删除错误', {
		    type: 'danger',
		    time:1500
		}).show();
	}
}