<?
// 기본 설정 로딩
require_once 'config.php';
require_once 'function.php';
require_once 'lib.php';

//한글문제로 변경
$sql = sprintf("SELECT s.*,c.Client_Name FROM servicedb AS s JOIN clientdb AS c ON s.client_code = c.Client_Code WHERE s.service_code='%s'", $_GET['service_code']);
$stmt = @mysql_query($sql, $db_connect) or die($db_q_error);
$service = mysql_fetch_assoc($stmt);

$sql = sprintf("SELECT * FROM memberdb WHERE id='%s'", $_GET['mkid']);
$stmt = @mysql_query($sql, $db_connect) or die($db_q_error);
$member = mysql_fetch_assoc($stmt);
//한글문제로 변경

if ($_GET[mode] == "coupon_reg") {
    $coupon_query = "update servicedb set coupon=(coupon-$_GET[onetime_sub]), coupon_use=(coupon_use+$_GET[onetime_sub]) where service_code='$_GET[service_code]'";
    @mysql_query($coupon_query, $db_connect) or die($db_i_error);

    $onetime_query = "INSERT INTO `onetimedb` (`client_code`, `service_code`, `onetime_case`, `onetime_sub`, `onetime_amount`, `onetime_payment`, `onetime_nonpayment`, `onetime_accountStatus`, `onetime_request_date`, `onetime_payment_date`, `onetime_note`, `onetime_handle_date`, `onetime_manager`, `client_name`, `server_code`) VALUES ('$_GET[client_code]', '$_GET[service_code]', 'domain', '$_GET[onetime_sub]', '0', '0', '0', 'complete', '$_GET[onetime_request_date]', '$_GET[onetime_request_date]', '도메인 쿠폰사용 / $_GET[onetime_note]', '$_GET[onetime_request_date]', '$_GET[mkid]', '$member[name]', '$service[server_code]')";
    @mysql_query($onetime_query, $db_connect) or die($db_i_error);

    $history_query = "INSERT INTO `historydb` (`service_code`, `server_code`, `behavior_case`, `behavior`, `behavior_date`, `note`, `client_name`) VALUES ('$_GET[service_code]', '$service[server_code]', '도메인 쿠폰 구매 / $_GET[onetime_sub] 개', '도메인 쿠폰 구매', '$_GET[onetime_request_date]', '$member[name]', '$service[Client_Name]')";
    @mysql_query($history_query, $db_connect) or die($db_i_error);
//echo $coupon_query;
    //echo "<br>";
    //echo $onetime_query;
    //echo "<br>";
    //echo $history_query;

//echo $_GET[client_code].$_GET[service_code].$_GET[client_name].$_GET[server_code].$_GET[coupon].$_GET[mkworker].$_GET[mkid];
    echo "
<script type='text/javascript'>
location.href='/serviceDetail.sev?client_code=" . $_GET[client_code] . "';
</script>
";
} else {
    //$sql = sprintf("select * from servicedb where service_code='%s'", $_GET['service_code']);
    //$stmt = @mysql_query($sql, $db_connect) or die($db_q_error);
    //$service = mysql_fetch_assoc($stmt);
?>
<div class="table-responsive">
<form method="get">
<input type="hidden" name="mode" value="coupon_reg">
<input type="hidden" name="mkworker" value="<?=$member['name']?>">
<input type="hidden" name="mkid" value="<?=$_GET[mkid]?>">
						<table class="table table-bordered table-hover table-striped">
							<thead>
							</thead>
							<tbody>
								<tr>
									<td>고객코드</td>
									<td colspan="5"><input type="hidden" name=client_code readonly="readonly" value="<?=$_GET[client_code]?>"><?=$_GET[client_code]?></td>
								</tr>
								<tr>
									<td>서비스코드</td>
									<td colspan="5"><input type="hidden" name=service_code readonly="readonly" value="<?=$_GET[service_code]?>"><?=$_GET[service_code]?></td>
								</tr>
								<tr>
									<td>고객명</td>
									<td colspan="5"><input type="hidden" name="client_name" readonly="readonly" value="<?=$service['Client_Name']?>"><?=$service['Client_Name']?></td>
								</tr>
								<tr>
									<td>장비번호</td>
									<td colspan="5"><input type="hidden" name="server_code" readonly="readonly" value="<?=$service['server_code']?>"><?=$service['server_code']?></td>
								</tr>
								<tr>
									<td>
										<input type="hidden" name="onetime_case" id="onetime_case" value="domain">
											도메인 구매 수량
									</td>
									<td colspan="5">
										<select name="onetime_sub" id="onetime_sub">
										<?
    for ($i = 0; $i < $service['coupon']; $i++) {
        $mkvalue = $i + 1;
        echo "<option value=\"$mkvalue\">$mkvalue 개</option>";
    }
    ?>
										</select> (개별 서버에 할당된 남은 쿠폰 수량 : <?=$service['coupon']?>)
										<!--<input type="text" name="onetime_sub" id="onetime_sub"/>  (개별 서버에 할당된 남은 쿠폰 수량 : <?=$service['coupon']?>)
										<br>* 도메인 쿠폰 사용시 반드시 숫자로만 갯수를 입력하세요 (* 3개 구매시 작성 예제 : <font color=red><b>3</b></font>)-->
									</td>
								</tr>
								<tr>
									<td>서비스 금액</td>
									<td colspan="3">
										<input type="hidden" name="onetime_amount" id="onetime_amount" onkeyup="calc()" value="0"/> 도메인 쿠폰 사용
										<input type="hidden" name="onetime_payment" id="onetime_payment" value="0"/>
										<input type="hidden" name="onetime_nonpayment" id="onetime_nonpayment"/>
									</td>
								</tr>
								<tr><?$today = date("Y-m-d", mktime(0, 0, 0, date(m), date(d), date(Y)));?>
									<td>도메인 신청일</td>
									<td>
										<input type="hidden" name="onetime_request_date"  value="<?=$today?>"/><?=$today?>
									</td>
									<td>쿠폰 사용일</td>
									<td colspan="3">
										<input type="hidden" name="onetime_payment_date" value="<?=$today?>"/><?=$today?>
									</td>
								</tr>
								<tr>
									<td>도메인 리스트</td>
									<td colspan="5"><textarea cols="100" rows="4" type="text" name="onetime_note" id="onetime_note" ></textarea>
									<br>(공백을 허용하지 않습니다. 예제처럼 붙여쓰기 하세요 / 예제 : test.com/123.com/idcjp.jp)
									</td>
								</tr>
							</tbody>
							<tfoot>
								<tr>
									<td colspan="6">
										<input class="btn btn-outline btn-primary" type="submit" value="저장하기">
										<input class="btn btn-outline btn-default" type="button" value="취소" onclick="location.href='/IdcCouponUseMK.cup?client_code=<?=$_GET[client_code]?>'">
									</td>
								</tr>
							</tfoot>
						</table>
</form>
					</div>
<?}?>
<?
//DB닫기
mysql_close($db_connect);
?>
