<?php

set_time_limit(3600);

//print_r($_POST);

//必要となる配列
$keiyaku_array=array(
	1=>'従量電灯B',
	2=>'従量電灯C',
	3=>'深夜電力B',
	4=>'深夜電力C',
	5=>'よりそう＋ シーズン＆タイム',
	6=>'よりそう＋ ナイト12',
	7=>'よりそう＋ ナイト＆ホリデー',
	8=>'よりそう＋ ナイト8',
	9=>'よりそう＋ ナイト10',
	10=>'よりそう＋ ナイトS',
	11=>'よりそう＋ サマーセーブ',
	12=>'よりそうB 季節別電灯'
);

//年月を指定してもらう。
$month_array=array(
		201608=>'2016年08月',
		201607=>'2016年07月',
		201606=>'2016年06月',
		201605=>'2016年05月',
		201604=>'2016年04月',
		201603=>'2016年03月',
		201602=>'2016年02月',
		201601=>'2016年01月',
		201512=>'2015年12月',
		201511=>'2015年11月',
		201510=>'2015年10月',
		201509=>'2015年09月'
);


function NenSai($month){
//燃料調整費、再エネ賦課金を返す関数
	switch($month){
		case '201608': $nenryo=-2.73;$saiene=2.25; break;
		case '201607': $nenryo=-2.45;$saiene=2.25; break;
		case '201606': $nenryo=-2.19;$saiene=2.25; break;
		case '201605': $nenryo=-1.8;$saiene=2.25; break;
		case '201604': $nenryo=-1.5;$saiene=1.58; break;
		case '201603': $nenryo=-1.19;$saiene=1.58; break;
		case '201602': $nenryo=-1;$saiene=1.58; break;
		case '201601': $nenryo=-0.82;$saiene=1.58; break;
		case '201512': $nenryo=-0.72;$saiene=1.58; break;
		case '201511': $nenryo=-0.69;$saiene=1.58; break;
		case '201510': $nenryo=-0.78;$saiene=1.58; break;
		case '201509': $nenryo=-0.67;$saiene=1.58; break;
	}
	return array($nenryo,$saiene);
}

//計算プログラム


	$month_btn='btn btn-primary';
	$nen_btn='btn btn-default';

	//従量電灯C以外の時は２６円にする

if($chk_type=='nen'){
	$nen_btn='btn btn-primary';
	$month_btn='btn btn-default';
}

