<?php
if( $a == 'coupons' ){
	if( ( $aActions['o1'] == 'generate' ) && isset( $_FILES['codes_file'] ) && ( $_FILES['codes_file']['error'] === UPLOAD_ERR_OK ) && isset( $_POST['sDiscountValue'] ) && preg_match( '/^[0-9]+(\\.[0-9]+)?\\%?$/', $_POST['sDiscountValue'] ) ){
		$aCodes = Array( );
		$aCodesFile = file( $_FILES['codes_file']['tmp_name'] );
		foreach( $aCodesFile as $sCode ){
			$sCode = trim( $sCode );
			if( $sCode ){
				$aCodes[$sCode] = $sCode;
			}
		}
		$sql = 'SELECT `sCode` FROM `' . $oDB->throwTableName( DB_DISCOUNT_COUPONS ) . '` WHERE `iUsed` < 1 AND `iTimeTo` > ' . time( ) . ' AND `sCode` IN ( \'' . implode( '\', \'', $aCodes ) . '\' );';
		$aCodesBad = $oDB->executeSQL( $sql, true, DataBase::SINGLE_COLUMN );
		foreach( $aCodesBad as $sCode ) unset( $aCodes[$sCode] );
		if( empty( $_POST['iProducer'] ) ) $_POST['iProducer'] = 'NULL';
		if( isset( $_POST['iNotExpiring'] ) ) $iUsed = - 1;
		else $iUsed = 0;
		$sql = 'INSERT INTO `' . $oDB->throwTableName( DB_DISCOUNT_COUPONS ) . '` ( `sCode`, `sDiscountValue`, `iUsed`, `iTimeTo`, `iProducer` ) VALUES ( \'' . implode( '\', \'' . $_POST['sDiscountValue'] . '\', ' . $iUsed . ', ' . dateToTime( $_POST['sTimeTo'], 'ymd' ) . ', ' . $_POST['iProducer'] . ' ), ( \'', $aCodes ) . '\', \'' . $_POST['sDiscountValue'] . '\', ' . $iUsed . ', ' . dateToTime( $_POST['sTimeTo'], 'ymd' ) . ', ' . $_POST['iProducer'] . ' );';
		$oDB->executeSQL( $sql );
		if( !empty( $aCodesBad ) ) $content.= 'Następujące kody <span style="font-weight: bold; color: red;">nie zostały wprowadzone</span>, gdyż powtarzają się z istniejącymi ważnymi kodami w systemie: <table><tr><td>' . implode( '</td></tr><tr><td>', $aCodesBad ) . '</td></tr></table>';
		else $content.= $oTpl->parse( 'message_done.tpl' );
	} elseif( ( $aActions['o1'] == 'generate' ) && isset( $_POST['count'] ) && is_numeric( $_POST['count'] ) && isset( $_POST['sDiscountValue'] ) && preg_match( '/^[0-9]+(\\.[0-9]+)?\\%?$/', $_POST['sDiscountValue'] ) ){
		$PK = file_get_contents( 'index.php' );
		$PK = preg_match( '/.*define\\(\\s*\'PK\'\\s*,\\s*\'([0-9A-Za-z]+)\'\\s*\\);.*/s', $PK, $aMatches );
		$PK = $aMatches[1];
		$aCodesAll = generateDiscountCodes( $_POST['iProducer'], $_POST['length'], $_POST['iNotExpiring'], $_POST['count'], $PK, $_POST['sPrefix'], $_POST['sDiscountValue'], dateToTime( $_POST['sTimeTo'], 'ymd' ) );
		$content.= 'Wygenerowano następujące kody: <table><tr><td>' . implode( '</td></tr><tr><td>', $aCodesAll ) . '</td></tr></table>';
	} elseif( $aActions['o1'] == 'generate' ){
		$sDefaultTimeTo = date( 'Y-m-d 23:59:59', time( ) + 15552000 ); //Pół roku /6*30 dni/ ważności domyślnie
		$aProducers = throwProducersForSelect( );
		$oTpl->assign( 'sDefaultTimeTo', $sDefaultTimeTo );
		$oTpl->assign( 'aProducers', $aProducers );
		$content.= $oTpl->parse( 'discounts_form.tpl' );
	} elseif( $aActions['o1'] == 'list' ){
		if( isset( $sOption ) ){
			$content.= $oTpl->parse( 'message_done.tpl' );
		}
		$sql = 'SELECT SQL_CALC_FOUND_ROWS `iDiscountCoupon`, `sCode`, `sDiscountValue`, `iUsed`, `iTimeTo`, IFNULL( `sName`, \'Wszystkie\' ) AS `sProducer` FROM `' . $oDB->throwTableName( DB_DISCOUNT_COUPONS ) . '` LEFT JOIN `' . $oDB->throwTableName( DB_PRODUCERS ) . '` USING( `iProducer` ) WHERE `iTimeTo` > ' . time( ) . ' AND `iUsed` < 1';
		if( isset( $_GET['sPhrase'] ) ) $sql.= ' AND `sCode` LIKE \'%' . $oDB->escape( $_GET['sPhrase'] ) . '%\'';
		$iList = $config['admin_list'];
		$iPageNumber = $iPage;
		if( !isset( $iPageNumber ) || !is_numeric( $iPageNumber ) || $iPageNumber < 1 ) $iPageNumber = 1;
		if( isset( $_POST['sort'] ) || isset( $_POST['productsPerPage'] ) ){
			$iPageNumber = 1;
			$aActions['o2'] = 1;
		}
		$iEnd = $iPageNumber * $iList;
		$iStart = $iEnd - $iList;
		$sql.= ' LIMIT ' . $iStart . ',' . $iList;
		$aCoupons = $oDB->executeSQL( $sql, true );
		$sql = 'SELECT FOUND_ROWS() AS `count`;';
		$iCount = $oDB->executeSQL( $sql, true, DataBase::SINGLE_VALUE );
		$aPagesCountData = Array( 'iCount' => ceil( $iCount / $iList ), 'iPageNumber' => $iPageNumber );
		$sCouponList = null;
		foreach( $aCoupons as &$aCoupon ){
			$aCoupon['sTimeTo'] = date( 'Y-m-d', $aCoupon['iTimeTo'] );
		}
		$oTpl->assign( 'aDiscounts', $aCoupons );
		$oTpl->assign( 'aPagesCountData', $aPagesCountData );
		$content.= $oTpl->parse( 'discounts_list.tpl' );
	} elseif( $aActions['o1'] == 'delete' ){
		$oDB->deleteInFile( DB_DISCOUNT_COUPONS, $iDiscountCoupon, 'iDiscountCoupon' );
		header( 'Location: admin.php?p=discount-coupons-list&sOption=delete' );
		exit;
	}
}
