<?php $this->view("includes/Inc_pagehead");?>

<!-- Page Styles Goes Here-->
<style>
.m-form .m-form__group {
  padding-top: 0px;
  padding-bottom: 0px;
}
</style>
<?php $this->view("includes/Inc_navigation");?>
<div class="m-grid__item m-grid__item--fluid m-wrapper">
  <!-- BEGIN: Subheader -->
  <div class="m-subheader ">
    <div class="d-flex align-items-center">
      <div class="mr-auto">
        <h3 class="m-subheader__title m-subheader__title--separator">
          Add Opening Stock
        </h3>
        <ul class="m-subheader__breadcrumbs m-nav m-nav--inline">
          <li class="m-nav__item m-nav__item--home">
            <a href="#" class="m-nav__link m-nav__link--icon">
              <i class="m-nav__link-icon la la-home"></i>
            </a>
          </li>
          <li class="m-nav__separator">
            -
          </li>
          <li class="m-nav__item">
            <a href="<?php echo base_url().'dashboard'?>" class="m-nav__link">
              <span class="m-nav__link-text">
                Home
              </span>
            </a>
          </li>
          <li class="m-nav__separator">
            -
          </li>
          <li class="m-nav__item">
            <a href="<?php echo base_url().'stock/opening_stock/add' ?>" class="m-nav__link">
              <span class="m-nav__link-text">
                Add Opening Stock
              </span>
            </a>
          </li>


        </ul>
      </div>
      <div>

      </div>
    </div>
  </div>
  <!-- END: Subheader -->
  <div class="m-content">
    <form autocomplete="off" id="form_request_add" data-form-type="opening_stock_create" method="post" action="<?php echo base_url()?>stock/create_opening_stock" enctype="multipart/form-data" class="m-form m-form--fit m-form--state m-form--label-align-right " >
      <div class="m-portlet m-portlet--full-height">
        <div class="m-portlet__head">
          <div class="m-portlet__head-caption">
            <div class="m-portlet__head-title">
              <h3 class="m-portlet__head-text">
                Add Opening Stock
              </h3>
            </div>
          </div>
          <div class="m-portlet__head-tools">

          </div>
        </div>
        <div class="row">
          <div class="col-md-12">
            <div class="modal-body">
              <div class="m-form__content">
                <div class="m-alert m-alert--icon alert alert-warning m--hide" role="alert" id="form_request_add_msg">
                  <div class="m-alert__icon">
                    <i class="la la-warning"></i>
                  </div>
                  <div class="m-alert__text">
                    Oh snap! Change a few things up and try submitting again.
                  </div>
                  <div class="m-alert__close">
                    <button type="button" class="close" data-close="alert" aria-label="Close"></button>
                  </div>
                </div>
                <div class="row">
                  <div class="col-lg-5">
                    <div class="form-group m-form__group" >
                      <label for="user_name">
                        Location
                      </label>
                      <div class="m-select2 m-select2--air m-select2--pill ">
                        <select required id="location_add" name="location" style="width: 100%" class="form-control m-input" aria-describedby="basic-addon1" data-placeholder="Select Location">
                          <?php if($this->config->item('app_module_stock_management')==1){?>
                            <option selected disabled>Select Location</option>
                          <?php }else{?>
                            <option selected disabled>Select Work</option>
                          <?php } ?>
                          <?php
                          foreach($stock_locations as $row):
                            $location_name = $row->location_name;
                            if($this->config->item('app_module_stock_management')==1){
                              if($row->location_type=='work_site'){
                                $location_name .= " [Work Site]";
                              }elseif($row->location_type=='ware_house'){
                                $location_name .= " [Warehouse]";
                              }
                            }
                            ?>
                            <option data-work-status="<?php echo $row->work_status?>" data-location-name="<?php echo $row->location_name ?>" data-location-place="<?php echo $row->work_location ?>"  value="<?php echo $row->location_id?>"><?php echo $location_name?></option>
                            <?php
                          endforeach;
                          ?>
                        </select>
                      </div>
                      <span class="m-form__help"></span>
                    </div>
                  </div>
                  <div class="col-lg-4">
                    <div class="form-group m-form__group" >
                      <label for="user_name">
                        Date
                      </label>
                      <div class="m-input-icon m-input-icon--right">
                        <input  data-date-format="dd-mm-yyyy" id="request_date" required type="text" name="date" class="form-control m-input m-input--pill m-input--air" placeholder="Date" >
                        <span class="m-input-icon__icon m-input-icon__icon--right"><span><i class="la la-calendar"></i></span></span>
                      </div>
                    </div>
                    <span class="m-form__help"></span>
                  </div>
                  <input type="hidden" name="redirect_url" value="<?php echo base_url(uri_string())?>" >
                </div>
              </div>
            </div>
            <!--end::Form-->
          </div>
        </div>
      </div>
      <div class="row">
        <div class="col-lg-12" >
          <div class="m-portlet ">
            <div class="m-portlet__head">
              <div class="m-portlet__head-caption">
                <div class="m-portlet__head-title">
                  <h4 class="m-portlet__head-text">Add Items to Cart</h4>
                </div>
              </div>
            </div>
            <div class="m-portlet__body">
              <div class="row" style="margin-bottom: 10px">
                <div class="col-lg-5" style="padding-right: 0px;">
                  <div class="form-group m-form__group" style="border: 4px solid #e2e2e2;padding-top: 10px;padding-right:0px !important">
                    <div class="input-group m-input-group m-input-group--pill" >
                      <div class="input-group-prepend">
                        <button type="button" style="margin-bottom: 10px" id="btn_add_all_item" class="btn btn-info">
                          Add All to Cart
                        </button>
                      </div>
                      <select style="width: 62%"  class="form-control m-select2 m-select2-general" id="category_add" data-placeholder="Select Category">

                      </select>
                    </div>
                  </div>
                </div>
                <div class="col-lg-7" style="padding-left: 0px;">
                  <div class="form-group m-form__group"  style="border: 4px solid #e2e2e2;border-left: none;padding-bottom: 10px;padding-top: 10px;padding-right:0px !important" >
                    <div class="input-group m-input-group m-input-group--pill">
                      <?php if ($this->session->userdata('user_role') != 'supervisor'){?>
                      <div class="input-group-prepend">
                        <a href="#material_add_modal"   data-toggle="modal" data-target="#material_add_modal" class="btn btn-focus">
                          <i class="fa fa-plus-circle"></i>
                        </a>
                      </div>
                    <?php } ?>
                      <select style="width: 68%"  class="form-control m-select2 m-select2-general" id="material_add" data-placeholder="Select Material">

                      </select>
                      <div class="input-group-prepend">
                        <button style="float: right" type="button" id="btn_add_item" class="btn btn-brand">
                          Add to Cart
                        </button>
                      </div>
                    </div>
                  </div>
                </div>

              </div>
              <div class="row">
                <div class="col-lg-12">
                  <table style="width: 100%;max-width: 100%"  id="table_items" class="table m-table m-table--head-bg-success order-list table-striped table-bordered table-advance table-hover">
                    <thead>
                      <tr>
                        <th style="width:10%">Sl No</th>
                        <th style="width:30%">Material</th>
                        <th style="width:12%">Unit</th>
                        <th style="width:13%">Qty</th>
                        <th style="width:16%">Unit Price</th>
                        <th style="width:16%">Total Amt</th>
                        <th style="width:6%">&nbsp;</th>
                      </tr>
                    </thead>
                    <tbody id="table_body">
                      <?php
                      $i=0;
                      ?>
                    </tbody>
                  </table>
                  <input type="hidden" value="<?php echo $i ?>" id="current_row_count" name="current_row_count" >
                  <input type="hidden" value="<?php echo $i ?>" id="actual_row_count" name="actual_row_count" >
                </div>
              </div>
            </div>
          </div>
          <div class="m-portlet ">
            <div class="row">
              <div class="col-lg-12">
                <div class="m-form__content">
                  <br/>
                  <div class="form-group m-form__group">
                    <label for="exampleTextarea">Remarks / Notes</label>
                    <textarea class="form-control m-input m-input--air m-input--pill" id="remarks" name="remarks" rows="3"></textarea>
                  </div>
                  <br/>
                </div>
                <div class="m-portlet__foot m-portlet__foot--fit">
                  <div class="m-form__actions m-form__actions">
                    <div class="row">
                      <div class="ml-lg-auto">
                        <button type="submit" class="btn btn-brand form_submit_btn">
                          Save & Submit
                        </button>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </form>
  </div>
  <!-- Include Modal -->
  <?php $this->view("modules/include_modals/modal_material_add");?>
  <?php $this->view("modules/include_modals/modal_unit_add");?>
