<?php
session_start();
include ('../inc/dbcon.php');
include ('../inc/functions.php');
include ('../inc/repfunctions.php');
if(isset($_SESSION["axeslogin"]) && $_SESSION["axeslogin"] == true){
$aid=$_SESSION['uid'];
$brid=$_SESSION['abrid'];    
}else{
header('Location:../index.php');
exit;     
}
$dtnow = date("Y-m-d h:i:s", time());
?>
<?php
if(isset($_POST['addseritem'])){
$name = remove_junk(escape($_POST['name']));
$mobile = remove_junk(escape($_POST['mobile']));

if(isset($_POST['mobile'])){
$sql="SELECT * FROM tbl_acmobile WHERE mobile = '$mobile'";           
$ducode = mysqli_query($con,$sql);
}
    
if($ducode->num_rows > 0) {
echo json_encode(array(
'status' => 'error',
'message'=> 'Sorry! Mobile Account Already Exists!!'
));
return;
exit;
}else{ 
$sql="INSERT INTO tbl_acmobile (name,mobile,brid,uid,date) VALUES ('$name','$mobile','$brid','$aid','$dtnow')";
$result = mysqli_query($con,$sql) or die(mysqli_error($con));
$pid=$con->insert_id;    
$efid=mysqli_affected_rows($con);    
if($efid>0){    
$act =remove_junk(escape('Mobile Account: '.$name.'-'.$mobile));    
write_activity($aid,'MOB','Mobile account has been created',$act);    
echo json_encode(array(
'status' => 'success',
'message'=> 'Data Save Successfully!!'   
));
exit;
}else{
echo json_encode(array(
'status' => 'error',
'message'=> 'Sorry! Data Fail to Saved!!'
));    
} 
}   
}

