<?php 
/*include_once "../ichat/admin/route.php";
require_once 'database.php';
if(isset($_SESSION[$elhash]) && $_SESSION[$elhash] != "") { 
    $random_hash = $_SESSION[$elhash];
} else {
    $random_hash = uniqid();
    $_SESSION[$elhash] = $random_hash;
}*/

session_start();
require_once "check_admin.php";
require_once 'database.php';
class edit_cenas extends database
{
   public function index($data,$id)
    {
		$where['id']= $id;
		return $this->update('cenas',$data, $where);
	}
	public function Getdata($id)
    {
		$where['id']= $id;
		return $this->fetchRow('cenas', $where);
	}

    public function getALlItems($id)
    {
        $where['cenas_id'] = $id;
        return $this->fetchAll('cenas_item', $where, 'nr', 'ASC', NULL, NULL, 'unsigned');
    }
}
$dev = new edit_cenas();	
$msg="";
if(!isset($_GET['id']))
{
	  header('Location:list_cenas.php');
}
$id=$_GET['id'];
if(isset($_POST['data']))
{
	$data=$_POST['data'];
	if(trim($data['name']) == '')
	{
		$msg.= "Device name is required<br/>";
	}

	if($msg=="")
	{
		 $res = $dev->index($data,$id);
            if($res)
            {
                    header('Location:list_cenas.php?message=Cenas Updated Successfully');
            }
            else {
                $msg = 'Something Went Wrong';
            }
	}
}
 $res1 = $dev->Getdata($id);
 $items = $dev->getAllItems($id);
include 'header.php';
?>
<div class="ordes-content">
    <div class="container midle-order">
        <div class="row">
        	<?php include 'menu.php'; ?>
            <div class="col-md-9">
                <div class="order-wraper">
                	<div class="col-md-12">
                    	<h1>Update Devices</h1>
						<?php if($msg !=''){echo '<span class="alert alert-danger pull-left">'.$msg.'</span>';}?>
                    </div>
                    <div class="clearfix"></div>
                    <div class="col-sm-12">
                    	<div class="order-contact">
                        	
                            <div class="row">
							
							<form method="post">
                            	<div class="col-md-12">
                                	<label> Name</label>
                                    <input type="text" name="data[name]" placeholder="Device Name" value="<?php echo htmlspecialchars($res1['name']);?>"/>
                                </div>
                            	
                            	
                                <div class="col-md-12 botm-cont">
                                	<button type="submit" class="track-btn">Update</button>
                                </div>
							</form>
                            <div class="table-responsive col-xs-12 ">
                                <table class="table table-striped table-condensed">
                                    <thead>
                                    <tr>
                                        <th>
                                            NR
                                        </th>
                                        <th>
                                            DARBĪBA
                                        </th>
                                        <th>
                                            PAKALPOJUMU CENA
                                        </th>
                                        <th>
                                            KOMPONENTES CENA
                                        </th>
                                        <th style="width:80px">
                                            Action
                                        </th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr>
                                        <td>
                                            <input class="form-control data-nr" placeholder="NR" />
                                        </td>
                                         <td>
                                            <input class="form-control data-dariba" placeholder="DARBĪBA" />
                                        </td>
                                         <td>
                                            <input class="form-control data-pakalpojumu" placeholder="PAKALPOJUMU CENA" />
                                        </td>
                                         <td>
                                            <input class="form-control data-kompnentes" placeholder="KOMPONENTES CENA" />
                                        </td>
                                        <td>
                                            <button type="button" class="btn btn-success btn-xs btn-add"><i class="fa fa-plus"></i></button>
                                            <div class="error_text"></div>
                                        </td>
                                    </tr>
                                    <?php
                                    if(!empty($items))
                                    {
                                        foreach($items AS $item)
                                        {
                                            ?>
                                            <tr>
                                                <td>
                                                    <input class="form-control data-nr" placeholder="NR" value="<?php echo htmlspecialchars($item['nr']) ?>" />
                                                </td>
                                                 <td>
                                                    <input class="form-control data-dariba" placeholder="DARBĪBA" value="<?php echo htmlspecialchars($item['dariba']) ?>"/>
                                                </td>
                                                 <td>
                                                    <input class="form-control data-pakalpojumu" placeholder="PAKALPOJUMU CENA" value="<?php echo htmlspecialchars($item['pakalpojumu_cena']) ?>"/>
                                                </td>
                                                 <td>
                                                    <input class="form-control data-kompnentes" placeholder="KOMPONENTES CENA" value="<?php echo htmlspecialchars($item['komponentes_cena']) ?>"/>
                                                </td>
                                                <td>
                                                    <button type="button" class="btn btn-success btn-xs btn-update" data-id="<?php echo htmlspecialchars($item['id']) ?>"><i class="fa fa-edit"></i></button>
                                                    <button type="button" class="btn btn-danger btn-xs btn-delete" data-id="<?php echo htmlspecialchars($item['id']) ?>"><i class="fa fa-trash-o"></i></button>
                                                    <div class="error_text"></div>
                                                </td>
                                            </tr>
                                            <?php
                                        }
                                    }
                                    ?>
                                </tbody>
                                </table>
                            </div>  
                            </div>
                        </div>
                    </div>
                </div>
                
            </div>
        </div>
	</div>
