@extends('layouts.admin')
@section('pageTitle', 'Vega Soft')
@section('page-content')

<input type="hidden" id="table_settings_field" branch_id="" branch_name=""  column_heading="<?= $rows->column_heading;?>" column_width="<?= $rows->column_width;?>" column_alighn="<?= $rows->column_alighn;?>"  column_conversion="<?= $rows->column_conversion;?>" settings_row_id="<?= $id;?>" sp_date_picker="<?= $rows->sp_date_picker;?>"  header_from_date="<?=date('d-m-Y', strtotime('-7 days')); ?>"   header_till_date="<?=date('d-m-Y'); ?>"  sp_sort="<?= $rows->menu_id; ?>"  active_mn="<?= $rows->root;?>"  active_mnt="<?= $rows->menu_type;?>">
<style type="text/css">
  #po_tab td{
    font-size: 0.8rem;
    font-weight: bold;
border: none;
padding: 0.3rem;
  }

 #po_tab  .bord_mid{
  width: 2%;
 }
 #po_tab  .second_td{
    width: 72%;
       font-weight: unset; 
  }
  #po_description{
    white-space: break-spaces;
  word-wrap: anywhere;
  }
</style>
<div id="form-div">
<input type="hidden" id="hidden_status" value="0">
<input type="hidden" id="row_id" value="0">


<div class="page-header form-header" >
  <h3 class="page-title text-white"> <?= $rows->menu_name;?> </h3>
    <nav aria-label="breadcrumb">
      <ol class="breadcrumb">
      
        <li class="breadcrumb-item text-white  icon-md " aria-current="page"><i class="icon-list slide1"></i> </li>
      </ol>
    </nav>
    

    <input type="hidden" id="table_settings_field" branch_id="" branch_name=""  column_heading="<?= $rows->column_heading;?>" column_width="<?= $rows->column_width;?>" column_alighn="<?= $rows->column_alighn;?>"  column_conversion="<?= $rows->column_conversion;?>" settings_row_id="<?= $id;?>" sp_date_picker="<?= $rows->sp_date_picker;?>"  header_from_date="<?=date('d-m-Y', strtotime('-7 days')); ?>"   header_till_date="<?=date('d-m-Y'); ?>"  sp_sort="<?= $rows->menu_id; ?>"   >
    @if ($rows->sedp != "")
  @foreach(explode(',', $rows->sedp) as  $z=>$info) 
     @if ($info == "1" && $z==1)
       <input type="hidden" class="additional_btns" btn_text="Edit" btn_click_function="call_edit_modal" btn_class="btn btn-success btn-sm btn-dtbl" col_width="30" >
     @endif 
     @if ($info == "1" && $z==2)
        <input type="hidden" class="additional_btns" btn_text="Cancel Payment" btn_click_function="call_remove_modal" btn_class="btn btn-danger btn-sm btn-dtbl" col_width="40"  >
     @endif 
     @if ($info == "1" && $z==3)
          <input type="hidden" class="additional_btns" btn_text="Print" btn_click_function="call_print_function2" btn_class="btn btn-info btn-sm btn-dtbl" col_width="40"  >
     @endif
  @endforeach
@endif
</div>
<div class="card">
    <div class="card-body">
      <form class="forms-sample" autocomplete="off">






<div class="rows">
<div class="row" style="border: solid burlywood;padding: 6px;border-radius: 8px;">
    <div class="col-md-4">
        <div class="form-group row ">
      <label class="col-sm-5 form-label">P.O  NO <i class="text-danger">*</i> </label>
      <div class="col-sm-7">
        <input type="text" class="form-control  mst_dt" id="po_no" >
      </div>
    </div>
    <div class="form-group row ">
      <label class="col-sm-5 form-label">P.O CONTRACT NO <i class="text-danger">*</i> </label>
      <div class="col-sm-7">
        <input type="text" class="form-control  mst_dt" id="po_con_no" >
      </div>
    </div>
    </div>
    <div class="col-md-4">
      <table class="table" id="po_tab">
                      <thead>

                      <tbody>
                        <tr>
                          <td>P.O DATE </td>
                          <td class="bord_mid">:</td>
                          <td id="po_date"  class="second_td"></td>
                        </tr>
                        <tr>
                          <td>P.O DESCRIPTION</td>
                          <td class="bord_mid">:</td>
                          <td id="po_description" class="second_td"></td>
                        </tr>
                        <tr>
                          <td>P.O Amount</td>
                          <td class="bord_mid">:</td>
                          <td id="po_amount" class="second_td"></td>
                        </tr>
                        
                      </tbody>
                    </table>
    </div>