if(isset($_POST['editseritem'])){
$name = remove_junk(escape($_POST['name']));
$mobile = remove_junk(escape($_POST['mobile']));    
$itemid = remove_junk(escape($_POST['itmid']));;
 
if(isset($_POST['mobile'])){
$sql="SELECT * FROM tbl_acmobile WHERE mobile = '$mobile' AND id!='$itemid'";           
$ducode = mysqli_query($con,$sql);
}
    
if($ducode->num_rows > 0) {
echo json_encode(array(
'status' => 'error',
'message'=> 'Sorry! Mobile Account Already Exists!!'
));
return;
exit;
}else{ 
$sql="UPDATE tbl_acmobile SET name='$name',mobile='$mobile' WHERE id='$itemid'";
$result = mysqli_query($con,$sql) or die(mysqli_error($con));
$pid=$con->insert_id;    
$efid=mysqli_affected_rows($con);    
if($efid>0){    
$act =remove_junk(escape('Mobile Account: '.$name.'-'.$mobile));    
write_activity($aid,'MOB','Mobile Account has been Updated',$act);    
echo json_encode(array(
'status' => 'success',
'message'=> 'Data Update Successfully!!'   
));
exit;
}else{
echo json_encode(array(
'status' => 'error',
'message'=> 'Sorry! Data Fail to Update!!'
));    
} 
}   
}
?> 
<?php 
if(isset($_POST['addsitem'])){ 
$item=intval($_POST['item']);   
?>
<div class="col-md-12 popup_details_div addservice">
<div class="row">
<div id="server-results"><!-- For server results --></div>    
</div>
<div class="row">
<div class="col-md-12">
<div class="col-md-1"></div>
<div class="col-md-10">
<?php if($item <=0){ ?>     
<div class="form-group">
<label>Name</label>
<input type="text" maxlength="25" value="" name="name" id="name" class="form-control" placeholder="e.g. bKash"/>
<input type="hidden" name="addseritem" readonly /> 
</div>
<div class="form-group">
<label>Mobile</label>
<input type="text" maxlength="18" value="" name="mobile" id="mobile" class="form-control" placeholder="e.g. 0161617xx7x"/>
</div>    
<?php 
}else{ 
$sql="SELECT * FROM tbl_acmobile WHERE id='".$item."' ORDER BY id ASC LIMIT 1";    
$admin=mysqli_query($con,$sql) or die(mysqli_error($con));    
$adm=mysqli_fetch_array($admin);    
?>
<div class="form-group">
<label>Name</label>
<input type="text" maxlength="25" value="<?php echo $adm['name'];?>" name="name" id="name" class="form-control" placeholder="e.g. bKash"/>
<input type="hidden" maxlength="11" value="<?php echo $adm['id'];?>" class="form-control" name="itmid" autocomplete="off" readonly>
<input type="hidden" name="editseritem" readonly />     
</div>
<div class="form-group">
<label>Mobile</label>
<input type="text" maxlength="18" value="<?php echo $adm['mobile'];?>" name="mobile" id="mobile" class="form-control" placeholder="e.g. 0161617xx7x"/>
</div>    
<?php } ?>   
</div>    
<div class="col-md-1"></div>    
</div>    
</div>    

</div>
<div class="clearfix" ></div>
<div class="col-md-12 nopadding widgets_area"></div>    
<div class="row"style="margin-top: 15px" >
<div class="col-md-8"></div>
<div class="col-md-4 text-right" >
<input type="button" id="additem" class="btn btn-flat bg-purple btn-sm " value="<?php if($item <=0){echo 'Save';}else{echo 'Update';} ?>"/>
</div> 
</div>
<script type="text/javascript">

function chek_error(){
var result = true;
var name = $('#name').val();
var mobile = $('#mobile').val();    


$('.LV_invalid_field').removeClass('LV_invalid_field');     
$('.LV_validation_message').remove();     

if(name.length<1){
$('#name').addClass('LV_invalid_field');   
$('#name').after("<span class='LV_validation_message LV_invalid'>Can't be empty!</span>").addClass('has-error'); 
result=false;
}else{
$('#name').removeClass('LV_invalid_field');
result=true;    
}    

if(mobile.length<1){
$('#mobile').addClass('LV_invalid_field');   
$('#mobile').after("<span class='LV_validation_message LV_invalid'>Can't be empty!</span>").addClass('has-error'); 
result=false;
}else{
$('#mobile').removeClass('LV_invalid_field');
result=true;    
}    
    
if(name.length<1 || mobile.length<1 || !result){
return false;    
}else{
return true;     
}        
}
    
$(document).on('blur', '#name, #mobile', function() {
chek_error();    
});    
</script> 

<?php } ?>

<?php if(isset($_POST['viewitem'])){
$sql="SELECT * FROM tbl_acmobile ORDER BY name ASC";    
$query=mysqli_query($con,$sql)or die(mysqli_error($con));
while ($row=mysqli_fetch_array($query)){
$id='MO'.$row['id'];    
$ldebit=get_ledgerval($id,'D','N');
$lcredit=get_ledgerval($id,'C','N');
$lnet=($ldebit-$lcredit);    
?>
<tr>
<td class="text-center"><?php echo count_id();?></td>
<td><?php echo $row['name'];?></td>      
<td><?php echo $row['mobile'];?></td>   
<td><?php echo numtolocal($ldebit,get_fild_data('tbl_currency','1','symbol'));?></td>
<td><?php echo numtolocal($lcredit,get_fild_data('tbl_currency','1','symbol'));?></td>
<td><?php echo numtolocal($lnet,get_fild_data('tbl_currency','1','symbol'));?></td>   
<td nowrap="">
<a class="btn btn-flat bg-purple details-invoice" href="#" id="MO_<?php echo $row['id']; ?>"><i class="fa fa-edit cat-child"></i></a>    
<a class="btn btn-flat bg-purple" href="#" onclick="remove_item('DL_<?php echo $row['id'];?>')"><i class="fa fa-trash"></i></a>
</td>    
</tr>    
<?php }} ?>

<?php 
if(isset($_POST['viewitemhis'])){
if($_SESSION['access']=='1'){echo read_activity($aid,'MOB','A');}else{echo read_activity($aid,'MOB','U');}
}?>