<!DOCTYPE html>
<html>
<head>
  @include('title')
  

  <!-- Tell the browser to be responsive to screen width -->
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
  <!-- Bootstrap 3.3.7 -->
  <link rel="stylesheet" href="{{asset('lte/bower_components/bootstrap/dist/css/bootstrap.min.css')}}">
  <!-- Font Awesome -->
  <link rel="stylesheet" href="{{asset('lte/bower_components/font-awesome/css/font-awesome.min.css')}}">
  <!-- Ionicons -->
  <link rel="stylesheet" href="{{asset('lte/bower_components/Ionicons/css/ionicons.min.css')}}">
  <!-- Theme style -->
  <link rel="stylesheet" href="{{asset('lte/dist/css/AdminLTE.min.css')}}">
  <!-- AdminLTE Skins. Choose a skin from the css/skins
       folder instead of downloading all of them to reduce the load. -->
  <link rel="stylesheet" href="{{asset('lte/dist/css/skins/_all-skins.min.css')}}">
  
  <!-- bootstrap wysihtml5 - text editor -->
  <link rel="stylesheet" href="{{asset('lte/plugins/bootstrap-wysihtml5/bootstrap3-wysihtml5.min.css')}}">

  <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
  <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
  <!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
  <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
  <![endif]-->

  <!-- Google Font -->
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700,300italic,400italic,600italic">

  <!-- iCheck for checkboxes and radio inputs -->
  <link rel="stylesheet" href="{{asset('lte/plugins/iCheck/all.css')}}">
  <!-- DataTables -->
  <link rel="stylesheet" href="{{asset('lte/bower_components/datatables.net-bs/css/dataTables.bootstrap.min.css')}}">
  
  