</div>
</div>

<!-- end:: Body -->
<!-- begin::Footer -->

<?php $this->view("includes/Inc_footer");?>
<!-- end::Footer -->

<!-- end:: Page -->

<?php $this->view("includes/Inc_sidebar");?>
<?php $this->view("includes/Inc_scripts");?>
<!--begin::Page Snippets -->

<script>


//    Toggle Sidebar
// $("#m_aside_left_minimize_toggle").addClass("m-brand__toggler--active");
// $("#body").addClass("m-brand--minimize m-aside-left--minimize");

$("#stock_menu").addClass('m-menu__item--active m-menu__item--open');
$("#stock_menu_5").addClass('m-menu__item--active');

$("#material_unit_add").select2({
  placeholder: "Select Unit",
  dropdownParent: $("#form_material_add")
})
$("#material_category_add").select2({
  placeholder: "Select Category",
  dropdownParent: $("#form_material_add")
})
$("#material_tax_percent_add").select2({
  placeholder: "Select Tax %",
  dropdownParent: $("#form_material_add")
})

$("#location_add").select2({
  dropdownParent: $("#form_request_add"),
  placeholder: "Select Location",
  templateResult: format_project_dropdown
})
$("#category_add").select2({
  placeholder: "Select Category",
  dropdownParent: $("#form_request_add")
})
$("#material_add").select2({
  dropdownParent: $("#form_request_add"),
  placeholder: "Select Material"
})

