<?php
/**
 * Настройки в админке
 * @author chayn1k [3DaVinci]
 */

class dv_imoney_admin {

	protected $db, $UI_CONFIG;
	protected $MODULE_FOLDER, $MODULE_PATH, $ADMIN_TEMPLATE;

	protected $settings;

	public function __construct() {
		// system superior object
		$nc_core = nc_Core::get_object();

		global $UI_CONFIG;

		// global variables to internal
		$this->db = &$nc_core->db;
		$this->UI_CONFIG = $UI_CONFIG;
		$this->ADMIN_TEMPLATE = $nc_core->ADMIN_TEMPLATE;
		$this->MODULE_FOLDER = $nc_core->MODULE_FOLDER;
		$this->MODULE_PATH = str_replace($nc_core->DOCUMENT_ROOT, "", $nc_core->MODULE_FOLDER)."3dvimoney/";

		$this->settings = $nc_core->get_settings('','3dvimoney');

	}

	public function get_mainsettings_url () {
		return "#module.3dvimoney.settings";
	}

	/**
	 * Показ информации о всех заказах
	 * @global <type> $nc_minishop
	 */
	public function info_show() {
		$html= '<p>Модуль для оплаты заказов в минимагазине карточкой
				или электронными деньгами.<br> Используется сервис
				<b><a href=\'//intellectmoney.ru/ru/\' target=\'_blank\'>IntellectMoney</a></b>.<p>';

		echo $html;
	}

	public function info_save () {
		return true;
	}

	/**
	 * Вкладка "Общие настройки"
	 */
	public function settings_show () {
		global $UI_CONFIG;
		$UI_CONFIG->add_settings_toolbar();
		$html = '';

		$this->settings['preference'] = unserialize($this->settings['preference']);

		$html.="<form method='post' action='admin.php' id='MainSettigsForm' style='padding:0; margin:0;'>\n".
		"<input type='hidden' name='view' value='settings' />\n".
		"<input type='hidden' name='act' value='save' />\n".
		"<fieldset>\n".
			"<legend>\n".
			"<b><font color='gray'>".DV_IMONEY_ADMIN_TEMPLATE_SETTINGS_TAB_TITLE."</font></b>\n".
			"</legend>\n".
			"<div style='margin:10px 0; _padding:0;'>
				".  nc_admin_input(DV_IMONEY_ADMIN_TEMPLATE_SETTINGS_TAB_SHOP_NAME, 'shop_name', $this->settings,40)."
			</div>".
			"<div style='margin:10px 0; _padding:0;'>
				".  nc_admin_input(DV_IMONEY_ADMIN_TEMPLATE_SETTINGS_TAB_SHOP_ID, 'shop_id', $this->settings,40)."
			</div>".
			"<div style='margin:10px 0; _padding:0;'>
				".  nc_admin_input(DV_IMONEY_ADMIN_TEMPLATE_SETTINGS_TAB_SHOP_SECRET_KEY, 'secret_key', $this->settings,40)."
			</div>".
			"<div style='margin:10px 0; _padding:0;'>
				".  nc_admin_input(DV_IMONEY_ADMIN_TEMPLATE_SETTINGS_TAB_SHOP_SUCCESS_URL, 'success_url', $this->settings,40)."
			</div>".
			"<div style='margin:10px 0; _padding:0;'>
				".  nc_admin_input(DV_IMONEY_ADMIN_TEMPLATE_SETTINGS_TAB_SHOP_FAIL_URL, 'fail_url', $this->settings,40)."
			</div>".
			"<div style='margin:10px 0; _padding:0;'>
				".  nc_admin_input(DV_IMONEY_ADMIN_TEMPLATE_SETTINGS_TAB_SHOP_HANDL_URL, 'handl_url', $this->settings,40)."
			</div>".

			"<div style='margin:20px 0; _padding:0;'>\n
				<legend>\n
					<b><font color='gray'>".DV_IMONEY_ADMIN_TEMPLATE_SETTINGS_TAB_SHOP_CURRENCY."</font></b>\n
				</legend>\n
				<input id='currency0' type='radio' name='currency' value='0' ".(!$this->settings['currency'] ? " checked='checked' " : "")." />
				<label for='currency0'>".DV_IMONEY_ADMIN_TEMPLATE_SETTINGS_TAB_CURRENCY_RUB."</label>,<br/>
				<input id='currency1' type='radio' name='currency' value='1' ".($this->settings['currency'] ? " checked='checked' " : "")." />
				<label for='currency1'>".DV_IMONEY_ADMIN_TEMPLATE_SETTINGS_TAB_CURRENCY_ALT."</label>
			</div>\n
			<div style='margin:20px 0; _padding:0;'>\n".
				"<legend>\n".
					"<b><font color='gray'>".DV_IMONEY_SET_PREFERENCE."</font></b>\n".
				"</legend>\n
				".DV_IMONEY_SET_PREFERENCE_REMARK."<br>\n
				".nc_admin_checkbox(DV_IMONEY_SET_PREFERENCE_INNER, 'preference[inner]', $this->settings['preference']['inner'])."
				".nc_admin_checkbox(DV_IMONEY_SET_PREFERENCE_BANKCARD, 'preference[bankCard]', $this->settings['preference']['bankCard'])."
				".nc_admin_checkbox(DV_IMONEY_SET_PREFERENCE_EXCHANGERS, 'preference[exchangers]', $this->settings['preference']['exchangers'])."
				".nc_admin_checkbox(DV_IMONEY_SET_PREFERENCE_TRANSFERS, 'preference[transfers]', $this->settings['preference']['transfers'])."
				".nc_admin_checkbox(DV_IMONEY_SET_PREFERENCE_TERMINALS, 'preference[terminals]', $this->settings['preference']['terminals'])."
				".nc_admin_checkbox(DV_IMONEY_SET_PREFERENCE_BANK, 'preference[bank]', $this->settings['preference']['bank'])."
				".nc_admin_checkbox(DV_IMONEY_SET_PREFERENCE_SMS, 'preference[sms]', $this->settings['preference']['sms'])."
			</div>\n".

		"</fieldset><br/>\n".
		"</div>\n".
		"</form>\n";

		$UI_CONFIG->actionButtons[] = array(
			"id" => "submit",
			"caption" => DV_IMONEY_ADMIN_SETTINGS_SAVE,
			"action" => "mainView.submitIframeForm('MainSettingsForm')"
		);
		echo $html;
	}

