@extends('backend.layouts.master')

@section('title', 'Stock List')

@push('css')
<style>
button#btn {
    margin-top: 24px;
}
</style>
<link rel="stylesheet" href="{{asset('backend/plugins/select2.min.css')}}">
@endpush
@section('content')
<div class="right_col" role="main">
	<div class="">
		@if(Session::has('success'))
			<div class="alert alert-success">
				{{ Session::get('success') }}
			</div>
		@endif
		@if(Session::has('error'))
			<div class="alert alert-danger">
				{{ Session::get('error') }}
			</div>
		@endif
		<div class="clearfix"></div>
		<div class="row">
			<div class="col-md-12 col-sm-12 col-xs-12">
				<div class="x_panel">
					<div class="x_title">
						<a href="{{ route('purchase.create')}}" calss="btn" ><h2><i class="fa fa-mail-reply"></i></h2></a>
						<h2>Stock List</h2>
						<ul class="nav navbar-right panel_toolbox">
							<li><a class="collapse-" onclick="printDiv('printableArea')" ><i class="fa fa-print fa-2x"></i></a>
							<li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a></li>
							<li class="dropdown">
								<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><i class="fa fa-wrench"></i></a>
								<ul class="dropdown-menu" role="menu">
									<li><a href="#">Settings 1</a>
									</li>
									<li><a href="#">Settings 2</a>
									</li>
								</ul>
							</li>
							<li><a class="close-link"><i class="fa fa-close"></i></a></li>
						</ul>
						<div class="clearfix"></div>
					</div>
					<div class="x_content">
						<div class="row search-section">
							<form action="{{route('stock.stocks')}}" id="searchForm" method="Post">
								{{csrf_field() }}
								<!-- <div class="col-sm-2">
									<div class="form-group">
										<label for="form-field-1"> Warehouse</label>
										<select class="form-control input-sm select2" name="warehouse" id="warehouse">
											<option value="All"> All </option>

										</select>
									</div>
								</div> -->
								<!-- <div class="col-sm-3">
									<div class="form-group">
										<label for="form-field-1"> Category Name</label>
										<select class="form-control input-sm select2" name="category" id="category">
											<option value="All"> All </option>
											@foreach($categories as $cat)
											<option value="{{$cat->id}}">{{$cat->name}}</option>
											@endforeach
										</select>
									</div>
								</div> -->
								<!-- <div class="col-sm-3">
									<div class="form-group">
										<label for="form-field-1"> Brand Name</label>
										<select class="form-control input-sm select2" name="brand" id="brand">
											<option value="All"> All </option>

										</select>
									</div>
								</div> -->
								<!-- <div class="col-sm-3">
									<div class="form-group">
										<label for="form-field-1">Product/Code No</label>
										<select class="form-control input-sm select2" name="model_no" id="product">
											<option value="All"> All </option>
										</select>
									</div>
								</div> -->
								<div class="col-sm-12">
									<div class="input-group">
										<label for="form-field-1">Search By</label>
										<input type="text" id="myInput" class="form-control" placeholder="Search">
										<span class="input-group-btn">
											<button type="button" id="btn" class="btn btn-primary">
												<span class="ace-icon fa fa-search icon-on-right bigger-110"></span>
											</button>
										</span>
									</div>
								</div>
							</form>
						</div>
						<hr />
						<div class="table-content table-responsive" id="printableArea">
							<table id="product_list" class="table table-bordered table-condensed" role="grid" aria-describedby="dynamic-table_info">
								<thead class="bg-primary">
									<tr>
										<th>SL</th>
										<th>Product</th>
										<th>Code No</th>
										<th>Category</th>
										<th>Unit</th>
										<th>Stock</th>
										<th>Action</th>
									</tr>
								</thead>
								<tbody>
									@php
										$total_stock = 0;
									@endphp
									@foreach($stocks as $key=>$pro)
									@php
										$total_stock = $total_stock + $pro->product_stock;
									@endphp
									<tr class="{{$pro->product_stock < $pro->re_order_label ? 'red' : ''}} ">
										<td>{{$key + 1}}</td>
										<td>{{$pro->product_name}}</td>
										<td>{{$pro->code}}</td>
										<td>{{$pro->category_name}}</td>
										<td>{{$pro->unit_name}}</td>
										<td>{{$pro->product_stock}}</td>
										<td>
											<a  href="{{route('stock.cutting_process',$pro->id)}}" title="Cutt the roll"><i class="fa fa-scissors bigger-130"></i></a>
										</td>
									</tr>
									@endforeach
								</tbody>
								<tfoot>
									<tr>
										<td colspan="4"></td>
										<td>Total =</td>
										<td id="total_stock">{{$total_stock}}</td>
										<td></td>
									</tr>
								</tfoot>
							</table>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
