<?php $this->view('ajax_loading_v'); ?>
<script type="text/javascript">
    $(function(){
        load_div_grid();

        $("div.pagination a").live("click",function(event){
            var p = {};
            p['txtsearch'] = $('#txtsearch').val();

            event.preventDefault();
            var url=$(this).attr("href");
            if(url!=undefined){
                $('#div_grid').load(url,p);
            }
            return false;
        });


        $('#txtsearch').focus();
        $('#txtsearch').keydown(function(e){
            if((e.keyCode==13)){
                load_div_grid();
                return false;
            }
        });
        
        $("#myform").validate({
            submitHandler: function() {
                save();
            }
        });
    });

    function load_div_grid(){
        var p = {};
        p['txtsearch'] = $('#txtsearch').val();
        $('#div_grid').load("<?php echo site_url('equip/ajax_get_grid') ?>",p);
        return false;
    }

    function save(){
        var p = {};
        p['id'] = $('#id').val();
        p['name'] = $('#name').val();
        p['detail'] = $('#detail').val();
        $.ajax({
            data:p,
            url:"<?php echo site_url('equip/ajax_save') ?>",
            type:'POST',
            dataType:'json',
            success: function(data){
                if(data.msg=='1'){
                    $('#alert_frm').text(data.msg_text).show();
                }else{
                    $('#myModal').modal('hide');
                    load_div_grid();
                }
            },
            error:function(){
                alert('ไม่สามารถทำรายการได้ !!');
            }
        });
        return false;
    }

    function grid_btn_edit(id){
        clear_form();
        var p = {};
        p['id'] = id;
        $.ajax({
            data:p,
            url:"<?php echo site_url('equip/ajax_get_data') ?>",
            type:'POST',
            dataType:'json',
            success: function(data){
                $('#id').val(data.id);
                $('#name').val(data.name);
                $('#detail').val(data.detail);

                $('#myModal').modal('show');
            },
            error:function(){
                alert('ไม่สามารถทำรายการได้ !!');
            }
        });
        return false;
    }
    
    function grid_btn_del(id){
        if(confirm('คุณต้องการลบข้อมูลรายการนี้ ใช่หรือไม่?')){
            var p = {};
            p['id'] = id;
            $.ajax({
                data:p,
                url:"<?php echo site_url('equip/ajax_delete') ?>",
                type:'POST',
                dataType:'json',
                success: function(){
                    load_div_grid();
                },
                error:function(){
                    alert('ไม่สามารถทำรายการได้ !!');
                }
            });
        }
        return false;
    }
    
    function clear_form(){
        $('label.error,#alert_frm').css('display', 'none');
        $('input').removeClass('error');

        $('#id').val('');
        $('#name').val('');
        $('#detail').val('');
        return false;
    }
</script>
<div class="textbox">
    <div class="toolbar">
        <div class="pull-left title"><?php echo $page_title; ?></div>
        <div class="pull-right">
            <a href="#myModal" role="button" class="btn" data-toggle="modal" onclick="return clear_form();"><i class="icon-plus"></i> เพิ่มข้อมูล</a>
            <?php echo anchor('main', "<i class='icon-remove-circle'></i> ปิด", array('class' => 'btn')); ?>
        </div>
        <div class="clear"></div>
    </div>
    <div class="textbox_content">
        <div class="table-search">
            <?php echo form_open('', array('class' => 'form-inline')); ?>
            <label><b>ค้นหา</b></label>
            <div class="input-append">
                <input class="span4" id="txtsearch" name="txtsearch" type="text">
                <button class="btn" type="button" onclick="return load_div_grid();"><i class="icon-search"></i> ค้นหา</button>
            </div>
            <?php echo form_close(); ?>
        </div>
        <div id="div_grid"></div>
    </div>
</div>

<!-- หน้าจอเพิ่มข้อมูล -->
<div class="modal hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <form method="post" class="form-horizontal" id="myform">
        <div class="modal-header" style="padding:10px;">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
            <h4><?php echo $page_title; ?></h4>
        </div>
        <div class="modal-body" style="padding:10px;">
            <div class="alert alert-error" style="display:none" id="alert_frm">
            </div>
            <input type="hidden" id="id"/>
            <table border="0" cellpadding="4" cellspacing="1" class="tblform">
                <tbody>
                    <tr>
                        <td width="120" class="td1">ชื่ออุปกรณ์ :</td>
                        <td>
                            <input type='text' id="name" name='name' style="width:350px" maxlength="50" required/>
                            <span class="required">*</span>
                        </td>
                    </tr>
                    <tr>
                        <td class="td1">รายละเอียด :</td>
                        <td>
                            <textarea id="detail" name="detail" rows="4" cols="20" style="width:350px"><?php echo @$row['detail']; ?></textarea>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
        <div class="modal-footer">
            <button class="btn" data-dismiss="modal" aria-hidden="true"> ปิด </button>
            <button class="btn btn-primary" type="submit"><i class="icon-hdd icon-white"></i> บันทึกข้อมูล</button>
        </div>
    </form>
</div>