</head>
<body class="hold-transition skin-blue fixed sidebar-mini">
<div class="wrapper">
  
  @include('headertag')
  <!-- Left side column. contains the logo and sidebar -->
  @include('leftside')
  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
      <!-- Content Header (Page header) -->
      <section class="content-header">
      <h1>
          Map Products
        </h1>
        <ol class="breadcrumb">
          <li><a href="{{route('location.master')}}"><i class="fa fa-dashboard"></i> Location</a></li>
          <li class="active">Map products</li>
        </ol>
      </section>

      <div class="box-header with-border" id="productMaps" style="display: none;">
               <div class="alert alert-success alert-dismissible">
                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                Product Successfully Mapped..!!
               </div>
      </div>

      <!-- Main content -->
      <section class="content">
        
        <!-- Main row -->
        <div class="row">
          <!-- Left col -->
          <section class="col-lg-12 connectedSortable">
            <!-- Custom tabs (Charts with tabs)-->
            
              <!-- Tabs within a box -->
             
              
               <!-- right column -->
        
       <!-- Horizontal Form -->
             <div class="box box-info">
              <br>
            <!-- /.box-header -->
            <!-- form start -->
            
             <form class="form-horizontal" action="{{route('location.product.show')}}" method="POST">
             <input type="hidden" name="_token" value="{{ csrf_token() }}">
             <input type="hidden" name="location_id" id="location_id" value="{{$location_id}}">
              <div class="box-body">
                <div class="form-group">
                  <label for="inputcategory" class="col-sm-2 control-label">Category<font color="red">*</font></label>
                   <div class="col-sm-4">
                   <select class="form-control" name="category" id="category">
                    <option value="">Select</option>
                    @foreach($categories as $category)
                    @if($selctCat != NULL && $selctCat == $category->id)
                    <option value="{{$category->id}}" selected="">{{$category->category}}</option>
                    @else
                    <option value="{{$category->id}}">{{$category->category}}</option>
                    @endif
                    @endforeach 
                  </select>
                  </div>
                  <button type="submit" class="btn btn-info pull-left" style="margin-left: 8px;">SHOW</button>
                  
                </div>
                
              </div>
              <!-- /.box-body -->
              <!-- /.box-footer -->
            </form>
          </div>
          <!-- /.box --> 
                
         
          

          <div class="box">
            <div class="box-header">
              <h3 class="box-title">Product List</h3>
            </div>
            <!-- /.box-header -->
            <div class="box-body">
              <table id="example1" class="table table-bordered table-striped">
                <thead>
                
                <tr>
                  <th>Sl No.</th>
                  <th>Product</th>
                  <th>Code</th>
                  <th>Grade</th>
                  <th>Map</th>
                </tr>

                </thead>

                <tbody>
                 <?php $c = 1; ?>
                 @if($products != NULL)
                 @foreach($products as $data)
                  <tr id="{{$data->id}}">
                  <td>{{$c++}}</td>
                  <td>{{$data->name}}</td>
                  <td>{{$data->code}}</td>

                  <td class="locGrade">
                  {{$loc_prd_obj->locationMaserGradeShow($data->id,$location_id)}}
                  </td>
                  <td>
                  @if(count($stnds[$data->id]) > 1)
                  <a class="btn" data-toggle="modal" data-target="#modal-default" onclick="jsStandrd({{$data->id}})"><i class="fa fa-fw fa-plus" style="color: red;"></i></a>  
                  @else
                    @if(in_array($data->id, $locationProducts))
                    <input type="checkbox" class="checkPdct" name="addCategory[]" id="addCategory" class="check" value="{{$data->id}}" checked="" >
                    @else
                    <input type="checkbox" class="checkPdct" name="addCategory[]" id="addCategory" class="check" value="{{$data->id}}" >
                    @endif
                  @endif
                  </td>
                  </tr>
                  @endforeach
                  @endif
                </tbody>
                
              </table>
            </div>
            <!-- /.nav-tabs-custom -->

            <!-- Chat box -->
            
            <!-- /.box (chat box) -->

            
            <!-- quick email widget -->
          </section>
          <!-- /.Left col -->
          
        </div>
        <!-- /.row (main row) -->

      </section>
      <!-- /.content -->
    </div>
  <!-- /.content-wrapper -->

  <div class="modal fade" id="modal-default">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close" onclick="jsRefresh()">
          <span aria-hidden="true">&times;</span></button>
          <h4 class="modal-title" id="modal_title">Grades</h4>
        </div>
        <div class="modal-body">
          <input type="hidden" name="prd_id" id="prd_id">
          <table id="grade_table" class="table table-bordered table-striped">
            <thead>
              <th>Sl No.</th>
              <th>Grade</th>
              <th>Packing</th>
              <th>Map</th>
            </thead>
            <tbody>
            </tbody>
          </table>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal" onclick="jsRefresh()">Close</button>
        </div>
      </div>
      <!-- /.modal-content -->
    </div>
    <!-- /.modal-dialog -->
  </div>
  <!-- /.modal -->
  
  @include('footertag')

  <!-- Control Sidebar -->
  @include('rightside')
  <!-- /.control-sidebar -->
  <!-- Add the sidebar's background. This div must be placed
       immediately after the control sidebar -->
  <div class="control-sidebar-bg"></div>
</div>
<!-- ./wrapper -->

  <!-- jQuery 3 -->
  <script src="{{asset('lte/bower_components/jquery/dist/jquery.min.js')}}"></script>
  <!-- jQuery UI 1.11.4 -->
  <script src="{{asset('lte/bower_components/jquery-ui/jquery-ui.min.js')}}"></script>
  <!-- Resolve conflict in jQuery UI tooltip with Bootstrap tooltip -->
  <script>
    $.widget.bridge('uibutton', $.ui.button);
  </script>
  <!-- Bootstrap 3.3.7 -->
  <script src="{{asset('lte/bower_components/bootstrap/dist/js/bootstrap.min.js')}}"></script>
  
  <!-- Sparkline -->
  <script src="{{asset('lte/bower_components/jquery-sparkline/dist/jquery.sparkline.min.js')}}"></script>
  
  <!-- Bootstrap WYSIHTML5 -->
  <script src="{{asset('lte/plugins/bootstrap-wysihtml5/bootstrap3-wysihtml5.all.min.js')}}"></script>
  <!-- Slimscroll -->
  <script src="{{asset('lte/bower_components/jquery-slimscroll/jquery.slimscroll.min.js')}}"></script>
  <!-- FastClick -->
  <script src="{{asset('lte/bower_components/fastclick/lib/fastclick.js')}}"></script>
  <!-- AdminLTE App -->
  <script src="{{asset('lte/dist/js/adminlte.min.js')}}"></script>
  
  <!-- AdminLTE for demo purposes -->
  <script src="{{asset('lte/dist/js/demo.js')}}"></script>

  <!-- iCheck 1.0.1 -->
