<div class="title">連結學生</div>
<ul class="table-toolbar" style="text-align: left;">
    <li><a class="toolbar-element btn btn-default btn-create btnLink"><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">
        <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>登入名稱</label>
                </div>
                <div class="col-sm-10 mas-box">
                    <input type="text" class="form-control mas-input" disabled="disabled" value="<?= $parent['username']; ?>" />
                </div>
            </div>
            <div class="form-group mas-col">
                <div class="col-sm-2 text-center mas-title">
                    <label>中文名</label>
                </div>
                <div class="col-sm-10 mas-box">
                    <input type="text" class="form-control mas-input" disabled="disabled" value="<?= $parent['chi_name']; ?>" />
                </div>
            </div>
            <div class="form-group mas-col">
                <div class="col-sm-2 text-center mas-title">
                    <label>英文姓名</label>
                </div>
                <div class="col-sm-10 mas-box">
                    <input type="text" class="form-control mas-input" disabled="disabled" value="<?= $parent['eng_name']; ?>" />
                </div>
            </div>
            <div class="form-group mas-col">
                <div class="col-sm-2 text-center mas-title">
                    <label>電話</label>
                </div>
                <div class="col-sm-10 mas-box">
                    <input type="text" class="form-control mas-input" disabled="disabled" value="<?= $parent['mobile']; ?>" />
                </div>
            </div>
            <div class="form-group mas-col">
                <div class="col-sm-2 text-center mas-title">
                    <label>電郵</label>
                </div>
                <div class="col-sm-10 mas-box">
                    <input type="email" class="form-control mas-input" disabled="disabled" value="<?= $parent['email']; ?>" />
                </div>
            </div>
        </fieldset>
        <?php echo $search_student; ?>

        <div class="form-footer">
            <a class="btn btn-footer reset" href="javascript:window.location.reload(true)">重設</a>
            <button class="btn btn-footer submit" type="button" id='btnAttach'>連結</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 attachStudent() {
        var student_code = $('input[name="student_code"]:checked').val();
        if (!student_code) {
            alert("請先選擇一位學生");
            return;
        }
        getRemoteData(
            "/api/attachstudent", {
                'username': '<?= $parent['username']; ?>',
                'student_code': student_code,
            },
            attachStudentSuccess,
            getRemoteDataFail
        );
    }

    function attachStudentSuccess(data) {
        alert("學生已連結");
        goBack();
    }

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

    function goBack() {
        var newForm = $('<form>', {
            'action': '/parent/edit',
            'method': 'post',
        }).append($('<input>', {
            'name': 'id',
            'value': <?= $parent['id']; ?>,
            'type': 'hidden'
        }));
        $(document.body).append(newForm);
        newForm.submit();
    }

    $(document).ready(function() {
        $("#btnAttach").on('click', function() {
            attachStudent();
        });

        $(".btnLink").on('click', function() {
            goBack();
        });
    });
</script>