<?php include_once(mvc::$cfg['PATH_TPL'].'public/headBase.php');?>
<button onclick="addItem()" style="margin:10px" class="btn btn-warning">添加一行</button>
<table class="table table-bordered table-hover" style="margin: 0px 10px 10px 10px;width: 500px;">
    <thead>
    <tr>
        <th style="display: none" width="60px;" class="center">ID</th>
        <th width="200px;" class="center">项目名称</th>
        <th width="80px;" class="center">排序</th>
        <th width="120px;" class="center">操作</th>
    </tr>
    </thead>
    <tbody>
    <?php if(!empty($list)){ ?>
        <?php foreach($list as $k => $v){ ?>
            <tr id="<?php echo $v['reps_id']; ?>">
                <td style="display: none"><?php echo $v['reps_id'];?></td>
                <td><input style="min-width: 230px;" maxlength="16" class="form-control reps_name" type="text" value="<?php echo $v['reps_name'];?>"></td>
                <td><input  style="width:70px;" oninput="value = value.replace(/[^\d]/g,'');if(value>32767)value=32767" type="number" class="form-control reps_seq" value="<?php echo $v['reps_seq'];?>"></td>
                <td>
                    <button class="btn btn-primary" onclick="saveItem(this)">保存</button>
                    <button style="margin-left: 20px;" class="btn btn-danger" onclick="deleteItem(this)">删除</button>
                </td>
            </tr>
        <?php } ?>
    <?php }else{ ?>
        <tr>
            <td colspan="15"><div class="shoplist-error-msg">无相关维修厂信息！</div></td>
        </tr>
    <?php } ?>
    </tbody>
</table>
<?php include_once(mvc::$cfg['PATH_TPL'].'public/footBase.php');?>
<?php echo $page;?>

<script>
    function addItem()
    {
        var html = '<tr><td style="display: none"></td><td><input style="min-width: 230px;" maxlength="16" class="form-control reps_name" type="text" value=""></td><td><input  style="width:70px;" oninput="value = value.replace(/[^\\d]/g,\'\');if(value>32767)value=32767" type="number" class="form-control reps_seq" value=""></td> <td> <button class="btn btn-primary" onclick="saveItem(this)">保存</button><button style="margin-left: 20px;" class="btn btn-danger" onclick="deleteItem(this)">删除</button></td></tr>';
        $('.table').append(html);
    }

    function deleteItem(that)
    {
        var id = $(that).parent().parent().find("td:first").text();
        if(id <= 0){
            $(that).parent().parent().remove();
            return true;
        }
        MESSAGE.confirm("是否需要删除?","confirmDelete("+id+")");
    }
    function confirmDelete(reps_id)
    {
        var data = {};
        data['reps_id'] = reps_id;
        data['isAjax'] = 1;
        $.post(ADMIN_URI+'pjlWxc/deleteServe',data,function(res){
            if(MESSAGE.ajaxError(res)){
                $("#"+reps_id).remove();
                MESSAGE.alert(res.data);
            }
        },'json');
    }

    function saveItem(that)
    {
        var data = {};
        data['reps_id'] =   $(that).parent().parent().find("td:first").text();
        data['reps_name'] = $(that).parent().parent().find(".reps_name").val();
        data['reps_seq'] = $(that).parent().parent().find(".reps_seq").val();
        data['isAjax'] = 1;
        $.post(ADMIN_URI+'pjlWxc/editServe',data,function(res){
            if(MESSAGE.ajaxError(res)){
                if(data['reps_id'] <= 0){
                    $(that).parent().parent().attr("id",res.data);
                    $(that).parent().parent().find("td:first").text(res.data);
                }
                MESSAGE.alert("保存成功");
            }
        },'json');
    }
</script>