@endsection

@push('js')
<script src="{{asset('backend/plugins/select2.min.js')}}"></script>
<script>
$(document).ready(function() {
    // select2 section
    $('.select2').select2();
    /*-------search section------------*/
    // $("#myInput").on("keyup", function() {
    //     var value = $(this).val().toLowerCase();
    //     $("tbody tr").filter(function() {
    //         $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
    //     });
    // });
    /*-----------warehouse wise stock list------------*/
    {{--$("#warehouse").change(function() {
        var warehouse = $(this).val();
        var token = "{{csrf_token()}}";
        $.ajax({
            url: '{{ route('stock.wpList ') }}',
            type: 'post',
            data: { warehouse: warehouse,'_token': token},
            dataType: 'json',
            success: function(response) {
                console.log(response)
                var len = response.length;
                $("#product").empty();
                $("#product").append("<option value=''> Select Product</option>");
                if (len == 0) {
                    $("#product").empty();
                    var id = "";
                    var name = "";
                    $("#product").append("<option value='" + id + "'>" + name +
                    "</option>");
                }
                //empty the table tbody
                $("#product_list tbody tr").remove();
                var total_stock = 0;
                for (var i = 0; i < len; i++) {
                    var id = response[i]['product_id'];
                    var name = response[i]['product_name'];
                    var part_no = response[i]['model_no'];
                    var warehouse = response[i]['warehouse_name'];
                    var category = response[i]['category_name'];
                    var brand = response[i]['brand_name'];
                    var unit = response[i]['unit_name'];
                    var stock = response[i]['product_stock'];
                    total_stock = parseInt(total_stock) + parseInt(stock);
                    var disable = "";
                    $("#product").append("<option " + disable + " value='" + id + "'>" + name + " ( " + part_no + " )</option>");
                    /*--------------*/
                    $("#product_list tbody").append(
                        "<tr>" +
                        "<td>" + (i + 1) + "</td>" +
                        "<td>" + name + "</td>" +
                        "<td>" + part_no + "</td>" +
                        "<td>" + warehouse + "</td>" +
                        "<td>" + category + "</td>" +
                        "<td>" + brand + "</td>" +
                        "<td>" + unit + "</td>" +
                        "<td>" + stock + "</td>" +
                        "</tr>"
                    )
                    $('#total_stock').html(total_stock);
                }
            }
        });
    });--}}
    /***-------------category wise product stock search------------------*/
   {{--}} $("#category").change(function() {
        var category = $(this).val();
        var token = "{{csrf_token()}}";
        $.ajax({
            url: '{{ route('stock.cpList ') }}',
            type: 'post',
            data: { category: category, '_token': token },
            dataType: 'json',
            success: function(response) {
                //console.log(response)
                var len = response.length;
                $("#product").empty();
                $("#product").append("<option value=''> Select Product</option>");
                if (len == 0) {
                    $("#product").empty();
                    var id = "";
                    var name = "";
                    $("#product").append("<option value='" + id + "'>" + name +
                    "</option>");
                }
                //empty the table tbody
                $("#product_list tbody tr").remove();
                var total_stock = 0;

                for (var i = 0; i < len; i++) {
                    var id = response[i]['product_id'];
                    var name = response[i]['product_name'];
                    var part_no = response[i]['model_no'];
                    var warehouse = response[i]['warehouse_name'];
                    var category = response[i]['category_name'];
                    var brand = response[i]['brand_name'];
                    var unit = response[i]['unit_name'];
                    var stock = response[i]['product_stock'];
                    total_stock = parseInt(total_stock) + parseInt(stock);
                    var disable = "";
                    $("#product").append("<option " + disable + " value='" + id + "'>" +
                        name + " ( " + part_no + " )</option>");
                    /*--------------*/
                    $("#product_list tbody").append(
                        "<tr>" +
                        "<td>" + (i + 1) + "</td>" +
                        "<td>" + name + "</td>" +
                        "<td>" + part_no + "</td>" +
                        "<td>" + warehouse + "</td>" +
                        "<td>" + category + "</td>" +
                        "<td>" + brand + "</td>" +
                        "<td>" + unit + "</td>" +
                        "<td>" + stock + "</td>" +
                        "</tr>"
                    )
                    $('#total_stock').html(total_stock);
                }
            }
        });
    });--}}
    /*--------brand wise product list------------*/
   {{--
	$("#brand").change(function() {
        var brand = $(this).val();
        var token = "{{csrf_token()}}";
        $.ajax({
            url: '{{ route('
            stock.bpList ') }}',
            type: 'post',
            data: {brand: brand,'_token': token},
            dataType: 'json',
            success: function(response) {
                //console.log(response)
                var len = response.length;
                $("#product").empty();
                $("#product").append("<option value=''> Select Product</option>");
                if (len == 0) {
                    $("#product").empty();
                    var id = "";
                    var name = "";
                    $("#product").append("<option value='" + id + "'>" + name +
                    "</option>");
                }
                //empty the table tbody
                $("#product_list tbody tr").remove();
                var total_stock = 0;

                for (var i = 0; i < len; i++) {
                    var id = response[i]['product_id'];
                    var name = response[i]['product_name'];
                    var part_no = response[i]['model_no'];
                    var warehouse = response[i]['warehouse_name'];
                    var category = response[i]['category_name'];
                    var brand = response[i]['brand_name'];
                    var unit = response[i]['unit_name'];
                    var stock = response[i]['product_stock'];
                    total_stock = parseInt(total_stock) + parseInt(stock);
                    var disable = "";
                    $("#product").append("<option " + disable + " value='" + id + "'>" +
                        name + " ( " + part_no + " )</option>");
                    /*--------------*/
                    $("#product_list tbody").append(
                        "<tr>" +
                        "<td>" + (i + 1) + "</td>" +
                        "<td>" + name + "</td>" +
                        "<td>" + part_no + "</td>" +
                        "<td>" + warehouse + "</td>" +
                        "<td>" + category + "</td>" +
                        "<td>" + brand + "</td>" +
                        "<td>" + unit + "</td>" +
                        "<td>" + stock + "</td>" +
                        "</tr>"
                    )
                    $('#total_stock').html(total_stock);
                }
            }
        });
    });--}}
    /*--------brand wise product list------------*/
    {{--$("#product").change(function() {
        var product = $(this).val();
        var token = "{{csrf_token()}}";
        $.ajax({
            url: '{{ route('
            stock.pList ') }}',
            type: 'post',
            data: { product: product,'_token': token },
            dataType: 'json',
            success: function(response) {
                console.log(response)
                var len = response.length;
                $("#product").empty();
                $("#product").append("<option value=''> Select Product</option>");
                if (len == 0) {
                    $("#product").empty();
                    var id = "";
                    var name = "";
                    $("#product").append("<option value='" + id + "'>" + name +"</option>");
                }
                //empty the table tbody
                $("#product_list tbody tr").remove();
                var total_stock = 0;

                for (var i = 0; i < len; i++) {
                    var id = response[i]['product_id'];
                    var name = response[i]['product_name'];
                    var part_no = response[i]['code'];
                   // var warehouse = response[i]['warehouse_name'];
                    var category = response[i]['category_name'];
                   // var brand = response[i]['brand_name'];
                    var unit = response[i]['unit_name'];
                    var stock = response[i]['product_stock'];
                    total_stock = parseInt(total_stock) + parseInt(stock);
                    var disable = "";
                    $("#product").append("<option " + disable + " value='" + id + "'>" +
                        name + " ( " + part_no + " )</option>");
                    /*--------------*/
                    $("#product_list tbody").append(
                        "<tr>" +
                        "<td>" + (i + 1) + "</td>" +
                        "<td>" + name + "</td>" +
                        "<td>" + part_no + "</td>" +
                        //"<td>" + warehouse + "</td>" +
                        "<td>" + category + "</td>" +
                     //   "<td>" + brand + "</td>" +
                        "<td>" + unit + "</td>" +
                        "<td>" + stock + "</td>" +
                        "</tr>"
                    )
                    $('#total_stock').html(total_stock);
                }
            }
        });
    });--}}
})
/*---------print section-------------*/
function printDiv(divName) {
    var printContents = document.getElementById(divName).innerHTML;
    var originalContents = document.body.innerHTML;

    document.body.innerHTML = printContents;

    window.print();

    document.body.innerHTML = originalContents;
}
</script>
@endpush