
@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;?>">

<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">
    	<form class="forms-sample" autocomplete="off">
<div class="rows">
  
  <div class="col-md-4">
    <div class="form-group row ">
      <label class="col-sm-4 form-label">Name <i class="text-danger">*</i> </label>
      <div class="col-sm-8">
        <input type="text" class="form-control focusable input_txt" id="c_name">
      </div>
    </div>
  </div>
  <div class="col-md-4">
    <div class="form-group row ">
      <label class="col-sm-4 form-label">Address</label>
      <div class="col-sm-8">
      <textarea name="" id="address" cols="30" rows="10" class="form-control input_txt" style="height:50px !important;"></textarea>
      </div>
    </div>
  </div>
  <div class="col-md-4">
    <div class="form-group row ">
      <label class="col-sm-4 form-label">Mobile </label>
      <div class="col-sm-8">

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

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

      <select class="select2-multiple form-control focusable" name="tags[]" multiple="multiple"
           id="product_list">
             
          </select>
      </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">

$(document).ready(function() {
  $(".select2-multiple").select2({
    ajax: {
      type: 'POST',
      url: "/load_pdts_sup",
      dataType: 'json',

      data: function (params) {
      var query = {
        '_token': "{{ csrf_token() }}",
        search: params.term
       
      }

   
      return query;
    }
      
     ,
      processResults: (data, params) => {
        
        const results = data.map(item => {
          return {
            id: item.product_id,
            text: item.p_name ,
          };
        });
        return {
          results: results,
        }
      },
    },
  });
})

// $(document).ready(function() {
//             // Select2 Multiple
//             $('.select2-multiple').select2({
//                 placeholder: "Select",
//                 allowClear: false
//             });

//            $('#select2Multiple').on("select2:unselecting", function(e){

           
//       var unselected_value = e.params.args.data.id;
//          $('.ug_id'+unselected_value).remove();
//     }).trigger('change');

//         });

function start_focus(){
  $('#c_name').focus().click();
}



//load_products();



// function load_products(){

//   arr={
               
              
//                      'row_id': '0',
// };

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

     
//         tr='';
//         $(datas).each(function(key,val){


// tr+='<option value="'+val['product_id']+'">'+val['p_name']+'</option>';


//         });
//      $("#product_list").html(tr);
   
    
//         }
          
//      });


// }



function save_submit_form() {
 status=0;
 if($.trim($('#c_name').val())==''){
    alert('Name is mandatory');
    $('#c_name').focus();
    status==1;
    return;
 }
/* if($.trim($('#phone').val())==''){
    alert('Phone is mandatory');
    $('#phone').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();
$('#hidden_status' ).val("1");
$('#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_contr_sup',
                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.cntrcts;

 pdts=response.pdts;




 if(pdts[0]['prdct_id']!=null){
           prdct_id= pdts[0]['prdct_id'].split(',');
           $('#product_list').val(prdct_id).trigger('change');
         }else{
            $('#product_list').val('').trigger('change');
         }





        
            $('#c_name').val(datas.c_name); 
            $('#address').val(datas.address); 
            $('#phone').val(datas.phone); 
            $('#phone2').val(datas.phone2); 
     
   
            
       // start_focus();   
        }
          
     });  
}









  function Save_form() {
    product_id = $("#product_list").val()||['0']; 
  
           $.ajax({
          url: "/op_supplier",
          type: "POST",
          data:{
             '_token': "{{ csrf_token() }}",
             'hidden_status':$.trim($('#hidden_status').val()),
              'row_id':$.trim($('#row_id').val()),
              'c_name':$.trim($('#c_name').val()),
              'address':$.trim($('#address').val()),
              'phone':$.trim($('#phone').val()),
              'phone2':$.trim($('#phone2').val()),
              'type':1,
              'product_id':product_id.join(),
              'added_by':"{{ Session::get('user_id')}}",
              'company_id':"{{ Session::get('CID')}}"
         
             
          },
          success:function(response){
            
            alert(response.msg);

            $('#row_id').val(0);
            $('.input_txt').val('');
            fetch_view_table();
           
            $('#product_list').val('').trigger('change');

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




</script>
@endsection