
@extends('layouts.admin')
@section('pageTitle', 'Vega Soft')
@section('page-content')
<style>.ui-autocomplete {
    max-height: 500px;
    overflow-y: auto;
    /* prevent horizontal scrollbar */
    overflow-x: hidden;
    /* add padding to account for vertical scrollbar */
    padding-right: 20px;
} 
.ui-state-focus{
    background: black !important;
    color: white;
}

#prev_pa_tb tr:hover{
  cursor: pointer;
background: #915353;
color: white;

}


</style>
<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;?>">

<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="Remove" 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">


<div class="row">


<div class="col-lg-6">
  
<div class="rows">

<div class="col-md-12">
 <div class="form-group row ">
   <label class="col-sm-4 form-label">P.O  NO/PO CO No <i class="text-danger">*</i> </label>
   <div class="col-sm-8">
     <input type="text" class="form-control  input_txt" id="po_no" >
   </div>
 </div>
</div>
<div class="col-md-12">
 <div class="form-group row ">
   <label class="col-sm-4 form-label">Work Order</label>
   <div class="col-sm-8">
     <input type="text" class="form-control  input_txt" readonly id="pa_no" >
   </div>
 </div>
</div>
<div class="col-md-12">
 <div class="form-group row ">
   <label class="col-sm-4 form-label">P.O Completion  DATE  </label>
   <div class="col-sm-4">

   <input type="text" class="form-control  input_txt " id="po_date" readonly>
   </div>
 </div>
</div>
<div class="col-md-12">
 <div class="form-group row ">
   <label class="col-sm-4 form-label"> DESCRIPTION <i class="text-danger">*</i></label>
   <div class="col-sm-8">
   <textarea name="" id="po_description" cols="30" rows="10" class="form-control input_txt" style="height:50px !important;"></textarea>
   </div>
 </div>
</div>


<div class="col-md-12">
 <div class="form-group row ">
   <label class="col-sm-4 form-label">Select Contractor <i class="text-danger">*</i></label>
   <div class="col-sm-8">
   <input type="text" class="form-control " id="contcrtr" >
   </div>
 </div>
</div>
<div class="col-md-12">
 <div class="form-group row ">
   <label class="col-sm-4 form-label">Start Date  </label>
   <div class="col-sm-4">

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

<div class="col-md-12">
 <div class="form-group row ">
   <label class="col-sm-4 form-label">Completion Date  </label>
   <div class="col-sm-4">

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

<div class="col-md-12">
 <div class="form-group row ">
   <label class="col-sm-4 form-label">P.O Amount </label>
   <div class="col-sm-4">

   <input type="text" class="form-control  input_txt " id="po_amt" readonly>
   </div>
 </div>
</div>
<div class="col-md-12">
 <div class="form-group row ">
   <label class="col-sm-4 form-label">Balance P.O Amount</label>
   <div class="col-sm-4">

   <input type="text" class="form-control  input_txt " id="blnc_po" readonly>
   </div>
 </div>
</div>


<div class="col-md-12">
 <div class="form-group row ">
   <label class="col-sm-4 form-label">Estimate Cost  <i class="text-danger">*</i></label>
   <div class="col-sm-4">

   <input type="text" class="form-control focusable input_txt custom4" id="est_cost" onKeyPress="return isNumber(event, this.value);" >
   </div>
 </div>

<span class="text-danger fw-bold" id="est_error" style="font-size: 12px;
font-weight: bold;"></span>

</div>



</div>

</div>

<div class="col-lg-6" style="overflow:scroll;">

<table class="table  table-sm">

<thead class="bg-dark text-white">
  <tr>
  <th>Sl No</th>
  <th>Contractor</th>
  <th>Description</th>
  <th>Satrt Date</th>
  <th>Est.Completion Date</th>
  <th>Est.Cost</th>
  <th></th>
  <th></th>

</tr>
</thead>



<tbody id="prev_pa_tb">



</tbody>

</table>

</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>
                  
    </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();


 $("#est_error").hide();

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

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

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

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

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

}
}));
}
});
},
        columns: [ {
            field: 'po_no',
            title: 'Po No',
            width: '50%'
        },
        {
            field: 'name',
            title: 'Po Co No',
            width: '50%'
        },
        {
            field: 'desc',
            title: 'Desc',
            width: '50%'
        }],
         change : function(event, ui) {
                   if ( !ui.item ) 
                   { 
                   
             
                   }
                   else
                   {
                      
                   }
           },
               open: function(event, ui) {}, 
        select: function (event, ui) {
          if (ui.item.value!=undefined)
                {
                  $(this).attr('po_id',ui.item.pod_id);
$("#po_description").val(ui.item.desc);
$("#po_date").val(ui.item.po_date);
$("#po_amt").val(ui.item.po_amount);
$("#blnc_po").val(ui.item.blnc_po);
                    

load_prev_assgns(ui.item.pod_id);
fetch_pa_max_series(ui.item.pod_id);
               
                }
              
  
        }
    });
    });

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

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

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

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

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

}
}));
}
});
},
        columns: [ {
            field: 'name',
            title: 'Contractor',
            width: '100%'
        }],
         change : function(event, ui) {
      
                   if ( !ui.item ) 
                   { 
                   
             
                   }
                   else
                   {
                      
                   }
           },
               open: function(event, ui) {



               }, 
        select: function (event, ui) {
          
          if (ui.item.value!=undefined)
                {
                  $(this).attr('contrct_id',ui.item.c_id);

                    
               
                }
              
  
        }
    });
    });





















