<section class="content-header">
  <h1 class="top-breadcrumb">&nbsp;</h1>
  <ol class="breadcrumb">
    <li><a href="<?php echo site_url('dashboard'); ?>"><i class="fa fa-dashboard"></i> Dashboard</a></li>
    <?php if(!empty($breadcrumbs)){ ?>
      <?php foreach($breadcrumbs as $key => $bc){ ?>
        <?php if($key == (count($breadcrumbs)-1)){ ?>
          <li class="active"><?php echo $bc['title']; ?></li>
        <?php }else{ ?>
          <li><a href="<?php echo $bc['href']; ?>"><?php echo $bc['title']; ?></a></li>
        <?php } ?>
      <?php } ?>
    <?php } ?>
    <a class="btn btn-back" href="javascript:window.history.go(-1);">Back</a>
  </ol>
</section>
<section class="content">
  <?php alert(); ?>
  <div class="row">
    <div class="col-lg-12">
      <div class="box box-success">       
        <div class="box-header with-border">
          <h3>Entry
            <a type="button" href="<?php echo site_url('entry'); ?>" class="btn pull-right btn-add" >Entry List</a></h3> 
          </div>
          <form method="post" id="frm-save" enctype="multipart/form-data">
            <div class="box-body" style="overflow: auto;">          
              <div class="panel1 panel-default">
                <div class="panel-body" style="padding-top: 0px !important;padding-bottom: 0px !important;">
                  <div class="row">
                    <div class="col-md-2 top15">
                      <span class="badge-label">Entry No<span class="redstar">*</span></span>
                      <input class="form-control" id="entry_no" readonly name="entry_no" value="<?=isset($entry_no)?$entry_no:'';?>">
                    </div>
                    <div class="col-lg-2 top15">
                      <div class="form-group">
                        <span class="badge-label">Entry Date<span class="redstar">*</span></span>
                        <div class="input-group temp-group">
                          <input readonly style="width:165px"  name="entry_date" id="entry_date" style="margin-top: 0;" value="<?=isset($entry['entry_date'])?date("d/m/Y h:i A", $entry['entry_date']):date('d/m/Y h:i A'); ?>" type="text" class="form-control form-temp" placeholder="Bill Date" autocomplete="off"> 
                        </div>
                      </div>
                    </div>
                    <div class="col-md-4 top15">
                      <span class="badge-label">Vehicle No.<span class="redstar">*</span></span>
                      <select onchange="get_veh_entry();" name="veh_no" class="select form-control" id="veh_no">
                        <option value="0" >Select</option>
                        <?php  
                        if($vnos){
                          foreach($vnos as $vn){ ?>
                            <option <?=(!empty($entry) && $entry['veh_no'] == $vn['veh_no'])?'selected':''  ?> value="<?= $vn["veh_no"] ?>" ><?= $vn["veh_no"] ?> </option>
                          <?php } ?>
                        <?php } ?>
                      </select>
                    </div> 

                    <div class="col-md-4 top15">
                      <span class="badge-label">Vehicle Type<span class="redstar">*</span></span>
                      <select onchange="get_price();" name="vt_id" class="select form-control" id="vt_id">
                        <option value="0" >Select</option>
                        <?php  
                        if($vts){
                          foreach($vts as $vt){ ?>
                            <option <?=(!empty($entry) && $entry['vt_id'] == $vt['vehicle_type_id'])?'selected':''  ?> value="<?= $vt["vehicle_type_id"] ?>" ><?= $vt["name"] ?> </option>
                          <?php } ?>
                        <?php } ?>
                      </select>
                    </div>  
                  </div>
                  <div class="row"> 
                    <div class="col-md-2 top15">
                      <span class="badge-label">Mobile No1<span class="redstar">*</span></span>
                      <input class="form-control" id="mobile1" name="mobile1" value="<?=isset($entry['mobile1'])?$entry['mobile1']:'';?>">
                    </div>
                    <div class="col-md-2 top15">
                      <span class="badge-label">Mobile No2</span>
                      <input class="form-control" id="mobile2" name="mobile2" value="<?=isset($entry['mobile2'])?$entry['mobile2']:'';?>">
                    </div>

                    <div class="col-md-4 top15">
                      <span class="badge-label">Container No.</span>
                      <input class="form-control" id="container_no" name="container_no" value="<?=isset($entry['container_no'])?$entry['container_no']:'';?>">
                    </div> 
                  </div>

                  <div class="row">
                   <div class="col-md-2 top30">
                    <span class="badge-label">Load Type<span class="redstar"></span></span>
                    <div>
                      <label for="type1" style="font-weight: normal;"><input class="load_type" type="radio" value="E" id="type1" name="load_type" <?php echo ((!empty($entry) && $entry['load_type']=='E') || !isset($entry['load_type']))?'checked':''; ?> />&nbsp;Empty&nbsp;&nbsp;</label>
                      <label for="type2" style="font-weight: normal;"><input class="load_type" type="radio" value="L" id="type2" name="load_type" <?php echo (!empty($entry) && $entry['load_type']=='L')?'checked':''; ?>/> &nbsp;Loaded</label>
                    </div>
                  </div>
                  <div id="mat_cont" style="display:none;" class="col-md-4 top15">
                    <span class="badge-label">Material Type</span>
                    <select name="mt_id" class="select form-control" id="mt_id">
                      <option value="0" >Select</option>
                      <?php  
                      if($mts){
                        foreach($mts as $mt){ ?>
                          <option <?=(!empty($entry) && $entry['mt_id'] == $mt['material_type_id'])?'selected':''  ?> value="<?= $mt["material_type_id"] ?>" ><?= $mt["name"] ?> </option>
                        <?php } ?>
                      <?php } ?>
                    </select>
                  </div> 
                </div>

                <div class="row" id="tot_cont1">
                  <div class="col-md-2 top15">
                    <span class="badge-label">Empty Weight<span class="redstar">*</span></span>
                    <input class="form-control calc_inp num-frac-only" id="empty_wt1" name="empty_wt1" value="<?=isset($entry['empty_wt'])?$entry['empty_wt']:'';?>">
                  </div> 
                  <div class="col-md-2 top15">
                    <span class="badge-label">Nett Amount<span class="redstar">*</span></span>
                    <input class="form-control num-frac-only" id="net_amt1" name="net_amt1" value="<?=isset($entry['net_amt'])?$entry['net_amt']:'';?>">
                  </div>
                </div>

                <div class="row" id="tot_cont2">
                 <div class="col-md-2 top15">
                  <span class="badge-label">Total Weight<span class="redstar">*</span></span>
                  <input class="form-control calc_inp calc_wt num-frac-only" id="tot_wt" name="tot_wt" value="<?=isset($entry['tot_wt'])?$entry['tot_wt']:'';?>">
                </div>
                <div class="col-md-2 top15">
                  <span class="badge-label">Empty Weight<span class="redstar">*</span></span>
                  <input class="form-control calc_inp calc_wt num-frac-only empty_wt" id="empty_wt2" name="empty_wt2" value="<?=isset($entry['empty_wt'])?$entry['empty_wt']:'';?>">
                </div> 
                <div class="col-md-2 top15">
                  <span class="badge-label">Nett Weight<span class="redstar">*</span></span>
                  <input class="form-control num-frac-only" id="net_wt" readonly name="net_wt" value="<?=isset($entry['net_wt'])?$entry['net_wt']:'';?>">
                </div>
                <div class="col-md-2 top15">
                  <span class="badge-label">Nett Amount<span class="redstar">*</span></span>
                  <input class="form-control num-frac-only" id="net_amt2" name="net_amt2" value="<?=isset($entry['net_amt'])?$entry['net_amt']:'';?>">
                </div>
              </div> 
              <div class="row">
                <div class="col-lg-6 top15"> 
                  <span class="badge-label">Upload Files</span>
                  <input type='file' name="upload1" id="upload1" onchange="readURL(this);" /><img  style="display: none;" id="blah" src="" alt="your image" />

                  <?php if(isset($entry['upload1'])){ ?>
                    <a target="_blank" href="<?=base_url('uploads/entry/'.$entry['upload1']);?>"><?=$entry['upload1'];?></a>
                  <?php } ?>

                  <br>
                  <input type='file' name="upload2" id="upload2" onchange="readURL(this);" /><img  style="display: none;" id="blah" src="" alt="your image" /> 
                  <?php if(isset($entry['upload2'])){ ?>
                    <a target="_blank" href="<?=base_url('uploads/entry/'.$entry['upload2']);?>"><?=$entry['upload2'];?></a>
                  <?php } ?>

                </div>
              </div>

              <div class="row">
                <div class="col-md-8 top15">
                  <span class="badge-label">Remarks</span>
                  <textarea rows="3" class="form-control" name="remarks"><?=isset($entry['remarks'])?$entry['remarks']:'';?></textarea> 
                </div>
              </div>

            </div>
          </div>            
        </div>
        <div class="box-body" style="overflow: auto;"> 
          <div class="col-md-12 pull-right top10 text-right top-alt" style="margin-bottom: 10px;"> 
            <input type="hidden" name="entry_id" value="<?= $entry_id; ?>" />
            <label id="err_lbl" class="err_lbl"></label>&nbsp;&nbsp;
            <button class="btn button-save btn-save" id="btn-save" title="Save" type="button"><i class="fa fa-check" aria-hidden="true"></i> Save</button>
            <button type="reset" title="Cancel" class="btn btn-clr button-cancel"><i class="fa fa-times" aria-hidden="true"></i> Clear</button>
          </div>
        </div>
      </div>
    </form>
  </div>
