<?php $data = $this->data; ?>
<?= view("header.php",$data); ?>
    <?= view("devManage/aside.php",$data); ?>
    <div class="span9" id="content" style="width:100%;">
        <div style="width:100%;min-height:700px;">
            <div style="width:auto;margin:10px 10px;">
            <div style="margin:0px 10px;margin-top:10px;padding:5px 10px;">
                <button class="btn btn-primary" href="#addFirstCatetoryWindow" data-toggle="modal">添加分类</button>
            </div>
                <div style="width:50%;height:300px;_background:pink;float:left;">
                    <!-- block -->
                    <div class="block" style="margin:0px 5px;">
                        <H4 style="padding:5px 50px;">设备分类</h4>
                        <div class="block-content collapse in" style="width:100%;margin:0px;">
                            <div class="span12" style="width:100%;margin:0px;">
                                <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["category"]["firstCategory"]);$i++){ ?>
                                    <tr id="<?= $data["category"]["firstCategory"][$i]["id"]; ?>">
                                        <td style="width:5%;"><?= $i + 1; ?></td>
                                        <td style="width:20%;"><?= $data["category"]["firstCategory"][$i]["category_name"]; ?></td>
                                        <td style="width:38%;"><?= $data["category"]["firstCategory"][$i]["detail"]; ?></td>
                                        <td style="width:37%;">
                                            <button class="btn btn-primary btn-mini" onclick="showSecondCategoryInFirstCategory(this)">显示子分类</button>
                                            <button class="btn btn-primary btn-mini" href="#addSecondCatetoryWindow" data-toggle="modal" onclick="showSecondAddWindow(this)">添加子分类</button>
                                            <button class="btn btn-primary btn-mini">编辑</button>
                                            <button class="btn btn-danger btn-mini" onclick="delFirstCategory(this)">删除</button>
                                        </td>
                                    </tr>
                                    <?php } ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                    <!-- /block -->
                </div>
                <div style="width:50%;height:300px;_background:yellow;float:left;">
                    <!-- block -->
                    <div class="block" style="margin:0px 5px;">
                        <H4 style="padding:5px 50px;"><?= $data["category"]["firstCategory"][0]["category_name"]; ?>子分类</h4>
                        <div class="block-content collapse in" style="width:100%;margin:0px;">
                            <div class="span12" style="width:100%;margin:0px;">
                                <table class="table table-striped">
                                    <thead>
                                    <tr>
                                        <th>#</th>
                                        <th>子分类名</th>
                                        <th>编辑</th>
                                    </tr>
                                    </thead>
                                    <tbody id="second_category_body">
                                    <?php for($i = 0;$i < count($data["category"]["secondCategory"]);$i++){ ?>
                                    <tr id="<?= $data["category"]["secondCategory"][$i]["id"]; ?>">
                                        <td><?= $i + 1; ?></td>
                                        <td><?= $data["category"]["secondCategory"][$i]["category_name"]; ?></td>
                                        <td>
                                            <button class="btn btn-primary btn-mini">编辑</button>
                                            <button class="btn btn-danger btn-mini" onclick="delSecondCategory(this)">删除</button>
                                        </td>
                                    </tr>
                                    <?php } ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                    <!-- /block -->
                </div>
            </div>
        </div>
    </div>
    <div id="addFirstCatetoryWindow" 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="first_category_name" type="text" value="" style="display:inline;margin: 0px;width:300px;"></td>
                </tr>
                <tr>
                    <td><label>一级分类描述：</label></td>
                    <td><textarea style="width:300px;" id="first_category_detail"></textarea></td>
                </tr>
                <tr>
                    <td></td>
                    <td><button data-dismiss="modal" class="btn btn-primary" style="width:100%;" onclick="addFirstCategory()">确定</button></td>
                </tr>
            </table>
            
        </div>
    </div>

    <div id="addSecondCatetoryWindow" 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 disable class="input-xlarge focused" disabled id="first_category_id_input" type="text" value="" style="display:inline;margin: 0px;width:300px;"></td>
                </tr>
                <tr>
                    <td><label>二级分类名：</label></td>
                    <td><input class="input-xlarge focused" id="second_category_name" type="text" value="" style="display:inline;margin: 0px;width:300px;"></td>
                </tr>
                <tr>
                    <td><label>二级分类描述：</label></td>
                    <td><textarea style="width:300px;" id="second_category_detail"></textarea></td>
                </tr>
                <tr>
                    <td></td>
                    <td><button data-dismiss="modal" class="btn btn-primary" style="width:100%;" onclick="addSecondCategory()">确定</button></td>
                </tr>
            </table>
        </div>
    </div>
    <?= view("footer.php",$data); ?>

