<div class="title">新增學生</div>
<ul class="table-toolbar" style="text-align: left;">
    <li><a class="toolbar-element btn btn-default btn-create" href="/student"><i style="margin-right: 8px;"><span class="iconify" data-icon="typcn-arrow-back" data-inline="false"></span></i>返回</a></li>
</ul>
<div class="form-container">
    <form class="form-horizontal" role="form" method="post" action="/student">
        <div class="form-group mas-col">
            <div class="col-sm-2 text-center mas-title"><label for="code">學生號碼</label></div>
            <div class="col-sm-10 text-center mas-title mas-box "><input id=code type="text" class="form-control mas-input" disabled name="code" value="<?= $default['student_no']; ?>" /></div>
        </div>
        <div class="form-group mas-col">
            <div class="col-sm-2 text-center mas-title"><label for="chi_name">中文姓名*</label></div>
            <div class="col-sm-10 text-center mas-title mas-box "><input id=chi_name type="text" class="form-control mas-input" disabled name="chi_name" value="<?= $default['chi_name']; ?>" /></div>
        </div>
        <div class="form-group mas-col">
            <div class="col-sm-2 text-center mas-title"><label for="eng_name">英文姓名*</label></div>
            <div class="col-sm-10 text-center mas-title mas-box "><input id=eng_name type="text" class="form-control mas-input" disabled name="eng_name" value="<?= $default['eng_name']; ?>" /></div>
        </div>
        <div class="form-group mas-col">
            <div class="col-sm-2 text-center mas-title"><label for="gender">性別</label></div>
            <div class="col-sm-10 text-center mas-title mas-box ">
                <select class="select mas-select" name="gender" disabled>
                    <option value="" selected="selected">--- 請選擇性別 ---</option>
                    <?php
                    foreach ($gender as $idx => $g) {
                        if ($idx == $default['gender']) {
                            $selected = ' selected="selected"';
                        } else {
                            $selected = '';
                        }
                        echo sprintf('<option value="%s"%s>%s</option>', $idx, $selected, $g);
                    }
                    ?>
                </select>
            </div>
        </div>
        <div class="form-group mas-col">
            <div class="col-sm-2 text-center mas-title"><label for="dob">出生日期</label></div>
            <div class="col-sm-10 text-center mas-title mas-box "><input id=dob type="text" class="form-control mas-input" disabled name="dob" value="<?= $default['dob']; ?>" /></div>
        </div>
        <fieldset>
            <div class="form-group">
                <div class="col-sm-2 text-center mas-group-title">
                    <label>主要聯絡人*</label>
                </div>
            </div>
            <div class="form-group mas-col">
                <div class="col-sm-2 text-center mas-title">
                    <label for="content1_name">姓名</label>
                </div>
                <div class="col-sm-4 mas-box">
                    <input type="text" name="content1_name" class="form-control mas-input" placeholder="" disabled="disabled" value="<?= $default['content1_name']; ?>">
                    <p class="help-block red hidden">Error Msg</p>
                </div>
                <div class="col-sm-2 text-center mas-title">
                    <label for="content1_relationship">親屬關係</label>
                </div>
                <div class="col-sm-4 mas-box">
                    <select name="content1_relationship" class="select mas-select" disabled="disabled">
                        <option value="0">--- 請選擇親屬關係 ---</option>
                        <?php
                        foreach ($relationship as $idx => $g) {
                            if ($idx == $default['content1_relationship']) {
                                $selected = ' selected="selected"';
                            } else {
                                $selected = '';
                            }
                            echo sprintf('<option value="%s"%s>%s</option>', $idx, $selected, $g);
                        }
                        ?>
                    </select>
                    <p class="help-block red hidden">Error Msg</p>
                </div>
            </div>
            <div class="form-group mas-col">
                <div class="col-sm-2 text-center mas-title">
                    <label for="content1_phone">聯絡電話</label>
                </div>
                <div class="col-sm-4 mas-box">
                    <input type="text" name="content1_phone" class="form-control mas-input" placeholder="" disabled="disabled" value="<?= $default['content1_phone']; ?>">
                    <p class="help-block red hidden">Error Msg</p>
                </div>
                <div class="col-sm-2 text-center mas-title">
                    <label for="content1_email">電郵</label>
                </div>
                <div class="col-sm-4 mas-box">
                    <input type="email" name="content1_email" class="form-control mas-input" placeholder="" disabled="disabled" value="<?= $default['content1_email']; ?>">
                    <p class="help-block red hidden">Error Msg</p>
                </div>
            </div>
        </fieldset>
        <fieldset>
            <div class="form-group">
                <div class="col-sm-2 text-center mas-group-title">
                    <label>次要聯絡人</label>
                </div>
            </div>
            <div class="form-group mas-col">
                <div class="col-sm-2 text-center mas-title">
                    <label for="content2_name">姓名</label>
                </div>
                <div class="col-sm-4 mas-box">
                    <input type="text" name="content2_name" class="form-control mas-input" placeholder="" disabled="disabled" value="<?= $default['content2_name']; ?>">
                    <p class="help-block red hidden">Error Msg</p>
                </div>
                <div class="col-sm-2 text-center mas-title">
                    <label for="content2_relationship">親屬關係</label>
                </div>
                <div class="col-sm-4 mas-box">
                    <select name="content2_relationship" class="select mas-select" disabled="disabled">
                        <option value="0">--- 請選擇親屬關係 ---</option>
                        <?php
                        foreach ($relationship as $idx => $g) {
                            if ($idx == $default['content2_relationship']) {
                                $selected = ' selected="selected"';
                            } else {
                                $selected = '';
                            }
                            echo sprintf('<option value="%s"%s>%s</option>', $idx, $selected, $g);
                        }
                        ?>
                    </select>
                    <p class="help-block red hidden">Error Msg</p>
                </div>
            </div>
            <div class="form-group mas-col">
                <div class="col-sm-2 text-center mas-title">
                    <label for="content2_phone">聯絡電話</label>
                </div>
                <div class="col-sm-4 mas-box">
                    <input type="text" name="content2_phone" class="form-control mas-input" placeholder="" disabled="disabled" value="<?= $default['content2_phone']; ?>">
                    <p class="help-block red hidden">Error Msg</p>
                </div>
                <div class="col-sm-2 text-center mas-title">
                    <label for="content2_email">電郵</label>
                </div>
                <div class="col-sm-4 mas-box">
                    <input type="email" name="content2_email" class="form-control mas-input" placeholder="" disabled="disabled" value="<?= $default['content2_email']; ?>">
                    <p class="help-block red hidden">Error Msg</p>
                </div>
            </div>
        </fieldset>
        <fieldset>
            <div class="form-group">
                <div class="col-sm-2 text-center mas-group-title">
                    <label>備用聯絡人</label>
                </div>
            </div>
            <div class="form-group mas-col">
                <div class="col-sm-2 text-center mas-title">
                    <label for="content3_name">姓名</label>
                </div>
                <div class="col-sm-4 mas-box">
                    <input type="text" name="content3_name" class="form-control mas-input" placeholder="" disabled="disabled" value="<?= $default['content3_name']; ?>">
                    <p class="help-block red hidden">Error Msg</p>
                </div>
                <div class="col-sm-2 text-center mas-title">
                    <label for="content3_relationship">親屬關係</label>
                </div>
                <div class="col-sm-4 mas-box">
                    <select name="content3_relationship" class="select mas-select" disabled="disabled">
                        <option value="0">--- 請選擇親屬關係 ---</option>
                        <?php
                        foreach ($relationship as $idx => $g) {
                            if ($idx == $default['content3_relationship']) {
                                $selected = ' selected="selected"';
                            } else {
                                $selected = '';
                            }
                            echo sprintf('<option value="%s"%s>%s</option>', $idx, $selected, $g);
                        }
                        ?>
                    </select>
                    <p class="help-block red hidden">Error Msg</p>
                </div>
            </div>
            <div class="form-group mas-col">
                <div class="col-sm-2 text-center mas-title">
                    <label for="content3_phone">聯絡電話</label>
                </div>
                <div class="col-sm-4 mas-box">
                    <input type="text" name="content3_phone" class="form-control mas-input" placeholder="" disabled="disabled" value="<?= $default['content3_phone']; ?>">
                    <p class="help-block red hidden">Error Msg</p>
                </div>
                <div class="col-sm-2 text-center mas-title">
                    <label for="content3_email">電郵</label>
                </div>
                <div class="col-sm-4 mas-box">
                    <input type="email" name="content3_email" class="form-control mas-input" placeholder="" disabled="disabled" value="<?= $default['content3_email']; ?>">
                    <p class="help-block red hidden">Error Msg</p>
                </div>
            </div>
        </fieldset>
        <div class="form-group mas-col">
            <div class="col-sm-2 text-center mas-title">
                <label>居住地區</label>
            </div>
            <div class="col-sm-2 mas-box">
                <select class="select mas-select" name="district_id" id="residentDistrict" disabled="disabled">
                    <option value="">--- 請選擇區域 ---</option>
                    <?php
                    foreach ($location as $loc) {
                        if ($default['district_id'] == $loc['id']) {
                            $selected = ' selected="selected"';
                        } else {
                            $selected = '';
                        }
                        echo sprintf('<option value="%s"%s>%s</option>', $loc['id'], $selected, $loc['chi_name']);
                    }
                    ?>
                </select>
                <p class="help-block red hidden">Error Msg</p>
            </div>
            <div class="col-sm-4 mas-box">
                <select class="select mas-select" name="area_id" id="residentArea" disabled="disabled">
                    <option value="">--- 請選擇地區 ---</option>
                    <?php
                    foreach ($district as $dist) {
                        if ($default['area_id'] == $dist['id']) {
                            $selected = ' selected="selected"';
                        } else {
                            $selected = '';
                        }
                        echo sprintf('<option value="%s"%s>%s</option>', $dist['id'], $selected, $dist['chi_name']);
                    }
                    ?>
                </select>
                <p class="help-block red hidden">Error Msg</p>
            </div>
            <div class="col-sm-4 mas-box">
                <select class="select mas-select" name="zone" id="residentZone" disabled="disabled">
                    <option value="">--- 請選擇地點 ---</option>
                    <?php
                    foreach ($zone as $z) {
                        if ($default['zone'] == $z) {
                            $selected = ' selected="selected"';
                        } else {
                            $selected = '';
                        }
                        echo sprintf('<option value="%s"%s>%s</option>', $z, $selected, $z);
                    }
                    ?>
                </select>
                <p class="help-block red hidden">Error Msg</p>
            </div>
        </div>
        <div class="form-group mas-col">
            <div class="col-sm-2 text-center mas-title"><label for="address">住址</label></div>
            <div class="col-sm-10 text-center mas-title mas-box "><input id=address type="text" class="form-control mas-input" disabled name="address" value="<?= $default['address']; ?>" placeholder="輸入住址" /></div>
        </div>
        <div class="form-group mas-col">
            <div class="col-sm-2 text-center mas-title"><label for="school_district_id">學校所在地區</label></div>
            <div class="col-sm-10 mas-box">
                <select id="school_district_id" class="select mas-select" name="school_district_id" disabled="disabled">
                    <option value="">--- 請選擇學校所在地區 ---</option>
                    <?php
                    foreach ($school_district as $sd) {
                        if ($default['school_district_id'] == $sd['id']) {
                            $selected = ' selected="selected"';
                        } else {
                            $selected = '';
                        }
                        echo sprintf('<option value="%s"%s>%s</option>', $sd['id'], $selected, $sd['chi_name']);
                    }
                    ?>
                </select>
            </div>
        </div>
        <div class="form-group mas-col">
            <div class="col-sm-2 text-center mas-title"><label for="school_id">就讀學校</label></div>
            <div class="col-sm-10 mas-box">
                <select id="school_id" class="select mas-select" name="school_id" disabled="disabled">
                    <option value="">--- 請選擇就讀學校 ---</option>
                    <?php
                    foreach ($school as $s) {
                        if ($default['school_id'] == $s['id']) {
                            $selected = ' selected="selected"';
                        } else {
                            $selected = '';
                        }
                        echo sprintf('<option value="%s"%s>%s</option>', $s['id'], $selected, $s['chi_name']);
                    }
                    ?>
                </select>
            </div>
        </div>
        <div class="form-group mas-col">
            <div class="col-sm-2 text-center mas-title"><label for="grade">就讀年級</label></div>
            <div class="col-sm-10 mas-box">
                <select id="grade" class="select mas-select" name="grade" disabled="disabled">
                    <option value="">--- 請選擇就讀年級 ---</option>
                    <?php
                    foreach ($student_grade as $grade) {
                        if ($default['grade'] == $grade['id']) {
                            $selected = ' selected="selected"';
                        } else {
                            $selected = '';
                        }
                        echo sprintf('<option value="%s"%s>%s</option>', $grade['id'], $selected, $grade['name']);
                    }
                    ?>
                </select>
            </div>
        </div>
        <div class="form-group mas-col">
            <div class="col-sm-2 text-center mas-title"><label for="source">來源</label></div>
            <div class="col-sm-10 mas-box">
                <select id="source" class="select mas-select" name="source" disabled="disabled">
                    <option value="">--- 請選擇來源 ---</option>
                    <?php
                    foreach ($student_source as $source) {
                        if ($default['source'] == $source['id']) {
                            $selected = ' selected="selected"';
                        } else {
                            $selected = '';
                        }
                        echo sprintf('<option value="%s"%s>%s</option>', $source['id'], $selected, $source['name']);
                    }
                    ?>
                </select>
            </div>
        </div>
        <div class="form-group mas-col">
            <div class="col-sm-2 text-center mas-title"><label for="recommended_by">推薦人</label></div>
            <div class="col-sm-10 text-center mas-title mas-box "><input id=recommended_by type="text" class="form-control mas-input" disabled name="recommended_by" value="<?= $default['recommended_by']; ?>" /></div>
        </div>
        <div class="form-group mas-col">
            <div class="col-sm-2 text-center mas-title"><label for="remark">備註</label></div>
            <div class="col-sm-10 mas-box"><textarea id="remark" class="form-control mas-textarea" rows="3" name="remark" disabled><?= $default['remark']; ?></textarea></div>
        </div>
        <div class="form-group mas-col">
            <div class="col-sm-2 text-center mas-title"><label for="status">狀態*</label></div>
            <div class="col-sm-10 mas-box">
                <select id="status" class="select mas-select" name="status" disabled="disabled">
                    <option value="">--- 請選擇狀態 ---</option>
                    <?php
                    foreach ($status as $idx => $s) {
                        if ($default['status'] == $idx) {
                            $selected = ' selected="selected"';
                        } else {
                            $selected = '';
                        }
                        echo sprintf('<option value="%s"%s>%s</option>', $idx, $selected, $s);
                    }
                    ?>
                </select>
            </div>
        </div>
        <div class="form-group mas-col">
            <div class="col-sm-2 text-center mas-title">
                <label>二維碼</label>
            </div>
            <div class="col-sm-2 mas-box">
                <img src="/uploads/qrcode/<?= $default['student_no']; ?>.png" />
            </div>
        </div>
        <div class="form-footer">
            <button class="btn btn-footer submit" style="width: auto;" type="button" id='btnAutoCreate'>請為我自動建立家長帳號</button>
            <button class="btn btn-footer submit" style="width: auto;" type="button" id='btnAttach'>我要把這個學生連接系統中已有的家長帳號</button>
            <button class="btn btn-footer submit" style="width: auto;" type="button" id='btnNoThank'>暫時不用了, 謝謝</button>
        </div>
    </form>