function save_submit_form() {
 status=0;
 if($.trim($('#po_no').val())==''){
    alert('P.O NO is mandatory');
    $('#po_no').focus();
    status=1;
    return;
 }
 if($.trim($('#po_description').val())==''){
    alert('Enter Description');
    $('#po_description').focus();
    status=1;
    return;
 }
 if($.trim($('#contcrtr').attr('contrct_id'))==''|| $.trim($('#contcrtr').attr('contrct_id'))==0){
    alert('Select Contractor');
    $('#contcrtr').focus();
    status=1;
    return;
 }
 if($.trim($('#est_cost').val())==''){
    alert('Enter Estimate');
    $('#est_cost').focus();
    status=1;
    return;
 }
 if($.trim($('#p_asg_dt').val())==''){
    alert('Start Date is Mandatory');
    $('#p_asg_dt').focus();
    status=1;
    return;
 }
 if($.trim($('#completion_dt').val())==''){
    alert('Completion Date is Mandatory');
    $('#completion_dt').focus();
    status=1;
    return;
 }





if(parseFloat($('#est_cost').val().replace(/,/g, ""))>parseFloat($('#po_amt').val().replace(/,/g, ""))){

  $("#est_error").show();
  $("#est_error").text('*Estimate Cost greater than PO Amount*');
  $('#est_cost').focus();
  status=1;
    return;

}

if($("#blnc_po").val()!="" && parseFloat($('#est_cost').val().replace(/,/g, ""))>parseFloat($('#blnc_po').val().replace(/,/g, ""))){

  $("#est_error").show();
  $("#est_error").text('*Estimate Cost greater than PO Balance Amount*');
  $('#est_cost').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();
$("#po_no").attr('readonly',true);

    $('#row_id').val(row_id);
    $('#hidden_status').val(1);
        $.ajax({
              type: 'POST',
               async: false,
                url: '/edit_po_assign',
                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[0];

     
        
            $('#po_no').val(datas.po_con_no); 
            $('#po_date').val(datas.po_date); 
            $('#pa_no').val(datas.pa_no); 
            
            $('#po_description').val(datas.descrpt); 
            $('#contcrtr').val(datas.c_name); 
            $('#contcrtr').attr('contrct_id',datas.c_id); 
            $('#p_asg_dt').val(datas.p_asg_dt); 
            $('#completion_dt').val(datas.completion_dt); 
            $('#est_cost').val(datas.est_cost); 
            
     
   
            
       // start_focus();   
        }
          
     });  
}









  function Save_form() {

           $.ajax({
          url: "/op_po_assign",
          type: "POST",
          data:{
             '_token': "{{ csrf_token() }}",
             'hidden_status':$.trim($('#hidden_status').val()),
              'row_id':$.trim($('#row_id').val()),

              'po_id':$('#po_no').attr('po_id'),
              'contrct_id':$('#contcrtr').attr('contrct_id'),
              'po_date':$.trim($('#po_date').val()),
              'pa_no':$.trim($('#pa_no').val()),
              'descrpt':$.trim($('#po_description').val()),
              'p_asg_dt':$.trim($('#p_asg_dt').val()),
              'completion_dt':$.trim($('#completion_dt').val()),
              'est_cost':$.trim($('#est_cost').val().replace(/,/g, ""))||0.00,

              'added_by':"{{ Session::get('user_id')}}",
              'branch_id':"{{ Session::get('CID')}}"
         
             
          },
          success:function(response){

            
            alert(response.msg);
if(response.flag!=0){
  $('#row_id').val(0);
  fetch_view_table();
  $("#est_error").hide();

$('.input_txt').val('');
$("#prev_pa_tb").html('');
$("#contcrtr").val('');
$("#contcrtr").attr('contrct_id',0);


}
         
           


          },
          error: function(response) {
          }
         });
  }



  function load_prev_assgns(pod_id){

    $.ajax({
              type: 'POST',
               async: false,
                url: '/load_prev_assgns',
                data: {
                     '_token': "{{ csrf_token() }}",
                    
                     pod_id: pod_id
         
                },
                error: function (request, error) {
                    alert("Please Wait, Loading...");
                },
                success: function (response) {
 datas=response;

tr='';
i=1;


     $(datas).each(function(key,val){
      tr+='<tr>';

tr+='<td>'+i+'</td>';
tr+='<td>'+val['c_name']+'</td>';
tr+='<td>'+val['descrpt']+'</td>';
tr+='<td>'+val['p_asg_dt']+'</td>';
tr+='<td>'+val['completion_dt']+'</td>';
tr+='<td>'+val['est_cost']+'</td>';
tr+='<td pa_id="'+val['pa_id']+'"><button class="btn btn-sm btn-outline-primary" onclick="edit_prev_pa(this);"><i class="icon-note" style="font-size:15px"></i></button></td>';
tr+='<td pa_id="'+val['pa_id']+'"><button class="btn btn-sm btn-outline-danger" onclick="del_prev_pa(this);"><i class="icon-trash" style="font-size:15px"></i></button></td>';

      tr+='</tr>';
      i++;

     });
        
     $("#prev_pa_tb").html(tr);
   
            
       // start_focus();   
        }
          
     });

  }

function edit_prev_pa(elm){

pa_id=$(elm).closest('td').attr('pa_id');
$(elm).closest('tr').remove();
set_edit_form(pa_id);

}
function del_prev_pa(elm){

pa_id=$(elm).closest('td').attr('pa_id');
$(elm).closest('tr').remove();
call_remove_modal(pa_id);

}





function fetch_pa_max_series(po_id){

$.ajax({
          type: 'POST',
           async: false,
            url: '/fetch_pa_max_series',
            data: {
                 '_token': "{{ csrf_token() }}",
                 po_id:po_id
                
                 
     
            },
            error: function (request, error) {
                alert("Please Wait, Loading...");
            },
            success: function (response) {
datas=response;

  

$("#pa_no").val(datas);

    }
      
 });

}








</script>
@endsection