<div class="col-lg-12 col-md-12"><button type="button" class="btn btn-sm btn-danger " style="float:right;font-size:12px;" onclick="clear_mst_dt1();">Clear</button></div>



</div>

 <div class="row" style="margin-top: 15px;">
      <div class="col-md-6">
    <div class="form-group row ">
      <label class="col-sm-4 form-label"> </label>
      <div class="col-sm-6">
     <label class="radio-inline">
      <input type="radio" name="optradio" onchange="set_sup_con(1);" checked value="1"> &nbsp; Supplier &nbsp; 
    </label>
    <label class="radio-inline">
      <input type="radio" name="optradio" onchange="set_sup_con(0);" value="0"> &nbsp; Cortractor &nbsp; 
    </label>
      </div>
    </div>

    <div class="form-group row ">
      <label class="col-sm-4 form-label" >WO No </label>
      <div class="col-sm-4">

      <input type="text" class="form-control " id="wo_no" >
      </div>
      <div class="col-sm-4" id="con_sapnname">
      </div>
    </div>

    <div class="form-group row hd_name ">
      <label class="col-sm-4 form-label" id="lbl_con_sup">Cortractor Name </label>
      <div class="col-sm-6">

      <input type="text" class="form-control input_txt" id="con_sup_name" >
      </div>
    </div>
    <div class="form-group row sup_met_class ">
    <!--   <label class="col-sm-4 form-label">Labour </label> -->
     <label class="col-sm-4 form-label" id="">Material Name </label>
      <div class="col-sm-6">

      <select class="select2-multiple form-control focusable  input_txt js-data-example-ajax input_txt" id="product_id" name="tags[]" multiple="multiple">
             
          </select>
     
      </div>
    </div>
    <div class="form-group row ">
      <label class="col-sm-4 form-label">Bill Date </label>
      <div class="col-sm-6">

      <input type="text" class="form-control focusable mdate input_txt" id="bill_date" >
      </div>
    </div>

    <div class="form-group row ">
      <label class="col-sm-4 form-label">Bill No </label>
      <div class="col-sm-6">

      <input type="text" class="form-control focusable input_txt" id="bill_no" >
      </div>
    </div>


      <div class="form-group row ">
      <label class="col-sm-4 form-label"> Amount <i class="text-danger">*</i> </label>
      <div class="col-sm-6">

      <input type="text" class="form-control focusable  input_txt custom4" id="bill_amount" onKeyPress="return isNumber(event, this.value);">
      </div>
    </div>
     <div class="form-group row ">
      <label class="col-sm-4 form-label"> Date </label>
      <div class="col-sm-6">

      <input type="text" class="form-control focusable today input_txt mdate" id="sub_date" >
      </div>
    </div>

    <div class="form-group row ">
      <label class="col-sm-4 form-label">Remark</label>
      <div class="col-sm-6">
      <textarea id="Remark" cols="30" rows="10" class="form-control input_txt" style="height:50px !important;"></textarea>
      </div>
    </div>
  </div>
  <div class="col-md-3 col-lg-3 col-sm-3">
    <div class="card" style="border:1px solid grey;">
  <div class="card-header">
   Bill Upload
  </div>
<div class="col-lg-12" id="edit_bill"></div>



 


  <div class="card-body">
   <input type="file" name="bill" id="bill" class="form-control"  accept="pdf" >

  </div>
  <div id="progress-div"><div id="progress-bar"></div></div>

<div id="rspns_div"></div>

</div>
  </div>

 </div>