<style>
label {
    display: inline;
}
td {
    padding: 2px 0px;
}
</style>

<script>
/*****************************************************
*          添加一级分类
*****************************************************/
function addFirstCategory(){
    var category_name = $("#first_category_name").val();
    var detail = $("#first_category_detail").val();
    var host = window.location.host;
    var url = "http://" + host + "/devManageCtr/addFirstCategory"
    var postData = {};
    postData["category_name"] = category_name;
    postData["detail"] = 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{
                
            }
        }
    });
}

function showSecondAddWindow(e){
    var second_category_id = $(e).parent().parent().attr("id");
    $("#first_category_id_input").val(second_category_id);

}

/*****************************************************
*          添加二级分类
*****************************************************/
function addSecondCategory(){
    var first_category_id = $("#first_category_id_input").val();
    var category_name = $("#second_category_name").val();
    var detail = $("#second_category_detail").val();
    var host = window.location.host;
    var url = "http://" + host + "/devManageCtr/addSecondCategory"
    var postData = {};
    postData["category_name"] = category_name;
    postData["detail"] = detail;
    postData["first_category_id"] = first_category_id;
    $.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 delFirstCategory(e){
    var devId = $(e).parent().parent().attr("id");
    var host = window.location.host;
    var url = "http://" + host + "/devManageCtr/delAnFirstCategoryItem"
    var postData = {};
    postData["id"] = devId;
    var isSure = confirm("确定删除该部门?");
    if(isSure == true){
        $.ajax({
            url: url,
            data: JSON.stringify(postData),
            type: "DELETE",
            dataType: "json",
            contentType:"application/json",
            success: function(data) {
                if(data.status == "200"){
                    location.reload();
                }else{
                    
                }
            }
        });
    }
}

/*****************************************************
*          删除二级分类
*****************************************************/
function delSecondCategory(e){
    var devId = $(e).parent().parent().attr("id");
    var host = window.location.host;
    var url = "http://" + host + "/devManageCtr/delAnSecondCategoryItem"
    var postData = {};
    postData["id"] = devId;
    var isSure = confirm("确定删除该部门?");
    if(isSure == true){
        $.ajax({
            url: url,
            data: JSON.stringify(postData),
            type: "DELETE",
            dataType: "json",
            contentType:"application/json",
            success: function(data) {
                if(data.status == "200"){
                    location.reload();
                }else{
                    
                }
            }
        });
    }
}
/*****************************************************
*          展示一级分类下的二级分类
*****************************************************/
function showSecondCategoryInFirstCategory(e){
    var categoryId = $(e).parent().parent().attr("id");
    var host = window.location.host;
    var url = "http://" + host + "/devManageCtr/getSecondCategoryByFirstCategoryId"
    var postData = {};
    postData["first_category_id"] = categoryId;
    $.ajax({
        url: url,
        data: JSON.stringify(postData),
        type: "DELETE",
        dataType: "json",
        contentType:"application/json",
        success: function(data) {
            if(data.status == "200"){
                var container_secondCategory = $("#second_category_body");
                container_secondCategory.empty();
                var items = data.data;
                for(var i = 0 ;i < items.length ;i++){
                    var content = "<tr id='" + items[i].id + "'>";
                    content = content + "<td>" + (i + 1) + "</td>";
                    content = content + "<td>" + items[i].category_name + "</td>";
                    content = content + '<td><button class="btn btn-primary btn-mini">编辑</button>';
                    content = content + '<button class="btn btn-danger btn-mini" onclick="delSecondCategory(this)">删除</button></td>';
                    content = content + '</tr>';
                    container_secondCategory.append(content);
                    container_secondCategory.trigger("create");
                    $("tr").trigger("create");
                    $("td").trigger("create");
                    $("button").trigger("create");
                }
            }else{
                
            }
        }
    });
}
</script>

        
