<?php
global $config;
if (!$config) {exit();}
?>
FR.customActions.crypt.run = function() {
	var item = FR.UI.gridPanel.getOneSel().data;
	this.fileName = item.filename;
	this.filePath = item.path ? item.path : FR.currentPath+'/'+item.filename;
	this.action = item.ext == 'aes' ? 'decrypt' : 'encrypt';
	if (this.action == 'encrypt') {
		var title = '<?php echo \S::safeJS(self::t('Encrypt "%1"', ['%1']));?>'.replace('%1', this.fileName);
	} else {
		var title = '<?php echo \S::safeJS(self::t('Decrypt "%1"', ['%1']));?>'.replace('%1', this.fileName);
	}
	if (!this.prompt) {
		this.prompt = new Ext.Window({
			title: title,
			width: 400,
			height: 'auto',
			maximized: FR.UI.layout.isPhone,
			bodyStyle: 'padding-top:var(--pad)',
			items: [
				{
					xtype: 'form',
					labelAlign: 'right',
					labelWidth: 140,
					items: [
						{
							xtype: 'textfield',
							width: '100%',
							fieldLabel: '<?php echo \S::safeJS(self::t("Password"));?>',
							name: 'pwd',
							inputType: 'password',
							autocomplete: (this.action == 'decrypt') ? 'on':'new-password'
						},
						{
							xtype: 'textfield',
							width: '100%',
							fieldLabel: '<?php echo \S::safeJS(self::t("Confirm password"));?>',
							labelStyle: 'white-space:nowrap',
							name: 'pwd',
							inputType: 'password',
							hidden: (this.action == 'decrypt')
						},
						{
							xtype: 'checkbox',
							fieldLabel: '',
							boxLabel: '<?php echo \S::safeJS(self::t("Delete the source file"));?>'
						}
					]
				}
			],
			bbar: [{
				text: 'Ok',
				cls: 'fr-btn-primary',
				handler: () => this.doAction()
			}, {
				text: 'Cancel',
				style: 'margin-left:15px',
				handler : () => this.prompt.hide()
			}]
		});
	}
	this.frm = this.prompt.items.first();
	this.frm.get(1).setVisible(this.action != 'decrypt');
	this.prompt.setTitle(title);
	this.prompt.show();
	this.frm.reset();
	this.frm.items.first().focus();
};
FR.customActions.crypt.doAction = function() {
	var pass = this.frm.items.first().getValue();
	var repass = this.frm.items.get(1).getValue();
	if (pass.length == 0) {
		new Ext.ux.prompt({text: '<?php echo \S::safeJS(self::t("Please type the password"));?>'});
		return false;
	}
	if (this.action == 'encrypt' && pass != repass) {
		new Ext.ux.prompt({text: '<?php echo \S::safeJS(self::t("Please confirm the password"));?>'});
		return false;
	}
	var deleteSrc = this.frm.items.get(2).getValue();
	var pars = {
		path: this.filePath,
		pass: pass,
		deleteSrc: deleteSrc
	};
	FR.actions.process({
		loadMsg: '<?php echo \S::safeJS(self::t("Processing file..."));?>',
		url: '/?module=custom_actions&action=crypt&method=run',
		params: pars,
		callback: function(rs) {
			if (rs.success) {
				FR.utils.reloadGrid();
			}
			this.prompt.hide();
		},
		scope: this
	});
}