</div>







                      <div class="template-demo">
                        
                     <button id="save_btn" type="button" onclick="save_submit_form();" class="btn btn-success btn-icon-text save_btn btn-sm" >
                            <i class="icon-doc btn-icon-prepend"></i> Submit </button>
                        
                     <button id="edit_btn" type="button" onclick="save_submit_form();" class="btn btn-primary btn-icon-text save_btn btn-sm" >
                            <i class="icon-doc btn-icon-prepend"></i> Edit </button>


                      <button type="reset" class="btn btn-danger btn-icon-text reset_btn btn-sm"  >
                            <i class="icon-reload btn-icon-prepend"></i> Reset </button>
                            <input type="hidden" id="row_id" value="0">
                          </div>
                    </form>
    </div>
</div>
</div>

<div id="table-div">
<div class="page-header form-header" >
  <h3 class="page-title text-white">  <?= $rows->menu_name;?> </h3>
    <nav aria-label="breadcrumb">
      <ol class="breadcrumb">
      
        <li class="breadcrumb-item text-white  icon-md " aria-current="page"><i class="icon-plus slide2"></i> </li>
      </ol>
    </nav>
</div>

  

<div class="card">
  


    <div class="col-lg-12 col-sm-12 col-md-12" id="table_result">
                                            
                                            </div>


    
    
</div>
</div>





<script type="text/javascript">

set_date();
set_sup_con(1);
  var _URL = window.URL || window.webkitURL;
             var fileExtension = ['pdf'];

document.getElementById('bill').onchange = function (evt) {
    var tgt = evt.target || window.event.srcElement,
        files = tgt.files;
    
    // FileReader support
        if ($.inArray($(this).val().split('.').pop().toLowerCase(), fileExtension) == -1) {
            alert("Only formats are allowed : "+fileExtension.join(', '));
      $('#bill').val('');
      return false;
        } 
file=files[0];
    if (FileReader && files && files.length) {
        var fr = new FileReader();
        fr.onload = function () {
          //  document.getElementById(outImage).src = fr.result; 
       
           if(file.size>500000){
          alert("Bill Should Be 500Kb");  
      $('#bill').val('');
        }
        }
        fr.readAsDataURL(files[0]);
    }
    
    // Not supported
    else {
        // fallback -- perhaps submit the input to an iframe and temporarily store
        // them on the server until the user's session ends.
    }
}

function save_submit_form() {
 status=0;
 if($.trim($('#po_no').val())==''){
    alert('P.O NO is mandatory');
    $('#po_no').focus();
    status==1;
    return;
 }
 sup_con=$.trim($('input[name="optradio"]:checked').val());
if(sup_con==1){
    lab='Supplier'
 }else{
  lab='Contractor';
 }
  if($.trim($('#con_sup_name').val())==''){
    alert(lab+' Name is mandatory');
    $('#con_sup_name').focus();
    status==1;
    return;
 }
  if($.trim($('#con_sup_name').val())==''){
    alert(lab+' Name is mandatory');
    $('#con_sup_name').focus();
    status==1;
    return;
 }
if($('input[name="lab_check"]').prop('checked')){
  if($.trim($('#labour_name').val())==''){
    alert(' Labour Name is mandatory');
    $('#labour_name').focus();
    status==1;
    return;
 }
}

 if($.trim($('#bill_amount').val())==''){
    alert('Amount is mandatory');
    $('#bill_amount').focus();
    status==1;
    return;
 }

 if (status==0) {
Save_form();
 }
}


function  show_form(){
    $("#form-div").show(500);
$("#table-div").hide();
}




