/**
 * route.js 
 * 路线管理界面
 * emial: yaoqiang@chinausky.com
 * create: 2018-12-08
 */
Ext.Loader.setConfig({
    enabled: true
});
Ext.Loader.setPath('Ext.ux', '../../res/extjs/examples/ux');
Ext.require([
    'Ext.grid.*',
    'Ext.data.*',
    'Ext.ux.RowExpander',
    'Ext.selection.CheckboxModel'
]);
var company_code;
var myStore=null;
var V_LOGINNAME='',V_PASSWORD='';
var sm = Ext.create('Ext.selection.CheckboxModel');
Ext.define('Route',{
	extend: 'Ext.data.Model',
	fields: [
		{name:'id',  type:'string'},
		{name:'route',  type:'string'},
		{name:'route_name',  type:'string'},
//		{name:'xjzq',  type:'string'},
		{name:'company_code1',  type:'string'},
	]
});
function exportbtn_click(){
	baseUrl = document.getElementById('basePath').value;
	V_LOGINNAME = $("#V_LOGINNAME").val();
	V_PASSWORD = $("#V_PASSWORD").val();
	route_name = document.getElementById('route_name').value;
	route = document.getElementById('route').value;
	var fields = '';
	var array = ['id','route','route_name'];
	var name = ['编号','路线','路线名称'];
	for(var i=0;i<3;i++){
		if(i>0)
			fields += ',';
	    fields += '{id:"'+array[i]+'",title:"'+name[i]+'",shown:"'+true+'"}';
	}
			var query = new Object();
			// query.V_LOGINNAME = V_LOGINNAME;
			// query.V_PASSWORD = V_PASSWORD;
			query.route_name = route_name;
			query.route = route;
			query.EXPORT_FILE = '巡检路线列表';
			query.fields = '['+fields+']';
			$.ajax({
		        type:'POST',
		        url: baseUrl+"iot/excel/view/routeExcel",
		        data: {
		        	queryJson : Ext.JSON.encode(query)
		    	 },
		    	 success: function(result){
		    		
			            var json = eval('(' + result + ')');
			            if(json.action=='dormExport'){
			                ConfirmStore = json.RESULT;
			               
			                var elemIF = document.createElement("iframe");
							elemIF.src = baseUrl+json.filename;
							elemIF.style.display = "none"; 
							document.body.appendChild(elemIF);
			            }
			    
			        }
		    });
			
}
Ext.onReady(function(){  
	var baseUrl = document.getElementById('basePath').value;
	var theme = document.getElementById('theme').value;
	var chkIcon = '
'
	var unchkIcon = '
'
//	company_code = document.getElementById('company_code').value;
	$("#V_LOGINNAME").val(sessionStorage.getItem('V_LOGINNAME'));
	$("#V_PASSWORD").val(sessionStorage.getItem('V_PASSWORD'));
	V_LOGINNAME = $("#V_LOGINNAME").val();
    V_PASSWORD = $("#V_PASSWORD").val();
	var countPerPage = 20;
	var maxHeight = 10000;
	
	var chkBoolean = function(flag) {
		if(flag)
			return chkIcon;
		return unchkIcon;
	}
	var queryJson = new Object();
	// queryJson.V_LOGINNAME = V_LOGINNAME;
	// queryJson.V_PASSWORD =  V_PASSWORD;
//	queryJson.COMMSTATUS = 'NO';
	if((company_code!=null)&&(company_code!='null')&&(company_code.length>0))
		queryJson.company_code = company_code;
	
	var clear_append_form = function (){
		Ext.getCmp('fa_route').setValue("");
		Ext.getCmp('fa_route_name').setValue("");
//		Ext.getCmp('fa_xjzq').setValue("");
//		Ext.getCmp('fa_company_code').setValue("");
		Ext.getCmp('RouteListPanel').getStore().reload();
	}
	
	
	myStore = Ext.create('Ext.data.Store', {
	    model: 'Route',
	    pageSize: countPerPage, 
	    proxy: {
	        type: 'ajax',
	        actionMethods: {
                create : 'POST',
                read   : 'POST', // by default GET
                update : 'POST',
                destroy: 'POST'
	        },
	        url: baseUrl+'iot/route/getList',
	        reader: {
	            type: 'json',
	            root: 'RESULT',
	            totalProperty: 'totalCount'
	        },
	        extraParams:{
	        	queryJson:Ext.JSON.encode(queryJson)
	        }
	    },
//	    sorters:[{
//	    	property:'data_time',
//	    	direction:'DESC'
//	    }],
	    remoteSort: true
	});
	Ext.define('PatrolPoint',{
		extend: 'Ext.data.Model',
		fields: [
			{name:'id',  type:'string'},
			{name:'xj_name',  type:'string'},
		]
	});
	
	myStore1 = Ext.create('Ext.data.Store', {
	    model: 'PatrolPoint',
	    pageSize: countPerPage, 
	    proxy: {
	        type: 'ajax',
	        actionMethods: {
                create : 'POST',
                read   : 'POST', // by default GET
                update : 'POST',
				destroy: 'POST'
	        },
	        url: baseUrl+'iot/patrolpoint/getList',
	        reader: {
	            type: 'json',
	            root: 'RESULT',
	            totalProperty: 'totalCount'
	        },
	        extraParams:{
	        	queryJson:Ext.JSON.encode(queryJson)
	        }
	    },
//	    sorters:[{
//	    	property:'data_time',
//	    	direction:'DESC'
//	    }],
	    remoteSort: true
	});
		
//	var myStore = Ext.create('Ext.data.Store', {
//	    model: 'Route',
//	    pageSize: countPerPage, 
//	    proxy: {
//	        type: 'ajax',
//	        url: baseUrl+'iot/route/getList',
//	        reader: {
//	            type: 'json',
//	            root: 'RESULT',
//	            totalProperty: 'totalCount'
//	        }
//	    },
//	    remoteSort: true
//	});
	
	
	var AppendForm = Ext.create('Ext.form.Panel', {
		id: 'RouteAppendForm',
		labelWidth: 55,
		url: baseUrl+'iot/route/append',
		defaultType: 'textfield',
		bodyPadding: 15,
		items: [{
			fieldLabel:'路线名称',
			id: 'fa_route_name',
			name:'route_name',
			maxLength:20,
			maxLengthText:'长度不得超出{0}',
			anchor:'75%'
		}
//		,{
//			fieldLabel:'路线',
//			id: 'fa_route',
//			name: 'route',
//			maxLength: 250,
//			maxLengthText:'长度不得超出{0}',
//			anchor:'95%'
//		}
		,{
			fieldLabel:'路线',
			id:'fa_route',
			name:'route',
			xtype:'combo',
			multiSelect: true,
			store:myStore1,
			displayField : 'xj_name',  
	        valueField : 'id',
//	        value : document.getElementById('id1').value,
	        editable : false
		}
//		,{
//			id: 'fm_company_code',
//			name:'company_code',
//			hidden:true
//		}
		,{
			id:'fa_queryJson',
			name:'queryJson',
			hidden:true
		}],
		buttons: [{
	        text: '提交',
	        iconCls:'ok_btn',
		    handler: function() {
		        var form = this.up('form').getForm();
		        if (form.isValid()) {
		        	var query = new Object();
		        	// query.V_LOGINNAME = V_LOGINNAME;
		        	query.route_name = Ext.getCmp('fa_route_name').getValue();
//		        	var a,b,c; 
		        	a = Ext.getCmp('fa_route').getValue();
		        	c = a.join(','); 
//		        	query.route = Ext.getCmp('fa_route').getValue();
		        	query.route = c;
//		        	query.xjzq = Ext.getCmp('fa_xjzq').getValue();
//		        	query.company_code = Ext.getCmp('fa_company_code').getValue();
		        	var jsonstr = Ext.JSON.encode(query);
		        	Ext.getCmp('fa_queryJson').setValue(jsonstr);
		            form.submit({
		            	method:'post',
		                success: function(form, action) {
		                   Ext.Msg.alert('操作成功', '已保存', function(btn,txt){
		                	   clear_append_form();
		                   });
		                },
		                failure: function(form, action) {
		                   Ext.Msg.alert('操作失败', action.Msg , function(btn,txt){
		                	   clear_append_form();
		                   });
		                }
		            });
		        }
		    }
		},{
			text: '关闭',
			iconCls: 'cancel_btn',
			handler: function() {
				this.up('window').hide();
			}
		}]
	});
	
	var appendwin = function(){
		Ext.create('Ext.window.Window', {
		    title: '新增',
		    height: 350,
		    width: 400,
		    layout: 'fit',
		    items:  AppendForm,
		    closeAction: 'hide'
		}).show();
	}
	
	//筛选
	var clear_filter_form = function(){
		Ext.getCmp('ff_route_name').setValue("");
		Ext.getCmp('ff_route').setValue("");
//		Ext.getCmp('ff_xjzq').setValue("");
		Ext.getCmp('ff_company_code').setValue("");
	}
	
	var FilterWin = Ext.create('Ext.form.Panel', {
		id: 'RouteFilterForm',
		labelWidth: 55,
		defaultType: 'textfield',
		bodyPadding: 15,
		items: [{
			fieldLabel:'路线名称',
			id: 'ff_route_name',
			name:'route_name',
			maxLength:20,
			maxLengthText:'长度不得超出{0}',
			value : document.getElementById('route_name').value,
			anchor:'75%'
		},{
			fieldLabel:'路线',
			id:'ff_route',
			name:'route',
			maxLength: 250,
			maxLengthText:'长度不得超出{0}',
	        value : document.getElementById('route_name').value,
	        editable : false
		},{
			id:'ff_queryJson',
			name:'queryJson',
			hidden:true
		}],
		buttons: [{
	        text: '提交',
	        iconCls:'ok_btn',
		    handler: function() {
		        var form = this.up('form').getForm();
		        if (form.isValid()) {
		        	var query = new Object();
//		        	if(Ext.getCmp('company_code').getValue().length>0){
//		        		query.company_code = company_code;
//		        		company_code = query.company_code;
//		        	}else{
//		        		company_code = null;
//		        	}
// 		        	query.V_LOGINNAME = V_LOGINNAME;
// 	        		query.V_PASSWORD =  V_PASSWORD;
		        	if(Ext.getCmp('ff_route_name').getValue().length>0)
		        		query.route_name = Ext.getCmp('ff_route_name').getValue();
		        	    $("#route_name").val(query.route_name);
		        	if(Ext.getCmp('ff_route').getValue().length>0)
		        		query.route = Ext.getCmp('ff_route').getValue();
		        	    $("#route").val(query.route);
//		        	if(Ext.getCmp('ff_xjzq').getValue().length>0)
//		        		query.xjzq = Ext.getCmp('ff_xjzq').getValue();
		        	
		        	var jsonstr = Ext.JSON.encode(query);
		        	myStore.getProxy().extraParams = {
		        		queryJson : jsonstr
		        	};	
		        	Ext.getCmp('RouteListPageToolbar').moveFirst();
		        	Ext.getCmp('route_name').setValue(query.route_name);
		        	Ext.getCmp('route').setValue(query.route);
//		        	Ext.getCmp('xjzq').setValue(query.xjzq);
//		        	Ext.getCmp('company_code').setValue(query.company_code);
//		        	myStore.reload();
		        	clear_filter_form();
		        	this.up('window').hide();
		        }
		    }
		},{
			text: '关闭',
			iconCls: 'cancel_btn',
			handler: function() {
				this.up('window').hide();
			}
		}]
	});
	
	
	
	var filterwin = function() {
		Ext.create('Ext.window.Window',{
			title: '筛选',
			height: 300,
			width: 350,
			layout: 'fit',
			items: FilterWin,
			closeAction: 'hide'
		}).show();
	}
	
	var clear_modify_form = function (){
		Ext.getCmp('fm_id').setValue("");
		Ext.getCmp('fm_route_name').setValue("");
		Ext.getCmp('fm_route').setValue("");
		Ext.getCmp('fm_company_code').setValue("");
//		Ext.getCmp('fm_xjzq').setValue("");	
		Ext.getCmp('RouteListPanel').getStore().reload();
	}
	
	
	
	var ModifyForm = Ext.create('Ext.form.Panel', {
		id: 'RouteEditForm',
		labelWidth: 55,
		url: baseUrl+'iot/route/update',
		defaultType: 'textfield',
		bodyPadding: 15,
		items: [{
				fieldLabel:'路线名称',
				id: 'fm_route_name',
				name:'route_name',
				maxLength:20,
				maxLengthText:'长度不得超出{0}',
				anchor:'75%'
			},{
				fieldLabel:'路线',
				id: 'fm_route',
				name: 'route',
				maxLength: 250,
				maxLengthText:'长度不得超出{0}',
				anchor:'95%'
			},
			{
				id: 'fm_id',
				name:'id',
				hidden:true
			},
			{
				id: 'fm_company_code',
				name:'company_code1',
				hidden:true
		},
		{
			id:'fm_queryJson',
			name:'queryJson',
			hidden:true
		}],
		buttons: [{
	        text: '修改',
	        iconCls:'ok_btn',
		    handler: function() {
		    	var form = this.up('form').getForm();
		        if (form.isValid()) {
		        	var query = new Object();
		        	query.id = Ext.getCmp('fm_id').getValue();
		        	query.route_name = Ext.getCmp('fm_route_name').getValue();
		        	query.route = Ext.getCmp('fm_route').getValue();
		        	query.company_code = Ext.getCmp('fm_company_code').getValue();
		        	var jsonstr = Ext.JSON.encode(query);
		        	Ext.getCmp('fm_queryJson').setValue(jsonstr);
		            form.submit({
		            	method:'post',
		                success: function(form, action) {
		                   Ext.Msg.alert('操作成功', '已修改', function(btn,txt){
		                	   clear_modify_form();
		                   });
		                },
		                failure: function(form, action) {
		                   Ext.Msg.alert('操作失败', action.Msg , function(btn,txt){
		                	   clear_modify_form();
		                   });
		                }
		            });
		            this.up('window').hide();
		        }
		    }
		},{
			text: '关闭',
			iconCls: 'cancel_btn',
			handler: function() {
				this.up('window').hide();
			}
		}]
	});
	
	var modifywin = function(){
		Ext.create('Ext.window.Window', {
		    title: '修改',
		    height: 350,
		    width: 400,
		    layout: 'fit',
		    items:  ModifyForm,
		    closeAction: 'hide'
		}).show();
	}
		
	var chk_sm = function() {
		if(sm.getCount()==1){
			modifywin();
			var selected = sm.getSelection( );
			Ext.getCmp('fm_id').setValue(selected[0].raw.id);
			Ext.getCmp('fm_route_name').setValue(selected[0].raw.route_name);
			Ext.getCmp('fm_route').setValue(selected[0].raw.route);
			Ext.getCmp('fm_company_code').setValue(selected[0].raw.company_code1);
//			Ext.getCmp('fm_xjzq').setValue(selected[0].raw.xjzq);
		}else{
			Ext.Msg.alert('请先选择','请先选择一条记录,再点击修改');
		}
	}
	
//	
//	var deletewin = function() {
//		window.alert("无此功能");
//		
//	}
	
	Ext.create('Ext.grid.Panel', {
	    title: '路线管理',
	    id: 'RouteListPanel',
	    store: myStore,
	    selModel: sm,
	    columns: [
	    	 //{ header: 'ID',  dataIndex: 'UQ_POINT_ID',hidden:true, menuDisabled:true },
	    	{ header: '序号',  dataIndex: 'id', width:37, menuDisabled:true },
		     { header: '路线名称', dataIndex: 'route_name', width:700,align:'center', menuDisabled:true },
		     { header: '路线', dataIndex: 'route', width:700,align:'center', menuDisabled:true },
//		     { header: '巡检周期', dataIndex: 'xjzq', width:120,align:'center', menuDisabled:true },
//		     { header: '记录版本', dataIndex: 'I_VERSION', width:120, align:'center', menuDisabled:true },
//		     { 
//	        	xtype : 'actioncolumn',
//	        	width:20,
//	        	sortable:false,
//	        	
//	        	
//	        	//这里
//	        	align:'center',
//	        	menuDisabled:true,
//	        	items:[{
//	        		icon: '../../res/img/common/forum.gif',
//	        		tooltip: '版本记录',
//	        		handler: function(grid, rowIndex, colIndex) {
//	                    var rec = grid.getStore().getAt(rowIndex);
//	        			window.location = 'version.jsp?theme='+theme+'&pointId='+rec.get('UQ_POINT_ID');
//	                }
//	        	}]
//    		}
	    ],
	    columnLines: true,
	    tbar: [
	    	{
				  xtype: 'button', 
				  iconCls:'filter_btn',  
				  text: '筛选',
				  listeners: {
				        click: filterwin
				  }
	    	},'-',
	    	{xtype: 'tbfill'},'-',
			{ 
				  xtype: 'button', 
				  iconCls:'append_btn',  
				  text: '新增',
				  listeners: {
				        click: appendwin
				  }
			},
			{ 
				xtype: 'button', 
				iconCls:'modify_btn', 
				text: '修改',
				listeners: {
					click: chk_sm
				}
			},
//			{ 
//				xtype: 'button',
//				iconCls:'delete_btn',
//				text: '注销',
//				listeners: {
//					click: deletewin
//				}
//			}
			{ 
				xtype: 'button',
				iconCls:'export_btn',
				text: '导出',
				listeners: {
					click: exportbtn_click
				}
			}
			
    	],
	    height: maxHeight,
	    width: '100%',
	    bbar: new Ext.PagingToolbar({  
            store: myStore,  
            id:'RouteListPageToolbar',
            displayInfo: true,  
            pageSize: countPerPage,  
            prependButtons: true,  
            displayMsg : '显示第 {0}条到 {1}条记录,总共 {2}条',     
            emptyMsg : "没有记录",  
            firstText: '第一页',
            prevText: '前一页',
            nextText: '后一页',
            lastText: '最后一页',
            refreshText: '刷新',
//            doRefresh:function(start){  
//            	alert(start);
//                relushDataFunction(start); // 调用函数刷新数据  
//                this.cursor = start; // 更新页签  
//            }
        }),
        frame: true,  
        border:false,  
        iconCls: 'icon-grid',
	    renderTo: Ext.getBody()
	});
	myStore.reload();
	maxHeight =  document.documentElement.clientHeight;
	Ext.getCmp('RouteListPanel').setHeight(maxHeight);
	
});