if($_POST[reg]=='計算'){

	if($_POST[keiyaku]==2){$tanka=27;}
	else{$tanka=26;}

	//１年の電気料金から調べる。
	if($chk_type=='nen'){

	//返す表の準備
	$result_str.='
	<table class="table table-bordered table-striped table-hover">
	<thead>
		<tr>
			<th>年月</th>
			<th>使用量</th>
			<th>今までの電気代</th>
			<th>looopでんき</th>
			<th>差分</th>
		</tr>
	</thead>
	<tbody>
		<tr>
	';	
	
//初期化
			$sum_val=0;
			$sum_kin=0;
			$sum_looop=0;
			$sum_sabun=0;
		
		foreach($month_array as $month=>$gomi){
//			print($_POST['val_'.$month]);

	list($nenryo,$saiene)=NenSai($month);	//燃料調整費と再エネ賦課金を算出
			${'looop_'.$month}=($_POST['val_'.$month]*$tanka)+$nenryo+$saiene;
			${'sabun_'.$month}=$_POST['kin_'.$month]-${'looop_'.$month};
			$result_str.="<td>".$month."</td>";
			$result_str.="<td align=right>".number_format(floor($_POST['val_'.$month]))."kWh</td>";
			$result_str.="<td align=right>".number_format(floor($_POST['kin_'.$month]))."</td>";
			$result_str.="<td align=right>".number_format(floor(${'looop_'.$month}))."</td>";
			$result_str.="<td align=right>".number_format(floor(${'sabun_'.$month}))."</td>";
			$result_str.="<td align=right>".number_format(floor($nenryo))."</td>";
			$result_str.="<td align=right>".number_format(floor($saiene))."</td>";
			$result_str.="<tr>";	
			$sum_val=$sum_val+floor($_POST['val_'.$month]);
			$sum_kin=$sum_kin+floor($_POST['kin_'.$month]);
			$sum_looop=$sum_looop+floor(${'looop_'.$month});
			$sum_sabun=$sum_sabun+floor(${'sabun_'.$month});
		}
		//合計値を出力
			$result_str.="<td>合計</td>";
			$result_str.="<td align=right>".number_format($sum_val)."kWh</td>";
			$result_str.="<td align=right>".number_format($sum_kin)."</td>";
			$result_str.="<td align=right>".number_format($sum_looop)."</td>";
			$result_str.="<td align=right>".number_format($sum_sabun)."</td>";




$result_msg.=$result_str;
$result_msg.="</tbody></table>";

	}
else{



list($nenryo,$saiene)=NenSai($month);	//燃料調整費と再エネ賦課金を算出
	$Looop_kin=floor(($_POST[val]*$tanka)+($nenryo*$_POST[val])+($saiene*$_POST[val]));
	$Sabun=floor($_POST[kin]-$Looop_kin);
	$wareki="平成".(substr($month,0,4)-1988)."年".substr($month,4,2)."月";

//ご使用量が入っていないとエラーにする。
if($_POST[val]>0 and $_POST[kin]>0){

	if($Sabun>0){
/*
		$result_msg="
			<h4 style='display:inline;'>Looopでんきにすると".$wareki."の電気料金は<h3 style='display:inline;'>".number_format($Looop_kin)."</h3><h4 style='display:inline;'>円となります。
			<br>現在の電気料金と比較すると当月は</h4><font color=red><b><h3 style='display:inline;'>".number_format($Sabun)."円</b></font></h3><h4 style='display:inline;'>お得になります。<br><br>
			単純に１年間を考えると、<br>".number_format($Sabun)." 円×12ヶ月=年間おおよそ</h4><h3 style='display:inline;'><font color=red><b>".number_format($Sabun*12)."</b></font></h3><h4 style='display:inline;'>円お得になります。</h4>
		<br>詳細に計算したい方は、12ヶ月の検針票をご用意の上、上の「１年間の電気料金から調べる」をクリックして下さい。
		";
*/
		$result_msg="
			Looopでんきにすると".$wareki."の電気料金は<h3 style='display:inline;'>".number_format($Looop_kin)."</h3>円となります。
			<br>現在の電気料金と比較すると当月は<font color=red><b><h3 style='display:inline;'>".number_format($Sabun)."円</b></font></h3>お得になります。<br><br>
			単純に１年間を考えると、<br>".number_format($Sabun)." 円×12ヶ月=年間おおよそ<h3 style='display:inline;'><font color=red><b>".number_format($Sabun*12)."</b></font></h3>円お得になります。
		<br>毎月、使用する量が異なりますので、詳細に計算したい方は、12ヶ月の検針票をご用意の上<br>上記の「１年間の電気料金から調べる」をクリックして下さい。
		";

/*
		$result_msg="
		<div class='alert alert-info' role='alert'>
		</div>	
			Looopでんきにすると".$wareki."の電気料金は<h3 style='display:inline;'>".number_format($Looop_kin)."</h3>円となります。
			<br>現在の電気料金と比較すると当月は<font color=red><b><h3 style='display:inline;'>".number_format($Sabun)."円</b></font></h3>お得になります。<br><br>
			単純に１年間を考えると、<br>".number_format($Sabun)." 円×12ヶ月=年間おおよそ<h3 style='display:inline;'><font color=red><b>".number_format($Sabun*12)."</b></font></h3>円お得になります。
		<br>毎月、使用する量が異なりますので、詳細に計算したい方は、12ヶ月の検針票をご用意の上<br>上記の「１年間の電気料金から調べる」をクリックして下さい。
		";
*/


	}
	else{
		$result_msg="
			<h3>お客様の現在の契約からは、逆に".number_format($Sabun*-1)."円高くなります。<br>
			現在のご契約を継続することをおすすめ致します。</h3>
		";
	}
	$result_msg.="
		<div class='alert alert-warning' role='alert'>
		<p>※本シミュレーションは、ご入力頂いた内容に基いて試算するものです。試算結果はあくまで参考目安であり、実際の金額をお約束するものではありません。<br>
			※燃料費調整額は".$wareki."の単価".$nenryo."円×".$_POST[val]."kWh=".($nenryo*$_POST[val])."円で計算しております。<br>
			※再エネ発電賦課金は".$wareki."の単価".$saiene."円×".$_POST[val]."kWh=".($saiene*$_POST[val])."円で計算しております。</p>
		</div>
	";
}
else{
	$result_msg.="<h4>ご使用量、ご使用金額を入力して下さい。</h4>";

}
//print($Looop_kin."\n");
//print($Sabun."\n");

}
}




$head_str='
<!DOCTYPE html>
<html lang="ja">
  <head>
    <meta charset="shift-jis">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
';


$head_str.="
<script type='text/javascript' src='https://www.google.com/jsapi'></script> 
<script type='text/javascript'> 



</script> 
";

    
    $head_str.='
    <title>Looopでんきシミュレーション</title>
    <!-- Bootstrap -->
		<link href="css/bootstrap.css" rel="stylesheet">
  </head>
  <body>
';




$head_str.='
<a href="sim_calc.php?chk_type=month"><button class="'.$month_btn.'">１ヶ月の電気料金から調べる</button></a>
<a href="sim_calc.php?chk_type=nen"><button class="'.$nen_btn.'">１年間の電気料金から調べる</button></a>
';

