<?php
// error_reporting(E_ALL);
// ini_set('display_errors', "On");

date_default_timezone_set('Asia/Tokyo');

require_once('config.php');
define('PAGETITLE', '問い合わせフォーム');
$site_key = SITEKEY;

// IDまたはURLがなければシステムエラー
if (!isset($_POST['contact_id']) || !isset($_POST['contact_url'])) {
    errorIndication();
    exit();
}

// POST値取得
$contact_id = $_POST['contact_id'];
$contact_title = $_POST['contact_title'];
$contact_url = $_POST['contact_url'];



// jsonデータ取得
$json = file_get_contents(JSONPATH);
// $json = mb_convert_encoding($json, 'UTF8', 'ASCII,JIS,UTF-8,EUC-JP,SJIS-WIN');
$jsonlists = json_decode($json, true);
if ($jsonlists === NULL) {
    errorIndication();
    exit();
}
$json_department = NULL;
foreach ($jsonlists as $jsonlist) {
    if ($jsonlist['ContactId'] === $contact_id) {
        $json_department = $jsonlist['Department'];
        break;
    }
}
if ($json_department === NULL) {
    errorIndication();
    exit();
}

// ページ生成
$notes = <<< EOF
<div class="txt1"><p>ご意見・お問い合わせがある場合にご利用ください。</p></div>

<h3>注意事項</h3>
<ol class="number">
<li>返信を希望する場合は、全ての項目に入力してください。ただし、以下の場合は、原則として回答いたしません。
    <ul>
        <li>氏名やメールアドレス、住所、電話番号が不明なもの</li>
        <li>個人や団体に対する誹謗中傷</li>
        <li>営利・営業を目的としていると思われるもの</li>
        <li>趣旨が不明・不明確なもの</li>
        <li>その他、回答を行うことが不適当と判断したもの</li>
    </ul>
</li>
<li>環境依存文字や特殊文字は正確に送信されない場合がありますので、入力しないでください。</li>
<li>回答には時間を要する場合がありますので、お急ぎの場合は、恐れ入りますが担当部署へ電話でお問い合わせください。</li>
<li>ご入力いただいた内容は、必要に応じて関係部署へ転送させていただく場合がありますのでご了解ください。</li>
<li>入力していただきました個人情報につきましては、ご意見内容の確認や長岡市からの回答の送信に利用し、他の目的には利用しません。</li>
<li>問い合わせを受け付けたことをお知らせするメールを自動送信します。<br>
携帯電話のメールアドレスを入力する場合は、パソコンからのメール受信を拒否する設定を解除してください。<br>
または@city.nagaoka.lg.jpからのメールを受信できるようにしてください。</li>
</ol>

<ul class="disc is-danger" id="error_wrapper">
</ul>
EOF;

$form = <<< EOF
<form id="contactForm" method="post" action="e8xiubyg.php">
<table  class="tableLayout3"  summary=""  style="width:100%">
<tr>
<th style="width:30%;">種別 <span class="is-danger">【必須】</span></th>
<td style="width:70%;" class="font">
    <p>以下から選択してください</p>
    <label class="radio"><input type="radio" name="type" value="ご意見" class="validate required" data-label="種別">ご意見</label><br>
    <label class="radio"><input type="radio" name="type" value="お問い合わせ">お問い合わせ</label>
</td>
</tr>
<tr>
<th style="width:30%;">担当課</th>
<td style="width:70%;" class="font">
    <p>{$json_department}</p>
</td>
</tr>
<tr>
<th style="width:30%;">ページタイトル</th>
<td style="width:70%;" class="font">
    <p>{$contact_title}</p>
</td>
</tr>
<tr>
<th style="width:30%;">氏名 <span class="is-danger">【必須】</span></th>
<td style="width:70%;" class="font">
    <input type="text" name="person" maxlength="100" class="input validate required" data-label="氏名" />
</td>
</tr>
<tr>
<th style="width:30%;">ふりがな</th>
<td style="width:70%;" class="font">
    <input type="text" name="furigana" maxlength="100" class="input" />
</td>
</tr>
<tr>
<th style="width:30%;">郵便番号（半角）</th>
<td style="width:70%;" class="font">
    <p>（例：940-8501）</p>
    <input type="tel" name="postnumber" maxlength="10" class="input validate postnumber" data-label="郵便番号" />
</td>
</tr>
<tr>
<th style="width:30%;">住所</th>
<td style="width:70%;" class="font">
    <p>（例：新潟県長岡市大手通1-4-10）</p>
    <input type="text" name="address" maxlength="150" class="input" />
</td>
</tr>
<tr>
<th style="width:30%;">電話番号（半角）</th>
<td style="width:70%;" class="font">
    <input type="tel" name="telephone" maxlength="20" class="input validate telnumber" data-label="電話番号" />
</td>
</tr>
<tr>
<th style="width:30%;">メールアドレス （半角）<span class="is-danger">【必須】</span></th>
<td style="width:70%;" class="font">
    <p>（例：nagaoka@city.nagaoka.lg.jp）</p>
    <input type="email" name="email1" maxlength="100" class="input validate required mail" data-label="メールアドレス" />
</td>
</tr>
<tr>
<th style="width:30%;">メールアドレス（確認用） （半角）<span class="is-danger">【必須】</span></th>
<td style="width:70%;" class="font">
    <input type="email" name="email2" maxlength="100" class="input validate required mailcheck" data-label="メールアドレス（確認用）" />
</td>
</tr>
<tr>
<th style="width:30%;">件名</th>
<td style="width:70%;" class="font">
    <p>（例：戸籍謄本の郵送請求方法について）</p>
    <input type="text" name="subject" maxlength="100" class="input" />
</td>
</tr>
<tr>
<th style="width:30%;">内容 <span class="is-danger">【必須】</span></th>
<td style="width:70%;" class="font">
    <textarea name="matter" maxlength="1000" class="textarea validate required" data-label="内容"></textarea>
</td>
</tr>
</table>

<input type="hidden" name="contact_id" value="{$contact_id}" />
<input type="hidden" name="contact_title" value="{$contact_title}" />
<input type="hidden" name="contact_url" value="{$contact_url}" />

<div class="txt2">
<div class="g-recaptcha" data-sitekey="{$site_key}" data-size="invisible"></div>
<input type="button" name="confirm_btn" class="button is-success confirm_btn" value="確認画面に進む">
</div>

<script src="https://www.google.com/recaptcha/api.js" async defer></script>
</form>

EOF;



// 画面出力
$replace = array(
    TITLETAG => '<title>'.PAGETITLE.'</title>',
    '<!-- {{$title}} -->' => PAGETITLE,
    '<!-- {{$contents}} -->' => $notes.$form
);

$buff = file_get_contents(TEMPLATEPATH);
$buff = strtr($buff, $replace);
echo $buff;
exit();


// エラー時はシステムエラーを表示
function errorIndication() {
    header('Location: error.php');
}