Ext.define('Ext.ux.importManager.Panel', {
	extend: 'Ext.form.Panel',
	alias: ['widget.importManagerPanel'],
	border: false,
	bodyStyle: 'padding:10px;',
	defaults: {
		xtype: 'textfield',
		labelAlign: 'left',
		labelWidth: 140
	},
	layout: {
		type: 'anchor'
	},
	initComponent: function() {
		var me = this;

		var languageCheckboxs = [];
		me.languages = (Ext.isString(me.record.data.exportlanguages)) ? me.record.data.exportlanguages.split(',') : me.record.data.exportlanguages;

		if (Ext.isEmpty(me.languages)) {
			me.hasLanguages = false;
		}
		else {
			me.hasLanguages = true;

			for (var i = 0; i < me.languages.length; i++) {
				var language = me.languages[i];
				var checked = (i == 0) ? true : false;
				languageCheckboxs.push({
					boxLabel: ip2.locale.getLanguageByCode(language),
					name: language,
					inputValue: '1',
					checked: checked
				});
			}
		}

		me.items = [{
			xtype: 'fieldset',
			title: me.mainOwner.getI18N('doImport'),
			collapsible: true,
			defaultType: 'textfield',
			defaults: { anchor: '100%', labelWidth: 140, style: { 'line-height': '20px' } },
			padding: 8,
			layout: 'anchor',
			items: [{
				xtype: 'displayfield',
				fieldLabel: me.mainOwner.getI18N('name'),
				name: 'name',
				value: me.record.data.title
			}, {
				xtype: 'radiogroup',
				fieldLabel: me.mainOwner.getI18N('existing'),
				items: [
					{ boxLabel: me.mainOwner.getI18N('doUpdate'), name: 'updateExisting', inputValue: '1', checked: true },
					{ boxLabel: me.mainOwner.getI18N('doCreate'), name: 'updateExisting', inputValue: '0' }
				]
			}]
		},{
			xtype: 'fieldset',
			title: me.mainOwner.getI18N('images'),
			collapsible: true,
			defaultType: 'textfield',
			defaults: { anchor: '100%', labelWidth: 140, style: { 'line-height': '20px' } },
			padding: 8,
			layout: 'anchor',
			items: [{
				xtype: 'checkbox',
				fieldLabel: me.mainOwner.getI18N('importImages'),
				name: 'importImages',
				checked: true,
				inputValue: '1'
			}, {
				xtype: 'radiogroup',
				fieldLabel: me.mainOwner.getI18N('existingImages'),
				items: [
					{ boxLabel: me.mainOwner.getI18N('overwrite'), name: 'updateExistingImages', inputValue: '1', checked: true },
					{ boxLabel: me.mainOwner.getI18N('doCreate'), name: 'updateExistingImages', inputValue: '0' }
				]
			}]
		}];

		if (me.hasLanguages) {
			me.items.push({
				xtype: 'fieldset',
				title: me.mainOwner.getI18N('languages'),
				collapsible: true,
				defaultType: 'textfield',
				defaults: { anchor: '100%', labelWidth: 140, style: { 'line-height': '20px' } },
				padding: 8,
				layout: 'anchor',
				items: [{
					xtype: 'checkboxgroup',
					fieldLabel: me.mainOwner.getI18N('importLanguages'),
					items: languageCheckboxs
				},{
					xtype: 'checkbox',
					fieldLabel: me.mainOwner.getI18N('existingTexts'),
					boxLabel: me.mainOwner.getI18N('overwrite'),
					name: 'updateTexts',
					checked: true,
					inputValue: '1'
				}]
			});
		}

		me.callParent();
	},

	/* ----------------------------------------------------------------------------------------------------
			Confirm import
		---------------------------------------------------------------------------------------------------- */
	confirmImport: function(config) {
		var me = this;

		Ext.MessageBox.show({
			title: me.mainOwner.getI18N('confirmImportTitle'),
			msg: me.mainOwner.getI18N('confirmImport') + ' <b>' + me.record.data.title + '</b>',
			buttons: Ext.MessageBox.YESNO,
			fn: function(btn, text) {
				switch (btn) {
					case 'yes':
						me.doImport(config);
						break;
					case 'no':
						break;
					default:
						break;
				}
			},icon: Ext.MessageBox.QUESTION
		});
	},

	/* ----------------------------------------------------------------------------------------------------
			Import
		---------------------------------------------------------------------------------------------------- */
	doImport: function(config) {
		var me = this;

		var params = {};

		params.folder = me.record.data.folder;

		me.query('.field').forEach(function(field) {
			var value = field.getValue();
			if (Ext.isBoolean(value)) {
				value = (value) ? '1' : '0';
			}
			if (field.xtype == 'radiofield') {
				if (value == '1') {
					params[field.name] = field.inputValue;
				}
			}
			else {
				params[field.name] = value;
			}
		});

		var languages = [];
		if (me.hasLanguages) {
			for (var i = 0; i < me.languages.length; i++) {
				var value = params[me.languages[i]];
				if (value == '1') {
					languages.push(me.languages[i]);
				}
				delete params[me.languages[i]];
			}
		}
		params.languages = languages.toString();

		switch (me.record.data.type) {
			case 'page':
				url = '/wsh-services/wshImpPag.aspx';
				break;
			case 'folder':
				url = '/wsh-services/wshImpFld.aspx';
				break;
		}

		Ext.Ajax.request({
			url: url,
			params: params,
			cb: config.success,
			success: function(response, request) {
				var result = Ext.decode(response.responseText);
				if (Ext.isFunction(request.cb)) {
					request.cb.call(me, result);
				}
			}
		});
	}
});