</div>
</section>      
<script type="text/javascript"> 
  var $set_veh_props=false;
  $(document).ready(function(){
    $( ".date_inp" ).datepicker({
      dateFormat: "dd-mm-yy",
    });     
    $('.select').select2({tags: true, selectOnBlur: true});  
    
    <?php if($entry_id){ 
      if($entry['load_type']=='E') { ?>
        $('#type1').trigger('click');
      <?php }else{ ?>
        $('#type2').trigger('click');
      <?php }  
    }else{ ?>
      $('#type1').trigger('click');
    <?php } ?>
  }); 
  // $(document).on('keydown','.select2-search__field',function(e){  
  //   if(e.which==9){
  //    // e.preventDefault();
  //     e.which=13; 
  //   }
  // });
  $(document).on('click','.load_type',function(){ 
    if($('#type1').is(':checked')) { 
      $('#mat_cont').hide(); 
      $("#tot_cont1").show();
      $("#tot_cont2").hide();
    }
    else{
      $('#mat_cont').show(); 
      $("#tot_cont1").hide();
      $("#tot_cont2").show();
    }
  }); 
  function readURL(input) {
    if (input.files && input.files[0]) {
      var reader = new FileReader();
      $('#blah').css('display','block');

      reader.onload = function (e) {
        $('#blah')
        .attr('src', e.target.result);
      };

      reader.readAsDataURL(input.files[0]);
    }
  }
  $(document).on( 'input', '.calc_inp', function() { 
    get_price();
  });

  $(document).on( 'input', '.calc_wt', function() { 
    calc_wt();
  });

  function calc_wt(){
    $tot_wt=$.trim($('#tot_wt').val());

    if($tot_wt=='')
      $tot_wt=0;
    else
      $tot_wt =parseFloat($tot_wt);

    $empty_wt2=$.trim($('#empty_wt2').val());

    if($empty_wt2=='')
      $empty_wt2=0;
    else
      $empty_wt2 =parseFloat($empty_wt2); 

    $('#net_wt').val($tot_wt-$empty_wt2);
  }

  function get_price(){
    if(!$set_veh_props){
      if($('#type1').is(':checked')){
        $wt = $('#empty_wt1').val();
        $('#net_amt1').val('0');  
      }
      else{
        $wt = $('#tot_wt').val();
        $('#net_amt2').val('0');  
      } 

      $vt_id=$('#vt_id').val();

      if($vt_id && $wt){
       $.ajax({
        url : "<?= site_url('aj_data') ?>",
        data: 'action=get_price&wt='+$wt+'&vt_id='+$vt_id,
        success: function (res) {
          if($('#type1').is(':checked'))
            $('#net_amt1').val(res);  
          else
            $('#net_amt2').val(res);   
        }
      });
     }
   }
 }

 function get_veh_entry(){
   $veh_no=$('#veh_no').val();

   if($veh_no){
     $.ajax({
      url : "<?= site_url('aj_data') ?>",
      data: 'action=get_veh_entry&veh_no='+$veh_no,
      success: function (resp) {
       if(resp !== '-1'){ 
         res=JSON.parse(resp);  
         $set_veh_props=true;
         $('#vt_id').val(res['vt_id']).trigger('change');
         $('#mt_id').val(res['mt_id']).trigger('change');

         $('#mobile1').val(res['mobile1']);
         $('#mobile2').val(res['mobile2']);
         $('#container_no').val(res['container_no']);

         if(res['load_type']=='E'){
          $('#type1').prop('checked', true);

          $('#mat_cont').hide(); 
          $("#tot_cont1").show();
          $("#tot_cont2").hide();

          $('#net_amt1').val(res['net_amt']); 
        }
        else{
          $('#type2').prop('checked', true);

          $('#mat_cont').show(); 
          $("#tot_cont1").hide();
          $("#tot_cont2").show();

          $('#tot_wt').val(res['tot_wt']); 
          $('#net_wt').val(res['net_wt']);
          $('#net_amt2').val(res['net_amt']);
        }

        $('#empty_wt1').val(res['empty_wt']);
        $('#empty_wt2').val(res['empty_wt']);

        $set_veh_props=false;
      }
    }
  });
   }
 }

 $(document).on('click','#btn-save',function(e){      
  $err_lbl= $(this).parent().find('.err_lbl');
  $err_lbl.html('');

  $frm = $('#frm-save');        
  $val = '';
  $frm.find('input:text').each(function(){
    $(this).val($.trim($(this).val()));
  });

  if($('#entry_no').val() ==''){
    $err_lbl.html('Please enter Entry No.!');
    return false;
  }  

  // if (!validate_date($("#entry_date").val())){
  //   $err_lbl.html('Please select valid Entry date!');
  //   return false;
  // }

  if($('#veh_no').val() =='0'){
    $err_lbl.html('Please select Vehicle No.!');
    return false;
  } 

  if($('#vt_id').val() =='0'){
    $err_lbl.html('Please select Vehicle type!');
    return false;
  }   

  if($('#mobile1').val() ==''){
    $err_lbl.html('Please enter mobile!');
    return false;
  } 

  if($('#type1').is(':checked')) { 
    if($('#empty_wt1').val() ==''){
      $err_lbl.html('Please enter Empty weight!');
      return false;
    } 
    if($('#net_amt1').val() =='' || $('#net_amt1').val() =='0'){
      $err_lbl.html('Invalid Net Amount!');
      return false;
    } 
  }
  else{
   if($('#mt_id').val() =='0'){
    $err_lbl.html('Please select Material type!');
    return false;
  }   

  if($('#tot_wt').val() ==''){
    $err_lbl.html('Please enter Total weight!');
    return false;
  } 
  if($('#empty_wt2').val() ==''){
    $err_lbl.html('Please enter Empty weight!');
    return false;
  } 
  if($('#net_wt').val() ==''){
    $err_lbl.html('Please enter Nett weight!');
    return false;
  }
  if($('#net_amt2').val() =='' || $('#net_amt2').val() =='0'){
    $err_lbl.html('Invalid Net Amount!');
    return false;
  } 
} 

$('#btn-save').html('Please wait...').attr('disabled',true);
$frm.submit();
});  

</script>
