<!DOCTYPE html>
<html lang="ja">
  <head>
    {% with subtitle = mainte_csv_up.subtitle %}
    {% include '_header.html' %}
    {% endwith %}
    <link href="/static/css/masterMainte.css" rel="stylesheet" />
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <script type="text/javascript">
        window.onload = function(){
            // {% if mainte_csv_up.is_insert %}
                sessionStorage.clear();
            // {% else %}
                if (sessionStorage["ctrl_select_function"]) {
                    var element = document.getElementById("upload_form");
                    if (sessionStorage["ctrl_select_function"] === "new") {
                        element.function_new.checked = true;
                    }
                    else if (sessionStorage["ctrl_select_function"] === "change") {
                        element.function_change.checked = true;
                    }
                }
                if (sessionStorage["ctrl_select_table"]) {
                    var element = document.getElementById("upload_form");
                    if (sessionStorage["ctrl_select_table"] === "dummy") {
                        element.dummy_table.checked = true;
                    }
                    else if (sessionStorage["ctrl_select_table"] === "real") {
                        element.real_table.checked = true;
                    }
                }
            // {% endif %}
        }
        function Form_Submit(){
            sessionStorage.clear();
            var element = document.getElementById("upload_form")
            sessionStorage.setItem("ctrl_select_function", element.ctrl_select_function.value);
            sessionStorage.setItem("ctrl_select_table", element.ctrl_select_table.value);
            showLoading();
            if (document.getElementById("ulMsg") !== null) {
                document.getElementById("ulMsg").style.display = "none";
            }
        }

        function Form_Submit_Disp_Dialog(){
            var msg = '{{ mainte_csv_up.select_function_message }}';
            if (confirmDialog(msg)) {
                showLoading();
                if (document.getElementById("ulMsg") !== null) {
                    document.getElementById("ulMsg").style.display = "none";
                }
            } else {
                return false;
            }
        }
    </script>

  </head>
  <body>
    <!-- タイトルと上部ボタン -->
    <h1>
        <table class="headerTable">
            <tr>
                <td class="headerTdLeft"><h1>施設担当者データCSVアップロード</h1></td>
                <td class="headerTdRight">
                    {% if mainte_csv_up.is_verified and mainte_csv_up.is_error_message_list_empty()  %}
                        <input type="button" class="header_buttonSize" onclick="location.href='/masterMainte/instEmpCsvUL' " value="戻る">
                    {% else %}
                        <input type="button" class="header_buttonSize" onclick="backToMainteMenu('_loading_for_back')" value="メニューへ" />
                    {% endif %}
                </td>
            </tr>
        </table>
    </h1>
    <!-- 選択フォーム -->
    <form name="input" action="{% if mainte_csv_up.is_verified and mainte_csv_up.is_error_message_list_empty() %}/masterMainte/newInst{% else %}/masterMainte/instEmpCsvUL{% endif %}"
        method="POST" enctype="multipart/form-data"
        {% if mainte_csv_up.is_verified and mainte_csv_up.is_error_message_list_empty() %}
            onsubmit="return Form_Submit_Disp_Dialog();"
        {% else %}
            onsubmit="Form_Submit();"
        {% endif %}
        id="upload_form"
    >
        <table class="inputTable">
            <tbody>
                <!-- 選択フォーム1行目 -->
                <tr>
                    <!-- 機能 -->
                    <td class="inputLabelTd">機能：</td>
                    <td class="inputTd">
                        <label>
                            <input type="radio" name="ctrl_select_function" value="new" id="function_new"
                                {{ "checked " if mainte_csv_up.select_function == 'new' or mainte_csv_up.is_select_function_empty() }}
                                {{ "disabled" if mainte_csv_up.is_verified and mainte_csv_up.is_error_message_list_empty() }}
                            >
                            新規施設登録
                        </label>
                    </td>
                    <td class="inputTd">
                        <label>
                            <input type="radio" name="ctrl_select_function" value="change" id="function_change"
                                {{ "checked " if mainte_csv_up.select_function == 'change' }}
                                {{ "disabled" if mainte_csv_up.is_verified and mainte_csv_up.is_error_message_list_empty() }}
                            >
                            施設担当者変更
                        </label>
                    </td>
                </tr>
                <!-- 選択フォーム2行目 -->
                <tr>
                    <!-- 登録テーブル -->
                    <td class="inputLabelTd">登録テーブル：</td>
                    <td class="inputTd">
                        <label>
                            <input type="radio" name="ctrl_select_table" value="dummy" id="dummy_table"
                                {{ "checked " if mainte_csv_up.select_table == 'dummy' or mainte_csv_up.is_select_table_empty() }}
                                {{ "disabled" if mainte_csv_up.is_verified and mainte_csv_up.is_error_message_list_empty() }}
                            >
                            ダミーテーブル
                        </label>
                    </td>
                    <td class="inputTd">
                        <label>
                            <input type="radio" name="ctrl_select_table" value="real" id="real_table"
                                {{ "checked " if mainte_csv_up.select_table == 'real' }}
                                {{ "disabled" if mainte_csv_up.is_verified and mainte_csv_up.is_error_message_list_empty() }}
                            >
                            本番テーブル
                        </label>
                    </td>
                </tr>
                <!-- 選択フォーム3行目 -->
                <tr>
                    <!-- 登録CSV -->
                    <td class="inputLabelTd">登録CSV：</td>
                    <td class="input_tb" colspan="2">
                        {% if mainte_csv_up.is_verified and mainte_csv_up.is_error_message_list_empty() %}
                            {{mainte_csv_up.csv_file_name}}
                        {% else %}
                            <input type="file" id="csvFile" size="50" name="ctrl_csv_file" accept=".csv" onchange="formInsertBtDisabled()">
                        {% endif %}
                    </td>
                    <!-- ボタン -->
                    <td class="inputButtonTd">
                        {% if mainte_csv_up.is_verified and mainte_csv_up.is_error_message_list_empty() and mainte_csv_up.select_table == 'real' %}
                            <input name="btName" value="insert_bt" type="hidden">
                            <div class="errorColor">本番テーブルが選択されています<input class="buttonSize" id="insert" name="insert_bt" value="登録" type="submit" ></div>
                        {% elif mainte_csv_up.is_verified and mainte_csv_up.is_error_message_list_empty() and mainte_csv_up.select_table == 'dummy' %}
                            <input name="btName" value="insert_bt" type="hidden">
                            <input class="buttonSize" id="insert" name="insert_bt" value="登録" type="submit">
                        {% else %}
                            <input name="btName" value="input_bt" type="hidden">
                            <input class="buttonSize" id="confirm" name="input_bt" value="確認" type="submit" disabled>
                        {% endif %}
                    </td>
                </tr>
            </tbody>
        </table>
{% if mainte_csv_up.is_verified and mainte_csv_up.is_error_message_list_empty() %}
        <input name="ctrl_json_upload_data" value="{{mainte_csv_up.json_upload_data}}" type="hidden">
        <input name="ctrl_select_function" value="{{mainte_csv_up.select_function}}" type="hidden">
        <input name="ctrl_select_table" value="{{mainte_csv_up.select_table}}" type="hidden">
{% endif %}
    </form>
    <p>
        <!-- 処理中メッセージ表示 -->
        {% if not mainte_csv_up.is_error_message_list_empty() %}
            <div id="ulMsg" class="footerMsg errorColor">
                {% for error_message in mainte_csv_up.error_message_list %}
                    {{error_message}}<br>
                {% endfor %}
            </div>
        {% elif mainte_csv_up.is_verified and mainte_csv_up.is_error_message_list_empty() %}
            <div id="ulMsg">
            <div class="dataCntDisp">件数：{{mainte_csv_up.csv_data_count()}}件</div>
                <!-- 登録されるデータのリスト -->
                <table class="inputData">
                    <tbody>
                        <tr>
                            {% for column_name in mainte_csv_up.upload_data_columns() %}
                                <th>{{column_name}}</th>
                            {% endfor %}
                        </tr>
                        {% for item in mainte_csv_up.csv_upload_list %}
                            <tr>
                                {% for key, value in item.items() %}
                                    <td>{{value}}</td>
                                {% endfor %}
                            </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        {% elif mainte_csv_up.is_insert %}
            <div id="ulMsg" class="footerMsg">
                {% for message in mainte_csv_up.result_message_list %}
                    {{ message }}<br>
                {% endfor %}
            </div>
        {% endif %}
    </p>
    <!-- ローディング -->
    <!-- 登録処理用 -->
    {% with progress_message = '処理中...しばらくお待ち下さい。'%}
    {% include '_loading.html' %}
    {% endwith %}
    <!-- 戻るボタン用 -->
    {% with progress_message = '', id = '_loading_for_back' %}
    {% include '_loading.html' %}
    {% endwith %}
  </body>
</html>