</div>

<script>
    $(document).ready(function(){
        $('.activ-cat').removeClass('activ-cat');
        $('#cenas-menu').addClass('activ-cat');


    }); 


    $(document).on('click', '.btn-add', function(){
        var current = $(this);
        $(current).attr('disabled', 'disabled');
        var parent = $(this).parents('tr').first();
        var data = new Object();
        data['nr'] = $(parent).find('.data-nr').val();
        data['dariba'] = $(parent).find('.data-dariba').val();
        data['pakalpojumu_cena'] = $(parent).find('.data-pakalpojumu').val();
        data['komponentes_cena'] = $(parent).find('.data-kompnentes').val();
        $(parent).find('.error_text').html('<div class="alert alert-warning">Saving Item.....</div>');
        $.post('cenas_add_item_ajax', {data:data, id:'<?php echo $id ?>' }, function(){
            $(parent).find('.error_text').html('<div class="alert alert-success">Item added Successfully</div>');
            location.reload();
        },'json').fail(function(xhr){
            $(current).removeAttr('disabled');
           $(parent).find('.error_text').html('<div class="alert alert-danger">' + xhr.responseJSON.error_text + '</div>');

        },'json');

    });

    $(document).on('click', '.btn-update', function(){
        var id = $(this).attr('data-id');
        var current = $(this);
        $(current).attr('disabled', 'disabled');
        var parent = $(this).parents('tr').first();
        var data = new Object();
        data['nr'] = $(parent).find('.data-nr').val();
        data['dariba'] = $(parent).find('.data-dariba').val();
        data['pakalpojumu_cena'] = $(parent).find('.data-pakalpojumu').val();
        data['komponentes_cena'] = $(parent).find('.data-kompnentes').val();
        $(parent).find('.error_text').html('<div class="alert alert-warning">Updating Item.....</div>');
        $.post('cenas_update_item_ajax', {data:data, id:id }, function(){
            $(current).removeAttr('disabled');
            $(parent).find('.error_text').html('<div class="alert alert-success">Item updated Successfully</div>');
        },'json').fail(function(xhr){
            $(current).removeAttr('disabled');
           $(parent).find('.error_text').html('<div class="alert alert-danger">' + xhr.responseJSON.error_text + '</div>');

        },'json');

    });

    $(document).on('click', '.btn-delete', function(){
         var conf = confirm("Are you sure you want to delete this item?");
            if(!conf)
            {
                return;
            }
        var id = $(this).attr('data-id');
        var current = $(this);
        $(current).attr('disabled', 'disabled');
        var parent = $(this).parents('tr').first();
        var data = new Object();
        data['nr'] = $(parent).find('.data-nr').val();
        data['dariba'] = $(parent).find('.data-dariba').val();
        data['pakalpojumu_cena'] = $(parent).find('.data-pakalpojumu').val();
        data['komponentes_cena'] = $(parent).find('.data-kompnentes').val();
        $(parent).find('.error_text').html('<div class="alert alert-warning">Deleting Item.....</div>');
        $.post('cenas_delete_item_ajax', {data:data, id:id }, function(){
            $(parent).remove();
            
        },'json').fail(function(xhr){
            $(current).removeAttr('disabled');
           $(parent).find('.error_text').html('<div class="alert alert-danger">' + xhr.responseJSON.error_text + '</div>');

        },'json');

    });
</script>

<?php include 'footer.php';?>