<?php include "header.php"; 
$gst_value = 3;

$statement_price = $pdo->prepare("SELECT tbl_prices.*,tbl_ids_purity.short_name
                    FROM `tbl_prices`
                    LEFT JOIN tbl_ids_purity ON tbl_ids_purity.id=tbl_prices.purity_id
                    WHERE tbl_prices.product_id=?");
$statement_price->execute(array(1));
$price_data = $statement_price->fetchAll(PDO::FETCH_ASSOC);
$price_totalData = $statement_price->rowCount(); 

?>

<style>

    .search-box{
        width: auto;
        display: inline-block;
        font-size: 14px;
    }
    .search-box {
        padding: 5px 10px;
        border: 1px solid #CCCCCC;
        font-size: 14px;
    }
    .result{
        /*position: absolute; */       
        z-index: 999;
        top: 100%;
        left: 0;
    }
    .search-box, .result{
        width: 100%;
        box-sizing: border-box;
    }
    /* Formatting result items */
    .result div{
        margin: 0;
        background: white;
        padding: 7px 10px;
        border: 1px solid #CCCCCC;
        border-top: none;
        cursor: pointer;
    }
    .result div:hover{
        background: blue;
        color: white;
    }
</style>

<style>
    thead{
        background: antiquewhite;
    }
    th{
        padding: 5px;
        text-align: center;
    }
    td{
        text-align: center;
    }
</style>
        

<div class="content-wrapper">
    
        <section class="content-header">
            <h1>Add Stock Transfer</h1>
            <ol class="breadcrumb">
                <li><a href="index.php"><i class="fa fa-dashboard"></i> Home</a></li>
                <li class="active">Add Stock Transfer</li>            
            </ol>
        </section>
        


        <div class="col-lg-12 alerts">
            <div id="custom-alerts" style="display:none;">
                <div class="alert alert-dismissable">
                    <div class="custom-msg"></div>
                </div>
            </div>
            
            <?php if ($error)  { ?>
            <div class="alert alert-danger alert-dismissable">
                <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
                <h4><i class="icon fa fa-ban"></i> Error</h4>
                <?= $error; ?>
            </div>
            <?php $error=0;} if ($warning) { ?>
            <div class="alert alert-warning alert-dismissable">
                <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
                <h4><i class="icon fa fa-warning"></i> Warning</h4>
                <?= $warning; ?>
            </div>
            <?php  $warning=0;} if ($message) { ?>
            <div class="alert alert-success alert-dismissable">
                <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
                <h4>    <i class="icon fa fa-check"></i> Success</h4>
                <?= $message; ?>
            </div>
            <?php $message=0; } ?>
        </div>
        <div class="clearfix"></div>







<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box box-primary" style="box-shadow: rgba(0, 0, 0, 0.16) 0px 3px 6px, rgba(0, 0, 0, 0.23) 0px 3px 6px;">
                <div class="box-body">
                    
                    
                    
                <div id="content-div">    
                    <div class="col-lg-12">

                        <form id="form_details" autocomplete="off">
                                                
                                                
                                                                 
                        <div class="row">
                            
                           
                            
                            
                            
                            <div class="col-md-12">
                                
                                
                                
                                
                    <div class="row">
                        
                            
                            
                            
                            
                            
                            <div class="col-md-2">
                                <div class="form-group">
                                    <label class="required" for="date">Date</label>                                    
                                    <input type="date" name="date" value="<?=date('Y-m-d')?>"  class="form-control" id="date"  required="required" />
                                </div>
                            </div>
                            
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label class="optional" for="reference_no">Reference No</label>                                    
                                    <input type="text" name="reference_no" value="" placeholder="Enter the Reference No" class="form-control" id="reference_no"  />
                                </div>
                            </div>
                            
                            
                            
                            <div class="col-md-2" hidden>
                                <div class="form-group">
                                    <label class="required" for="transfer_status">Status</label>                                   
                                    <select name="transfer_status" class="form-control select2" style="width:100%" id="transfer_status" required="required">
                                        <option value="1" <?php if(isset($_GET['transfer_status']) && $_GET['transfer_status']=="1"){ echo "selected"; } ?>>Completed</option>
                                        <option value="0" <?php if(isset($_GET['transfer_status']) && $_GET['transfer_status']=="0"){ echo "selected"; } ?>>Pending</option>
                                        <option value="2" <?php if(isset($_GET['transfer_status']) && $_GET['transfer_status']=="2"){ echo "selected"; } ?>>In-Transit</option>
                                    </select>
                                </div>
                            </div>
                            
                            
                            
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label class="required" for="sales_man_code">Sales Man</label>                                   
                                    <select name="sales_man_code" class="form-control select2" style="width:100%" id="sales_man_code" required="required">
                                        <?php $statement = $pdo->prepare("SELECT * FROM `tbl_users` where status=1");
                                            $statement->execute();
                                            $location_result = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                            foreach ($location_result as $result) { ?>
                                                <option value="<?=$result['id']?>" <?php if($session_id==$result['id']){echo "selected";}?>><?=$result['name']?> - <?=$result['emp_code']?></option>
                                        <?php  }  ?>
                                    </select>
                                </div>
                            </div>
                            
                            
                    </div> 
                                
                                
                    
                    <div class="row">
                        <div class="col-md-6">
                                <div class="form-group">
                                    <label class="required" for="from_location_id">From Locations</label>                                   
                                    <select name="from_location_id" class="form-control select2" style="width: 100%;" id="from_location_id" required="required">
                                        <?php 
                                            $statement = $pdo->prepare("SELECT bb.* FROM `tbl_business_branch` bb WHERE bb.status=1");
                                            $statement->execute();
                                            $location_result = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                            foreach ($location_result as $result) { ?>
                                                <option value="<?=$result['id']?>"><?=$result['location_name']?></option>
                                        <?php  }  ?>
                                    </select>
                                </div>
                        </div>
                        
                        <div class="col-md-6">
                                <div class="form-group">
                                    <label class="required" for="to_location_id">To Locations</label>                                   
                                    <select name="to_location_id" class="form-control select2" style="width: 100%;" id="to_location_id" required="required">
                                        <?php 
                                            $statement = $pdo->prepare("SELECT bb.* FROM `tbl_business_branch` bb WHERE bb.status=1");
                                            $statement->execute();
                                            $location_result = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                            foreach ($location_result as $result) { ?>
                                                <option value="<?=$result['id']?>"><?=$result['location_name']?></option>
                                        <?php  }  ?>
                                    </select>
                                </div>
                        </div>
                    </div>
                            
                                
                                <div class="form-group search-box" style="margin-top: 30px;">
                                    <label class="" for="search_products">Search Products</label>   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                       
                                    <input type="text" name="search_products" placeholder="Enter the Products" value=""  class="form-control" id="search_products"  />
                                    <div class="result" style="max-height: 200px; overflow-y: scroll;"></div>
                                </div>
                                
                                <table style="width: 100%;" >
                                    <thead>
                                        <tr>
                                            <th style="width: 10%">Product&nbsp;Name</th>
                                            <th style="width: 10%">Karat</th>
                                            <th style="width: 7%">Was(%)</th>
                                            <th style="width: 15%">Cross - Stone - Net</th>
                                            <th style="width: 10%">Rates</th>
                                            <th style="width: 8%">Product</th>
                                            <th style="width: 8%">Stone</th>
                                            <th style="width: 8%">Wastage</th>
                                            <th style="width: 10%">Total</th>
                                            <th style="width: 5%"><i class="fa fa-times"></i></th>
                                        </tr>
                                    </thead>
                                    <tbody id="product_tbody">
                                        
                                    </tbody>
                                </table>
                                

                                
                            </div>
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            <div class="col-md-12" style="margin-top: 30px;">
                                
                                
                                
        <!-- Hidden Values Starts Here -->         
        
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <label class="required" for="total_amount">Total Amount</label>                                   
                                        <input type="text" name="total_amount" placeholder="Total Amount" value="0" class="form-control" id="total_amount"  required="required"  readonly />
                                    </div>
                                </div>
                                
                                <div class="col-md-6" style="margin-top: 10px;" hidden>
                                    <div class="form-group" style="">
                                        <label for="discount_type">Discount Type <span style="color: red">*</span></label>
                                        <select name="discount_type" id="discount_type" onchange="discountType()" class="form-control select22" style="width: 100%;">
                                            <option value="1">Percentage</option>
                                            <option value="2">Fixed Amount</option>
                                        </select>
                                    </div>
                                </div>
                                
                                
                                <div class="col-md-6" style="margin-top: 10px;" hidden>
                                    <div class="form-group">
                                        <label class="optional" for="discount">Discount <span id="discount_span"></span></label>                                   
                                         <input type="text" name="discount" placeholder="Discount" value="0"  oninput="calculateTotal()" class="form-control" id="discount"  />
                                    </div>
                                </div>
                                
                                <div class="col-md-12" hidden><b style="color: blue;">Discount (<i class="fa fa-inr"></i>) : <span id="discount_amount_span">0.00</span></b></div>
                                
                                <input type="hidden" name="discount_amount" id="discount_amount" value="" >
                                <input type="hidden" name="old_product_amount" id="old_product_amount" value="" >
                                
        <!-- Hidden Values Endsss Here -->
                                
                                

                                
                                
                                
                                
                                
                                
                                
                            </div>
                            
                            
                           
                            
                            
                            
                        </div>
                        
                        
                        

                        <div class="form-group" style="text-align:center;">
                            <input type="submit"  id="submit" value="Add Stock Transfer" class="btn btn-success" />
                        </div>
                        
                        </form>

                    </div>
                    
                    
                        <div class="form-group" style="text-align:center;">
                            <span onclick="location.reload();" class="btn btn-primary" ><i class="fa fa-refresh"></i> Reset Form</span>
                            <a href="stock-transfer-list.php"><button class="btn btn-warning" ><i class="fa fa-chevron-left"></i> Back to List</button></a>
                        </div>
                </div>
                
                
<div class="container" id="loading-div" style="display: none; margin-top: 40px; margin-bottom: 40px;">
        <center> <img src="img/loading.gif" style="width: 250px;"><br><b class="blinking">Processing Please Wait...</b></center>
</div>
                    
                    
                    
                    
                    <div class="container" id="success-div" style="display: none; margin-top: 40px; margin-bottom: 40px;">
                        <center><b class="blinking" style="font-size:22px;">Stock Transfer Added Successfully...</b><br>
                            <img src="img/success.gif" style="width: 250px;">
                            <br><br>
                            <span onclick="location.reload();" class="btn btn-info"><i class="fa fa-refresh"></i> Reset Form</span>
                            &nbsp;&nbsp;
                            <a href="stock-transfer-list.php"><button class="btn btn-warning"><i class="fa fa-chevron-left"></i> Back to List</button></a>
                        </center>
                    </div>
                    
            
                        
                        
                    <div class="clearfix"></div>
                </div>
            </div>
        </div>
    </div>
</section>
<?php include "footer.php"; ?>


<script>

    function grand_total(){
        var tot=0;
          $(".total").each(function(){
            tot+=Number($(this).val());
          });
          $("#total_amount").val(tot);
    }
    
    
    $(document).on('click', '.remove_product', function() {
        $(this).closest('.porow').remove();
        productCount();
        grand_total();
    });
    

    
        


function productCount(){
    var rowCount = $('#product_tbody tr').length;
    console.log("Product Row Count: "+rowCount);
    $('#total_product_items').text(rowCount);
    $('#payment_total_items').text(rowCount);
}
</script>



<script>
$(document).ready(function() {
    
   $('#form_details').on('submit', function(e){
                e.preventDefault();
                $('#loading-div').show();
                $('#content-div').hide();
                $("#submit").prop('disabled', true);
                var formData = new FormData(this);
                formData.append('type', 'create_stock_transfer');
                    $.ajax({
                            url: 'apis/stock_transfer.php',
                            type: 'post',
                            dataType: 'json',
                            data: formData,
                            cache: false,
                            contentType: false,
                            processData: false,
                            enctype: 'multipart/form-data',
                            success: function (response) {
                                console.log(response);
                                if(response['status']==200){
                                     toastr.success(response['message']);
                                    //  $('#success-div').show();
                                     $("#submit").prop('disabled', false);
                                     $('#loading-div').hide();
                                     setTimeout(function(){ window.location.href="stock-transfer-list.php"; },1500);
                                }else{
                                    toastr.error(response['message']);
                                    $("#submit").prop('disabled', false);
                                    $('#loading-div').hide();
                                    $('#content-div').show();
                                }
                            }
                        });
    });
    
});
</script>



<script>
function product_selection(id, quantity){ 
    $('.search-box input[type="text"]').val('');
    $(".result").empty();
    $('.search-box input[type="text"]').focus();
    console.log("Product Selection ID: "+id);
    
    
    var product_present = 0;
    $('.product_ids').each(function(){
        if(id==$(this).val()){
            product_present = 1;
        }
    });
    
    if(product_present==0){
            $.ajax({
                url: 'api/product-search-append-api.php',
                type: 'post',
                dataType: 'text',
                data:{pro_id:id, quantity:quantity },
                success: function (response) {
                    console.log('Id   :'+id);
                    $(".select22").select2();
                    console.log('Response   :'+response);
                    $('#product_tbody').append(response);
                    $('.search-box input[type="text"]').val('');
                    $(".result").empty();
                    $('.search-box input[type="text"]').focus();
                    productCount();
                    grand_total();
                }
            });
    }else{
        alert("Product Already Added");
    }
}

$(document).ready(function(){
    var n='';
    n=parseFloat(n) || 0;

    
    $('.search-box input[type="text"]').on("keyup input", function(){
        console.log('Search Value='+n);
        var search_text = $(this).val();
        var resultDropdown = $(this).siblings(".result");
        if(search_text.length>1){
            $.get("api/product-search-api.php", {search_text: search_text}).done(function(data){
                resultDropdown.html(data);
            });
        } else{
            resultDropdown.empty();
        }
    });
});



</script>


<script>
    $(document).ready(function() {
        $("form").bind("keypress", function(e) {
            if (e.keyCode == 13) {
                return false;
            }
        });
    });
</script>
<script>
$(document).ready(function(){
    
    
    $(".finnal_enter").keypress(function(event) {
        if(event.keyCode == 13) { 
        textboxes = $("input.finnal_enter");
        debugger;
        currentBoxNumber = textboxes.index(this);
        if (textboxes[currentBoxNumber + 1] != null) {
            nextBox = textboxes[currentBoxNumber + 1]
            nextBox.focus();
            nextBox.select();
            event.preventDefault();
            return false 
            }
        }
    });
    
    
    
});
</script>

<style>
    .checkboxx{
        position: absolute;
        top: -20%;
        left: -20%;
        display: block;
        width: 140%;
        height: 140%;
        margin: 0px;
        padding: 0px;
        background: rgb(255, 255, 255);
        border: 0px;
        opacity: 0;
    }
</style>