<?php $data = $this->data; ?>

<?php $pager = \Config\Services::pager(); ?>

<?= view("header.php",$data); ?>
    <?= view("home/aside.php",$data); ?>
    <div class="span9" id="content" style="width:100%;">
        <div style="width:100%;min-height:700px;">
            <div style="width:100%;height:200px;_background:pink;margin:auto;">
                <div style="margin:0px 10px;margin-top:10px;padding:0px 10px;_background:pink;min-height:50px;">
                    <input disable class="input-xlarge focused" id="first_category_id_input" type="text" value="" style="display:inline;margin: 0px;width:300px;">
                    <button class="btn btn-primary">查询设备</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>
                                        <th>型号</th>
                                        <th>系统</th>
                                        <th>分类</th>
                                        <th>部门</th>
                                        <th>签借人</th>
                                        <th>操作</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                        <?php for($i = 0;$i < count($data["devices"]);$i++){ ?>
                                        <tr dev_id='<?= $data["devices"][$i]["id"] ?>'>
                                            <td><?= $i +1 ?></td>
                                            <td><?= $data["devices"][$i]["dev_name"] ?></td>
                                            <td><?= $data["devices"][$i]["dev_code"] ?></td>
                                            <td><?= $data["devices"][$i]["dev_brand"] ?></td>
                                            <td><?= $data["devices"][$i]["dev_model"] ?></td>
                                            <td><?= $data["devices"][$i]["dev_system"] ?></td>
                                            <td first_category_id="<?= $data["devices"][$i]["first_category_id"] ?>"><?= $data["devices"][$i]["category_name"] ?></td>
                                            <td><?= $data["devices"][$i]["org_section_name"] ?></td>
                                            <td><span style="padding:4px;" class="label label-success" title="系统用户">签借人</span></td>
                                            <td>
                                                <button class="btn btn-primary btn-mini" onclick="gotoShowDevDetail(this)">详情</button>
                                                <?php if($data["devices"][$i]["borrow_status"] != 2){ ?>
                                                <button userId='<?php if($data["userInfo"] != []){ echo $data["userInfo"]["id"];}else{ echo "0";} ?>' class="btn btn-primary btn-mini"  href="#showBorrowWindow" data-toggle="modal" onclick="showBorrowWindow(this)">签借</button>
                                                <?php } ?>
                                            </td>
                                        </tr>
                                        <?php } ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                        <div class="pagination" style="text-align:right;padding-right:30px;">
                            <div>
                                <div style="display:inline;">
                                    <select id="per_page" size="1" style="width:100px;margin:0px;float:right;height:30px;margin-left:5px;" onchange="perPageChange()">
                                        <option value="10" <?php if($data["pageInfo"]["perPage"] == 15){ echo 'selected="selected"';}?>>10 条/页</option>
                                        <option value="20"  <?php if($data["pageInfo"]["perPage"] == 20){ echo 'selected="selected"';}?>>20 条/页</option>
                                        <option value="50" <?php if($data["pageInfo"]["perPage"] == 50){ echo 'selected="selected"';}?>>50 条/页</option>
                                        <option value="100" <?php if($data["pageInfo"]["perPage"] == 100){ echo 'selected="selected"';}?>>100 条/页</option>
                                    </select>
                                </div>
                                <?= $pager->makeLinks($data["pageInfo"]["page"], $data["pageInfo"]["perPage"],$data["pageInfo"]["total"]) ?>
                            </div>
                        </div>
                    </div>
                    <!-- /block -->           
                </div>
            </div>
        </div>
    </div>

    <!-- 展示签借弹窗 -->
    <div id="showBorrowWindow" 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">
            <?php if($data["userInfo"] != []){ ?>
            <table>
                <tr>
                    <td><label>签借原因：</label></td>
                    <td><textarea style="width:380px;height:40px;margin-bottom:0px;" id="borrow_comment"></textarea></td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <button id="send_borrow_button" devId="" userId="<?= $data["userInfo"]["id"] ?>" user_name="<?= $data["userInfo"]["nick"] ?>" data-dismiss="modal" class="btn btn-primary" style="width:100%;" onclick="sendBorrow(this)">确 定</button>
                    </td>
                </tr>
            </table>
            <?php }else{ ?>
            <table>
                <tr>
                    <td><label>输入名字：</label></td>
                    <td><input class="input-xlarge focused" id="user_name" type="text" value="" style="display:inline;margin: 0px;width:380px;"></td>
                </tr>
                <tr>
                    <td><label>签借原因：</label></td>
                    <td><textarea style="width:100%;height:40px;margin-bottom:0px;" id="borrow_comment"></textarea></td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <button id="send_borrow_button" userId="0" devId="" data-dismiss="modal" class="btn btn-primary" style="width:380px;" onclick="sendBorrow(this)">确 定</button>
                    </td>
                </tr>
            </table>
            <?php } ?>
        </div>
    </div>