function set_edit_form(row_id) {
  show_form();
$('#row_id' ).val(row_id);
$("#edit_btn").show();
$("#save_btn").hide();

    $('#row_id').val(row_id);
    $('#hidden_status').val(1);
        $.ajax({
              type: 'POST',
               async: false,
                url: '/edit_bill_submit',
                data: {
                     '_token': "{{ csrf_token() }}",
                    'cid':"{{ Session::get('cid')}}",
                     row_id: row_id
         
                },
                error: function (request, error) {
                    alert("Please Wait, Loading...");
                },
                success: function (response) {
 datas=response;

     
        
            $('#po_con_no').val(datas.po_con_no); 
            $('#po_no').val(datas.po_no); 
            $('#po_date').html(datas.po_date); 
            $('#phone2').val(datas.phone2); 
            $('#po_description').html(datas.po_description); 
            $('#po_amount').html(datas.po_amount); 
            $('#po_limit').val(datas.po_limit); 
            $('#est_comp_date').val(datas.est_comp_date); 
     
   
            
       // start_focus();   
        }
          
     });  
}









  function Save_form() {

 var file_data = $('#bill').prop('files')[0]; 
  var form_data = new FormData();
    form_data.append('bill_pdf', file_data); 
               form_data.append('_token', "{{ csrf_token() }}");
              form_data.append('hidden_status',$.trim($('#hidden_status').val()));
              form_data.append('row_id',$.trim($('#row_id').val()));
              form_data.append('po_id',$.trim($('#po_no').attr('pod_a_id')));
              form_data.append('po_no',$.trim($('#po_no').val()));
              form_data.append('type',$.trim($('input[name="optradio"]:checked').val()));
              form_data.append('trans_type','0');
              form_data.append('product_id',$.trim($('#product_id').val()));
              form_data.append('c_id',$.trim($('#con_sup_name').attr('c_id')));
              form_data.append('c_name',$.trim($('#con_sup_name').val()));
              form_data.append('bill_no',$.trim($('#bill_no').val()));
              form_data.append('bill_date',$.trim($('#bill_date').val().replace(/,/g, "")));
              form_data.append('bill_amt',$.trim($('#bill_amount').val().replace(/,/g, "")));
              form_data.append('labr_name',$.trim($('#labour_name').val()));
              form_data.append('contractor_id',$.trim($('#wo_no').attr('contrctr_id')));
              form_data.append('wo_no',$.trim($('#wo_no').attr('pad_id')));
              form_data.append('bill_amt',$.trim($('#bill_amount').val().replace(/,/g, "")));
              form_data.append('submsn_date',$.trim($('#sub_date').val().replace(/,/g, "")));
              form_data.append('remark',$.trim($('#Remark').val()));
              form_data.append('added_by',"{{ Session::get('user_id')}}");
              form_data.append('company_id',"{{ Session::get('CID')}}");

              $.ajax({
        type: 'POST',
        url: '/op_bill_submit',
        cache: false,
        contentType: false,
        processData: false,
        data: form_data,      
        error: function (request, error) {
                alert("Please Wait, Loading...");
                },
        success: function (response) { 
                  alert(response.msg);
if(response.flag!=0){
  $('#row_id').val(0);

$('.input_txt').val('');

/*$('#con_sup_name').attr('c_id','0');
$('#wo_no').attr('pad_id','0');*/
$('input[name="lab_check"]').prop('checked',false);

  fetch_view_table();
  $('#con_sapnname').html('');
   $('#product_id').val('').trigger('change');

$("#bill").val('');
$("#bill").text('');


}
         
           


       }
        
     });

       
  }


//.......................script.............................................

function set_sup_con(argument) {
  if (argument==0) {
      $('.sup_class,.lab_class').hide();
      $('.con_class').show();
      $('#con_sup_name').val('');
      $('#con_sup_name').attr('c_id',0);
    $("#lbl_con_sup").html('Cortractor');
     $('.sup_met_class').hide();
  }
  else if (argument==1){
    $('#con_sup_name').val('');
    $("#lbl_con_sup").html('Supplier name');
    $('#con_sup_name').attr('c_id',0);
     $('.sup_met_class').show();
  }

}