</div>
<script>
    function getRemoteData(remote_url, data, callback, callbackFail) {
        $.ajax({
            //async: false,
            //timeout: 30000, //最長等候回應時間
            type: "GET", //提交類型
            url: remote_url, //提交地址
            data: data, //提交內容
            dataType: 'json', //返回數據類型
            success: function(data) { //請求完成並成功
                if (data.success) {
                    callback(data.data);
                } else {
                    callbackFail();
                }
            },
            error: function(data) { //請求返回錯誤信息
                callbackFail();
            },
            beforeSend: function(XHR) { //请求開始前执行
            },
            complete: function(XHR, status) { //请求完成后最终执行
            }
        });
    }

    function getRemoteDataFail() {
        alert("發生錯誤");
    }

    function buildDistrict(district) {
        $('SELECT[name="area_id"]').empty();
        $('SELECT[name="area_id"]').append(
            $('<option>').attr({
                value: "",
                selected: "selected",
            }).text('--- 請選擇地區 ---')
        );
        district.forEach(function(row) {
            $('SELECT[name="area_id"]').append(
                $('<option>').attr({
                    value: row['id'],
                }).text(row['chi_name'])
            );
        });
    }

    function buildSchool(schools) {
        $('SELECT[name="school_id"]').empty();
        $('SELECT[name="school_id"]').append(
            $('<option>').attr({
                value: "",
                selected: "selected",
            }).text('--- 請選擇就讀學校 ---')
        );
        schools.forEach(function(row) {
            $('SELECT[name="school_id"]').append(
                $('<option>').attr({
                    value: row['id'],
                }).text(row['chi_name'])
            );
        });
    }

    $(document).ready(function() {
        var li = $('#sidebar-menu li').filter(function(i) {
            return $(this).find('.menu-dropdown').find('.menu-text').text() === '學生';
        });
		/*li.addClass('active open');
        $('.date-picker').pickadate({
            format: 'yyyy-mm-dd'
        });*/
        $('SELECT[name="district_id"]').on('change', function() {
            var id = $('SELECT[name="district_id"]').val();
            getRemoteData(
                "/api/location/district", {
                    location_id: id,
                },
                buildDistrict,
                getRemoteDataFail
            );
        });

        $('SELECT[name="area_id"]').on('change', function() {
            $('SELECT[name="zone"]').empty();
            $('SELECT[name="zone"]').append(
                $('<option>').attr({
                    value: "",
                    selected: "selected",
                }).text('--- 請選擇地點 ---')
            );
            var area = $('SELECT[name="area_id"] option:selected').text().split(",");
            area.forEach(function(row) {
                $('SELECT[name="zone"]').append(
                    $('<option>').attr({
                        value: row,
                    }).text(row)
                );
            });
        });

        $('SELECT[name="zone"]').on('change', function() {
            $('INPUT[name="address"]').val($('SELECT[name="district_id"] option:selected').text() + $('SELECT[name="zone"] option:selected').text());
        });

        $('SELECT[name="school_district_id"]').on('change', function() {
            var district = $('SELECT[name="school_district_id"] option:selected').text();
            getRemoteData(
                "/api/school/school", {
                    school_district: district,
                },
                buildSchool,
                getRemoteDataFail
            );
        });
        $('#btnAutoCreate').on('click', function() {
            window.location.href = '/student/create_parent/<?= $default['student_no']; ?>';
        });

        $('#btnAttach').on('click', function() {
            window.location.href = '/student/attach_parent/<?= $default['student_no']; ?>';
        });

        $('#btnNoThank').on('click', function() {
            window.location.href = '/student';
        });
    });
</script>