	public function settings_save () {
		$nc_core = nc_core::get_object();
		$params = array('shop_name', 'shop_id', 'secret_key', 'success_url', 'fail_url', 'handl_url',
			'currency', 'preference' );
		foreach ( $params as $v ) {
			$val = $nc_core->input->fetch_get_post($v);
			if (!is_array($val)){
				$nc_core->set_settings( $v, $val, '3dvimoney' );
			} else {
				$nc_core->set_settings( $v, serialize($val), '3dvimoney' );
			}
		}

		$this->settings = $nc_core->get_settings('','3dvimoney');
	}


	public function display_show() {
		global $UI_CONFIG;
		$UI_CONFIG->add_settings_toolbar();
		$html = "";


		$html.="<form method='post' action='admin.php' id='DisplaySettingsForm' style='padding:0; margin:0;'>\n".
		"<input type='hidden' name='view' value='display' />\n".
		"<input type='hidden' name='act' value='save' />\n".

		"<fieldset>\n".
		"<legend>\n".
			"<b style='color: gray'>".DV_IMONEY_ADMIN_TEMPLATE_DISPLAY_TAB_TITLE."</b>\n".
		"</legend>\n".
		"<div style='margin:10px 0; _padding:0;'>\n".
			nc_admin_textarea(DV_IMONEY_ADMIN_TEMPLATE_DISPLAY_TAB_PAYMENT_BUTTON." (<a href='#' onclick='gen(\"pay_button_alternate\");return false;'>".DV_IMONEY_ADMIN_SETTINGS_GENERATE_TEMPLATE."</a>)", 'pay_button_alternate', $this->settings, 1).
			nc_admin_textarea(DV_IMONEY_ADMIN_TEMPLATE_DISPLAY_TAB_PAYMENT_FORM." (<a href='#' onclick='gen(\"pay_form_alternate\");return false;'>".DV_IMONEY_ADMIN_SETTINGS_GENERATE_TEMPLATE."</a>)", 'pay_form_alternate', $this->settings, 1).
		"</div>\n".
		"</fieldset><br/>".

		// письмо покупателю
		"<fieldset><legend><b style='color: gray'>".DV_IMONEY_ADMIN_TEMPLATE_MAILS_TAB_CLIENTMAIL."</b></legend>".
			nc_admin_input(DV_IMONEY_ADMIN_TEMPLATE_MAILS_TAB_ADMINMAIL_SUBJECT, 'mail_subject', $this->settings,0, 'width:100%').
			nc_admin_textarea(DV_IMONEY_ADMIN_TEMPLATE_MAILS_TAB_ADMINMAIL_TEXT, 'mail_body', $this->settings, 1, 0, 'height: 15em;').
			nc_admin_checkbox(DV_IMONEY_ADMIN_TEMPLATE_MAILS_TAB_ADMINMAIL_HTML, 'mail_ishtml', $this->settings, 1).
		"</fieldset><br/>".

		nc_admin_js_resize().
		"</form>";


		$UI_CONFIG->actionButtons[] = array(
			"id" => "submit",
			"caption" => DV_IMONEY_ADMIN_SETTINGS_SAVE,
			"action" => "mainView.submitIframeForm('DisplaySettingsForm')"
		);
		echo $html;
	}