$(document).on('keyup keydown', '#po_no,#po_con_no', function (e) {
  ename=$.trim($(this).val());
  id=$(this).attr('id');
  $(this).tableAutocomplete({
    appendTo:".autocomplete_result2",
        source:  function( request, response ) {
$.ajax({
url: "/fetch_po_no_val",
type: "POST",

data: {
  '_token': "{{ csrf_token() }}",
ename: ename,
id:id

},
dataType: "json",
success: function( data ) {

response( $.map( data, function( item ) {
return {

 name: item.po_con_no,                   
 value: item.value,
 po_no:item.po_no,
 pod_a_id:item.pod_a_id,
 desc:item.po_description,                 
 po_amount:item.po_amount,                 
 po_date:item.po_date ,                
 po_con_no:item.po_con_no                 
                  
                  

}
}));
}
});
},
        columns: [ {
            field: 'po_no',
            title: 'Po No',
            width: '30%'
        },{
            field: 'name',
            title: 'Po Contract No',
            width: '30%'
        }],
         change : function(event, ui) {
                   if ( !ui.item ) 
                   { 
                    alert('Not Found');
                   $(this).val('')
                  $('#po_no').val('');
                  $('#po_con_no').val('');
                  $('#po_no').attr('pod_a_id','0');
$("#po_amount").html('');
$("#po_description").html('');
$("#po_date").html('');
             
                   }
                   else
                   {
                      
                   }
           },
               open: function(event, ui) {}, 
        select: function (event, ui) {
          if (ui.item.value!=undefined)
                {

                  $(this).val(ui.item.value);
                  $('#po_no').val(ui.item.po_no);
                  $('#po_con_no').val(ui.item.po_con_no);
                  $('#po_no').attr('pod_a_id',ui.item.pod_a_id);
$("#po_description").html(ui.item.desc);
$("#po_amount").html(ui.item.po_amount);
$("#po_date").html(ui.item.po_date);
                    
               
                }
              
  
        }
    });
    });




$(document).on('keyup', '#con_sup_name', function (e) {
  ename=$.trim($(this).val());
  po_id=$.trim($('#po_no').attr('pod_a_id'));
  if($.trim($('#po_no').val())==''){
    alert('Select P.o Number first');
    $('#po_no').focus();
     $('#con_sup_name').val('');
  }else{

 
  type=$.trim($('input[name="optradio"]:checked').val());
 
  $(this).tableAutocomplete({
    appendTo:".autocomplete_result2",
        source:  function( request, response ) {
$.ajax({
url: "/fetch_sup_con",
type: "POST",

data: {
  '_token': "{{ csrf_token() }}",
ename: ename,
po_id: po_id,
type:type

},
dataType: "json",
success: function( data ) {

response( $.map( data, function( item ) {
return {

 name: item.c_name,                   
 value: item.c_name,
 pa_no: item.pa_no,
 pad_id: item.pad_id,
 c_id:item.c_id                
                  
                  

}
}));
}
});
},
        columns: [ 
        {
            field: 'name',
            title: 'Name',
            width: '50%'
        },{
            field: 'pa_no',
            title: 'Work Order',
            width: '50%'
        }],
         change : function(event, ui) {
                   if ( !ui.item ) 
                   { 
                   $('#con_sup_name').val('');
                  $('#con_sup_name').attr('c_id','0');
                  if(type==0){
                    $('#wo_no').val('');
                  $('#wo_no').attr('pad_id','0');
                  }
               
             
                   }
                   else
                   {
                      
                   }
           },
               open: function(event, ui) {}, 
        select: function (event, ui) {
          if (ui.item.value!=undefined)
                {
                  $(this).val(ui.item.name);
                  $('#con_sapnname').html(ui.item.name);
                  $(this).attr('c_id',ui.item.c_id);
                  if(type==0){
                  $('#wo_no').val(ui.item.pa_no);
                  $('#wo_no').attr('pad_id',ui.item.pad_id);
                  $('#wo_no').attr('contrctr_id',ui.item.c_id);

                  }
                 // get_details_receipt();
               
                }
              
  
        }
    });
   }
    });