var selected_supplier = selected_location = "";

select_category();
select_material();
select_units();
function select_category(){
  $('#material_category_add').html('');
  $("#material_category_add").append("<option selected disabled >Select Category</option>");

  $('#category_add').html('');
  $("#category_add").append("<option selected value='all' >All Categories</option>");

  $.post("<?php echo base_url() ?>material/select_category",{},function(data) {
    var obj = $.parseJSON(data);
    var i=0;
    if(obj.count>0) {
      while (i < obj.id.length) {
        $("#material_category_add").append("<option value=" + obj.id[i] + ">" + obj.name[i] + "</option>");
        $("#category_add").append("<option value=" + obj.id[i] + ">" + obj.name[i] + "</option>");
        i++;
      }
    }
  })
}

function select_material(category=''){
  $('#material_add').html('');
  $("#material_add").append("<option disabled selected >Select Material</option>");

  $.post("<?php echo base_url() ?>material/select",{category:category},function(data) {
    var obj = $.parseJSON(data);
    var i=0;
    if(obj.count>0) {
      while (i < obj.id.length) {
        $("#material_add").append("<option value=" + obj.id[i] + ">" + obj.name[i] + " [" + obj.unit_name[i] + "]</option>");
        i++;
      }
    }
  })
}
function select_units(){
  $('#material_unit_add').html('');
  $("#material_unit_add").append("<option disabled selected >Select Unit</option>");
  $.post("<?php echo base_url() ?>units/select",{},function(data) {
    var obj = $.parseJSON(data);
    var i=0;
    if(obj.count>0) {
      while (i < obj.id.length) {
        $("#material_unit_add").append("<option value=" + obj.id[i] + ">" + obj.name[i] + "</option>");
        i++;
      }
    }
  })
}
$('#category_add').on("change", function(e) {
  var category = $('#category_add :selected').val();
  select_material(category);
});