<style>
.modal-body table tr td label,input,select {
    margin: 10px 0px;
}

.pagination {
    margin: 0px 0px;
}
</style>

<script>
/*****************************************************
*          改变页码后，刷新页面
*****************************************************/
function perPageChange(){
    var perPage = $("#per_page").val();
    var host = window.location.host;
    var oriParam = window.location.search;
    var paramStr = oriParam.substring(1);
    paramArr = paramStr.split("&");
    param = {};                            
    for(var i = 0;i < paramArr.length;i++){
        keyVal = paramArr[i].split("=");
        param[keyVal[0]] = keyVal[1];
    }
    var url = window.location.href.split("?")[0];
    delete param["perPage"];
    for(var key in param){
        if(url.indexOf("?") !== -1){
            url = url + "&" + key + "=" + param[key];
        }else{
            url = url + "?" +  key + "=" + param[key];
        }
    }
    if(url.indexOf("&") !== -1){
            url = url + "&perPage=" + perPage;
        }else{
            url = url + "?perPage=" + perPage;
        }
    window.location.href = url;
}

/*****************************************************
*          进入设备详情页面
*****************************************************/
function gotoShowDevDetail(e){
    var devId = $(e).parent().parent().attr("dev_id");
    var host = window.location.host;
    var username = $.cookie('username');
    var token = $.cookie('token');
    if(username == undefined){
        username = "";
    }
    if(token == undefined){
        token = "";
    }
    window.location.href = "http://" + host + "/home/showDevDetail" + "?username=" + username + "&token=" + token + "&devId=" + devId;
}

/*****************************************************
*          展示签借弹窗
*****************************************************/
function showBorrowWindow(e){
    var userId = $(e).attr("userId");
    if(userId == 0){
        var devId = $(e).parent().parent().attr("dev_id");
        $("#send_borrow_button").attr("devId",devId);
    }else{
        var devId = $(e).parent().parent().attr("dev_id");
        $("#send_borrow_button").attr("devId",devId);
    }
}

/*****************************************************
*          发送签借信息
*****************************************************/
function sendBorrow(e){
    var userId = $(e).attr("userId");
    var devId = $(e).attr("devId");
    var comment = $("#borrow_comment").val();
    if(userId == 0){
        var borrower = $(e).attr("user_name");
    }else{
        var borrower = $(e).attr("user_name");
    }
    var host = window.location.host;
    var url = "http://" + host + "/devManageCtr/addDevBorrowInfo"
    var postData = {};
    postData["userId"] = userId;
    postData["devId"] = devId;
    postData["comment"] = comment;
    postData["borrower"] = borrower;
    $.ajax({
        url: url,
        data: JSON.stringify(postData),
        type: "DELETE",
        dataType: "json",
        contentType:"application/json",
        success: function(data) {
            if(data.status == "200"){
                location.reload();
            }else{
                alert(data.message);
            }
        }
    });
}
</script>
        

        
