/**
 * File: app/cdma/CdmaCell.js
 * Author: liusha
 */

Ext.define('zhwh.cdma.CdmaCell', {
    extend: 'zhwh.cdma.ui.CdmaCell',

    initComponent: function() {
        var me = this;

        me.cellStore = Ext.create('zhwh.cdma.store.CdmaCellJsonStore');

        me.callParent(arguments);

        me.down('button[text="增加基站"]').on('click', me.OnAddCellBtnClick, me);
        me.down('button[text="修改基站"]').on('click', me.OnModifyCellBtnClick, me);
        me.down('button[text="删除基站"]').on('click', me.OnRemoveCellBtnClick, me);
        me.down('button[text="查找"]').on('click', me.OnSearchBtnClick, me);
        me.down('button[text="重置"]').on('click', me.OnResetBtnClick, me);
        me.down('button[text="导出"]').on('click', me.OnExportCellBtnClick, me);

    },

    OnAddCellBtnClick: function(self, e, options) {
    	var me = this;
    	zhwh.user.Rights.hasRights('ZHWH-BGZC-BGSB-1', function() {
	    	Ext.create('zhwh.cdma.CdmaCellWindow', {
	    		url: '../cdma/add_cell',
	    	    grid: self.up('gridpanel')
	    	}).show();
    	});
    },

    OnModifyCellBtnClick: function(self, e, options) {
    	var me = this,
    	    grid = self.up('gridpanel'),
    	    sm = grid.getSelectionModel(),
    	    rows = sm.getSelection();

    	zhwh.user.Rights.hasRights('ZHWH-BGZC-BGSB-2', function() {
	    	if (rows.length > 0) {
	    		var wForm = Ext.create('zhwh.cdma.CdmaCellWindow', {
	    	        title: '修改基站',
	    	        url: '../cdma/modify_cell',
	    	        grid: grid,
	    	        ID: rows[0].get('ID_VIEW')
	    	    });
	    	    wForm.down('form').getForm().load({
	                url: '../cdma/get_cell',
	                params: {
	                	ID: rows[0].get('ID_VIEW')
	                },
	                method: 'get',
	                success: function(form, action) {
	            	    wForm.show();
	                },
	                failure: function(form, action) {
	            	    wForm.destroy();
	            	    Ext.Msg.alert('提示','无法修改！');
	                }
	            });
	    	} else {
	    		Ext.Msg.alert('提示','请选择要修改的基站！');
	    	}
    	});
    },

    OnRemoveCellBtnClick: function(self, e, options) {
    	var me = this,
    	    grid = self.up('gridpanel'),
    	    store = grid.getStore(),
    	    sm = grid.getSelectionModel(),
    	    rows = sm.getSelection();

    	zhwh.user.Rights.hasRights('ZHWH-BGZC-BGSB-3', function() {
	    	if (rows.length > 0) {
	    		Ext.MessageBox.confirm('提示', '确定删除该基站吗？', function(id) {
	            	if (id == 'yes') {
	            		//TODO 删除基站
	            		Ext.Ajax.request({
				    	    url: '../cdma/remove_cell',
				    	    method: 'get',
				    	    params: {
				    	    	ID: rows[0].get('ID_VIEW')
				    	    },
				    	    success: function(response, opts) {
				    	    	var i = store.indexOf(rows[0]);

				    	    	store.remove(rows);

				    	    	var count = store.getCount();

				    	    	if (count > 0) {
				    	    		sm.select((i == count)? --i : i);
				    	    	}
							},
							failure: function(response, opts) {
								Ext.Msg.alert('提示','删除失败！');
							}
				    	});
	                }
	            });
	    	} else {
	    		Ext.Msg.alert('提示','请选择要删除的基站！');
	    	}
    	});
    },

    OnSearchBtnClick: function(self, e, options) {
    	var me = this;

    	var form = self.up('form').getForm();
    	if (!form.isValid()) return;
    	Ext.apply(me.cellStore.getProxy(), {
    	     extraParams: form.getValues()
    	});
    	me.cellStore.loadPage(1);
    },

    OnResetBtnClick: function(self, e, options) {
    	self.up('form').getForm().reset();
    },

    OnExportCellBtnClick: function(self, e, options) {
    	var me = this;
    	//导出为excel文件
    	zhwh.user.Rights.hasRights('ZHWH-BGZC-BGSB-4', function() {
    		me.cellStore.load({
       	    	limit: me.cellStore.getTotalCount(),
       	    	scope: this,
       	    	callback: function(records, operation, success) {
       	    		var excelXml = Ext.ux.exporter.Exporter.exportGrid(self.up('gridpanel'), 'excel', {title: 'C网基站资源'});
        		    document.location = 'data:application/vnd.ms-excel;base64,' + Ext.ux.exporter.Base64.encode(excelXml);
       	    	}
       	    });
    	});
    }
});