<script src="{{asset('lte/plugins/iCheck/icheck.min.js')}}"></script>

<!-- DataTables -->
<script src="{{asset('lte/bower_components/datatables.net/js/jquery.dataTables.min.js')}}"></script>
<script src="{{asset('lte/bower_components/datatables.net-bs/js/dataTables.bootstrap.min.js')}}"></script>
</body>

<script type="text/javascript">
  $(function () {
    $('#example1').DataTable()
    
  })
  function jsStandrd(value){
   $("#prd_id").val(value);
   var location_id = $("#location_id").val();
   $('#grade_table tbody').empty();
   $.get("{{url('get/location/productStandrd')}}",
     {prd: value, location_id : location_id},
     function(data){
       
       // alert(data[2]);
       var i = 1;
       $('#modal_title').text('Grades- '+data[1]);
       $.each( data[0], function( index, element ) {
         var key = false;
             key = jQuery.grep(data[2], function(n, i) {
                       if(n.grade_id == element.gid && n.standard_id == element.sid) {
                         return true;
                       } else {
                         return false;
                       }
                   });
         var td_id = 'pop'+i;
         
         if(key && key != '') {
           
           $('#grade_table tbody').append('<tr><td>'+i+'</td><td>'+element.standard+'</td><td>'+element.grade+'</td><td id="'+td_id+'"><input type="checkbox" name="map" class="map" value="'+element.id+'" checked="checked"></td></tr>');
         }
         else{
           $('#grade_table tbody').append('<tr><td>'+i+'</td><td>'+element.standard+'</td><td>'+element.grade+'</td><td id="'+td_id+'"><input type="checkbox" name="map" class="map" value="'+element.id+'"></td></tr>');
         }
         i++;
       });
     });
  }

  function jsRefresh()
  {
    var location_id = $("#location_id").val();
    var prod_id = $("#prd_id").val();
    $("#"+prod_id).find(".locGrade").empty();
    $.get("{{ url('get/location/grades')}}", 
      { product: prod_id,location_id: location_id}, 
        function(data) {
          
        $("#"+prod_id).find(".locGrade").text(data);
    });
  }
</script>

<script>

$(document).ready(function(){
    $("#example1").on("click", ".checkPdct", function(){

      var location_id = $('#location_id').val();
      var product  = $(this).val();
      if ($(this).prop("checked") == true) {

       $(this).closest('td').append('<b class="bt">Product Mapped !!!</b>').find('.bt').fadeOut(2000);
      
       $.get("{{ url('location/add/product')}}", 
              { option: location_id ,option1: product }, 
              function(data) {
       });
      }
      
    });
  }); 

$(document).ready(function(){
    $("#example1").on("click", ".checkPdct", function(){

      
      if ($(this).prop("checked") == false) {
      var location_id = $('#location_id').val();
      var product  = $(this).val();

        $(this).closest('td').append('<b class="bt">Product Unmapped !!!</b>').find('.bt').fadeOut(2000);
       
        $.get("{{ url('location/delete/product')}}", 
              { option: location_id ,option1: product }, 
              function(data) {
              
              
            });  
      }
    });

    $("#grade_table").on("click", ".map", function(){
      var row = $(this).closest('td').attr('id');
      if ($(this).prop("checked") == true) {
        // alert('true');
        // var grade_id = $(this).val();
        var p_std = $(this).val();
        var location_id = $("#location_id").val();
        var prod_id = $("#prd_id").val();
        
       $.get("{{ url('add/location/product/grade')}}", 
              { product: prod_id,location_id: location_id, p_std: p_std  }, 
              function(data) {
              $('#'+row).append('<b class="bt">Product Mapped !!!</b>').find('.bt').fadeOut(2000);
            });          
      }
      
    });

    $("#grade_table").on("click", ".map", function(){
      var row = $(this).closest('td').attr('id');
      if ($(this).prop("checked") == false) {

        // var grade_id = $(this).val();
        var p_std = $(this).val();
        var location_id = $("#location_id").val();
        var prod_id = $("#prd_id").val();
        
       $.get("{{ url('update/location/product/grade')}}", 
              { product: prod_id,location_id: location_id, p_std: p_std  }, 
              function(data) {
              $('#'+row).append('<b class="bt">Product Unlinked !!!</b>').find('.bt').fadeOut(2000);
            });          
      }
      
    });
  }); 
 

</script>
  

</html>
  