	public function display_save() {
		$nc_core = nc_core::get_object();
		$params = array('mail_subject', 'mail_body', 'mail_ishtml');

		foreach ( $params as $v ) {
			$val = $nc_core->input->fetch_get_post($v);
			if (!is_array($val)){
				$nc_core->set_settings( $v, $val, '3dvimoney' );
			} else {
				$nc_core->set_settings( $v, serialize($val), '3dvimoney' );
			}

		}
		$this->settings = $nc_core->get_settings('','3dvimoney');
	}

	public function system_show () {
		global $UI_CONFIG;
		$UI_CONFIG->add_settings_toolbar();
		$settings = $this->settings;
		$nc_core = nc_Core::get_object();
		if($settings['dv_status_clft']){
		$status_clft = $nc_core->db->get_var("SELECT `Classificator_ID` FROM `Classificator` WHERE `Table_Name` = '".$settings['dv_status_clft']."'");
		}
		$settings['dv_status'] = unserialize($settings['dv_status']);


		$html = "<form  action='admin.php' method='post' id='SettingsForm' style='padding:0; margin:0;'>\n".
		"<input type='hidden' name='view' value='system' />
		<input type='hidden' name='act' value='save' />".
		"<fieldset style='margin:10px 0; _padding:0;'>\n".
			"<legend>\n".
			"<b><font color='gray'>".DV_IMONEY_ADMIN_TEMPLATE_SYSTEM_TAB_STATUS_TITLE.
			"</font></b>\n".
			"</legend>\n".
			"<div style='margin:10px 0; _padding:0;'>
				".nc_admin_classificators(DV_IMONEY_ADMIN_TEMPLATE_DISPLAY_TAB_SELECT_STATUS_TABLE, 'dv_status_clft', $this->settings['dv_status_clft'])."\n
				".($status_clft ? " <a target='_blank' href='".$nc_core->SUB_FOLDER."/netcat/admin/#classificator.edit(".$status_clft.")'>".DV_IMONEY_ADMIN_SETTINGS_TAB_SETTINGS."</a>" : "")."
			</div>".
			"<table id='statusSettings' style='width:80%;'>
			<tr>
			<th style='background:#ddd; width: 190px;'>".DV_IMONEY_PAYMENT_ADMIN_STATUS_ID."</td>
			<th style='background:#ddd;'>".DV_IMONEY_PAYMENT_ADMIN_STATUS_DESCRIPTION."</td>
			</tr><tr>
			<td><div style='margin:10px 0; _padding:0;'>
				<input type='text' name='dv_status[3]' size='15' value='".$settings['dv_status'][3]."'/>\n
			</div></td>
			<td>".DV_IMONEY_PAYMENT_STATUS_3."</td>
			</tr><tr>
			<td><div style='margin:10px 0; _padding:0;'>
				<input type='text' name='dv_status[4]' size='15' value='".$settings['dv_status'][4]."'/>\n
			</div></td>
			<td>".DV_IMONEY_PAYMENT_STATUS_4."</td>
			</tr>
			<tr>
			<td><div style='margin:10px 0; _padding:0;'>
				<input type='text' name='dv_status[5]' size='15' value='".$settings['dv_status'][5]."'/>\n
			</div></td>
			<td>".DV_IMONEY_PAYMENT_STATUS_5."</td>
			</tr>
			<tr>
			<td><div style='margin:10px 0; _padding:0;'>
				<input type='text' name='dv_status[6]' size='15' value='".$settings['dv_status'][6]."'/>\n
			</div></td>
			<td>".DV_IMONEY_PAYMENT_STATUS_6."</td>
			</tr>
			<tr>
			<td><div style='margin:10px 0; _padding:0;'>
				<input type='text' name='dv_status[7]' size='15' value='".$settings['dv_status'][7]."'/>\n
			</div></td>
			<td>".DV_IMONEY_PAYMENT_STATUS_7."</td>
			</tr>
			</table>".
		"</fieldset>\n<br>".


		"</form>";

		$UI_CONFIG->actionButtons[] = array(
			"id" => "submit",
			"caption" => DV_IMONEY_ADMIN_SETTINGS_SAVE,
			"action" => "mainView.submitIframeForm('SettingsForm')"
		);

		echo $html;
	}

	public function system_save() {
		$nc_core = nc_core::get_object();
		$params = array('dv_status_clft', 'dv_status');

		foreach ( $params as $v ) {
			$val = $nc_core->input->fetch_get_post($v);
			if (!is_array($val)){
				$nc_core->set_settings( $v, $val, '3dvimoney' );
			} else {
				$nc_core->set_settings( $v, serialize($val), '3dvimoney' );
			}

		}
		$this->settings = $nc_core->get_settings('','3dvimoney');
	}


	public function var_show ($var, $met = 'print_r') {
		echo '<pre>';
			if($met == 'print_r'){
				print_r($var);
			}
			if($met == 'var_dump'){
				var_dump($var);
			}
		echo '</pre><br>';
	}
}