<?php if(isset($location_data) && $location_data->num_rows()==1){?>
  var location_id = "<?php echo $location_data->row()->location_id ?>";
  $("#location_add").val(location_id).trigger('change');
  <?php } ?>
  </script>


  <script>
  $("#request_date,#paid_date,#reference_date_add").datepicker( {
    todayHighlight:!0, orientation:"bottom left", templates: {
      leftArrow: '<i class="la la-angle-left"></i>', rightArrow: '<i class="la la-angle-right"></i>'
    },

    })


    jQuery(document).ready(function() {

      var request_item_table=function() {
        var request_id = $("#request_id").val();
        // var table = $('#table_items').DataTable({
        //   "scrollX": true,
        //   bFilter: false,
        //   "bLengthChange": false,
        //   "bPaginate": false,
        //   "dom": "<'row' <'col-md-12'>><'row'<'col-md-6 col-sm-12'l><'col-md-6 col-sm-12'f>r><'table-scrollable't><'row'<'col-md-5 col-sm-12'i><'col-md-7 col-sm-12'p>>", // horizobtal scrollable datatable
        //   "lengthMenu": [
        //     [25, 50, 100, -1],
        //     [25, 50, 100, "All"] // change per page values here
        //   ],
        //   'ordering': false
        // });
        $(".hidden").hide();
      }
      request_item_table();
      $("#btn_add_item").on("click", function () {
        var material_id = $('#material_add :selected').val();
        if(material_id!=null && material_id!='' && material_id!='Select Material'){
          // Get Material Details
          $.post("<?php echo base_url() ?>material/select",{id:material_id},function(data) {
            var obj = $.parseJSON(data);
            var i=0;
            if(obj.count==1) {
              var already_added=0;
              $(".item_material_id").each(function(){
                if($(this).val()==material_id){
                  already_added=1;
                }
              });
              if(already_added==0){
                // $('#table_items').dataTable().fnDestroy();
                var counter = parseInt($("#current_row_count").val());
                var actual_row_count = parseInt($("#actual_row_count").val());
                var newRow = '<tr class="row_'+counter+'">';
                newRow += '<td><div class="form-group m-form__group" style="padding: 0px !important;margin: 0px !important;"><div class="input-group m-input-group m-input-group--pill m-input-group--air"><input style="padding: 10px" readonly type="text" class="form-control m-input item_slno" data-sl-no="' + (counter+1) + '" name="item[sl_no]['+(counter)+']" value="' + (actual_row_count+1) + '"/></div></div></td>';
                newRow += '<td><div class="form-group m-form__group" style="padding: 0px !important;margin: 0px !important;"><div class="input-group m-input-group m-input-group--pill m-input-group--air">';
                // newRow += '<input type="text" readonly class="form-control m-input" name="item[material_name]['+(counter)+']" value="'+obj.name[0]+'" id="material_name_' + (counter+1) + '"/></div>';
                newRow += "<input type='text' readonly class='form-control m-input' name='item[material_name]["+(counter)+"]' value='"+obj.name[0]+"' id='material_name_" + (counter+1) + "'/></div>";

                newRow += '<input type="hidden" name="item[material_id]['+(counter)+']" value="'+obj.id[0]+'" class="item_material_id" id="material_id_' + (counter+1) + '"/>';
                newRow += '</div></td>';
                newRow += '<td><div class="form-group m-form__group" style="padding: 0px !important;margin: 0px !important;"><div class="input-group m-input-group m-input-group--pill m-input-group--air">';
                newRow += '<input type="text" readonly class="form-control m-input" value="'+obj.unit_name[0]+'" name="item[unit_name]['+(counter)+']" id="unit_name_' + (counter+1) + '"/></div>';
                newRow += '<input type="hidden" name="item[unit_id]['+(counter)+']" value="'+obj.unit[0]+'" id="unit_id_' + (counter+1) + '"/>';
                newRow += '</div></td>';
                newRow += '<td><div class="form-group m-form__group" style="padding: 0px !important;margin: 0px !important;"><div class="input-group m-input-group m-input-group--pill m-input-group--air"><input type="text" required class="form-control m-input numeric_fields quantity_field item_rate_fields" data-sl-no="' + (counter+1) + '" value="1" name="item[quantity]['+(counter)+']" id="quantity_' + (counter+1) + '"/></div></div></td>';
                newRow += '<td><div class="form-group m-form__group" style="padding: 0px !important;margin: 0px !important;"><div class="input-group m-input-group m-input-group--pill m-input-group--air"><input type="text" required class="form-control m-input numeric_fields unit_price_field item_rate_fields" data-sl-no="' + (counter+1) + '" value="'+obj.unit_price[0]+'" name="item[unit_price]['+(counter)+']" id="unit_price_' + (counter+1) + '"/></div></div></td>';
                newRow += '<td><div class="form-group m-form__group" style="padding: 0px !important;margin: 0px !important;"><div class="input-group m-input-group m-input-group--pill m-input-group--air"><input readonly type="text" required class="form-control m-input numeric_fields item_amount_field total_amount_field" data-sl-no="' + (counter+1) + '" value="'+obj.unit_price[0]+'" name="item[total_amount]['+(counter)+']" id="total_amount_' + (counter+1) + '"/></div></div></td>';
                newRow += '<td><a href="javascript:" data-row-sl-no='+counter+' class="ibtnDel btn btn-danger btn-icon-only" ><i class="fa fa-trash"></i></td>';
                newRow += '</tr>';

                newRow += '<tr class="row_'+counter+'">';
                newRow += '<td></td>';
                newRow += '<td colspan="5"><div class="form-group m-form__group" style="padding: 0px !important;margin: 0px !important;"><div class="input-group m-input-group m-input-group--pill m-input-group--air"><input placeholder="Description Here" type="text" class="form-control m-input" data-sl-no="' + (counter+1) + '" name="item[description]['+(counter)+']" id="description_' + (counter+1) + '"/></div></div></td>';
                newRow += '<td></td>';
                newRow += '</tr>';

                $("#table_body").append(newRow);

                counter++;
                actual_row_count++;
                $("#current_row_count").val(counter);
                $("#actual_row_count").val(actual_row_count);
                // Clear Selectbox Selection
                $("#material_add").val("").trigger('change');
                // change_amount_type();
                request_item_table();
              }
              else{
                toastr["error"]("Item Already Added to Cart", "Already Added");
              }
            }else{
              toastr["error"]("Some Error Associated with Selected Material", "Error Occurred");
            }
          })
        }else{
          toastr["warning"]("Please Select a Material From List", "Select Item");
        }

      });
      $("#btn_add_all_item").on("click", function () {
        var category = $('#category_add :selected').val();
        if(category!=null && category!='' && category!='Select Category' && category!='all'){
          // Get Materials
          $.post("<?php echo base_url() ?>material/select",{category:category},function(data) {
            var obj = $.parseJSON(data);
            var i=0;
            if(obj.count>0) {
              while (i < obj.id.length) {
                // $('#table_items').dataTable().fnDestroy();
                var counter = parseInt($("#current_row_count").val());
                var actual_row_count = parseInt($("#actual_row_count").val());
                var newRow = '<tr class="row_'+counter+'">';
                newRow += '<td><div class="form-group m-form__group" style="padding: 0px !important;margin: 0px !important;"><div class="input-group m-input-group m-input-group--pill m-input-group--air"><input style="padding: 10px" readonly type="text" class="form-control m-input item_slno" data-sl-no="' + (counter+1) + '" name="item[sl_no]['+(counter)+']" value="' + (actual_row_count+1) + '"/></div></div></td>';
                newRow += '<td><div class="form-group m-form__group" style="padding: 0px !important;margin: 0px !important;"><div class="input-group m-input-group m-input-group--pill m-input-group--air">';
                // newRow += '<input type="text" readonly class="form-control m-input" name="item[material_name]['+(counter)+']" value="'+obj.name[i]+'" id="material_name_' + (counter+1) + '"/></div>';
                newRow += "<input type='text' readonly class='form-control m-input' name='item[material_name]["+(counter)+"]' value='"+obj.name[i]+"' id='material_name_" + (counter+1) + "'/></div>";

                newRow += '<input type="hidden" name="item[material_id]['+(counter)+']" value="'+obj.id[i]+'" class="item_material_id" id="material_id_' + (counter+1) + '"/>';
                newRow += '</div></td>';
                newRow += '<td><div class="form-group m-form__group" style="padding: 0px !important;margin: 0px !important;"><div class="input-group m-input-group m-input-group--pill m-input-group--air">';
                newRow += '<input type="text" readonly class="form-control m-input" value="'+obj.unit_name[i]+'" name="item[unit_name]['+(counter)+']" id="unit_name_' + (counter+1) + '"/></div>';
                newRow += '<input type="hidden" name="item[unit_id]['+(counter)+']" value="'+obj.unit[i]+'" id="unit_id_' + (counter+1) + '"/>';
                newRow += '</div></td>';
                newRow += '<td><div class="form-group m-form__group" style="padding: 0px !important;margin: 0px !important;"><div class="input-group m-input-group m-input-group--pill m-input-group--air"><input type="text" required class="form-control m-input numeric_fields quantity_field item_rate_fields" data-sl-no="' + (counter+1) + '" value="1" name="item[quantity]['+(counter)+']" id="quantity_' + (counter+1) + '"/></div></div></td>';
                newRow += '<td><div class="form-group m-form__group" style="padding: 0px !important;margin: 0px !important;"><div class="input-group m-input-group m-input-group--pill m-input-group--air"><input type="text" required class="form-control m-input numeric_fields unit_price_field item_rate_fields" data-sl-no="' + (counter+1) + '" value="'+obj.unit_price[i]+'" name="item[unit_price]['+(counter)+']" id="unit_price_' + (counter+1) + '"/></div></div></td>';
                newRow += '<td><div class="form-group m-form__group" style="padding: 0px !important;margin: 0px !important;"><div class="input-group m-input-group m-input-group--pill m-input-group--air"><input type="text" required readonly class="form-control m-input numeric_fields item_amount_field total_amount_field" data-sl-no="' + (counter+1) + '" value="'+obj.unit_price[i]+'" name="item[total_amount]['+(counter)+']" id="total_amount_' + (counter+1) + '"/></div></div></td>';
                newRow += '<td><a href="javascript:" data-row-sl-no='+counter+' class="ibtnDel btn btn-danger btn-icon-only" ><i class="fa fa-trash"></i></td>';
                newRow += '</tr>';

                newRow += '<tr class="row_'+counter+'">';
                newRow += '<td></td>';
                newRow += '<td colspan="5"><div class="form-group m-form__group" style="padding: 0px !important;margin: 0px !important;"><div class="input-group m-input-group m-input-group--pill m-input-group--air"><input placeholder="Description Here" type="text" class="form-control m-input" data-sl-no="' + (counter+1) + '" name="item[description]['+(counter)+']" id="description_' + (counter+1) + '"/></div></div></td>';
                newRow += '<td></td>';
                newRow += '</tr>';

                $("#table_body").append(newRow);

                counter++;
                actual_row_count++;
                $("#current_row_count").val(counter);
                $("#actual_row_count").val(actual_row_count);
                i++;
              }
              // Clear Selectbox Selection
              $("#category_add").val("").trigger('change');
              // change_amount_type();
              request_item_table();
            }else{
              toastr["error"]("Some Error Associated with Selected Material", "Error Occurred");
            }
          })
        }else{
          toastr["warning"]("Please Select a Category From List", "Select Category");
        }

      });
      $("#table_body").on("click", ".ibtnDel", function (event) {
        var row_sl_no = $(this).data('row-sl-no');
        var counter = parseInt($("#actual_row_count").val());
        $('.row_'+row_sl_no).remove();
        counter -= 1;
        $("#actual_row_count").val(counter);

        //rearrange higher sl no
        var new_sl_no = 0;
        $(".item_slno").each(function () {
          new_sl_no++;
          $(this).val(new_sl_no);
        })
        // calculate_summary();
      });

      $('body').on('change keyup', '.item_rate_fields', function() {
        var sl_no = $(this).attr('data-sl-no');
        calculate_item_total(sl_no);
      })

      function calculate_item_total(sl_no){
        var unit_price = ($("#unit_price_"+sl_no).val()=='') ? 0 : parseFloat($("#unit_price_"+sl_no).val());
        var quantity = ($("#quantity_"+sl_no).val()=='') ? 0 : parseFloat($("#quantity_"+sl_no).val());
        var total_amount = unit_price*quantity;
        $("#total_amount_"+sl_no).val(total_amount.toFixed(<?php echo $this->config->item('Currency_amount_decimals_2')?>));
      }

      $(".form_submit_btn").click(function (e) {

        e.preventDefault();
        var a = $(this),
        l = $(this).closest("form"),
        action = l.attr('action');
        var form_id = l.attr('id');
        var item_count = $("#actual_row_count").val();
        var form_type = l.attr('data-form-type');
        var item_count_error = 0;

        l.validate(
          {
            rules: {
              request_paid_amount: {
                number: !0,
                required: !0
              },
              request_total_amount: {
                required: !0
              },
              bill_discount_amount: {
                number: !0,
                required: !0
              },
              commission_amount: {
                number: !0,
                required: !0
              }
            }, invalidHandler: function (e, r) {
              var i = $("#" + form_id + "_msg");
              i.removeClass("m--hide").show(), mUtil.scrollTo(i, -200)
            }, submitHandler: function (e) {
            }
          }),// ADD validation to item fields
          $(".numeric_fields").each(function () {
            if(form_type=="opening_stock_create"){
              $("#"+$(this).attr("id")).rules('add', {
                number: !0
              });
            }
          });
          if(l.valid()==true){
            if(item_count==0 && form_type=='opening_stock_create'){
              toastr['warning']('No Items Selected', 'No Items Selected');
            }else{
              form_submit_function(l,a);
            }
            function form_submit_function(l,a){
              a.addClass("m-loader m-loader--right m-loader--light").attr("disabled", !0),
              l.ajaxSubmit({
                url: action,
                success: function (e, t, r, s) {
                  var modal_id = l.attr('data-modal-id');
                  var form_type = l.attr('data-form-type');
                  $("#" + form_id + "_msg").addClass("m--hide");
                  // Block UI
                  mApp.blockPage({
                    overlayColor: "#000000",
                    type: "loader",
                    state: "success",
                    message: "Please wait..."
                  }), setTimeout(function () {
                    mApp.unblockPage()
                  }, 2e3);
                  var obj = $.parseJSON(e);
                  if (obj.status == 1) {
                    $("#"+modal_id).modal('toggle');
                    $("."+form_id+"_fields").val('');
                    $("."+form_id+"_select").val('').trigger("change");
                    a.removeClass("m-loader m-loader--right m-loader--light").attr("disabled", !1);
                    if(form_type=="material_create"){
                      select_material();
                      $("#material_add").trigger("change");
                      toastr[obj.flashdata_type](obj.flashdata_msg, obj.flashdata_title);
                    }else if(form_type=="supplier_create"){
                      select_supplier();
                      $("#supplier_add").trigger("change");
                      toastr[obj.flashdata_type](obj.flashdata_msg, obj.flashdata_title);
                    }else if(form_type=="unit_create"){
                      select_units();
                      $("#material_unit_add").trigger("change");
                      toastr[obj.flashdata_type](obj.flashdata_msg, obj.flashdata_title);
                    }else if(form_type=='opening_stock_create'){
                      swal({
                        width: 500,
                        title: obj.flashdata_title,
                        text: obj.flashdata_msg,
                        type: obj.flashdata_type,
                        showCancelButton: !0,
                        confirmButtonText: "View Stock Opening",
                        cancelButtonText: "Add Opening Stock",
                        confirmButtonColor: '#3085d6',
                        cancelButtonColor: '#d33',
                        reverseButtons: !0,
                        closeOnClickOutside: false,
                        allowOutsideClick: false,
                      }).then(function(e) {
                        e.value ? window.location.replace("<?php echo base_url()?>stock/opening_stock") : "cancel" === e.dismiss && window.location.replace("<?php echo base_url()?>stock/opening_stock/add/");
                      })
                    }
                  } else {
                    a.removeClass("m-loader m-loader--right m-loader--light").attr("disabled", !1);
                    toastr[obj.flashdata_type](obj.flashdata_msg, obj.flashdata_title);
                  }
                  if (obj == null) {
                    setTimeout(function () {
                      a.removeClass("m-loader m-loader--right m-loader--light").attr("disabled", !1);
                      toastr["warning"]("Your Attempt is Timed Out. Please Try Again.", "Timed Out");
                    }, 2e4)
                  }
                },error: function() {
                  a.removeClass("m-loader m-loader--right m-loader--light").attr("disabled", !1);
                  toastr["error"]("Sorry. There Has Been Some Error Occurred. Please Try Again.", "Error");
                }
              })
            }
          }
        });

      });

      </script>
