<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">添加学生</button>
<!-- 模态框（Modal） -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">

    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title " id="myModalLabel "><span class="glyphicon glyphicon-user"></span> 学生信息录入</h4>
            </div>
            <form class="form-horizontal" role="form" action="action.php?a=add" method="post">
                <div class="modal-body">

                    <div class="form-group">
                        <label for="firstname" class="col-sm-2 control-label">姓名</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="firstname"
                                   placeholder="请输入名字" name="s_name">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">性别</label>
                        <div class="col-sm-10">


                            <select class="  form-control" name="s_sex">
                                <option lass="form-control" value="0" selected>未知</option>
                                <option class="form-control" value="1">男</option>
                                <option lass="form-control" value="2">女</option>


                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="age" class="col-sm-2 control-label">年龄</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="age"
                                   placeholder="请输年龄" name="s_age">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">科目</label>
                        <div class="col-sm-10">

                            <select class="  form-control" name="s_sub">

                                <?php require_once 'action.php';
                                $sql->showSubject();


                                ?>


                            </select>
                        </div>
                    </div>


                </div>
                <div class="modal-footer">
                    <div class="alert alert-success alert-dismissable" style="display: none">
                        <button type="button" class="close" data-dismiss="alert"
                                aria-hidden="true">
                            &times;
                        </button>
                        <span>学生添加成功</span>
                    </div>
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary" id="sub">确定</button>
                </div>
            </form>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>
<div class="modal fade" id="delete">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">

            <!-- 模态框头部 -->
            <div class="modal-header">
                <h4 class="modal-title "><span class="btn btn-danger  glyphicon glyphicon-trash "
                                               style="margin-left: 10px;padding: 6px 10px;cursor: initial;"></span> 刪除用戶
                </h4>

            </div>

            <!-- 模态框主体 -->
            <div class="modal-body" id="deuser">
                模态框内容..
            </div>
            <div class="alert alert-success alert-dismissable" style="display: none">
                <button type="button" class="close" data-dismiss="alert"
                        aria-hidden="true">
                    &times;
                </button>
                <span>删除成功</span>
            </div>
            <!-- 模态框底部 -->
            <div class="modal-footer">

                <button type="button" class=" btn btn-secondary" data-dismiss="modal">取消</button>
                <button class="btn btn-primary" id="debutton">确定</button>
            </div>


        </div>
    </div>
</div>
<div class="modal fade" id="edit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">

    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title " id="myModalLabel1 "><span class="glyphicon glyphicon-edit"></span> 学生信修改</h4>
            </div>
            <form class="form-horizontal" role="form" action="action.php?a=add" method="post">
                <div class="modal-body">

                    <div class="form-group">
                        <label for="firstname" class="col-sm-2 control-label">姓名</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control"
                                   placeholder="请输入名字" name="s_name">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">性别</label>
                        <div class="col-sm-10">


                            <select class="  form-control" name="s_sex" id="editsex">
                                <option lass="form-control" value="0">未知</option>
                                <option class="form-control" value="1">男</option>
                                <option lass="form-control" value="2">女</option>


                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="age" class="col-sm-2 control-label">年龄</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control"
                                   placeholder="请输年龄" name="s_age">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">科目</label>
                        <div class="col-sm-10">

                            <select class="  form-control" name="s_sub" id="edisb">

                                <?php require_once 'action.php';
                                     $sql->showSubject();
                                ?>


                            </select>
                        </div>
                    </div>


                </div>
                <div class="modal-footer">
                    <div class="alert alert-success alert-dismissable" style="display: none">
                        <button type="button" class="close" data-dismiss="alert"
                                aria-hidden="true">
                            &times;
                        </button>
                        <span>信息修改成功</span>
                    </div>
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary" id="editsub">确定</button>
                </div>
            </form>

        </div><!-- /.modal-content -->
    </div><!-- /.modal -->

</div>


<script>
    //删除
    function deleteuser(uid, uname) {

        $("#deuser").text("姓名：" + uname);
        $("#debutton").click(function () {
            $.post("/action.php?a=delete", {
                'uid': uid
            }, function (data, status) {
                if (status == 'success') {

                    $("#delete .alert-success").show();
                    setTimeout(function () {
                        window.location.reload();
                    }, 500)

                } else {
                    alert('删除失败');
                }

            });
        })

    }

    function edituser(a) {
        console.log(a)

        $("#edit input[name='s_name']").val(a['uname']);

        index = parseInt(a['usexv']);

        $("#editsex option").eq(index).attr('selected', 'selected')
        $("#edit input[name='s_age']").val(a['uage']);
        $("#edisb option").attr('selected', false);
        $("#edisb option").eq(parseInt(a['usubv']) - 1).attr('selected', 'selected');


        $("#editsub").click(function () {


            $.post("/action.php?a=edit", {
                'a': $("#edit input[name='s_name']").val(),
                'b': parseInt($("#edit select[name='s_sex']").val()),
                'c': parseInt($("#edit input[name='s_age']").val()),
                'd': parseInt($("#edit select[name='s_sub']").val()),
                'e': parseInt(a['uid'])


            }, function (data, status) {
                if (status == 'success') {

                    $("#edit .alert-success").show();
                    setTimeout(function () {
                        window.location.reload();
                    }, 600)


                } else {
                    alert('修改失败');
                }

            })
        })


    }

    $(document).ready(function () {


        $("#sub").click(function () {

            $('#myModal .form-horizontal').submit();
            $("#myModal .alert-success").show();


        })


    })
</script>