$form_str.="<div class='container-fluid'>";
$form_str.="<div class='row'>";

if($reg=='計算'){

	$form_str.="<div class='col-md-7'><div class='panel panel-default'>
	".$result_msg."
	</div></div>";
}
else{

	$form_str.="<div class='col-md-7'><div class='panel panel-default'>
		下記の見本をご参考に、必要情報をご入力下さい。
		<img src='kensin.jpg' class='img-responsive img-rounded' alt='検針票イメージ画像'>>
		</div></div>";
}



$form_str.="<div class='col-md-5'><div class='panel panel-default'>";

$form_str.="<form name='f1' action='sim_calc.php' method='POST' enctype='multipart/form-data'>";
$form_str.='
<form class="form-inline">
			<div class="form-group">
				<div class="input-group">
					<span class="input-group-addon">プラン名　</span>
	<select name=keiyaku class="form-control">';
	  $str .="<OPTION value='". $col2[$col_no]. "'" . ($col2[$col_no] == $no ? " selected" : "") . ">". $col2[$col_name]." </OPTION>";


//契約内容を選択してもらう。

	//単月比較処理
	foreach($keiyaku_array as $keiyaku_no=>$keiyaku_mei){
		  $form_str.="<OPTION value='". $keiyaku_no. "'" . ($keiyaku_no == $keiyaku ? " selected" : "") . ">". $keiyaku_mei." </OPTION>";
	}
	$form_str.='</select></div></div>';	

if($chk_type=='nen'){
	$form_str.='
	<form class="form-inline">
				<div class="form-group">';

			foreach($month_array as $month_no=>$month_mei){
				$form_str.='
					<div class="input-group">
						<span class="input-group-addon">'.$month_mei.'</span>
						<input name="'.val_.$month_no.'" value="'.${val_.$month_no}.'" type="number" class="form-control">
						<span class="input-group-addon">kWh</span>
						<input name="'.kin_.$month_no.'" value="'.${kin_.$month_no}.'" type="number" class="form-control">
						<span class="input-group-addon">円</span>
					</div>
				';
			}
			$form_str.="<input name=chk_type value='nen' type=hidden>";
}
else{



	$form_str.='<div class="form-group">
				<label class="sr-only" for="InputEmail">金額</label>
				<div class="input-group">
				<span class="input-group-addon">ご使用月　</span>
				<select name=month class="form-control">
			';



	foreach($month_array as $month_no=>$month_mei){
		  $form_str.="<OPTION value='". $month_no. "'" . ($month_no == $month ? " selected" : "") . ">". $month_mei." </OPTION>";
	}

	$form_str.='</select></div></div>';




	$form_str.='
				<div class="form-group">
					<label class="sr-only" for="InputEmail">金額</label>
					<div class="input-group">
						<span class="input-group-addon">ご使用量　</span>
						<input name="val" type="number" class="form-control" value='.$_POST[val].'>
						<span class="input-group-addon">kWh</span>
					</div>
				</div>	
			
				<div class="form-group">
					<label class="sr-only" for="InputEmail">金額</label>
					<div class="input-group">
						<span class="input-group-addon">ご使用金額</span>
						<input name="kin" type="number" class="form-control" value='.$_POST[kin].'>
						<span class="input-group-addon">　円</span>
					</div>
				</div>';

//ここまで、単月比較
}


$form_str.='
		<input class="btn  btn-primary active" name="reg" type="submit" value="計算">
		<a href="sim_calc.php?chk_type=month"><input class="btn btn-primary"  disabled="disabled" value="クリア"></a>
	</form>
	';








$form_str.="</div>";
$form_str.="</div>";

function denki_keisan($val,$kin){


}







$bootdir="../bootstrap";


/*
$body_str.='
   <div class="container" style="padding:5px 0">
		<br><br>
';		




$body_str.='
		<div class="row">
			<div class="col-md-5 "  >'.$form_str.'</div>
			<div class="col-md-7" style=""><div id="chart_div" style="width:700px;height: 50%; " ></div> </div>
		</div>
';
$body_str.='
		<div class="row">
			<div class="col-md-12 " >
				'.$sim_str.'
			</div>
		</div>
';

$body_str.='
		<div class="row">
			<div class="col-md-12 " >
				'.$ryokin_str.'
			</div>
		</div>
';


//				<div id="chart_div" style="width: 1000px; height: 50%; " ></div> 	
*/
$body_str.='
 

    <!-- jQuery (necessary for Bootstrap"s JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="'.$bootdir.'/js/bootstrap.min.js"></script>
  </body>
</html>
';

//$body.=Header_Print("テスト");
$body.=$head_str;
$body.=$form_str;
$body.=$body_str;
//$body.=$ryokin_str;
print($body);



?>