<?php $data = $this->data; ?>
<?= view("header.php",$data); ?>
    <?= view("orgManage/aside.php",$data); ?>
    <div class="span9" id="content" style="width:100%;">
        <div style="width:100%;min-height:700px;">
            <div style="margin:0px 10px;margin-top:10px;padding:0px 10px;">
                <button class="btn btn-primary" href="#addOrgSectionWindow" data-toggle="modal">添加部门</button>
            </div>
            <div class="row-fluid" style="width:auto;margin:0px 10px;">
                <!-- block -->
                <div class="block">
                    <div class="block-content collapse in">
                        <div class="span12">
                            <table class="table table-striped">
                                <thead>
                                <tr>
                                    <th>#</th>
                                    <th>部门名</th>
                                    <th>部门描述</th>
                                    <th>编辑</th>
                                </tr>
                                </thead>
                                <tbody>
                                <?php for($i = 0;$i < count($data["orgSections"]);$i++){ ?>
                                <tr id="<?= $data["orgSections"][$i]["id"] ?>">
                                    <td><?= $i + 1 ?></td>
                                    <td><?= $data["orgSections"][$i]["org_section_name"] ?></td>
                                    <td><?= $data["orgSections"][$i]["org_section_detail"] ?></td>
                                    <td>
                                        <button class="btn btn-primary btn-mini" href="#editOrgSectionWindow" data-toggle="modal" onclick="showEdtiWindow(this)">编辑</button>
                                        <button class="btn btn-danger btn-mini" onclick="delOrgSection(this)">删除</button>
                                    </td>
                                </tr>
                                <?php } ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
                <!-- /block -->           
            </div>
        </div>
    </div>
    <div id="addOrgSectionWindow" class="modal hide" style="width:500px;transform: translateX(-50%);margin:0px;left:50%;">
        <div class="modal-header">
            <button data-dismiss="modal" class="close" type="button">&times;</button>
            <h3>添加部门</h3>
        </div>
        <div class="modal-body">
            <table>
                <tr>
                    <td><label>部门名：</label></td>
                    <td><input class="input-xlarge focused" id="org_section_name" type="text" value="" style="display:inline;margin: 0px;width:300px;"></td>
                </tr>
                <tr>
                    <td><label>部门描述：</label></td>
                    <td><textarea style="width:300px;" id="org_section_detail"></textarea></td>
                </tr>
                <tr>
                    <td></td>
                    <td><button data-dismiss="modal" class="btn btn-primary" style="width:100%;" onclick="addOrgSection()">确定</button></td>
                </tr>
            </table>
        </div>
    </div>

    <div id="editOrgSectionWindow" class="modal hide" style="width:500px;transform: translateX(-50%);margin:0px;left:50%;">
        <div class="modal-header">
            <button data-dismiss="modal" class="close" type="button">&times;</button>
            <h3>编辑部门</h3>
        </div>
        <div class="modal-body">
            <table>
                <tr>
                    <td><label>部门id：</label></td>
                    <td><input class="input-xlarge focused" id="org_section_id_edit" disabled type="text" value="" style="display:inline;margin: 0px;width:300px;"></td>
                </tr>
                <tr>
                    <td><label>部门名：</label></td>
                    <td><input class="input-xlarge focused" id="org_section_name_edit" type="text" value="" style="display:inline;margin: 0px;width:300px;"></td>
                </tr>
                <tr>
                    <td><label>部门描述：</label></td>
                    <td><textarea style="width:300px;" id="org_section_detail_edit"></textarea></td>
                </tr>
                <tr>
                    <td></td>
                    <td><button data-dismiss="modal" class="btn btn-primary" style="width:100%;" onclick="updateOrgSection()">确定</button></td>
                </tr>
            </table>
        </div>
    </div>
    <?= view("footer.php",$data); ?>
        
<script>
/*****************************************************
*          添加部门
*****************************************************/
function addOrgSection(){
    var org_section_name = $("#org_section_name").val();
    var org_section_detail = $("#org_section_detail").val();
    var host = window.location.host;
    var url = "http://" + host + "/orgManageCtr/addOrgSection"
    var postData = {};
    postData["org_section_name"] = org_section_name;
    postData["org_section_detail"] = org_section_detail;
    if(org_section_name == ""){
        alert("部门名不能为空！");
    }else{
        $.ajax({
            url: url,
            data: JSON.stringify(postData),
            type: "POST",
            dataType: "json",
            contentType:"application/json",
            success: function(data) {
                if(data.status == "200"){
                    location.reload();
                }else{
                    
                }
            }
        });
    }
}

/*****************************************************
*          删除部门
*****************************************************/
function delOrgSection(e){
    var orgSectionId = $(e).parent().parent().attr("id");
    var host = window.location.host;
    var url = "http://" + host + "/orgManageCtr/delOrgSection"
    var postData = {};
    postData["org_section_id"] = orgSectionId;
    var isSure = confirm("确定删除该部门?");
    if(isSure == true){
        $.ajax({
            url: url,
            data: JSON.stringify(postData),
            type: "POST",
            dataType: "json",
            contentType:"application/json",
            success: function(data) {
                if(data.status == "200"){
                    location.reload();
                }else{
                    
                }
            }
        });
    }
}

/*****************************************************
*          展示编辑弹窗
*****************************************************/
function showEdtiWindow(e){
    var org_section_id = $(e).parent().parent().attr("id");
    var org_section_name = $($(e).parent().prevAll()[1]).text();
    var org_section_detail = $($(e).parent().prevAll()[0]).text();
    $("#org_section_id_edit").val(org_section_id);
    $("#org_section_name_edit").val(org_section_name);
    $("#org_section_detail_edit").val(org_section_detail);
}


/*****************************************************
*          编辑部门
*****************************************************/
function updateOrgSection(){
    var org_section_id = $("#org_section_id_edit").val();
    var org_section_name = $("#org_section_name_edit").val();
    var org_section_detail = $("#org_section_detail_edit").val();
    var host = window.location.host;
    var url = "http://" + host + "/orgManageCtr/updateOrgSection"
    var postData = {};
    postData["org_section_id"] = org_section_id;
    postData["org_section_name"] = org_section_name;
    postData["org_section_detail"] = org_section_detail;
    $.ajax({
        url: url,
        data: JSON.stringify(postData),
        type: "POST",
        dataType: "json",
        contentType:"application/json",
        success: function(data) {
            if(data.status == "200"){
                location.reload();
            }else{
                
            }
        }
    });
}

</script>
    