$(document).on('keyup', '#wo_no', function (e) {
  po_id=$.trim($('#po_no').attr('pod_a_id'));
  if($.trim($('#po_no').val())==''){
    alert('Select P.o Number first');
    $('#po_no').focus();
     $('#con_sup_name').val('');
  }else{

 
  ename=$.trim($(this).val());

 
  $(this).tableAutocomplete({
    appendTo:".autocomplete_result2",
        source:  function( request, response ) {
$.ajax({
url: "/fetch_wo_no",
type: "POST",

data: {
  '_token': "{{ csrf_token() }}",
ename: ename,
po_id:po_id

},
dataType: "json",
success: function( data ) {

response( $.map( data, function( item ) {
return {
 name: item.c_name,                   
 value: item.pa_no,
 pa_no: item.pa_no,
 pa_id: item.pa_id,
 pad_id: item.pad_id,
 c_id:item.c_id                   
                  
                  

}
}));
}
});
},
        columns: [ 
       
        {
            field: 'name',
            title: 'Name',
            width: '50%'
        },{
            field: 'pa_no',
            title: 'Work Order',
            width: '50%'
        }],
         change : function(event, ui) {
                   if ( !ui.item ) 
                   { 
                  $('#con_sup_name').val('');
                  $('#con_sup_name').attr('c_id','0');
                  $('#wo_no').val('');
             
                   }
                   else
                   {
                      
                   }
           },
               open: function(event, ui) {}, 
        select: function (event, ui) {
          if (ui.item.value!=undefined)
                {
                  type=$.trim($('input[name="optradio"]:checked').val());

                  if(type==0){

                    $('#con_sup_name').val(ui.item.name);
                  $('#con_sup_name').attr('c_id',ui.item.c_id);
                  }else{
                     $('#con_sapnname').html(ui.item.name);
                  }

                  $('#wo_no').attr('pad_id',ui.item.pa_id);
                  $('#wo_no').attr('contrctr_id',ui.item.c_id);
                  $('#wo_no').val(ui.item.pa_no);
                  $("#wo_est").html(ui.item.est_cost)
                   // get_details_receipt();
               
                
                    
               
                }
              
  
        }
    });
}
    });

/*
$(document).on('keyup', '#product_id', function (e) {
  ename=$.trim($(this).val());

 
  $(this).tableAutocomplete({
    appendTo:".autocomplete_result2",
        source:  function( request, response ) {
$.ajax({
url: "/fetch_item",
type: "POST",

data: {
  '_token': "{{ csrf_token() }}",
ename: ename

},
dataType: "json",
success: function( data ) {

response( $.map( data, function( item ) {
return {

 name: item.p_name,                   
 value: item.p_name,
 product_id:item.product_id                
                  
                  

}
}));
}
});
},
        columns: [ 
        {
            field: 'name',
            title: 'Product Name',
            width: '100%'
        }],
         change : function(event, ui) {
                   if ( !ui.item ) 
                   { 
                   $(this).val('');
                  $(this).attr('product_id','0');
             
                   }
                   else
                   {
                      
                   }
           },
               open: function(event, ui) {}, 
        select: function (event, ui) {
          if (ui.item.value!=undefined)
                {
                  $(this).val(ui.item.name);
                  $(this).attr('product_id',ui.item.product_id);
                    
               
                }
              
  
        }
    });
    });*/



</script>
@endsection


@section('page-script')
<script type="text/javascript">
  $(document).ready(function() {
  $('#product_id').select2({
  ajax: {
    type: "POST",
    url: '/fetch_item',
    data: function (params) {
      var query = {
        ename: params.term,
        '_token': "{{ csrf_token() }}",
        type: 'public'
      }

      // Query parameters will be ?search=[term]&type=public
      return query;
    }
  }
});

  $(".js-data-example-ajax").select2(
        {

            ajax: {
                url: '/fetch_item',
                dataType: 'json',
                type: "POST",
                delay: 250,
                data: function (params) {
                    return {
                      ename: params.term,
        '_token': "{{ csrf_token() }}",
        type: 'public'
                    };
                },
             processResults: function (response) {
            return {
              results: response
            };
          },
            },

           });
           });


           function clear_mst_dt1(){
    $("#po_no").val('');
    $("#po_con_no,#wo_no").val('');
    $("#con_sapnname").html('');
    $("#wo_no").attr('pod_a_id',0);
    $("#po_con_no").attr('pod_a_id',0);
    $("#po_amount").html('');
$("#po_description").html('');
$("#po_date").html('');
$("#prev_pay").hide();
$("#po_con_no").val('');

}




</script>


@endsection